|
| 1 | +package format |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestPrice(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + amount float64 |
| 9 | + currency string |
| 10 | + want string |
| 11 | + }{ |
| 12 | + {"USD", 150000, "USD", "$150,000"}, |
| 13 | + {"EUR symbol after", 150000, "EUR", "150,000 €"}, |
| 14 | + {"GBP", 99.99, "GBP", "£100"}, |
| 15 | + {"PLN symbol after", 1500, "PLN", "1,500 zł"}, |
| 16 | + {"ARS", 50000, "ARS", "$50,000"}, |
| 17 | + {"BRL", 1000, "BRL", "R$1,000"}, |
| 18 | + {"MXN", 25000, "MXN", "$25,000"}, |
| 19 | + {"unknown currency uses code", 100, "XYZ", "XYZ100"}, |
| 20 | + {"zero", 0, "USD", "$0"}, |
| 21 | + {"negative", -500, "USD", "$-500"}, |
| 22 | + } |
| 23 | + |
| 24 | + for _, tt := range tests { |
| 25 | + t.Run(tt.name, func(t *testing.T) { |
| 26 | + got := Price(tt.amount, tt.currency) |
| 27 | + if got != tt.want { |
| 28 | + t.Errorf("Price(%v, %q) = %q, want %q", tt.amount, tt.currency, got, tt.want) |
| 29 | + } |
| 30 | + }) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func TestPriceWithDecimals(t *testing.T) { |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + amount float64 |
| 38 | + currency string |
| 39 | + want string |
| 40 | + }{ |
| 41 | + {"USD", 99.99, "USD", "$99.99"}, |
| 42 | + {"EUR symbol after", 99.99, "EUR", "99.99 €"}, |
| 43 | + {"whole number", 100.00, "USD", "$100"}, |
| 44 | + {"unknown currency", 50.5, "XYZ", "XYZ50.5"}, |
| 45 | + } |
| 46 | + |
| 47 | + for _, tt := range tests { |
| 48 | + t.Run(tt.name, func(t *testing.T) { |
| 49 | + got := PriceWithDecimals(tt.amount, tt.currency) |
| 50 | + if got != tt.want { |
| 51 | + t.Errorf("PriceWithDecimals(%v, %q) = %q, want %q", tt.amount, tt.currency, got, tt.want) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestPriceRange(t *testing.T) { |
| 58 | + tests := []struct { |
| 59 | + name string |
| 60 | + min float64 |
| 61 | + max float64 |
| 62 | + currency string |
| 63 | + want string |
| 64 | + }{ |
| 65 | + {"USD range", 100, 500, "USD", "$100 - $500"}, |
| 66 | + {"EUR range", 100, 500, "EUR", "100 € - 500 €"}, |
| 67 | + {"same value", 100, 100, "USD", "$100 - $100"}, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + got := PriceRange(tt.min, tt.max, tt.currency) |
| 73 | + if got != tt.want { |
| 74 | + t.Errorf("PriceRange(%v, %v, %q) = %q, want %q", tt.min, tt.max, tt.currency, got, tt.want) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestRegisterCurrency(t *testing.T) { |
| 81 | + RegisterCurrency("CLP", "$", SymbolBefore) |
| 82 | + got := Price(1000, "CLP") |
| 83 | + want := "$1,000" |
| 84 | + if got != want { |
| 85 | + t.Errorf("Price after RegisterCurrency = %q, want %q", got, want) |
| 86 | + } |
| 87 | + |
| 88 | + RegisterCurrency("JPY", "¥", SymbolBefore) |
| 89 | + got = Price(10000, "JPY") |
| 90 | + want = "¥10,000" |
| 91 | + if got != want { |
| 92 | + t.Errorf("Price for JPY = %q, want %q", got, want) |
| 93 | + } |
| 94 | +} |
0 commit comments