func TestJSONUnMarshalSingle(t *testing.T) { tests := []struct { haveEnc money.Encoder jsonData []byte want string wantErr error }{ {money.JSONNumber, []byte{0xf1, 0x32, 0xd8, 0x8a, 0x12, 0x8a, 0x74, 0x2a, 0x5, 0x5d, 0x18, 0x39, 0xf9, 0xd7, 0x99, 0x8b}, `NaN`, nil}, {money.JSONNumber, []byte(`1999.0000`), `1999.0000`, nil}, {money.JSONNumber, []byte(`-0.01`), `-0.0100`, nil}, {money.JSONNumber, []byte(`null`), `NaN`, nil}, {money.JSONNumber, []byte(`1234.56789`), `1234.5679`, nil}, {money.JSONNumber, []byte(`-1234.56789`), `-1234.5679`, nil}, {money.JSONNumber, []byte(`2999x.0156`), `2999.0156`, nil}, {money.JSONNumber, []byte(`""`), `NaN`, nil}, {money.JSONLocale, []byte(`$ 999.00 `), `999.0000`, nil}, {money.JSONLocale, []byte(`EUR 999.00`), `999.0000`, nil}, {money.JSONLocale, []byte(`EUR 99x9.0'0`), `999.0000`, nil}, {money.JSONLocale, []byte("EUR \x00 99x9.0'0"), `999.0000`, nil}, {money.JSONLocale, []byte(`2 345 678,45 €`), `2345678.4500`, nil}, {money.JSONLocale, []byte(`2 345 367,834456 €`), `2345367.8345`, nil}, {money.JSONLocale, []byte(`null`), `NaN`, nil}, {money.JSONLocale, []byte(`1.705,99 €`), `1705.9900`, nil}, {money.JSONLocale, []byte(`705,99 €`), `705.9900`, nil}, {money.JSONLocale, []byte(`$ 5,123,705.94`), `5123705.9400`, nil}, {money.JSONLocale, []byte(`$ -6,705.99`), `-6705.9900`, nil}, {money.JSONLocale, []byte(`$ 705.99`), `705.9900`, nil}, {money.JSONLocale, []byte(`$ 70789`), `70789.0000`, nil}, {money.JSONExtended, []byte(`[999.0000,"$","$ 999.00"]`), `999.0000`, nil}, {money.JSONExtended, []byte(`[999.0000,null,null]`), `999.0000`, nil}, {money.JSONExtended, []byte(`[1,999.00236,null,null]`), `1.0000`, nil}, {money.JSONExtended, []byte(`[1999.00236,null,null]`), `1999.0024`, nil}, {money.JSONExtended, []byte(`null`), `NaN`, nil}, {money.JSONExtended, []byte(`[null,"$",null]`), `NaN`, nil}, {money.JSONExtended, []byte(`[null,null,null]`), `NaN`, nil}, {money.JSONExtended, []byte(`[ ]`), `NaN`, money.ErrDecodeMissingColon}, } for _, test := range tests { var c money.Money err := c.UnmarshalJSON(test.jsonData) if test.wantErr != nil { assert.Error(t, err) assert.EqualError(t, err, test.wantErr.Error()) } else { var buf []byte assert.NoError(t, err) buf = c.FtoaAppend(buf) have := string(buf) if test.want != have { t.Errorf("\nHave: %s\n\nWant: %s\n", have, test.want) } } } }
func benchmark_JSONUnMarshalSingle(b *testing.B, data []byte, want int64) { b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { var c money.Money if err := c.UnmarshalJSON(data); err != nil { b.Error(err) } benchmark_JSONUnMarshalSingleValue = c.Raw() if benchmark_JSONUnMarshalSingleValue != want { b.Errorf("Have: %d\nWant: %d", benchmark_JSONUnMarshalSingleValue, want) } } }
// Benchmark_MoneyScan 3000000 504 ns/op 136 B/op 2 allocs/op => Go 1.4.2 // Benchmark_MoneyScan 5000000 386 ns/op 144 B/op 2 allocs/op => Go 1.5.0 func Benchmark_MoneyScan(b *testing.B) { var d interface{} d = []byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33} var want float64 = 705.993300 b.ReportAllocs() b.ResetTimer() for i := 0; i < b.N; i++ { var c money.Money c.Scan(d) benchmarkMoneyScan = c.Getf() if benchmarkMoneyScan != want { b.Errorf("Have: %f\nWant: %f", benchmarkMoneyScan, want) } } }
func TestScan(t *testing.T) { tests := []struct { src interface{} want string wantErr error }{ {nil, `NaN`, nil}, {[]byte{0x39, 0x39, 0x39, 0x2e, 0x30, 0x30, 0x30, 0x30}, `999.0000`, nil}, {[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil}, {[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil}, {[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil}, {[]byte{0x37, 0x30, 0x35, 0x2e, 0x39, 0x39, 0x33, 0x33}, `705.9933`, nil}, {[]byte{0x37, 0x30, 0x35, 0x2e, 0x19, 0x39, 0x33, 0x13}, `0.0000`, strconv.ErrSyntax}, {[]byte{0x37, 0x33}, `73.0000`, nil}, {[]byte{0x37, 0x38}, `78.0000`, nil}, {[]byte{0x37, 0x34}, `74.0000`, nil}, {[]byte{0x37, 0x37}, `77.0000`, nil}, {[]byte{0xa7, 0x3e}, `0.0000`, strconv.ErrSyntax}, {int(33), `0.0000`, errors.New("Unsupported Type int for value. Supported: []byte")}, } var buf bytes.Buffer for _, test := range tests { var c money.Money err := c.Scan(test.src) c.FmtCur = testFmtCur c.FmtNum = testFmtNum if test.wantErr != nil { assert.Error(t, err, "%v", test) assert.Contains(t, err.Error(), test.wantErr.Error()) } else { assert.NoError(t, err, "%v", test) assert.EqualValues(t, test.want, string(c.Ftoa()), "%v", test) if _, err := c.NumberWriter(&buf); err != nil { t.Error(err) } buf.WriteString("; ") } } want := `NaN; 999.000; 705.993; 705.993; 705.993; 705.993; 73.000; 78.000; 74.000; 77.000; ` have := buf.String() if want != have { t.Errorf("\nHave: %s\n\nWant: %s\n", have, want) } }