mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
test: add test case for Decimal.UnmarshalJSON
This commit is contained in:
parent
4d6a9ddee4
commit
a1d592f085
1 changed files with 70 additions and 0 deletions
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -595,3 +596,72 @@ func BenchmarkPrettyDuration(b *testing.B) {
|
||||||
}
|
}
|
||||||
b.Logf("Post %s", a)
|
b.Logf("Post %s", a)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecimalUnmarshalJSON(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
input []byte
|
||||||
|
}
|
||||||
|
d := Decimal(0)
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
d *Decimal
|
||||||
|
args args
|
||||||
|
wantErr bool
|
||||||
|
expectedVal uint64
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "invalid number string",
|
||||||
|
d: &d,
|
||||||
|
args: args{[]byte(``)},
|
||||||
|
wantErr: true,
|
||||||
|
}, {
|
||||||
|
name: "invalid number string",
|
||||||
|
d: &d,
|
||||||
|
args: args{[]byte(`"`)},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid empty string",
|
||||||
|
d: &d,
|
||||||
|
args: args{[]byte(`""`)},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "invalid negetive number string",
|
||||||
|
d: &d,
|
||||||
|
args: args{[]byte(`"-1"`)},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid number 0 string",
|
||||||
|
d: &d,
|
||||||
|
args: args{[]byte(`"0"`)},
|
||||||
|
wantErr: false,
|
||||||
|
expectedVal: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid number MaxInt64 string",
|
||||||
|
d: &d,
|
||||||
|
args: args{[]byte(fmt.Sprintf(`"%d"`, math.MaxInt64))},
|
||||||
|
wantErr: false,
|
||||||
|
expectedVal: math.MaxInt64,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "valid number MaxUint64 string",
|
||||||
|
d: &d,
|
||||||
|
args: args{[]byte(fmt.Sprintf(`"%d"`, uint64(math.MaxUint64)))},
|
||||||
|
wantErr: false,
|
||||||
|
expectedVal: math.MaxUint64,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if err := tt.d.UnmarshalJSON(tt.args.input); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("Decimal.UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
if !tt.wantErr && uint64(d) != tt.expectedVal {
|
||||||
|
t.Errorf("Decimal.UnmarshalJSON() doesn't expected:\n got %d\nwant %d", uint64(d), tt.expectedVal)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue