github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/com/string_test.go (about) 1 // Copyright 2013 com authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // 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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package com 16 17 import ( 18 "testing" 19 20 . "github.com/insionng/yougam/libraries/smartystreets/goconvey/convey" 21 ) 22 23 func TestIsLetter(t *testing.T) { 24 if IsLetter('1') { 25 t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true) 26 } 27 28 if IsLetter('[') { 29 t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", false, true) 30 } 31 32 if !IsLetter('a') { 33 t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false) 34 } 35 36 if !IsLetter('Z') { 37 t.Errorf("IsLetter:\n Expect => %v\n Got => %v\n", true, false) 38 } 39 } 40 41 func TestExpand(t *testing.T) { 42 match := map[string]string{ 43 "domain": "gowalker.org", 44 "subdomain": "github.com", 45 } 46 s := "http://{domain}/{subdomain}/{0}/{1}" 47 sR := "http://gowalker.org/yougam/libraries/Unknwon/gowalker" 48 if Expand(s, match, "Unknwon", "gowalker") != sR { 49 t.Errorf("Expand:\n Expect => %s\n Got => %s\n", sR, s) 50 } 51 } 52 53 func TestReverse(t *testing.T) { 54 if Reverse("abcdefg") != "gfedcba" { 55 t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "gfedcba", Reverse("abcdefg")) 56 } 57 if Reverse("上善若水厚德载物") != "物载德厚水若善上" { 58 t.Errorf("Reverse:\n Except => %s\n Got =>%s\n", "物载德厚水若善上", Reverse("上善若水厚德载物")) 59 } 60 } 61 62 func Test_ToSnakeCase(t *testing.T) { 63 cases := map[string]string{ 64 "HTTPServer": "http_server", 65 "_camelCase": "_camel_case", 66 "NoHTTPS": "no_https", 67 "Wi_thF": "wi_th_f", 68 "_AnotherTES_TCaseP": "_another_tes_t_case_p", 69 "ALL": "all", 70 "_HELLO_WORLD_": "_hello_world_", 71 "HELLO_WORLD": "hello_world", 72 "HELLO____WORLD": "hello____world", 73 "TW": "tw", 74 "_C": "_c", 75 76 " sentence case ": "__sentence_case__", 77 " Mixed-hyphen case _and SENTENCE_case and UPPER-case": "_mixed_hyphen_case__and_sentence_case_and_upper_case", 78 } 79 Convey("Convert string into snake case", t, func() { 80 for old, new := range cases { 81 So(ToSnakeCase(old), ShouldEqual, new) 82 } 83 }) 84 } 85 86 func BenchmarkIsLetter(b *testing.B) { 87 for i := 0; i < b.N; i++ { 88 IsLetter('a') 89 } 90 } 91 92 func BenchmarkExpand(b *testing.B) { 93 match := map[string]string{ 94 "domain": "gowalker.org", 95 "subdomain": "github.com", 96 } 97 s := "http://{domain}/{subdomain}/{0}/{1}" 98 for i := 0; i < b.N; i++ { 99 Expand(s, match, "Unknwon", "gowalker") 100 } 101 } 102 103 func BenchmarkReverse(b *testing.B) { 104 s := "abscef中文" 105 for i := 0; i < b.N; i++ { 106 Reverse(s) 107 } 108 }