github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/strhelp/string_help_test.go (about) 1 // Copyright 2019 Dolthub, 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 // 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 strhelp 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 func TestNthTokenTest(t *testing.T) { 24 tests := []struct { 25 in string 26 n int 27 expectedStr string 28 expectedOk bool 29 }{ 30 { 31 "", 32 0, 33 "", 34 true, 35 }, 36 { 37 "", 38 1, 39 "", 40 false, 41 }, 42 { 43 "short", 44 0, 45 "short", 46 true, 47 }, 48 { 49 "short", 50 1, 51 "", 52 false, 53 }, 54 { 55 "0/1/2", 56 0, 57 "0", 58 true, 59 }, 60 { 61 "0/1/2", 62 1, 63 "1", 64 true, 65 }, 66 { 67 "0/1/2", 68 2, 69 "2", 70 true, 71 }, 72 { 73 "0/1/2", 74 3, 75 "", 76 false, 77 }, 78 { 79 "/1/2/", 80 0, 81 "", 82 true, 83 }, 84 { 85 "/1/2/", 86 1, 87 "1", 88 true, 89 }, 90 { 91 "/1/2/", 92 2, 93 "2", 94 true, 95 }, 96 { 97 "/1/2/", 98 3, 99 "", 100 true, 101 }, 102 { 103 "/1/2/", 104 4, 105 "", 106 false, 107 }, 108 } 109 110 for _, test := range tests { 111 token, ok := NthToken(test.in, '/', test.n) 112 113 if token != test.expectedStr || ok != test.expectedOk { 114 t.Error(test.in, test.n, "th token should be", test.expectedStr, "but it is", token) 115 } 116 } 117 } 118 119 func TestCommaIfy(t *testing.T) { 120 tests := map[int64]string{ 121 1: "1", 122 10: "10", 123 100: "100", 124 1000: "1,000", 125 10000: "10,000", 126 100000: "100,000", 127 1000000: "1,000,000", 128 10000000: "10,000,000", 129 } 130 131 for i, expected := range tests { 132 str := CommaIfy(i) 133 assert.Equal(t, expected, str) 134 } 135 }