func TestUnit(t *testing.T) { assert.Equal(t, b, 1) assert.Equal(t, kb, 1024) assert.Equal(t, mb, 1024*1024) assert.Equal(t, gb, 1024*1024*1024) assert.Equal(t, tb, 1024*1024*1024*1024) }
func TestHost(t *testing.T) { defHandler := func(w http.ResponseWriter, r *http.Request) bool { return true } defFunc := MatcherFunc(defHandler) fn := func(host string, hh *Host, wont bool) { r, err := http.NewRequest("GET", "", nil) assert.NotError(t, err) r.Host = host assert.Equal(t, r.Host, host) assert.Equal(t, hh.ServeHTTP2(nil, r), wont, "域名[%v]无法正确匹配", host) } h := NewHost(defFunc, "www.example.com") fn("www.example.com", h, true) fn("www.abc.com", h, false) h = NewHost(defFunc, "\\w+.example.com") fn("www.example.com", h, true) fn("api.example.com", h, true) fn("www.abc.com", h, false) }
func TestSGR(t *testing.T) { // 多个 sgr := SGR(SGRBBlack, SGRBBlue, SGRFRed) result := "\033[" + SGRBBlack + ";" + SGRBBlue + ";" + SGRFRed + "m" assert.Equal(t, sgr, result) // 传递单个值 sgr = SGR(SGRBBlack) result = "\033[" + SGRBBlack + "m" assert.Equal(t, sgr, result) // 传递空值,相当于SReset sgr = SGR() assert.Equal(t, sgr, Reset) }
/////////////////////////////// Slice func TestSlice(t *testing.T) { fn := func(val interface{}, result []interface{}) { ret, err := Slice(val) assert.Nil(t, err) assert.Equal(t, ret, result) } fn("123", []interface{}{int32(49), int32(50), int32(51)}) fn([]int{1, 2, 3}, []interface{}{int(1), int(2), int(3)}) fn([]string{"1", "ss"}, []interface{}{"1", "ss"}) }
func TestMap(t *testing.T) { fn := func(val interface{}, result map[string]interface{}) { ret, err := Map(val) assert.Nil(t, err) assert.Equal(t, ret, result) } fn(map[string]interface{}{"1": 1, "2": 3}, map[string]interface{}{"1": 1, "2": 3}) obj := &test1{5, "admin", "abc"} fn(obj, map[string]interface{}{"Id": 5, "Name": "admin"}) }
//////////////////////////////// Bytes func TestBytes(t *testing.T) { fn := func(val interface{}, result []byte) { ret, err := Bytes(val) assert.Nil(t, err) assert.Equal(t, ret, result) } fn(11, []byte{49, 49}) fn("1.11", []byte{49, 46, 49, 49}) fn(-1.11, []byte{45, 49, 46, 49, 49, 48, 48, 48}) fn(0, []byte{48}) }
////////////////////////////////// String func TestString(t *testing.T) { fn := func(val interface{}, result string) { ret, err := String(val) assert.Nil(t, err) assert.Equal(t, ret, result) } fn(5, "5") fn(int32(5), "5") fn(uint(0), "0") fn(1.32, "1.32") fn("0.00", "0.00") fn(-1.1, "-1.1") fn(1.0, "1") }
////////////////////////////////// Uint64 func TestFloat32(t *testing.T) { fn := func(val interface{}, result float32) { ret, err := Float32(val) assert.Nil(t, err) assert.Equal(t, ret, result) } fn(5, 5) fn(int32(5), 5) fn(uint(0), 0) fn(1.32, 1.32) fn("0.00", 0) fn("1.1", 1.1) fn("123", 123) }
////////////////////////////////// Uint64 func TestUint64(t *testing.T) { fn := func(val interface{}, result uint64) { ret, err := Uint64(val) assert.Nil(t, err) assert.Equal(t, ret, result) } fn(5, 5) fn(int32(5), 5) fn(uint(0), 0) fn(1.32, 1) fn(0.00, 0) fn("1.1", 1) fn("123", 123) }
////////////////////////////////// Int64 func TestInt64(t *testing.T) { fn := func(val interface{}, result int64) { ret, err := Int64(val) assert.Nil(t, err) assert.Equal(t, ret, result) } fn(5, 5) fn(int(-11), -11) fn(int32(5), 5) fn(uint(3), 3) fn(uint64(0), 0) fn(1.32, 1) fn(0.00, 0) fn(-1.3, -1) fn("-1.1", -1) fn("123", 123) }
/////////////////////////// Bool func TestBool(t *testing.T) { fn := func(val interface{}, result bool) { ret, err := Bool(val) assert.Equal(t, ret, result) assert.Nil(t, err) } fn(5, true) fn(int64(-11), true) fn(int32(5), true) fn(uint(3), true) fn(uint64(0), false) fn(1.32, true) fn(0.00, false) fn("off", false) fn("true", true) fn(-1.3, true) }
func TestMethod(t *testing.T) { defHandler := func(w http.ResponseWriter, r *http.Request) bool { return true } defFunc := MatcherFunc(defHandler) fn := func(method string, m Matcher, wont bool) { r, err := http.NewRequest(method, "", nil) assert.NotError(t, err) assert.Equal(t, m.ServeHTTP2(nil, r), wont) } m := NewMethod().Get(defFunc) fn("GET", m, true) fn("POST", m, false) m = NewMethod().Get(defFunc).Post(defFunc) fn("POST", m, true) fn("GET", m, true) fn("OPTIONS", m, false) }
func TestPath(t *testing.T) { defHandler := func(w http.ResponseWriter, r *http.Request) bool { return true } defFunc := MatcherFunc(defHandler) fn := func(pattern string, p *Path, wont bool) { r, err := http.NewRequest("GET", pattern, nil) assert.NotError(t, err) assert.Equal(t, p.ServeHTTP2(nil, r), wont) } p := NewPath(defFunc, "/api") fn("/api", p, true) fn("/api/v1", p, true) p = NewPath(defFunc, "/api/v(\\d+)") fn("/api", p, false) fn("/api/v1", p, true) fn("/api/v1/post/1", p, true) }
func TestGetWeight(t *testing.T) { assert.Equal(t, gb11643Weight, getWeight()) }