report_test.go47 lines, 1.6 KB
| 1 | package wallet_test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/fiston-user/payments/internal/wallet" |
| 8 | ) |
| 9 | |
| 10 | func TestStatementRecord(t *testing.T) { |
| 11 | t.Parallel() |
| 12 | tests := []struct { |
| 13 | name string |
| 14 | description string |
| 15 | amount int64 |
| 16 | currency string |
| 17 | want string // the description, the amount, and the balance: 1,000 minor units more |
| 18 | }{ |
| 19 | {"dollars", "deposit", 1500, "USD", "deposit | 15.00 | 25.00"}, |
| 20 | {"cents out", "hold", -5, "USD", "hold | -0.05 | 9.95"}, |
| 21 | {"yen", "deposit", 1500, "JPY", "deposit | 1500 | 2500"}, |
| 22 | {"dinars", "deposit", 1500, "KWD", "deposit | 1.500 | 2.500"}, |
| 23 | {"a formula", `=HYPERLINK("https://evil.example","Refund")`, 100, "USD", |
| 24 | `'=HYPERLINK("https://evil.example","Refund") | 1.00 | 11.00`}, |
| 25 | {"plus", "+1 555 0100", 100, "USD", "'+1 555 0100 | 1.00 | 11.00"}, |
| 26 | {"minus", "-2+3", 100, "USD", "'-2+3 | 1.00 | 11.00"}, |
| 27 | {"at", "@SUM(A1:A9)", 100, "USD", "'@SUM(A1:A9) | 1.00 | 11.00"}, |
| 28 | {"tab", "\t=1+1", 100, "USD", "'\t=1+1 | 1.00 | 11.00"}, |
| 29 | {"inside is fine", "rent = 500", 100, "USD", "rent = 500 | 1.00 | 11.00"}, |
| 30 | } |
| 31 | for _, tt := range tests { |
| 32 | t.Run(tt.name, func(t *testing.T) { |
| 33 | line := wallet.StatementLine{Description: tt.description, Amount: tt.amount, |
| 34 | Balance: 1000 + tt.amount, Currency: tt.currency} |
| 35 | rec, err := line.Record() |
| 36 | if err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | if got := strings.Join(rec[1:4], " | "); got != tt.want { |
| 40 | t.Errorf("Record = %q, want %q", got, tt.want) |
| 41 | } |
| 42 | }) |
| 43 | } |
| 44 | if _, err := (wallet.StatementLine{Currency: "XYZ"}).Record(); err == nil { |
| 45 | t.Error("an unknown currency: err = nil, want an error") |
| 46 | } |
| 47 | } |