-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathstring_name_test.rs
151 lines (122 loc) · 3.83 KB
/
string_name_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) godot-rust; Bromeon and contributors.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
use std::collections::HashSet;
use crate::framework::{assert_eq_self, itest};
use godot::builtin::{GString, NodePath, StringName};
#[itest]
fn string_name_default() {
let name = StringName::default();
let back = GString::from(&name);
assert_eq!(back, GString::new());
}
#[itest]
fn string_name_conversion() {
// Note: StringName::from(&str) uses direct FFI constructor from Godot 4.2 onwards.
let string = GString::from("some string");
let name = StringName::from(&string);
let back = GString::from(&name);
assert_eq!(string, back);
let second = StringName::from(string.clone());
let back = GString::from(second);
assert_eq!(string, back);
}
#[itest]
fn string_name_node_path_conversion() {
let string = StringName::from("some string");
let name = NodePath::from(&string);
let back = StringName::from(&name);
assert_eq!(string, back);
let second = NodePath::from(string.clone());
let back = StringName::from(second);
assert_eq!(string, back);
}
#[itest]
fn string_name_equality() {
let string = StringName::from("some string");
let second = StringName::from("some string");
let different = StringName::from("some");
assert_eq!(string, second);
assert_ne!(string, different);
}
#[itest]
#[allow(clippy::eq_op)]
fn string_name_transient_ord() {
// We can't deterministically know the ordering, so this test only ensures consistency between different operators.
let low = StringName::from("Alpha");
let high = StringName::from("Beta");
let mut low = low.transient_ord();
let mut high = high.transient_ord();
if low > high {
std::mem::swap(&mut low, &mut high);
}
assert!(low < high);
assert!(low <= high);
assert!(high > low); // implied.
assert!(high >= low);
// Check PartialEq/Eq relation.
assert_eq_self!(low);
assert_eq_self!(high);
assert!(low != high);
}
#[itest]
fn string_name_clone() {
let first = StringName::from("some string");
#[allow(clippy::redundant_clone)]
let cloned = first.clone();
assert_eq!(first, cloned);
}
#[itest]
fn string_name_hash() {
let set: HashSet<StringName> = [
"string_1",
"SECOND string! :D",
"emoji time: 😎",
r#"got/!()%)=!"/]}¡[$½{¥¡}@£symbol characters"#,
"some garbageTƉ馧쟻�韂ꮛཾ̶D@/8ݚ-䌗8",
]
.into_iter()
.map(StringName::from)
.collect();
assert_eq!(set.len(), 5);
}
#[itest]
fn string_name_length() {
let string = "hello!";
let name = StringName::from(string);
assert_eq!(name.len(), string.len());
let empty = StringName::default();
assert_eq!(empty.len(), 0);
}
#[itest]
fn string_name_is_empty() {
let name = StringName::from("hello!");
assert!(!name.is_empty());
let empty = StringName::default();
assert!(empty.is_empty());
}
#[itest]
#[cfg(since_api = "4.2")]
fn string_name_from_latin1_with_nul() {
let cases: [(&[u8], &str); 3] = [
(b"pure ASCII\t[~]\0", "pure ASCII\t[~]\0"),
(b"\xB1\0", "±"),
(b"Latin-1 \xA3 \xB1 text \xBE\0", "Latin-1 £ ± text ¾"),
];
for (bytes, string) in cases.into_iter() {
let a = StringName::from_latin1_with_nul(bytes);
let b = StringName::from(string);
println!();
println!(
"Arrays: a={:?}, b={:?}",
a.to_string().as_bytes(),
b.to_string().as_bytes()
);
println!("Hashes: a={:?}, b={:?}", a.hash(), b.hash());
println!("Lengths: a={}, b={}", a.len(), b.len());
assert_eq!(a, b);
}
}