github.com/XiaoMi/Gaea@v1.2.5/parser/tidb-types/enum_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(&testEnumSuite{})
    23  
    24  type testEnumSuite struct {
    25  }
    26  
    27  func (s *testEnumSuite) TestEnum(c *C) {
    28  	defer testleak.AfterTest(c)()
    29  	tbl := []struct {
    30  		Elems    []string
    31  		Name     string
    32  		Expected int
    33  	}{
    34  		{[]string{"a", "b"}, "a", 1},
    35  		{[]string{"a"}, "b", 0},
    36  		{[]string{"a"}, "1", 1},
    37  	}
    38  
    39  	for _, t := range tbl {
    40  		e, err := ParseEnumName(t.Elems, t.Name)
    41  		if t.Expected == 0 {
    42  			c.Assert(err, NotNil)
    43  			c.Assert(e.ToNumber(), Equals, float64(0))
    44  			c.Assert(e.String(), Equals, "")
    45  			continue
    46  		}
    47  
    48  		c.Assert(err, IsNil)
    49  		c.Assert(e.String(), Equals, t.Elems[t.Expected-1])
    50  		c.Assert(e.ToNumber(), Equals, float64(t.Expected))
    51  	}
    52  
    53  	tblNumber := []struct {
    54  		Elems    []string
    55  		Number   uint64
    56  		Expected int
    57  	}{
    58  		{[]string{"a"}, 1, 1},
    59  		{[]string{"a"}, 0, 0},
    60  	}
    61  
    62  	for _, t := range tblNumber {
    63  		e, err := ParseEnumValue(t.Elems, t.Number)
    64  		if t.Expected == 0 {
    65  			c.Assert(err, NotNil)
    66  			continue
    67  		}
    68  
    69  		c.Assert(err, IsNil)
    70  		c.Assert(e.ToNumber(), Equals, float64(t.Expected))
    71  	}
    72  }