@@ -34,3 +34,104 @@ func TestValueUnquoted(t *testing.T) {
34
34
}
35
35
}
36
36
}
37
+
38
+ func TestValueEqual (t * testing.T ) {
39
+ tests := []struct {
40
+ v1 , v2 Value
41
+ want bool
42
+ }{
43
+ {Value {}, Value {}, true },
44
+ {BoolValue (false ), Value {}, false },
45
+ {BoolValue (true ), Value {}, false },
46
+ {IntegerValue (0 ), Value {}, false },
47
+ {FloatValue (0 ), Value {}, false },
48
+ {StringValue ("" ), Value {}, false },
49
+ {BoolValue (false ), BoolValue (false ), true },
50
+ {BoolValue (true ), BoolValue (true ), true },
51
+ {BoolValue (true ), BoolValue (false ), false },
52
+ {IntegerValue (42 ), IntegerValue (42 ), true },
53
+ {IntegerValue (42 ), IntegerValue (- 42 ), false },
54
+ {IntegerValue (42 ), FloatValue (42 ), true },
55
+ {IntegerValue (math .MaxInt64 - 1023 ), FloatValue (math .MaxInt64 - 1023 ), true },
56
+ {IntegerValue (math .MinInt64 ), FloatValue (math .MinInt64 ), true },
57
+ {IntegerValue (42 ), FloatValue (- 42 ), false },
58
+ {FloatValue (3.14 ), FloatValue (3.14 ), true },
59
+ {FloatValue (math .NaN ()), FloatValue (42 ), false },
60
+ {FloatValue (math .NaN ()), FloatValue (math .NaN ()), false },
61
+ {StringValue ("" ), StringValue ("" ), true },
62
+ {StringValue ("" ), StringValue ("123" ), false },
63
+ {StringValue ("123" ), StringValue ("123" ), true },
64
+ {StringValue ("123" ), StringValue ("456" ), false },
65
+ {StringValue ("123" ), IntegerValue (123 ), false },
66
+
67
+ // Float values that can't be represented as an integer.
68
+ {IntegerValue (math .MaxInt64 ), FloatValue (math .MaxInt64 ), false },
69
+ {IntegerValue (math .MinInt64 ), FloatValue (math .MinInt64 - 1025 ), false },
70
+ }
71
+
72
+ for _ , test := range tests {
73
+ if got := test .v1 .Equal (test .v2 ); got && ! test .want {
74
+ t .Errorf ("%v == %v" , test .v1 , test .v2 )
75
+ } else if ! got && test .want {
76
+ t .Errorf ("%v != %v" , test .v1 , test .v2 )
77
+ }
78
+
79
+ if got := test .v2 .Equal (test .v1 ); got && ! test .want {
80
+ t .Errorf ("%v == %v" , test .v2 , test .v1 )
81
+ } else if ! got && test .want {
82
+ t .Errorf ("%v != %v" , test .v2 , test .v1 )
83
+ }
84
+ }
85
+ }
86
+
87
+ func TestValueIdenticalTo (t * testing.T ) {
88
+ identicalTests := []Value {
89
+ {},
90
+ BoolValue (false ),
91
+ BoolValue (true ),
92
+ IntegerValue (42 ),
93
+ IntegerValue (0 ),
94
+ IntegerValue (- 42 ),
95
+ FloatValue (3.14 ),
96
+ FloatValue (42 ),
97
+ FloatValue (math .Inf (1 )),
98
+ FloatValue (math .Inf (- 1 )),
99
+ FloatValue (math .NaN ()),
100
+ FloatValue (math .MinInt64 ),
101
+ FloatValue (math .MaxInt64 ),
102
+ StringValue ("" ),
103
+ StringValue ("abc" ),
104
+ }
105
+ notIdenticalTests := []struct {
106
+ v1 , v2 Value
107
+ }{
108
+ {BoolValue (false ), Value {}},
109
+ {BoolValue (true ), Value {}},
110
+ {IntegerValue (0 ), Value {}},
111
+ {FloatValue (0 ), Value {}},
112
+ {StringValue ("" ), Value {}},
113
+ {BoolValue (false ), BoolValue (true )},
114
+ {IntegerValue (123 ), IntegerValue (- 123 )},
115
+ {FloatValue (123 ), FloatValue (- 123 )},
116
+ {FloatValue (3.14 ), FloatValue (- 3.14 )},
117
+ {FloatValue (math .Inf (1 )), FloatValue (math .Inf (- 1 ))},
118
+ {StringValue ("123" ), StringValue ("456" )},
119
+ {IntegerValue (123 ), FloatValue (123 )},
120
+ {IntegerValue (123 ), StringValue ("123" )},
121
+ {FloatValue (123 ), StringValue ("123" )},
122
+ }
123
+
124
+ for _ , v := range identicalTests {
125
+ if ! v .IdenticalTo (v ) {
126
+ t .Errorf ("(%v).IdenticalTo(%v) = false; want true" , v , v )
127
+ }
128
+ }
129
+ for _ , test := range notIdenticalTests {
130
+ if test .v1 .IdenticalTo (test .v2 ) {
131
+ t .Errorf ("(%v).IdenticalTo(%v) = true; want false" , test .v1 , test .v2 )
132
+ }
133
+ if test .v2 .IdenticalTo (test .v1 ) {
134
+ t .Errorf ("(%v).IdenticalTo(%v) = true; want false" , test .v2 , test .v1 )
135
+ }
136
+ }
137
+ }
0 commit comments