| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package stringtemplate |
| 6 |
| 7 import ( |
| 8 "testing" |
| 9 |
| 10 . "github.com/luci/luci-go/common/testing/assertions" |
| 11 . "github.com/smartystreets/goconvey/convey" |
| 12 ) |
| 13 |
| 14 func TestResolve(t *testing.T) { |
| 15 t.Parallel() |
| 16 |
| 17 Convey(`Testing substitution resolution`, t, func() { |
| 18 shouldResolve := func(v string, subst map[string]string) string
{ |
| 19 val, err := Resolve(v, subst) |
| 20 So(err, ShouldBeNil) |
| 21 return val |
| 22 } |
| 23 |
| 24 resolveErr := func(v string) error { |
| 25 _, err := Resolve(v, nil) |
| 26 return err |
| 27 } |
| 28 |
| 29 Convey(`Can resolve a string without any substitutions.`, func()
{ |
| 30 So(shouldResolve("", nil), ShouldEqual, "") |
| 31 So(shouldResolve("foo", nil), ShouldEqual, "foo") |
| 32 So(shouldResolve(`$${foo}`, nil), ShouldEqual, `${foo}`) |
| 33 }) |
| 34 |
| 35 Convey(`Will error if there is an invalid substitution.`, func()
{ |
| 36 So(resolveErr("$"), ShouldErrLike, "invalid template") |
| 37 So(resolveErr("hello$"), ShouldErrLike, "invalid templat
e") |
| 38 So(resolveErr("foo-${bar"), ShouldErrLike, "invalid temp
late") |
| 39 So(resolveErr("${not valid}"), ShouldErrLike, "invalid t
emplate") |
| 40 |
| 41 So(resolveErr("${uhoh}"), ShouldErrLike, "no substitutio
n for") |
| 42 So(resolveErr("$noooo"), ShouldErrLike, "no substitution
for") |
| 43 }) |
| 44 |
| 45 Convey(`With substitutions defined, can apply.`, func() { |
| 46 m := map[string]string{ |
| 47 "pants": "shirt", |
| 48 "wear_pants": "12345", |
| 49 } |
| 50 |
| 51 So(shouldResolve("", m), ShouldEqual, "") |
| 52 |
| 53 So(shouldResolve("$pants", m), ShouldEqual, "shirt") |
| 54 So(shouldResolve("foo/$pants", m), ShouldEqual, "foo/shi
rt") |
| 55 |
| 56 So(shouldResolve("${wear_pants}", m), ShouldEqual, "1234
5") |
| 57 So(shouldResolve("foo/${wear_pants}", m), ShouldEqual, "
foo/12345") |
| 58 So(shouldResolve("foo/${wear_pants}/bar", m), ShouldEqua
l, "foo/12345/bar") |
| 59 So(shouldResolve("${wear_pants}/bar", m), ShouldEqual, "
12345/bar") |
| 60 So(shouldResolve("foo/${wear_pants}/bar/${wear_pants}/ba
z", m), ShouldEqual, "foo/12345/bar/12345/baz") |
| 61 |
| 62 So(shouldResolve(`$$pants`, m), ShouldEqual, "$pants") |
| 63 So(shouldResolve(`$${pants}`, m), ShouldEqual, "${pants}
") |
| 64 So(shouldResolve(`$$$pants`, m), ShouldEqual, "$shirt") |
| 65 So(shouldResolve(`$$${pants}`, m), ShouldEqual, "$shirt"
) |
| 66 So(shouldResolve(`foo/${wear_pants}/bar/$${wear_pants}/b
az`, m), ShouldEqual, `foo/12345/bar/${wear_pants}/baz`) |
| 67 }) |
| 68 }) |
| 69 } |
| OLD | NEW |