vxx / vlib / v / tests / option_struct_eq_test.v
131 lines · 118 sloc · 1.78 KB · 45ffbccabad9928f39cab7f767d3f1c3865e6971
Raw
1struct Id {
2 v int
3}
4
5struct Person {
6 name string
7 age int
8}
9
10fn test_option_struct_ne() {
11 assert ?Id(Id{
12 v: 1
13 }) != ?Id(Id{
14 v: 2
15 })
16 assert ?Id(Id{
17 v: 1
18 }) != ?Id(none)
19}
20
21fn test_option_struct_eq() {
22 assert ?Id(Id{
23 v: 1
24 }) == ?Id(Id{
25 v: 1
26 })
27 assert ?Id(none) == ?Id(none)
28}
29
30fn test_option_struct_ne_with_strings() {
31 assert ?Person(Person{
32 name: 'Alice'
33 age: 30
34 }) != ?Person(Person{
35 name: 'Bob'
36 age: 25
37 })
38 assert ?Person(Person{
39 name: 'Alice'
40 age: 30
41 }) != ?Person(none)
42}
43
44fn test_option_struct_eq_with_strings() {
45 assert ?Person(Person{
46 name: 'Alice'
47 age: 30
48 }) == ?Person(Person{
49 name: 'Alice'
50 age: 30
51 })
52 assert ?Person(none) == ?Person(none)
53}
54
55fn cmp(a ?Id, b ?Id) bool {
56 return a != b
57}
58
59fn test_option_struct_eq_in_fn() {
60 assert cmp(Id{ v: 1 }, Id{
61 v: 2
62 }) == true
63 assert cmp(Id{ v: 1 }, Id{
64 v: 1
65 }) == false
66 assert cmp(none, none) == false
67 assert cmp(Id{ v: 1 }, none) == true
68}
69
70fn make_id(v int) ?Id {
71 return Id{
72 v: v
73 }
74}
75
76fn make_none_id() ?Id {
77 return none
78}
79
80fn test_option_struct_eq_fn_call_result() {
81 assert make_id(1) == make_id(1)
82 assert make_id(1) != make_id(2)
83 assert make_none_id() == make_none_id()
84 assert make_id(1) != make_none_id()
85}
86
87fn test_option_struct_eq_in_short_circuit() {
88 a := ?Id(Id{
89 v: 1
90 })
91 b := ?Id(Id{
92 v: 1
93 })
94 c := ?Id(Id{
95 v: 2
96 })
97 assert true && a == b
98 assert true && a != c
99 assert false || a == b
100 assert !(false && a != b)
101}
102
103fn test_option_ptr_struct_eq() {
104 a := ?&Id(&Id{
105 v: 1
106 })
107 b := ?&Id(&Id{
108 v: 1
109 })
110 c := ?&Id(&Id{
111 v: 2
112 })
113 d := ?&Id(none)
114 e := ?&Id(none)
115 assert a == b
116 assert a != c
117 assert a != d
118 assert d == e
119 assert a != ?&Id(none)
120}
121
122fn test_option_ptr_struct_ne() {
123 a := ?&Id(&Id{
124 v: 1
125 })
126 b := ?&Id(&Id{
127 v: 2
128 })
129 assert a != b
130 assert a != ?&Id(none)
131}
132