go.uber.org/yarpc@v1.72.1/internal/interpolate/parse_test.go (about) 1 // Copyright (c) 2022 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 interpolate 22 23 import ( 24 "testing" 25 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestParseSuccess(t *testing.T) { 30 tests := []struct { 31 give string 32 want String 33 }{ 34 { 35 give: "foo", 36 want: String{literal("foo")}, 37 }, 38 { 39 give: "foo ${bar} baz", 40 want: String{ 41 literal("foo "), 42 variable{Name: "bar"}, 43 literal(" baz"), 44 }, 45 }, 46 { 47 give: "foo $ {bar} baz", 48 want: String{literal("foo "), literal("$ "), literal("{bar} baz")}, 49 }, 50 { 51 give: "foo ${bar:}", 52 want: String{ 53 literal("foo "), 54 variable{Name: "bar", HasDefault: true}, 55 }, 56 }, 57 { 58 give: "${foo:bar}", 59 want: String{ 60 variable{ 61 Name: "foo", 62 Default: "bar", 63 HasDefault: true, 64 }, 65 }, 66 }, 67 { 68 give: `foo \${bar:42} baz`, 69 want: String{ 70 literal("foo "), 71 literal("$"), 72 literal("{bar:42} baz"), 73 }, 74 }, 75 { 76 give: "$foo${bar}", 77 want: String{ 78 literal("$f"), 79 literal("oo"), 80 variable{Name: "bar"}, 81 }, 82 }, 83 { 84 give: "foo${b-a-r}", 85 want: String{ 86 literal("foo"), 87 variable{Name: "b-a-r"}, 88 }, 89 }, 90 { 91 give: "foo ${bar::baz} qux", 92 want: String{ 93 literal("foo "), 94 variable{Name: "bar", HasDefault: true, Default: ":baz"}, 95 literal(" qux"), 96 }, 97 }, 98 { 99 give: "a ${b:hello world} c", 100 want: String{ 101 literal("a "), 102 variable{Name: "b", HasDefault: true, Default: "hello world"}, 103 literal(" c"), 104 }, 105 }, 106 { 107 give: "foo $${bar}", 108 want: String{literal("foo "), literal("$$"), literal("{bar}")}, 109 }, 110 } 111 112 for _, tt := range tests { 113 out, err := Parse(tt.give) 114 assert.NoError(t, err) 115 assert.Equal(t, tt.want, out) 116 } 117 } 118 119 func TestParseFailures(t *testing.T) { 120 tests := []string{ 121 "${foo", 122 "${foo.}", 123 "${foo-}", 124 "${foo--bar}", 125 } 126 127 for _, tt := range tests { 128 _, err := Parse(tt) 129 assert.Error(t, err, tt) 130 } 131 }