Skip to content

Commit e12ed33

Browse files
committed
Add many tests
1 parent df25b76 commit e12ed33

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

crates/mac_core/src/array.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,16 @@ mod tests {
8989
assert_eq!(arr[0].as_str(), "foo");
9090
assert_eq!(arr[1].as_str(), "bar");
9191
}
92+
93+
#[test]
94+
fn test_array_len() {
95+
let empty = CFArray::empty();
96+
let two = CFArray::new(&[CFString::new("foo"), CFString::new("bar")]);
97+
98+
assert!(empty.is_empty());
99+
assert_eq!(empty.len(), 0);
100+
101+
assert!(!two.is_empty());
102+
assert_eq!(empty.len(), 2);
103+
}
92104
}

crates/mac_core/src/dict.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,31 @@ impl<K: CoreType, V: CoreType> CFDictionary<K, V> {
9696
unsafe { CFDictionary::new_owned(ptr) }
9797
}
9898
}
99+
100+
#[cfg(test)]
101+
mod tests {
102+
use super::*;
103+
use crate::CFString;
104+
105+
#[test]
106+
fn test_new_arr() {
107+
let one = CFString::new("1");
108+
let two = CFString::new("2");
109+
let three = CFString::new("3");
110+
let four = CFString::new("4");
111+
112+
let _ = CFDictionary::new([]);
113+
let _ = CFDictionary::new([(one, two), (three, four)]);
114+
}
115+
116+
#[test]
117+
fn test_new_vec() {
118+
let one = CFString::new("1");
119+
let two = CFString::new("2");
120+
let three = CFString::new("3");
121+
let four = CFString::new("4");
122+
123+
let _ = CFDictionary::new(vec![]);
124+
let _ = CFDictionary::new(vec![(one, two), (three, four)]);
125+
}
126+
}

crates/mac_core/src/set.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,16 @@ impl<T: CoreType> CFSet<T> {
2626
unsafe { CFSet::new_owned(ptr) }
2727
}
2828
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
use crate::CFString;
34+
35+
#[test]
36+
fn test_new() {
37+
// Ensure empty and with-value set construction don't segfault
38+
let _ = CFSet::new(&[]);
39+
let _ = CFSet::new(&[CFString::new("foo")]);
40+
}
41+
}

crates/mac_core/src/string.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,36 @@ impl CFString {
175175
}
176176
}
177177
}
178+
179+
#[cfg(test)]
180+
mod tests {
181+
use super::*;
182+
183+
#[test]
184+
fn test_as_str() {
185+
let str = CFString::new("foo");
186+
assert_eq!(str.as_str(), Cow::Borrowed("foo"));
187+
188+
let cstr = CFString::new(c"foo");
189+
assert_eq!(str.as_str(), Cow::Borrowed("foo"));
190+
191+
let non_str = CFString::new(c"\xC3\x28bar");
192+
assert_eq!(non_str.as_str(), Cow::Owned("\0\0bar"));
193+
}
194+
195+
#[test]
196+
fn test_as_cstr() {
197+
let str = CFString::new("foo");
198+
assert_eq!(str.as_cstr(), Cow::Owned(c"foo"));
199+
200+
let cstr = CFString::new(c"foo");
201+
assert_eq!(cstr.as_cstr(), Cow::Borrowed(c"foo"));
202+
}
203+
204+
#[test]
205+
#[should_panic]
206+
fn test_as_non_cstr() {
207+
let non_cstr = CFString::new("fo\0o");
208+
non_cstr.as_cstr();
209+
}
210+
}

0 commit comments

Comments
 (0)