github.com/astaxie/beego@v1.12.3/orm/utils_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package orm
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestCamelString(t *testing.T) {
    22  	snake := []string{"pic_url", "hello_world_", "hello__World", "_HelLO_Word", "pic_url_1", "pic_url__1"}
    23  	camel := []string{"PicUrl", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "PicUrl1"}
    24  
    25  	answer := make(map[string]string)
    26  	for i, v := range snake {
    27  		answer[v] = camel[i]
    28  	}
    29  
    30  	for _, v := range snake {
    31  		res := camelString(v)
    32  		if res != answer[v] {
    33  			t.Error("Unit Test Fail:", v, res, answer[v])
    34  		}
    35  	}
    36  }
    37  
    38  func TestSnakeString(t *testing.T) {
    39  	camel := []string{"PicUrl", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "XyXX"}
    40  	snake := []string{"pic_url", "hello_world", "hello_world", "hel_l_o_word", "pic_url1", "xy_x_x"}
    41  
    42  	answer := make(map[string]string)
    43  	for i, v := range camel {
    44  		answer[v] = snake[i]
    45  	}
    46  
    47  	for _, v := range camel {
    48  		res := snakeString(v)
    49  		if res != answer[v] {
    50  			t.Error("Unit Test Fail:", v, res, answer[v])
    51  		}
    52  	}
    53  }
    54  
    55  func TestSnakeStringWithAcronym(t *testing.T) {
    56  	camel := []string{"ID", "PicURL", "HelloWorld", "HelloWorld", "HelLOWord", "PicUrl1", "XyXX"}
    57  	snake := []string{"id", "pic_url", "hello_world", "hello_world", "hel_lo_word", "pic_url1", "xy_xx"}
    58  
    59  	answer := make(map[string]string)
    60  	for i, v := range camel {
    61  		answer[v] = snake[i]
    62  	}
    63  
    64  	for _, v := range camel {
    65  		res := snakeStringWithAcronym(v)
    66  		if res != answer[v] {
    67  			t.Error("Unit Test Fail:", v, res, answer[v])
    68  		}
    69  	}
    70  }