209 lines
6.2 KiB
Go
209 lines
6.2 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func ptr[T any](v T) *T { return &v }
|
||
|
|
|
||
|
|
func TestParseDettaglio(t *testing.T) {
|
||
|
|
tests := []struct {
|
||
|
|
input string
|
||
|
|
wantDisp *string
|
||
|
|
wantPerm *[]int
|
||
|
|
wantPers *[]string
|
||
|
|
}{
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3-6 persone:single",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp: perm: persone:",
|
||
|
|
wantDisp: nil,
|
||
|
|
wantPerm: nil,
|
||
|
|
wantPers: nil,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp: perm:3-6 persone:single", // disp missing
|
||
|
|
wantDisp: nil,
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm: persone:single", // perm missing
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: nil,
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3-6 persone:", // persone missing
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: nil,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp: perm: persone:", // all fields missing
|
||
|
|
wantDisp: nil,
|
||
|
|
wantPerm: nil,
|
||
|
|
wantPers: nil,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:bad_date perm:3-6 persone:single", // invalid date
|
||
|
|
wantDisp: nil,
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:abc-6 persone:single", // non-numeric perm
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3- persone:single", // trailing delimiter in perm
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:-6 persone:single", // leading delimiter in perm
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3-6 persone:single/", // trailing delimiter in persone
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3-6 persone:/single", // leading delimiter in persone
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3-6 persone:single//coppia", // double delimiter in persone
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single", "coppia"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "perm:3-6 persone:single", // disp missing
|
||
|
|
wantDisp: nil,
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3-6", // persone missing
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: nil,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm: persone:", // perm empty
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: nil,
|
||
|
|
wantPers: nil,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "", // completely empty string
|
||
|
|
wantDisp: nil,
|
||
|
|
wantPerm: nil,
|
||
|
|
wantPers: nil,
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp: 01/10 perm: 3-6 persone: single ",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "persone:single perm:3-6 disp:01/10",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3--6 persone:single",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10\tperm:3-6\tpersone:single",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 perm:3-6 persone:single/coppia!",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single", "coppia!"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10 disp:02/11 perm:3-6 persone:single",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())), // Only first disp: is parsed
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
{
|
||
|
|
input: "disp:01/10perm:3-6persone:single",
|
||
|
|
wantDisp: ptr(fmt.Sprintf("01/10/%d", time.Now().Year())),
|
||
|
|
wantPerm: ptr([]int{3, 6}),
|
||
|
|
wantPers: ptr([]string{"single"}),
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, tt := range tests {
|
||
|
|
disp_time, perm_arr, persone_arr, _ := ParseDettaglio(tt.input)
|
||
|
|
|
||
|
|
//Handle *string comparison, including nil
|
||
|
|
if tt.wantDisp != nil {
|
||
|
|
if disp_time == nil {
|
||
|
|
t.Errorf("disp_time: got nil, want %v, input: %v", *tt.wantDisp, tt.input)
|
||
|
|
} else if *disp_time != *tt.wantDisp {
|
||
|
|
t.Errorf("disp_time: got %v, want %v, input: %v", *disp_time, *tt.wantDisp, tt.input)
|
||
|
|
}
|
||
|
|
} else if disp_time != nil {
|
||
|
|
t.Errorf("disp_time: got %v, want nil, input: %v", *disp_time, tt.input)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handle *[]int comparison, including nil
|
||
|
|
if tt.wantPerm != nil {
|
||
|
|
if perm_arr == nil || len(*perm_arr) != len(*tt.wantPerm) {
|
||
|
|
t.Errorf("perm_arr: got %v, want %v, input: %v", perm_arr, *tt.wantPerm, tt.input)
|
||
|
|
} else {
|
||
|
|
for i := range *perm_arr {
|
||
|
|
if (*perm_arr)[i] != (*tt.wantPerm)[i] {
|
||
|
|
t.Errorf("perm_arr[%d]: got %v, want %v, input: %v", i, (*perm_arr)[i], (*tt.wantPerm)[i], tt.input)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if perm_arr != nil && len(*perm_arr) != 0 {
|
||
|
|
t.Errorf("perm_arr: got %v, want nil, input: %v", perm_arr, tt.input)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handle *[]string comparison, including nil
|
||
|
|
if tt.wantPers != nil {
|
||
|
|
if persone_arr == nil || len(*persone_arr) != len(*tt.wantPers) {
|
||
|
|
t.Errorf("persone_arr: got %v, want %v, input: %v", persone_arr, *tt.wantPers, tt.input)
|
||
|
|
} else {
|
||
|
|
for i := range *persone_arr {
|
||
|
|
if (*persone_arr)[i] != (*tt.wantPers)[i] {
|
||
|
|
t.Errorf("persone_arr[%d]: got %v, want %v, input: %v", i, (*persone_arr)[i], (*tt.wantPers)[i], tt.input)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if persone_arr != nil && len(*persone_arr) != 0 {
|
||
|
|
t.Errorf("persone_arr: got %v, want nil, input: %v", persone_arr, tt.input)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|