package form import ( "net/http" "net/http/httptest" "strings" "testing" ) func TestGet(t *testing.T) { r := testReq() if v := Get(r, "a"); v != "b" { t.Error(v) } if v := Get(r, "c"); v != "4" { t.Error(v) } if v := Get(r, "d"); v != "e, f,g" { t.Error(v) } } func TestToStrArr(t *testing.T) { cases := []struct { in string out int }{ { in: "4", out: 1, }, { in: "a,b,c", out: 3, }, { in: " a, b, c ", out: 3, }, { in: "a,b, c", out: 3, }, { in: "a,b", out: 2, }, { in: "a,", out: 1, }, { in: "a", out: 1, }, { in: "", out: 0, }, } for _, c := range cases { if len(ToStrArr(c.in)) != c.out { t.Error(c) } } } func TestToTime(t *testing.T) { cases := []struct { in string out string }{ { in: "2001-02-03 04:05:06", out: "2001-02-03 04:05:06", }, { in: "5", out: "0001-01-01 00:00:00", }, { in: "1/2/03", out: "2003-01-02 00:00:00", }, { in: "11/12/03", out: "2003-11-12 00:00:00", }, { in: "01+Jan+2020+12:00+PM", out: "2020-01-01 12:00:00", }, } for _, c := range cases { time := ToTime(c.in) if v := time.Format("2006-01-02 15:04:05"); v != c.out { t.Error(c, v) } } } func TestToInt(t *testing.T) { cases := []struct { in string out int }{ { in: "4", out: 4, }, { in: "a", out: 0, }, { in: "", out: 0, }, { in: "-1", out: -1, }, { in: "5", out: 5, }, } for _, c := range cases { if ToInt(c.in) != c.out { t.Error(c) } } } func testReq() *http.Request { return httptest.NewRequest("POST", "/path/to", strings.NewReader(`{ "a": "b", "c": 4, "d": "e, f,g" }`)) }