|
| 1 | +package serpent_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + serpent "github.com/coder/serpent" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDuration(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + input string |
| 18 | + expected time.Duration |
| 19 | + wantErr bool |
| 20 | + }{ |
| 21 | + // Standard time.Duration formats (should still work) |
| 22 | + { |
| 23 | + name: "Nanoseconds", |
| 24 | + input: "100ns", |
| 25 | + expected: 100 * time.Nanosecond, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "Microseconds", |
| 29 | + input: "100us", |
| 30 | + expected: 100 * time.Microsecond, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "Milliseconds", |
| 34 | + input: "100ms", |
| 35 | + expected: 100 * time.Millisecond, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Seconds", |
| 39 | + input: "30s", |
| 40 | + expected: 30 * time.Second, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "Minutes", |
| 44 | + input: "5m", |
| 45 | + expected: 5 * time.Minute, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "Hours", |
| 49 | + input: "2h", |
| 50 | + expected: 2 * time.Hour, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Combined", |
| 54 | + input: "1h30m", |
| 55 | + expected: 90 * time.Minute, |
| 56 | + }, |
| 57 | + // New formats with days and weeks support |
| 58 | + { |
| 59 | + name: "Days", |
| 60 | + input: "1d", |
| 61 | + expected: 24 * time.Hour, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "MultipleDays", |
| 65 | + input: "7d", |
| 66 | + expected: 7 * 24 * time.Hour, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Weeks", |
| 70 | + input: "1w", |
| 71 | + expected: 7 * 24 * time.Hour, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "MultipleWeeks", |
| 75 | + input: "2w", |
| 76 | + expected: 14 * 24 * time.Hour, |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "CombinedWithDays", |
| 80 | + input: "1d12h", |
| 81 | + expected: 36 * time.Hour, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "CombinedWithWeeks", |
| 85 | + input: "1w2d", |
| 86 | + expected: (7 + 2) * 24 * time.Hour, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "ComplexCombination", |
| 90 | + input: "2w3d4h5m6s", |
| 91 | + expected: (14 + 3) * 24 * time.Hour + 4*time.Hour + 5*time.Minute + 6*time.Second, |
| 92 | + }, |
| 93 | + // Error cases |
| 94 | + { |
| 95 | + name: "Invalid", |
| 96 | + input: "invalid", |
| 97 | + wantErr: true, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "Empty", |
| 101 | + input: "", |
| 102 | + wantErr: true, |
| 103 | + }, |
| 104 | + } |
| 105 | + |
| 106 | + for _, tt := range tests { |
| 107 | + t.Run(tt.name, func(t *testing.T) { |
| 108 | + t.Parallel() |
| 109 | + |
| 110 | + var d serpent.Duration |
| 111 | + err := d.Set(tt.input) |
| 112 | + |
| 113 | + if tt.wantErr { |
| 114 | + require.Error(t, err) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + require.NoError(t, err) |
| 119 | + require.Equal(t, tt.expected, d.Value()) |
| 120 | + |
| 121 | + // Verify String() returns a parseable value |
| 122 | + str := d.String() |
| 123 | + var d2 serpent.Duration |
| 124 | + err = d2.Set(str) |
| 125 | + require.NoError(t, err) |
| 126 | + require.Equal(t, d.Value(), d2.Value(), "String() should return a parseable value") |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestDurationOf(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + |
| 134 | + td := 5 * time.Minute |
| 135 | + d := serpent.DurationOf(&td) |
| 136 | + require.NotNil(t, d) |
| 137 | + require.Equal(t, td, d.Value()) |
| 138 | + |
| 139 | + // Test modification through pointer |
| 140 | + newVal := 10 * time.Minute |
| 141 | + *d = serpent.Duration(newVal) |
| 142 | + require.Equal(t, newVal, time.Duration(td)) |
| 143 | +} |
0 commit comments