Exemplo n.º 1
0
func TestJSONMarshal(t *testing.T) {

	// @todo these tests will fail once i18n has been fully implemented. so fix this.
	var prefix = `"` + string(i18n.DefaultCurrencySign) + " "
	tests := []struct {
		prec      int
		haveI     int64
		haveEnc   money.JSONMarshaller
		haveValid bool
		want      string
		wantErr   error
	}{
		{100, 123456, money.JSONNumber, true, `1234.56`, nil},
		{1000, 123456, money.JSONNumber, true, `123.456`, nil},
		{10000, 123456, money.JSONNumber, true, `12.3456`, nil},
		{10, 123456, money.JSONNumber, true, `12345.6`, nil},
		{100, 123456, money.JSONNumber, false, `null`, nil},
		{0, 123456, money.JSONNumber, true, `123456`, nil},

		{100, 123456, money.JSONLocale, true, prefix + `1,234.56"`, nil},
		{1000, 123456, money.JSONLocale, true, prefix + `123.46"`, nil},
		{10000, 123456, money.JSONLocale, true, prefix + `12.35"`, nil},
		{10, 123456, money.JSONLocale, true, prefix + `12,345.60"`, nil},
		{100, 123456, money.JSONLocale, false, `null`, nil},
		{0, 123456, money.JSONLocale, true, prefix + `123,456.00"`, nil},

		{100, 123456, money.JSONExtended, true, `[1234.56, "$", "$ 1,234.56"]`, nil},
		{1000, 123456, money.JSONExtended, true, `[123.456, "$", "$ 123.46"]`, nil},
		{10000, 123456, money.JSONExtended, true, `[12.3456, "$", "$ 12.35"]`, nil},
		{10, 123456, money.JSONExtended, true, `[12345.6, "$", "$ 12,345.60"]`, nil},
		{100, 123456, money.JSONExtended, false, `null`, nil},
		{0, 123456, money.JSONExtended, true, `[123456, "$", "$ 123,456.00"]`, nil},
	}

	for _, test := range tests {
		c := money.New(
			money.Precision(test.prec),
			money.JSONMarshal(test.haveEnc),
			money.FormatCurrency(testFmtCur),
			money.FormatNumber(testFmtNum),
		).Set(test.haveI)
		c.Valid = test.haveValid

		have, err := c.MarshalJSON()
		if test.wantErr != nil {
			assert.Error(t, err, "%v", test)
			assert.Nil(t, have, "%v", test)
		} else {
			haveS := string(have)
			assert.NoError(t, err, "%v", test)
			if haveS != test.want {
				// assert.Equal... is not useful in this case
				t.Errorf("\nHave: %s\nWant: %s\n", haveS, test.want)
			}
		}
	}
}
Exemplo n.º 2
0
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.Currency
		err := c.Scan(test.src)
		c.Option(
			money.FormatCurrency(testFmtCur),
			money.FormatNumber(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)
	}
}
Exemplo n.º 3
0
func TestMulNumber(t *testing.T) {
	tests := []struct {
		prec  int
		have1 int64
		have2 int64
		want  string
	}{
		{0, 1300, 1300, "1690000.000"},
		{100, 1300, 1300, "169.000"},
		{1000, 18100, 18100, "327.610"},
		{100, 1319, 1488, "196.270"},
		{1000, 1319, 1488, "1.963"},
		{100, 13, -13, "-0.020"},
		{100, 1300, -1300, "-169.000"},
		{1000, 1300, -1300, "-1.690"},
		{100, 13, 13, "0.020"},
		{100, 45628734653, -45628734653, "250065429529630.200"}, // overflow of int64 ?
		{100, 45628734653, -456287346, "-237307016244604.920"},
		{100, math.MaxInt64, 2, "0.000"},
	}

	for _, test := range tests {
		c := money.New(
			money.Precision(test.prec),
			money.FormatCurrency(testFmtCur),
			money.FormatNumber(testFmtNum),
		).Set(test.have1)

		c = c.Mul(money.New(money.Precision(test.prec)).Set(test.have2))

		haveB, err := c.Number()
		assert.NoError(t, err)
		have := string(haveB)
		if have != test.want {
			t.Errorf("\nWant: %s\nHave: %s\nSign %d\nIndex: %v\n", test.want, have, c.Sign(), test)
		}
	}
}
Exemplo n.º 4
0
func TestString(t *testing.T) {
	tests := []struct {
		prec int
		have int64
		want string
	}{
		{0, 13, "$ 13.00"},
		{10, 13, "$ 1.30"},
		{100, 13, "$ 0.13"},
		{1000, 13, "$ 0.01"},
		{100, -13, "$ -0.13"},
		{0, -45628734653, "$ -45,628,734,653.00"},
		{10, -45628734653, "$ -4,562,873,465.30"},
		{100, -45628734653, "$ -456,287,346.53"},
		{1000, -45628734653, "$ -45,628,734.65"},
		{100, 256, "$ 2.56"},
		10: {1234, -45628734653, "$ -4,562,873.47"},
		{100, -45628734655, "$ -456,287,346.55"},
		{100, -45628734611, "$ -456,287,346.11"},
		{100, -45628734699, "$ -456,287,346.99"},
		14: {10000000, 45628734699, "$ 4,562.87"},
		15: {10000000, 45628734655, "$ 4,562.87"},
	}

	for i, test := range tests {
		c := money.New(
			money.Precision(test.prec),
			money.FormatCurrency(testFmtCur),
			money.FormatNumber(testFmtNum),
		).Set(test.have)
		have := c.String()
		if have != test.want {
			t.Errorf("\nWant: %s\nHave: %s\nIndex: %d\n", test.want, have, i)
		}
	}

}
Exemplo n.º 5
0
func TestSwedishNumber(t *testing.T) {

	tests := []struct {
		prec int
		iv   money.Interval
		have int64
		want string
	}{
		{0, money.Interval005, 25689, "25689.000"},
		{100, money.Interval005, 25600, "256.000"},
		{10, money.Interval005, 25689, "2568.900"},
		{100, money.Interval005, 25689, "256.900"},
		{1000, money.Interval005, 25689, "25.700"},
		{100, money.Interval005, 25642, "256.400"},
		{100, money.Interval005, 25644, "256.450"},

		{0, money.Interval010, 25689, "25689.000"},
		{10, money.Interval010, 25689, "2568.900"},
		{100, money.Interval010, 25689, "256.900"},
		{1000, money.Interval010, 25689, "25.700"},
		{100, money.Interval010, 25642, "256.400"},
		{100, money.Interval010, 25644, "256.400"},
		{100, money.Interval010, 25639, "256.400"},
		{100, money.Interval010, 25635, "256.400"},
		{100, money.Interval010, 25634, "256.300"},
		{100, money.Interval010, 256345, "2563.500"},

		{0, money.Interval015, 25689, "25689.000"},
		{10, money.Interval015, 25689, "2568.900"},
		{10, money.Interval015, 25685, "2568.400"},
		{100, money.Interval015, 25689, "256.900"},
		{1000, money.Interval015, 25689, "25.700"},
		{100, money.Interval015, 25642, "256.400"},
		{100, money.Interval015, 25644, "256.400"},
		{100, money.Interval015, 25639, "256.400"},
		{100, money.Interval015, 25635, "256.300"},
		{100, money.Interval015, 25636, "256.400"},
		{100, money.Interval015, 25634, "256.300"},
		{100, money.Interval015, 256345, "2563.400"},

		{0, money.Interval025, 25689, "25689.000"},
		{10, money.Interval025, 25689, "2569.000"},
		{10, money.Interval025, 25685, "2568.500"},
		{100, money.Interval025, 25689, "257.000"},
		{1000, money.Interval025, 25689, "25.750"},
		{100, money.Interval025, 25642, "256.500"},
		{100, money.Interval025, 25644, "256.500"},
		{100, money.Interval025, 25639, "256.500"},
		{100, money.Interval025, 25624, "256.250"},
		{100, money.Interval025, 25625, "256.250"},
		{100, money.Interval025, 25634, "256.250"},
		{100, money.Interval025, 256345, "2563.500"},

		{0, money.Interval050, 25689, "25689.000"},
		{10, money.Interval050, 25689, "2569.000"},
		{10, money.Interval050, 25685, "2568.500"},
		{100, money.Interval050, 25689, "257.000"},
		{1000, money.Interval050, 25689, "25.500"},
		{100, money.Interval050, 25642, "256.500"},
		{100, money.Interval050, 25644, "256.500"},
		{100, money.Interval050, 25639, "256.500"},
		{100, money.Interval050, 25624, "256.000"},
		{100, money.Interval050, 25625, "256.500"},
		{100, money.Interval050, 25634, "256.500"},
		{100, money.Interval050, 256345, "2563.500"},

		{0, money.Interval100, 25689, "25689.000"},
		{10, money.Interval100, 25689, "2569.000"},
		{10, money.Interval100, 25685, "2569.000"},
		{10, money.Interval100, 25684, "2568.000"},
		{100, money.Interval100, 25689, "257.000"},
		{1000, money.Interval100, 25689, "26.000"},
		{100, money.Interval100, 25642, "256.000"},
		{100, money.Interval100, 25644, "256.000"},
		{100, money.Interval100, 25639, "256.000"},
		{100, money.Interval100, 25624, "256.000"},
		{100, money.Interval100, 25625, "256.000"},
		{100, money.Interval100, 25634, "256.000"},
		{100, money.Interval100, 256345, "2563.000"},
	}
	for _, test := range tests {
		c := money.New(
			money.Precision(test.prec),
			money.FormatCurrency(testFmtCur),
			money.FormatNumber(testFmtNum),
		).Set(test.have)

		haveB, err := c.Swedish(money.Swedish(test.iv)).Number()
		assert.NoError(t, err, "%v", test)
		have := string(haveB)
		if have != test.want {
			t.Errorf("\nWant: %s\nHave: %s\nIndex: %v\n", test.want, have, test)
		}
	}
}
Exemplo n.º 6
0
func TestJSONUnMarshalSlice(t *testing.T) {

	tests := []struct {
		haveEnc  money.JSONMarshaller
		jsonData []byte
		want     string
		wantErr  error
	}{
		{
			money.JSONNumber, []byte(`[{"ValueID":1,"AttributeID":73,"StoreID":0,"EntityID":1,"Value":1999.0000},{"ValueID":2,"AttributeID":78,"StoreID":0,"EntityID":1,"Value":null},{"ValueID":3,"AttributeID":74,"StoreID":0,"EntityID":1,"Value":-0.01},{"ValueID":4,"AttributeID":77,"StoreID":0,"EntityID":1,"Value":null},{"ValueID":5,"AttributeID":73,"StoreID":1,"EntityID":1,"Value":705.9933},{"ValueID":6,"AttributeID":73,"StoreID":4,"EntityID":1,"Value":705.9933},{"ValueID":7,"AttributeID":73,"StoreID":2,"EntityID":1,"Value":705.9933},{"ValueID":8,"AttributeID":73,"StoreID":3,"EntityID":1,"Value":705.9933}]`),
			`1999.000; NaN; -0.010; NaN; 705.993; 705.993; 705.993; 705.993; `,
			nil,
		},
		{
			money.JSONNumber, []byte(`[
			{"ValueID":1,"AttributeID":73,"StoreID":0,"EntityID":1,"Value": "2999.0156"},
			{"ValueID":2,"AttributeID":78,"StoreID":0,"EntityID":1,"Value":null},
			{"ValueID":3,"AttributeID":74,"StoreID":0,"EntityID":1,"Value":0.01},
			{"ValueID":4,"AttributeID":77,"StoreID":0,"EntityID":1,"Value":null},
			{"ValueID":5,"AttributeID":73,"StoreID":1,"EntityID":1,"Value":7059933},
			{"ValueID":6,"AttributeID":73,"StoreID":4,"EntityID":1,"Value":705.9933},
			{"ValueID":7,"AttributeID":73,"StoreID":2,"EntityID":1,"Value":705.9933},
			{"ValueID":8,"AttributeID":73,"StoreID":3,"EntityID":1,"Value":705.9933}
			]`),
			`2999.016; NaN; 0.010; NaN; 7059933.000; 705.993; 705.993; 705.993; `,
			nil,
		},
		{
			money.JSONNumber, []byte(`[{"ValueID":1,"AttributeID":73,"StoreID":0,"EntityID":1,"Value": "2999x.0156"}]`),
			`2999.016; `,
			nil,
		},
		{
			money.JSONLocale, []byte(`[
			{"ValueID":1,"AttributeID":73,"StoreID":0,"EntityID":1,"Value":"$ 999.00 "},
			{"ValueID":2,"AttributeID":78,"StoreID":0,"EntityID":1,"Value":"2 345 678,45 €"},
			{"ValueID":3,"AttributeID":74,"StoreID":0,"EntityID":1,"Value":null},
			{"ValueID":5,"AttributeID":73,"StoreID":1,"EntityID":1,"Value":"1.705,99 €"},
			{"ValueID":6,"AttributeID":73,"StoreID":4,"EntityID":1,"Value":"$ 5,123,705.94"},
			{"ValueID":7,"AttributeID":73,"StoreID":2,"EntityID":1,"Value":"$ -6,705.99"},
			{"ValueID":8,"AttributeID":73,"StoreID":3,"EntityID":1,"Value":"$ 705.99"},
			{"ValueID":8,"AttributeID":73,"StoreID":3,"EntityID":1,"Value":"$ 70789"}
			]`),
			`999.000; 2345678.450; NaN; 1705.990; 5123705.940; -6705.990; 705.990; 70789.000; `,
			nil,
		},
		{
			money.JSONExtended, []byte(`[{"ValueID":1,"AttributeID":73,"StoreID":0,"EntityID":1,"Value":[999.0000,"$","$ 999.00"]},
					{"ValueID":2,"AttributeID":78,"StoreID":0,"EntityID":1,"Value":null},
					{"ValueID":3,"AttributeID":74,"StoreID":0,"EntityID":1,"Value":null},
					{"ValueID":4,"AttributeID":77,"StoreID":0,"EntityID":1,"Value":null},
					{"ValueID":5,"AttributeID":73,"StoreID":1,"EntityID":1,"Value":[705.9933,"$","$ 705.99"]},
					{"ValueID":6,"AttributeID":73,"StoreID":4,"EntityID":1,"Value":[705.9933,"$","$ 705.99"]},
					{"ValueID":7,"AttributeID":73,"StoreID":2,"EntityID":1,"Value":[705.9933,"$","$ 705.99"]},
					{"ValueID":8,"AttributeID":73,"StoreID":3,"EntityID":1,"Value":[705.9933,"$","$ 705.99"]}
					]`),
			`999.000; NaN; NaN; NaN; 705.993; 705.993; 705.993; 705.993; `,
			nil,
		},
	}

	for _, test := range tests {
		var peds TableProductEntityDecimalSlice
		if err := json.Unmarshal(test.jsonData, &peds); err != nil {
			t.Error(err)
		}

		var buf bytes.Buffer
		for _, ped := range peds {
			ped.Value.Option(
				money.FormatNumber(testFmtNum),
			)
			_, err := ped.Value.NumberWriter(&buf)
			if test.wantErr != nil {
				assert.Error(t, err)
				assert.EqualError(t, err, test.wantErr.Error())
			} else {
				assert.NoError(t, err)
			}
			buf.WriteString("; ")
		}
		have := buf.String()
		if test.want != have {
			t.Errorf("\nHave: %s\n\nWant: %s\n", have, test.want)
		}
	}
}