|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestDailyParser(t *testing.T){ |
| 9 | + tests := []struct{ |
| 10 | + name string |
| 11 | + tag string |
| 12 | + expected time.Time |
| 13 | + wantErr bool |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "valid begining of the year tag", |
| 17 | + tag: "d_2024_01_01", |
| 18 | + expected: time.Date(2024,1,1,0,0,0,0,time.UTC), |
| 19 | + wantErr: false, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "valid may 21 2025", |
| 23 | + tag: "d_2025_05_21", |
| 24 | + expected: time.Date(2025,5,21,0,0,0,0,time.UTC), |
| 25 | + wantErr: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "invalid daily", |
| 29 | + tag: "d_2025_05", |
| 30 | + wantErr: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "invalid daily, impossible day format", |
| 34 | + tag: "d_2025_05_32", |
| 35 | + wantErr: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "invalid daily, bad date format", |
| 39 | + tag: "d_2025_05_3232", |
| 40 | + wantErr: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "invalid daily, impossible month", |
| 44 | + tag: "d_2025_14_30", |
| 45 | + wantErr: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "invalid daily, bad year format", |
| 49 | + tag: "d_225_05_09", |
| 50 | + wantErr: true, |
| 51 | + }, |
| 52 | + } |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T){ |
| 55 | + got, err := parse_d_tag(tt.tag) |
| 56 | + if (err != nil) != tt.wantErr{ |
| 57 | + t.Errorf("parse_d_tag(%s) got %v, want %v. Error: %s",tt.tag, got, tt.expected, err) |
| 58 | + } |
| 59 | + if !tt.wantErr && !got.Equal(tt.expected){ |
| 60 | + t.Errorf("parse_d_tag(%s) = %v, want %v",tt.tag, got, tt.expected) |
| 61 | + } |
| 62 | + }) |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +func TestWeeklyParser(t *testing.T) { |
| 68 | + tests := []struct{ |
| 69 | + name string |
| 70 | + tag string |
| 71 | + expected time.Time |
| 72 | + wantErr bool |
| 73 | + }{ |
| 74 | + { |
| 75 | + name: "valid begining of the year tag", |
| 76 | + tag: "W_2024_01", |
| 77 | + expected: time.Date(2024,1,1,0,0,0,0,time.UTC), |
| 78 | + wantErr: false, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "valid may 21 2025", |
| 82 | + tag: "w_2025_21", |
| 83 | + expected: time.Date(2025,5,21,0,0,0,0,time.UTC), |
| 84 | + wantErr: false, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "valid Jan 29 2025", |
| 88 | + tag: "w_2025_05", |
| 89 | + expected: time.Date(2025,1,29,0,0,0,0,time.UTC), |
| 90 | + wantErr: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "valid tag", |
| 94 | + tag: "W_2024_12", |
| 95 | + expected: time.Date(2024,3,18,0,0,0,0,time.UTC), |
| 96 | + wantErr: false, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "valid ending of the year tag", |
| 100 | + tag: "W_2024_52", |
| 101 | + expected: time.Date(2024,12,23,0,0,0,0,time.UTC), |
| 102 | + wantErr: false, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "valid 12/24/2025 tag", |
| 106 | + tag: "W_2025_52", |
| 107 | + expected: time.Date(2025,12,24,0,0,0,0,time.UTC), |
| 108 | + wantErr: false, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "invalid tag", |
| 112 | + tag: "W_2025_df", |
| 113 | + wantErr: true, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "invalid tag wrong year format", |
| 117 | + tag: "W_205_01", |
| 118 | + wantErr: true, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "invalid tag wrong week format", |
| 122 | + tag: "W_2025_1", |
| 123 | + wantErr: true, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "invalid tag impossible week", |
| 127 | + tag: "W_2025_54", |
| 128 | + wantErr: true, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tt := range tests { |
| 133 | + t.Run(tt.name, func(t *testing.T){ |
| 134 | + got, err := parse_w_tag(tt.tag) |
| 135 | + if (err != nil) != tt.wantErr{ |
| 136 | + t.Errorf("parse_w_tag(%s) error = %v, want %v. ",tt.tag, got, tt.expected) |
| 137 | + } |
| 138 | + if !tt.wantErr && !got.Equal(tt.expected){ |
| 139 | + t.Errorf("parse_w_tag(%s) = %v, want %v",tt.tag, got, tt.expected) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments