github.com/go-chrono/chrono@v0.0.0-20240102183611-532f0d0d7c34/interval_test.go (about) 1 package chrono_test 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/go-chrono/chrono" 8 ) 9 10 func TestParseInterval(t *testing.T) { 11 for _, tr := range []struct { 12 str string 13 expected string 14 reps int 15 }{ 16 {"", "", 0}, 17 {"R1/", "R1/", 1}, 18 {"R10/", "R10/", 10}, 19 {"R/", "R/", -1}, 20 {"R-1/", "R/", -1}, 21 {"R-10/", "R/", -1}, 22 } { 23 for _, tt := range []struct { 24 str string 25 start chrono.OffsetDateTime 26 startOk bool 27 end chrono.OffsetDateTime 28 endOk bool 29 period chrono.Period 30 duration chrono.Duration 31 durationOk bool 32 }{ 33 { 34 str: "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z", 35 start: chrono.OffsetDateTimeOf(2007, chrono.March, 1, 13, 0, 0, 0, 0, 0), 36 startOk: true, 37 end: chrono.OffsetDateTimeOf(2008, chrono.May, 11, 15, 30, 0, 0, 0, 0), 38 endOk: true, 39 duration: chrono.DurationOf(10490*chrono.Hour + 30*chrono.Minute), 40 durationOk: true, 41 }, 42 { 43 str: "2007-03-01T13:00:00Z/P1Y2M10DT2H30M", 44 start: chrono.OffsetDateTimeOf(2007, chrono.March, 1, 13, 0, 0, 0, 0, 0), 45 startOk: true, 46 period: chrono.Period{Years: 1, Months: 2, Days: 10}, 47 duration: chrono.DurationOf(2*chrono.Hour + 30*chrono.Minute), 48 durationOk: true, 49 }, 50 { 51 str: "P1Y2M10DT2H30M/2008-05-11T15:30:00Z", 52 period: chrono.Period{Years: 1, Months: 2, Days: 10}, 53 duration: chrono.DurationOf(2*chrono.Hour + 30*chrono.Minute), 54 durationOk: true, 55 end: chrono.OffsetDateTimeOf(2008, chrono.May, 11, 15, 30, 0, 0, 0, 0), 56 endOk: true, 57 }, 58 { 59 str: "P1Y2M10DT2H30M", 60 period: chrono.Period{Years: 1, Months: 2, Days: 10}, 61 duration: chrono.DurationOf(2*chrono.Hour + 30*chrono.Minute), 62 durationOk: true, 63 }, 64 } { 65 str := tr.str + tt.str 66 67 t.Run(str, func(t *testing.T) { 68 in := str 69 expected := tr.expected + tt.str 70 71 checkParse := func(t *testing.T) chrono.Interval { 72 i, err := chrono.ParseInterval(in) 73 if err != nil { 74 t.Errorf("failed to parse interval: %v", err) 75 } else if dt, err := i.Start(); tt.startOk && (err != nil || dt.Compare(tt.start) != 0) { 76 t.Errorf("i.Start() = %v, %v, want %v, true", dt, err, tt.start) 77 } else if dt, err := i.End(); tt.endOk && (err != nil || dt.Compare(tt.end) != 0) { 78 t.Errorf("i.End() = %v, %v, want %v, true", dt, err, tt.end) 79 } else if p, d, err := i.Duration(); tt.durationOk && (err != nil || !p.Equal(tt.period) || d.Compare(tt.duration) != 0) { 80 t.Errorf("i.Duration() = %v, %v, %v, want %v, %v, true", p, d, err, tt.period, tt.duration) 81 } else if r := i.Repetitions(); r != tr.reps { 82 t.Errorf("i.Repetions() = %v, want %v", r, tr.reps) 83 } 84 return i 85 } 86 87 t.Run("slash", func(t *testing.T) { 88 i := checkParse(t) 89 90 if formatted := i.String(); formatted != expected { 91 t.Errorf("i.String() = %v, want %v", formatted, expected) 92 } 93 }) 94 95 t.Run("hyphens", func(t *testing.T) { 96 in = strings.ReplaceAll(str, "/", "--") 97 i := checkParse(t) 98 99 if formatted := i.String(); formatted != expected { 100 t.Errorf("i.String() = %v, want %v", formatted, expected) 101 } 102 }) 103 }) 104 } 105 } 106 }