Skip to content

Commit 52b58a6

Browse files
committed
Add Bool tests
1 parent a6cbfc9 commit 52b58a6

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

objc2/src/bool.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,57 @@ unsafe impl Encode for Bool {
106106
unsafe impl RefEncode for Bool {
107107
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING);
108108
}
109+
110+
#[cfg(test)]
111+
mod tests {
112+
use super::*;
113+
114+
#[test]
115+
fn test_basic() {
116+
let b = Bool::new(true);
117+
assert!(b.is_true());
118+
assert!(!b.is_false());
119+
assert!(bool::from(b));
120+
assert_eq!(b.as_raw() as usize, 1);
121+
122+
let b = Bool::new(false);
123+
assert!(!b.is_true());
124+
assert!(b.is_false());
125+
assert!(!bool::from(b));
126+
assert_eq!(b.as_raw() as usize, 0);
127+
}
128+
129+
#[test]
130+
fn test_associated_constants() {
131+
let b = Bool::YES;
132+
assert!(b.is_true());
133+
assert_eq!(b.as_raw() as usize, 1);
134+
135+
let b = Bool::NO;
136+
assert!(b.is_false());
137+
assert_eq!(b.as_raw() as usize, 0);
138+
}
139+
140+
#[test]
141+
fn test_impls() {
142+
let b: Bool = Default::default();
143+
assert!(b.is_false());
144+
145+
assert!(Bool::from(true).is_true());
146+
assert!(Bool::from(false).is_false());
147+
148+
assert!(Bool::from(true).is_true());
149+
assert!(Bool::from(false).is_false());
150+
}
151+
152+
// Can't really do this test since it won't compile on platforms where
153+
// type BOOL = bool.
154+
//
155+
// #[test]
156+
// fn test_outside_normal() {
157+
// let b = Bool::from_raw(42);
158+
// assert!(b.is_true());
159+
// assert!(!b.is_false());
160+
// assert_eq!(b.as_raw(), 42);
161+
// }
162+
}

objc2_sys/src/types.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ use crate::{
55
};
66

77
#[cfg(all(apple, not(target_arch = "aarch64")))]
8+
// C: (explicitly) signed char
89
type BOOL_INNER = i8;
910

1011
#[cfg(all(gnustep, not(target_arch = "aarch64")))]
1112
// TODO: Only if STRICT_APPLE_COMPATIBILITY is NOT defined.
1213
// TODO: (__vxworks || _WIN32) becomes BOOL = c_int.
14+
// C: unsigned char
1315
type BOOL_INNER = u8;
1416

1517
#[cfg(target_arch = "aarch64")]
18+
// C: _Bool
1619
type BOOL_INNER = bool;
1720

1821
/// The Objective-C `BOOL` type.

0 commit comments

Comments
 (0)