|
| 1 | +package t_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + calT "github.com/invopop/gobl.html/components/t" |
| 9 | + "github.com/invopop/gobl.html/internal" |
| 10 | + "github.com/invopop/gobl/cal" |
| 11 | + "github.com/invopop/gobl/num" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +func TestLocalizeDateTime(t *testing.T) { |
| 16 | + // 2026-03-25T12:34 – matches the co-dian-invoice example that has both |
| 17 | + // issue_date and issue_time, which is the scenario fixed by APP-472. |
| 18 | + dt := cal.MakeDateTime(2026, time.March, 25, 12, 34, 0) |
| 19 | + |
| 20 | + nf := num.Formatter{} |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + date string |
| 24 | + timeF string |
| 25 | + expected string |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "ISO defaults", |
| 29 | + date: "", |
| 30 | + timeF: "", |
| 31 | + expected: "2026-03-25 · 12:34", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "custom date format keeps date_format when issue_time present", |
| 35 | + date: "02/01/2006", |
| 36 | + timeF: "", |
| 37 | + expected: "25/03/2026 · 12:34", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "custom date and time format", |
| 41 | + date: "02/01/2006", |
| 42 | + timeF: "3:04 PM", |
| 43 | + expected: "25/03/2026 · 12:34 PM", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "default date with custom time format", |
| 47 | + date: "", |
| 48 | + timeF: "3:04 PM", |
| 49 | + expected: "2026-03-25 · 12:34 PM", |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tc := range tests { |
| 54 | + t.Run(tc.name, func(t *testing.T) { |
| 55 | + cf := internal.CalFormatterISO |
| 56 | + if tc.date != "" { |
| 57 | + cf.Date = tc.date |
| 58 | + } |
| 59 | + if tc.timeF != "" { |
| 60 | + cf.Time = tc.timeF |
| 61 | + } |
| 62 | + |
| 63 | + opts := &internal.Opts{ |
| 64 | + CalFormatter: &cf, |
| 65 | + NumFormatter: &nf, |
| 66 | + } |
| 67 | + ctx := internal.WithOptions(context.Background(), opts) |
| 68 | + |
| 69 | + got := calT.Localize(ctx, dt) |
| 70 | + assert.Equal(t, tc.expected, got) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments