Debian Codegraph @

debian/missing-sources/glib/tests/value.rs

raw
1 use std::ops::Deref;
2
3 use glib::{BoxedValue, Value, prelude::*};
4
5 // FIXME all .get::<i32>() should be replaced with .get(); the compiler is totally able to infer the type itself.
6 // But somehow without some tests are failing on Windows because the type inference doesn't work or something.
7
8 // Test that `ToValue` (and conversely, `FromValue`) uphold the promised invariants
9 #[test]
10 pub fn to_value_invariants() {
11 // Inverse
12 assert_eq!(0i32, 0i32.to_value().get::<i32>().unwrap());
13 assert_eq!(0i32, 0i32.to_value().get::<i32>().unwrap());
14
15 // Idempotence
16 assert_eq!(
17 &0i32.to_value().type_(),
18 &0i32.to_value().to_value().type_()
19 );
20 assert_eq!(0i32, 0i32.to_value().to_value().get::<i32>().unwrap());
21 assert_eq!(
22 0i32,
23 0i32.to_value()
24 .get::<Value>()
25 .unwrap()
26 .get::<i32>()
27 .unwrap()
28 );
29 assert_eq!(
30 0i32,
31 0i32.to_value()
32 .get::<Value>()
33 .unwrap()
34 .get::<i32>()
35 .unwrap()
36 );
37 }
38
39 // Test that `ToValue` and `FromValue` handle nested boxed values correctly (as per the documentation)
40 #[test]
41 pub fn to_value_boxed() {
42 let x = 0i32.to_value();
43 let boxed = BoxedValue(x);
44 assert_eq!(
45 0i32,
46 boxed
47 .to_value()
48 .to_value()
49 .get::<BoxedValue>()
50 .unwrap()
51 .deref()
52 .get::<i32>()
53 .unwrap()
54 );
55 assert_eq!(
56 0i32,
57 boxed
58 .to_value()
59 .get::<Value>()
60 .unwrap()
61 .get::<BoxedValue>()
62 .unwrap()
63 .deref()
64 .get::<i32>()
65 .unwrap()
66 );
67 }