Skip to content

Commit d869865

Browse files
committed
chore: fix architecture dependent results in tests
Now we return the converted value with error, we can face issues when the architecture provides different results for the same input. For example, -1.0 as uint64 is 18446744073709551615 on Linux/amd64, but 0 on Darwin/arm64. Same for -1 to uint that is 18446744073709551615 on Linux/amd64, but -18446744073709551615 on Darwin/arm64. For this reason, we removed the converted value from the examples that fails depending on the architecture.
1 parent f8aef44 commit d869865

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

examples_64bit_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ func ExampleToUint() {
2626
fmt.Println(i, err)
2727

2828
a = int8(-1)
29-
i, err = safecast.ToUint(a)
30-
fmt.Println(i, err)
29+
_, err = safecast.ToUint(a)
30+
fmt.Println(err)
3131

3232
// Output:
3333
// 42 <nil>
34-
// 18446744073709551615 conversion issue: -1 (int8) is less than 0 (uint): minimum value for this type exceeded
34+
// conversion issue: -1 (int8) is less than 0 (uint): minimum value for this type exceeded
3535
}
3636

3737
func ExampleToInt() {
@@ -40,9 +40,10 @@ func ExampleToInt() {
4040
fmt.Println(i, err)
4141

4242
b := float32(math.MaxFloat32)
43-
i, err = safecast.ToInt(b)
44-
fmt.Println(i, err)
43+
_, err = safecast.ToInt(b)
44+
45+
fmt.Println(err)
4546
// Output:
4647
// 42 <nil>
47-
// -9223372036854775808 conversion issue: 3.4028235e+38 (float32) is greater than 9223372036854775807 (int): maximum value for this type exceeded
48+
// conversion issue: 3.4028235e+38 (float32) is greater than 9223372036854775807 (int): maximum value for this type exceeded
4849
}

examples_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ func ExampleParse() {
195195
i, err = safecast.Parse[uint64]("abc")
196196
fmt.Println(i, err)
197197

198-
i, err = safecast.Parse[uint64]("-1.1")
199-
fmt.Println(i, err)
198+
_, err = safecast.Parse[uint64]("-1.1")
199+
fmt.Println(err)
200200

201201
i, err = safecast.Parse[uint64](".12345E+5")
202202
fmt.Println(i, err)
@@ -209,6 +209,6 @@ func ExampleParse() {
209209
// 17 <nil>
210210
// 18446744073709551615 conversion issue: -1 (int64) is less than 0 (uint64): minimum value for this type exceeded
211211
// 0 conversion issue: cannot convert from string "abc" to uint64
212-
// 18446744073709551615 conversion issue: -1.1 (float64) is less than 0 (uint64): minimum value for this type exceeded
212+
// conversion issue: -1.1 (float64) is less than 0 (uint64): minimum value for this type exceeded
213213
// 12345 <nil>
214214
}

0 commit comments

Comments
 (0)