-
Notifications
You must be signed in to change notification settings - Fork 45
/
currency_test.go
138 lines (124 loc) · 3.17 KB
/
currency_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
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package currency_test
import (
"testing"
"github.com/bojanz/currency"
)
func TestForCountryCode(t *testing.T) {
tests := []struct {
countryCode string
wantCurrencyCode string
wantOK bool
}{
{"FR", "EUR", true},
{"RS", "RSD", true},
{"XX", "", false},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
gotCurrencyCode, gotOK := currency.ForCountryCode(tt.countryCode)
if gotOK != tt.wantOK {
t.Errorf("got %v, want %v", gotOK, tt.wantOK)
}
if gotCurrencyCode != tt.wantCurrencyCode {
t.Errorf("got %q, want %q", gotCurrencyCode, tt.wantCurrencyCode)
}
})
}
}
func TestGetCurrencyCodes(t *testing.T) {
currencyCodes := currency.GetCurrencyCodes()
var got [10]string
copy(got[:], currencyCodes[0:10])
want := [10]string{"AUD", "CAD", "CHF", "EUR", "GBP", "JPY", "NOK", "NZD", "SEK", "USD"}
// Confirm that the first 10 currency codes are the "G10" ones.
if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
func TestIsValid(t *testing.T) {
tests := []struct {
currencyCode string
want bool
}{
{"", true},
{"INVALID", false},
{"XXX", false},
{"usd", false},
{"USD", true},
{"EUR", true},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
got := currency.IsValid(tt.currencyCode)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestGetNumericCode(t *testing.T) {
numericCode, ok := currency.GetNumericCode("USD")
if !ok {
t.Errorf("got %v, want true", ok)
}
if numericCode != "840" {
t.Errorf("got %v, want 840", numericCode)
}
// Non-existent currency code.
numericCode, ok = currency.GetNumericCode("XXX")
if ok {
t.Errorf("got %v, want false", ok)
}
if numericCode != "000" {
t.Errorf("got %v, want 000", numericCode)
}
}
func TestGetDigits(t *testing.T) {
digits, ok := currency.GetDigits("USD")
if !ok {
t.Errorf("got %v, want true", ok)
}
if digits != 2 {
t.Errorf("got %v, want 2", digits)
}
// Non-existent currency code.
digits, ok = currency.GetDigits("XXX")
if ok {
t.Errorf("got %v, want false", ok)
}
if digits != 0 {
t.Errorf("got %v, want 0", digits)
}
}
func TestGetSymbol(t *testing.T) {
tests := []struct {
currencyCode string
locale currency.Locale
wantSymbol string
wantOk bool
}{
{"XXX", currency.NewLocale("en"), "XXX", false},
{"usd", currency.NewLocale("en"), "usd", false},
{"CHF", currency.NewLocale("en"), "CHF", true},
{"USD", currency.NewLocale("en"), "$", true},
{"USD", currency.NewLocale("en-US"), "$", true},
{"USD", currency.NewLocale("en-AU"), "US$", true},
{"USD", currency.NewLocale("es"), "US$", true},
{"USD", currency.NewLocale("es-ES"), "US$", true},
// An empty locale should use "en" data.
{"USD", currency.NewLocale(""), "$", true},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
gotSymbol, gotOk := currency.GetSymbol(tt.currencyCode, tt.locale)
if gotSymbol != tt.wantSymbol {
t.Errorf("got %v, want %v", gotSymbol, tt.wantSymbol)
}
if gotOk != tt.wantOk {
t.Errorf("got %v, want %v", gotOk, tt.wantOk)
}
})
}
}