github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/set_test.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package types 15 16 import ( 17 . "github.com/pingcap/check" 18 19 "github.com/XiaoMi/Gaea/util/testleak" 20 ) 21 22 var _ = Suite(&testSetSuite{}) 23 24 type testSetSuite struct { 25 } 26 27 func (s *testSetSuite) TestSet(c *C) { 28 defer testleak.AfterTest(c)() 29 elems := []string{"a", "b", "c", "d"} 30 tbl := []struct { 31 Name string 32 ExpectedValue uint64 33 ExpectedName string 34 }{ 35 {"a", 1, "a"}, 36 {"a,b,a", 3, "a,b"}, 37 {"b,a", 3, "a,b"}, 38 {"a,b,c,d", 15, "a,b,c,d"}, 39 {"d", 8, "d"}, 40 {"", 0, ""}, 41 {"0", 0, ""}, 42 } 43 44 for _, t := range tbl { 45 e, err := ParseSetName(elems, t.Name) 46 c.Assert(err, IsNil) 47 c.Assert(e.ToNumber(), Equals, float64(t.ExpectedValue)) 48 c.Assert(e.String(), Equals, t.ExpectedName) 49 } 50 51 tblNumber := []struct { 52 Number uint64 53 ExpectedName string 54 }{ 55 {0, ""}, 56 {1, "a"}, 57 {3, "a,b"}, 58 {9, "a,d"}, 59 } 60 61 for _, t := range tblNumber { 62 e, err := ParseSetValue(elems, t.Number) 63 c.Assert(err, IsNil) 64 c.Assert(e.String(), Equals, t.ExpectedName) 65 c.Assert(e.ToNumber(), Equals, float64(t.Number)) 66 } 67 68 tblErr := []string{ 69 "a.e", 70 "e.f", 71 } 72 for _, t := range tblErr { 73 _, err := ParseSetName(elems, t) 74 c.Assert(err, NotNil) 75 } 76 77 tblNumberErr := []uint64{ 78 100, 16, 64, 79 } 80 for _, t := range tblNumberErr { 81 _, err := ParseSetValue(elems, t) 82 c.Assert(err, NotNil) 83 } 84 }