/
td_nan.go
110 lines (91 loc) · 2.23 KB
/
td_nan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2018, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
package td
import (
"math"
"reflect"
"github.com/maxatome/go-testdeep/internal/ctxerr"
"github.com/maxatome/go-testdeep/internal/types"
)
type tdNaN struct {
base
}
var _ TestDeep = &tdNaN{}
// summary(NaN): checks a floating number is [`math.NaN`]
// input(NaN): float
// NaN operator checks that data is a float and is not-a-number.
//
// got := math.NaN()
// td.Cmp(t, got, td.NaN()) // succeeds
// td.Cmp(t, 4.2, td.NaN()) // fails
//
// See also [NotNaN].
func NaN() TestDeep {
return &tdNaN{
base: newBase(3),
}
}
func (n *tdNaN) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
switch got.Kind() {
case reflect.Float32, reflect.Float64:
if math.IsNaN(got.Float()) {
return nil
}
return ctx.CollectError(&ctxerr.Error{
Message: "values differ",
Got: got,
Expected: n,
})
}
return ctx.CollectError(&ctxerr.Error{
Message: "type mismatch",
Got: types.RawString(got.Type().String()),
Expected: types.RawString("float32 OR float64"),
})
}
func (n *tdNaN) String() string {
return "NaN"
}
type tdNotNaN struct {
base
}
var _ TestDeep = &tdNotNaN{}
// summary(NotNaN): checks a floating number is not [`math.NaN`]
// input(NotNaN): float
// NotNaN operator checks that data is a float and is not not-a-number.
//
// got := math.NaN()
// td.Cmp(t, got, td.NotNaN()) // fails
// td.Cmp(t, 4.2, td.NotNaN()) // succeeds
// td.Cmp(t, 4, td.NotNaN()) // fails, as 4 is not a float
//
// See also [NaN].
func NotNaN() TestDeep {
return &tdNotNaN{
base: newBase(3),
}
}
func (n *tdNotNaN) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error {
switch got.Kind() {
case reflect.Float32, reflect.Float64:
if !math.IsNaN(got.Float()) {
return nil
}
return ctx.CollectError(&ctxerr.Error{
Message: "values differ",
Got: got,
Expected: n,
})
}
return ctx.CollectError(&ctxerr.Error{
Message: "type mismatch",
Got: types.RawString(got.Type().String()),
Expected: types.RawString("float32 OR float64"),
})
}
func (n *tdNotNaN) String() string {
return "not NaN"
}