Skip to content

Commit

Permalink
add more unit test cases for periods
Browse files Browse the repository at this point in the history
  • Loading branch information
BugsGuru committed Jun 18, 2024
1 parent 1cb3f6d commit 0151220
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 25 deletions.
1 change: 1 addition & 0 deletions pkg/periods/periods.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func ParsePeriods(s string) (Periods, error) {
ps := make(Periods, len(start2ends))
for k, v := range start2ends {
p := Period{}
// if sth follows "%d:%d-%d:%d", it will be ignored
_, err := fmt.Sscanf(v, "%d:%d-%d:%d", &p.StartHour, &p.StartMinute, &p.EndHour, &p.EndMinute)
if err != nil {
return nil, err
Expand Down
149 changes: 124 additions & 25 deletions pkg/periods/periods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,68 +292,167 @@ func TestParsePeriods(t *testing.T) {
wantErr bool
}{
{
name: "ok",
args: "09:30-11:30;11:30-13:30;20:30-21:30",
name: "blank string",
args: "",
want: nil,
wantErr: true,
},
{
name: "invalid string",
args: "whatever",
want: nil,
wantErr: true,
},

{
name: "ok 1 period",
args: "09:30-11:30",
want: []*Period{
{9, 30, 11, 30},
{11, 30, 13, 30},
{20, 30, 21, 30},
},
wantErr: false,
},
{
name: "ok period disorder",
args: "09:30-11:30;20:30-21:30;11:30-13:30",
name: "ok 1 period, support(-0)",
args: "-0:10-11:-0",
want: []*Period{
{0, 10, 11, 0},
},
wantErr: false,
},
{
name: "fail 1 period",
args: "09:30a-11:30",
want: nil,
wantErr: true,
},
{
name: "fail 1 period",
args: "09:30 11:30",
want: nil,
wantErr: true,
},
{
name: "fail 1 period",
args: "09:00-11:-10",
want: nil,
wantErr: true,
},
{
name: "fail 1 period, invalid minute",
args: "09:60-11:30",
want: nil,
wantErr: true,
},
{
name: "fail 1 period, invalid hour",
args: "09:30-24:30",
want: nil,
wantErr: true,
},
{
name: "fail 1 period",
args: "9:30-11:300",
want: nil,
wantErr: true,
},
{
name: "fail 1 period, invalid period",
args: "09:30-08:30",
want: nil,
wantErr: true,
},

{
name: "ok 2 periods, no leading zero",
args: "9:30-11:30;11:30-13:30",
want: []*Period{
{9, 30, 11, 30},
{20, 30, 21, 30},
{11, 30, 13, 30},
},
wantErr: false,
},
{
name: "ok no leading zero",
args: "9:3-11:30;20:30-21:30;11:30-13:30",
name: "ok 2 periods, periods disorder",
args: "11:30-13:30;9:30-11:30",
want: []*Period{
{9, 3, 11, 30},
{20, 30, 21, 30},
{11, 30, 13, 30},
{9, 30, 11, 30},
},
wantErr: false,
},
{
name: "fail blank string",
args: "",
want: nil,
wantErr: true,
name: "ok 2 periods,support(-0)",
args: "-0:-0--0:30;9:-0-11:-0",
want: []*Period{
{0, 0, 0, 30},
{9, 0, 11, 0},
},
wantErr: false,
},
{
name: "fail suffix ;",
args: "09:30-11:30;",
name: "fail 2 periods,blank",
args: "11:30-13:30;",
want: nil,
wantErr: true,
},
{
name: "fail invalid period",
args: "09:30-09:30",
name: "fail 2 periods, unexpected newline",
args: "11:3-13:3;21:0-\n23:0",
want: nil,
wantErr: true,
},
{
name: "fail invalid period",
args: "09:30-08:30",
name: "fail 2 periods, periods overlap",
args: "9:3-51:30;9:30-21:30",
want: nil,
wantErr: true,
},

{
name: "fail invalid hour",
args: "09:30-24:30",
name: "ok 3 periods disorder",
args: "09:30-11:30;20:30-21:30;11:30-13:30",
want: []*Period{
{9, 30, 11, 30},
{20, 30, 21, 30},
{11, 30, 13, 30},
},
wantErr: false,
},
{
name: "ok 3 periods, no leading zero",
args: "9:3-11:30;20:30-21:30;11:30-13:30",
want: []*Period{
{9, 3, 11, 30},
{20, 30, 21, 30},
{11, 30, 13, 30},
},
wantErr: false,
},
{
name: "fail 3 periods, periods overlap",
args: "9:3-51:30;9:30-21:30;11:30-13:30",
want: nil,
wantErr: true,
},

{
name: "ok",
args: "01:30-2:00;2:30-3:0;6:30-7:0;3:30-4:0;7:30-8:0;8:30-9:00;9:30-10:0",
want: []*Period{
{1, 30, 2, 0},
{2, 30, 3, 0},
{6, 30, 7, 0},
{3, 30, 4, 0},
{7, 30, 8, 0},
{8, 30, 9, 0},
{9, 30, 10, 0},
},
wantErr: false,
},
{
name: "fail invalid minute",
args: "09:30-09:60",
name: "fail ;;",
args: "01:30-2:00;2:30-3:0;;6:30-7:0;3:30-4:0;7:30-8:0;8:30-9:00;9:30-10:0",
want: nil,
wantErr: true,
},
Expand Down

0 comments on commit 0151220

Please sign in to comment.