func TestMap(t *testing.T) { assert.Init(t) func() { array := ArrayA{} newArray := array.Map_D_(func(val A, index int, array ArrayA) B { assert.Ok(false) return 0 }) assert.Ok(reflect.DeepEqual(ArrayB{}, newArray)) }() func() { array := ArrayA{1, 2, 3, 4, 5, 6} newArray := array.Map_D_(func(val A, index int, array ArrayA) B { return B(val) }) assert.Ok(reflect.DeepEqual(ArrayB{1, 2, 3, 4, 5, 6}, newArray)) }() func() { array := ArrayA{1, 2, 3, 4, 5, 6} newArray := array.Map_D_(func(val A, index int, array ArrayA) B { return B(val + 1) }) assert.Ok(reflect.DeepEqual(newArray, ArrayB{2, 3, 4, 5, 6, 7})) }() }
func TestMap(t *testing.T) { assert.Init(t) func() { array := Array{} newArray := array.Map(func(val A, index int, array Array) A { assert.Ok(false) return val }) assert.Ok(reflect.DeepEqual(array, newArray)) }() func() { array := Array{1, 2, 3, 4, 5, 6} newArray := array.Map(func(val A, index int, array Array) A { return val }) assert.Ok(reflect.DeepEqual(array, newArray)) }() func() { array := Array{1, 2, 3, 4, 5, 6} newArray := array.Map(func(val A, index int, array Array) A { return val * val }) assert.Ok(reflect.DeepEqual(newArray, Array{1, 4, 9, 16, 25, 36})) }() }
func TestComplie(t *testing.T) { assert.Init(t) array := ArraypInt{&Int{0}} array[0] = nil assert.Ok(len(array) == 1) assert.Ok(nil == array[0]) }
func TestSort(t *testing.T) { assert.Init(t) func() { array := make(Array, 0, 10) assert.Ok(sort.IsSorted(array.Sort())) }() func() { array := Array{1, 5, 9, 8, 7, 6, 2, 4, 3} assert.Ok(sort.IsSorted(array.Sort())) }() }
func TestRevese(t *testing.T) { assert.Init(t) func() { array := Array{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} assert.Ok(reflect.DeepEqual(array.Reverse(), Array{10, 9, 8, 7, 6, 5, 4, 3, 2, 1})) }() }
func TestFilter(t *testing.T) { assert.Init(t) func() { array := Array{} newArray := array.Filter(func(val A, index int, array Array) bool { t.Fail() return false }) assert.Ok(nil != newArray) assert.Ok(newArray.Len() == 0 && len(newArray) == 0) assert.Ok(reflect.TypeOf(array) == reflect.TypeOf(newArray)) }() func() { array := Array{1, 2, 3, 4, 5, 6} newArray := array.Filter(func(val A, index int, array Array) bool { return false }) assert.Ok(nil != newArray) assert.Ok(newArray.Len() == 0 && len(newArray) == 0) assert.Ok(reflect.TypeOf(array) == reflect.TypeOf(newArray)) }() func() { array := Array{1, 2, 3, 4, 5, 6} newArray := array.Filter(func(val A, index int, array Array) bool { return 0 == val%2 }) assert.Ok(reflect.DeepEqual(newArray, Array{2, 4, 6})) }() }
func TestIndexOf(t *testing.T) { assert.Init(t) func() { array := Array{} assert.Ok(-1 == array.IndexOf(0)) }() func() { array := Array{0, 1, 2, 3, 4, 5, 6} for i, v := range array { assert.Ok(i == array.IndexOf(v)) } }() func() { array := Array{0, 1, 2, 3, 4, 5, 6} assert.Ok(-1 == array.IndexOf(7)) }() }
func TestSome(t *testing.T) { assert.Init(t) //Test empty array func() { array := make(Array, 0, 10) assert.Ok(!array.Some(func(value A, index int, _array Array) bool { t.Errorf("Should not be here") return false })) }() //Test return true func() { array := make(Array, 10, 10) assert.Ok(array.Some(func(value A, index int, _array Array) bool { return true })) }() //Test return false func() { array := make(Array, 10, 10) assert.Ok(!array.Some(func(value A, index int, _array Array) bool { return false })) }() //Test array is same from callback func() { array := make(Array, 10, 10) for i := range array { array[i] = A(7 - i) } array.Some(func(value A, index int, _array Array) bool { assert.Ok(array[index] == _array[index]) _array[index] = A(0) return false }) assert.Ok(!array.Some(func(value A, index int, _array Array) bool { return 0 != value })) for _, value := range array { assert.Ok(0 == value) } }() }
func TestKeys(t *testing.T) { assert.Init(t) func() { obj := Object{ 0: "Zero", 1: "RainInFall", 3: "Leo.Xiongs", 2: "Canyd", } keys := obj.Keys() assert.Ok(len(keys) == 4) occurs := make([]bool, 4, 4) for _, v := range keys { if occurs[v] { t.FailNow() } occurs[v] = true } }() }
func TestFormat(t *testing.T) { assert.Init(t) func() { str, err := Format(&ContentType{Type: "text/html"}) assert.Ok(nil == err) assert.Ok("text/html" == str) }() func() { str, err := Format(&ContentType{Type: "image/svg+xml"}) assert.Ok(nil == err) assert.Ok("image/svg+xml" == str) }() func() { str, err := Format(&ContentType{ Type: "text/html", Parameters: map[string]string{ "charset": "utf-8", }, }) assert.Ok(nil == err) assert.Ok("text/html; charset=utf-8" == str) }() func() { str, err := Format(&ContentType{ Type: "text/html", Parameters: map[string]string{ "foo": "bar or \"baz\"", }, }) assert.Ok(nil == err) assert.Ok("text/html; foo=\"bar or \\\"baz\\\"\"" == str) }() func() { str, err := Format(&ContentType{ Type: "text/html", Parameters: map[string]string{ "foo": "", }, }) assert.Ok(nil == err) assert.Ok("text/html; foo=\"\"" == str) }() func() { str, err := Format(&ContentType{ Type: "text/html", Parameters: map[string]string{ "charset": "utf-8", "foo": "bar", "bar": "baz", }, }) assert.Ok(nil == err) assert.Ok("text/html; bar=baz; charset=utf-8; foo=bar" == str) }() func() { _, err := Format(nil) assert.Ok(nil != err && regexp.MustCompile("argument ContentType is required").MatchString(err.Error())) }() func() { _, err := Format(&ContentType{}) assert.Ok(nil != err && regexp.MustCompile("invalid type").MatchString(err.Error())) }() func() { _, err := Format(&ContentType{Type: "text/"}) assert.Ok(nil != err && regexp.MustCompile("invalid type").MatchString(err.Error())) }() func() { _, err := Format(&ContentType{Type: " text/html"}) assert.Ok(nil != err && regexp.MustCompile("invalid type").MatchString(err.Error())) }() func() { _, err := Format(&ContentType{ Type: "image/svg", Parameters: map[string]string{ "foo/": "bar", }, }) assert.Ok(nil != err && regexp.MustCompile("invalid parameter name").MatchString(err.Error())) }() func() { _, err := Format(&ContentType{ Type: "image/svg", Parameters: map[string]string{ "foo": "bar\u0000", }, }) assert.Ok(nil != err && regexp.MustCompile("invalid parameter value").MatchString(err.Error())) }() }
func TestParse(t *testing.T) { assert.Init(t) func() { typ, err := Parse("text/html") assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) }() func() { typ, err := Parse("image/svg+xml") assert.Ok(nil == err) assert.Ok("image/svg+xml" == typ.Type) }() func() { typ, err := Parse(" text/html") assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) }() func() { typ, err := Parse("text/html; charset=utf-8; foo=bar") assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) assert.Ok(reflect.DeepEqual(typ.Parameters, Parameters(map[string]string{ "charset": "utf-8", "foo": "bar", }))) assert.Ok(reflect.DeepEqual(typ.Parameters, Parameters(map[string]string{ "charset": "utf-8", "foo": "bar", }))) }() func() { typ, err := Parse("text/html ; charset=utf-8 ; foo=bar") assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) assert.Ok(reflect.DeepEqual(typ.Parameters, Parameters(map[string]string{ "charset": "utf-8", "foo": "bar", }))) }() func() { typ, err := Parse("IMAGE/SVG+XML") assert.Ok(nil == err) assert.Ok("image/svg+xml" == typ.Type) }() func() { typ, err := Parse("text/html; Charset=UTF-8") assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) assert.Ok(reflect.DeepEqual(typ.Parameters, Parameters(map[string]string{ "charset": "UTF-8", }))) }() func() { typ, err := Parse("text/html; charset=\"UTF-8\"") assert.Ok(err == nil) assert.Ok(reflect.DeepEqual(typ.Parameters, Parameters(map[string]string{ "charset": "UTF-8", }))) }() func() { typ, err := Parse("text/html; charset = \"UT\\F-\\\\\\\"8\\\"\"") assert.Ok(nil == err) assert.Ok(reflect.DeepEqual(typ.Parameters, Parameters(map[string]string{ "charset": "UTF-\\\"8\"", }))) }() func() { typ, err := Parse("text/html; param=\"charset=\\\"utf-8\\\"; foo=bar\"; bar=foo") assert.Ok(nil == err) assert.Ok(reflect.DeepEqual(typ.Parameters, Parameters(map[string]string{ "param": "charset=\"utf-8\"; foo=bar", "bar": "foo", }))) }() for _, v := range invalidTypes { _, err := Parse(v) assert.Ok(nil != err && regexp.MustCompile("invalid media type").MatchString(err.Error())) } func() { _, err := Parse("text/plain; foo=\"bar") assert.Ok(nil != err && regexp.MustCompile("invalid parameter format").MatchString(err.Error())) }() func() { _, err := Parse("text/plain; profile=http://localhost; foo=bar") assert.Ok(nil != err && regexp.MustCompile("invalid parameter format").MatchString(err.Error())) }() func() { _, err := Parse("text/plain; profile=http://localhost") assert.Ok(nil != err && regexp.MustCompile("invalid parameter format").MatchString(err.Error())) }() func() { header := make(http.Header) header.Set("content-type", "text/html") typ, err := ParseHeader(header) assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) }() func() { req := http.Request{ Header: http.Header{}, } _, err := ParseRequest(req) assert.Ok(nil != err && regexp.MustCompile("content-type header is missing").MatchString(err.Error())) }() func() { _, err := ParseRequest(nil) assert.Ok(nil != err && regexp.MustCompile("content-type header is missing").MatchString(err.Error())) }() func() { req := TempResponse{} _, err := ParseRequest(req) assert.Ok(nil != err && regexp.MustCompile("content-type header is missing").MatchString(err.Error())) }() func() { req := http.Request{ Header: http.Header{ "Content-Type": []string{"text/html"}, }, } typ, err := ParseRequest(req) assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) }() func() { res := TempResponse{http.Header{ "Content-Type": []string{"text/html"}, }} typ, err := ParserResponse(res) assert.Ok(nil == err) assert.Ok("text/html" == typ.Type) }() func() { res := TempResponse{} _, err := ParserResponse(res) assert.Ok(nil != err && regexp.MustCompile("content-type header is missing").MatchString(err.Error())) }() }
func TestCheck(t *testing.T) { assert.Init(t) func() { var req http.Header var res http.Header assert.Ok(!Check(req, res)) }() func() { var req http.Header = map[string][]string{ http.CanonicalHeaderKey("if-none-match"): []string{"tobi"}, } var res http.Header = map[string][]string{ http.CanonicalHeaderKey("etag"): []string{"tobi"}, } assert.Ok(Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "tobi") res.Set("etag", "tobi") assert.Ok(Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "tobi") res.Set("etag", "luna") assert.Ok(!Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "tobi") assert.Ok(!Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "W/\"foo\"") res.Set("etag", "W/\"foo\"") assert.Ok(Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "W/\"foo\"") res.Set("etag", "\"foo\"") assert.Ok(Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "\"foo\"") res.Set("etag", "\"foo\"") assert.Ok(Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "\"foo\"") res.Set("etag", "W/\"foo\"") assert.Ok(!Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "*") res.Set("etag", "hey") assert.Ok(Check(req, res)) }() func() { now := time.Now() req := make(http.Header) res := make(http.Header) duration, err := time.ParseDuration("-4s") req.Set("if-modified-since", now.Add(duration).Format(http.TimeFormat)) assert.Ok(err == nil) duration, err = time.ParseDuration("-2s") assert.Ok(err == nil) res.Set("last-modified", now.Add(duration).Format(http.TimeFormat)) assert.Ok(!Check(req, res)) }() func() { now := time.Now() req := make(http.Header) res := make(http.Header) duration, err := time.ParseDuration("-2s") req.Set("if-modified-since", now.Add(duration).Format(http.TimeFormat)) assert.Ok(err == nil) duration, err = time.ParseDuration("-4s") assert.Ok(err == nil) res.Set("last-modified", now.Add(duration).Format(http.TimeFormat)) assert.Ok(Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", time.Now().Format(http.TimeFormat)) assert.Ok(!Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", "foo") assert.Ok(!Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("if-none-match", time.Now().Format(http.TimeFormat)) req.Set("modified-since", "foo") assert.Ok(!Check(req, res)) }() func() { now := time.Now() req := make(http.Header) res := make(http.Header) duration, err := time.ParseDuration("-2s") assert.Ok(err == nil) req.Set("if-modified-since", now.Add(duration).Format(http.TimeFormat)) req.Set("if-none-match", "tobi") duration, err = time.ParseDuration("-4s") assert.Ok(err == nil) res.Set("last-modified", now.Add(duration).Format(http.TimeFormat)) res.Set("etag", "tobi") assert.Ok(Check(req, res)) }() func() { now := time.Now() req := make(http.Header) res := make(http.Header) duration, err := time.ParseDuration("-4s") assert.Ok(err == nil) req.Set("if-modified-since", now.Add(duration).Format(http.TimeFormat)) req.Set("if-none-match", "tobi") duration, err = time.ParseDuration("-2s") assert.Ok(err == nil) res.Set("last-modified", now.Add(duration).Format(http.TimeFormat)) res.Set("etag", "tobi") assert.Ok(!Check(req, res)) }() func() { now := time.Now() req := make(http.Header) res := make(http.Header) duration, err := time.ParseDuration("-2s") assert.Ok(err == nil) req.Set("if-modified-since", now.Add(duration).Format(http.TimeFormat)) req.Set("if-none-match", "tobi") duration, err = time.ParseDuration("-4s") assert.Ok(err == nil) res.Set("last-modified", now.Add(duration).Format(http.TimeFormat)) res.Set("etag", "luna") assert.Ok(!Check(req, res)) }() func() { now := time.Now() req := make(http.Header) res := make(http.Header) duration, err := time.ParseDuration("-4s") assert.Ok(err == nil) req.Set("if-modified-since", now.Add(duration).Format(http.TimeFormat)) req.Set("if-none-match", "tobi") duration, err = time.ParseDuration("-2s") assert.Ok(err == nil) res.Set("last-modified", now.Add(duration).Format(http.TimeFormat)) res.Set("etag", "tobi") assert.Ok(!Check(req, res)) }() func() { req := make(http.Header) res := make(http.Header) req.Set("cache-control", "no-cache") assert.Ok(!Check(req, res)) }() }