-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag_reflect_number_big_test.go
149 lines (130 loc) · 4.57 KB
/
flag_reflect_number_big_test.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package clif
import (
"context"
"errors"
"math"
"math/big"
"reflect"
"strconv"
"testing"
)
func TestNewValueFromBigInt_success(t *testing.T) {
t.Parallel()
type testCase struct {
input FlagValue
expected *big.Int
target any
}
testCases := map[string]testCase{
// *big.Int
"*big.Int/0": {input: FlagValue{Set: true, Raw: "0"}, expected: big.NewInt(0), target: big.NewInt(0)},
"*big.Int/-1": {input: FlagValue{Set: true, Raw: "-1"}, expected: big.NewInt(-1), target: big.NewInt(0)},
"*big.Int/1": {input: FlagValue{Set: true, Raw: "1"}, expected: big.NewInt(1), target: big.NewInt(0)},
"*big.Int/max-int": {input: FlagValue{Set: true, Raw: strconv.FormatInt(math.MaxInt64, 10)}, expected: big.NewInt(math.MaxInt64), target: big.NewInt(0)},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
res, err := newValueFromBigInt(ctx, test.input, reflect.ValueOf(test.target))
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
got, ok := res.Interface().(*big.Int)
if !ok {
t.Fatalf("Result was not a *big.Int, was %T", res.Interface())
}
if got.Cmp(test.expected) != 0 {
t.Fatalf("Expected %v, got %v", test.expected, got)
}
})
}
}
func TestNewValueFromBigInt_error_syntax(t *testing.T) {
t.Parallel()
testCases := map[string]string{
"empty": "",
"invalidValue": "not a number",
}
for name, input := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var target *big.Int
_, err := newValueFromBigInt(ctx, FlagValue{Set: true, Raw: input}, reflect.ValueOf(target))
if err == nil {
t.Fatal("Expected error, got none")
}
conversionErr := InvalidConversionError{}
if !errors.As(err, &conversionErr) {
t.Fatalf("Expected clif.InvalidConversionError, got %T: %v", err, err)
}
if !conversionErr.Source.Set || conversionErr.Source.Raw != input {
t.Fatalf("Expected conversion error's source to be set with a value of %q, got set: %v value: %q", input, conversionErr.Source.Set, conversionErr.Source.Raw)
}
if !conversionErr.Target.Equal(reflect.ValueOf(target)) {
t.Fatalf("Expected conversion error's target to be %v, got %v", target, conversionErr.Target.Interface())
}
})
}
}
func TestNewValueFromBigFloat_success(t *testing.T) {
t.Parallel()
type testCase struct {
input FlagValue
expected *big.Float
target any
}
testCases := map[string]testCase{
// *big.Float
"*big.Float/0": {input: FlagValue{Set: true, Raw: "0"}, expected: big.NewFloat(0), target: big.NewFloat(0)},
"*big.Float/-1": {input: FlagValue{Set: true, Raw: "-1"}, expected: big.NewFloat(-1), target: big.NewFloat(0)},
"*big.Float/1": {input: FlagValue{Set: true, Raw: "1"}, expected: big.NewFloat(1), target: big.NewFloat(0)},
"*big.Float/max-float": {input: FlagValue{Set: true, Raw: strconv.FormatFloat(math.MaxFloat64, 'f', -1, 64)}, expected: big.NewFloat(math.MaxFloat64), target: big.NewFloat(0)},
}
for name, test := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
res, err := newValueFromBigFloat(ctx, test.input, reflect.ValueOf(test.target))
if err != nil {
t.Fatalf("Unexpected error %s", err)
}
got, ok := res.Interface().(*big.Float)
if !ok {
t.Fatalf("Expected result to be *big.Float, was %T", res.Interface())
}
if got.Cmp(test.expected) != 0 {
t.Fatalf("Expected %v, got %v", test.expected, got)
}
})
}
}
func TestNewValueFromBigFloat_error_syntax(t *testing.T) {
t.Parallel()
testCases := map[string]string{
"empty": "",
"invalidValue": "not a number",
}
for name, input := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
var target *big.Float
_, err := newValueFromBigFloat(ctx, FlagValue{Set: true, Raw: input}, reflect.ValueOf(target))
if err == nil {
t.Fatal("Expected error, got none")
}
conversionErr := InvalidConversionError{}
if !errors.As(err, &conversionErr) {
t.Fatalf("Expected clif.InvalidConversionError, got %T: %v", err, err)
}
if !conversionErr.Source.Set || conversionErr.Source.Raw != input {
t.Fatalf("Expected conversion error's source to be set with a value of %q, got set: %v value: %q", input, conversionErr.Source.Set, conversionErr.Source.Raw)
}
if !conversionErr.Target.Equal(reflect.ValueOf(target)) {
t.Fatalf("Expected conversion error's target to be %v, got %v", target, conversionErr.Target.Interface())
}
})
}
}