github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/names_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package dosa_test 22 23 import ( 24 "fmt" 25 "testing" 26 27 "github.com/stretchr/testify/assert" 28 "github.com/uber-go/dosa" 29 ) 30 31 func TestIsValidName(t *testing.T) { 32 dataProvider := []struct { 33 arg string 34 allowed bool 35 }{ 36 { 37 arg: "has_underscore", 38 allowed: true, 39 }, 40 { 41 arg: "mixeDCase", 42 allowed: false, 43 }, 44 { 45 arg: "md5", 46 allowed: true, 47 }, 48 { 49 arg: "_name", 50 allowed: true, 51 }, 52 { 53 arg: "_alreadynormalized9", 54 allowed: true, 55 }, 56 { 57 arg: "123numberprefix", 58 allowed: false, 59 }, 60 { 61 arg: "", 62 allowed: false, 63 }, 64 { 65 arg: "longname012345678901234567890123456789", 66 allowed: false, 67 }, 68 { 69 arg: "世界", 70 allowed: false, 71 }, 72 { 73 arg: "an apple", 74 allowed: false, 75 }, 76 } 77 78 for _, testData := range dataProvider { 79 err := dosa.IsValidName(testData.arg) 80 if testData.allowed { 81 assert.NoError(t, err, fmt.Sprintf("got error while expecting no error for %s", testData.arg)) 82 } else { 83 assert.Error(t, err, fmt.Sprintf("expect error but got no error for %s", testData.arg)) 84 } 85 } 86 } 87 88 func TestNormalizeName(t *testing.T) { 89 dataProvider := []struct { 90 arg string 91 allowed bool 92 expected string 93 }{ 94 { 95 arg: "lOwerEVeryTHiNG", 96 allowed: true, 97 expected: "lowereverything", 98 }, 99 { 100 arg: "MD5", 101 allowed: true, 102 expected: "md5", 103 }, 104 { 105 arg: "_MyName", 106 allowed: true, 107 expected: "_myname", 108 }, 109 { 110 arg: "_alreadynormalized9", 111 allowed: true, 112 expected: "_alreadynormalized9", 113 }, 114 { 115 arg: "an apple", 116 allowed: false, 117 expected: "", 118 }, 119 } 120 121 for _, testData := range dataProvider { 122 name, err := dosa.NormalizeName(testData.arg) 123 if testData.allowed { 124 assert.NoError(t, err, fmt.Sprintf("got error while expecting no error for %s", testData.arg)) 125 assert.Equal(t, testData.expected, name, 126 fmt.Sprintf("unexpected normalized name for %s", testData.arg)) 127 } else { 128 assert.Error(t, err, fmt.Sprintf("expect error but got no error for %s", testData.arg)) 129 } 130 } 131 } 132 133 func TestToFQN(t *testing.T) { 134 f, err := dosa.ToFQN("service.foo") 135 assert.NoError(t, err) 136 assert.EqualValues(t, "service.foo", f) 137 138 f, err = dosa.ToFQN("MyService.Foo.V2") 139 assert.NoError(t, err) 140 assert.EqualValues(t, "myservice.foo.v2", f) 141 142 f, err = dosa.ToFQN("") 143 assert.NoError(t, err) 144 assert.EqualValues(t, "", f) 145 146 _, err = dosa.ToFQN("service.an entity") 147 assert.Error(t, err) 148 149 _, err = dosa.ToFQN("germanRush.über") 150 assert.Error(t, err) 151 } 152 153 func TestFQNChild(t *testing.T) { 154 fqn, err := dosa.ToFQN("foo.bar") 155 assert.NoError(t, err) 156 157 c, err := fqn.Child("qux") 158 assert.NoError(t, err) 159 assert.EqualValues(t, "foo.bar.qux", c) 160 161 _, err = fqn.Child("世界") 162 assert.Error(t, err) 163 } 164 165 func TestFQNStringer(t *testing.T) { 166 fqn, err := dosa.ToFQN("foo.bar") 167 assert.NoError(t, err) 168 assert.Equal(t, "foo.bar", fqn.String()) 169 }