-
Notifications
You must be signed in to change notification settings - Fork 2
/
substenv_test.go
57 lines (52 loc) · 1.57 KB
/
substenv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"fmt"
"os"
"testing"
"github.com/dchest/uniuri"
)
func assertThat(template string, expected string, t *testing.T) {
actual := RegexParserExpand(template)
if actual != expected {
t.Errorf("Template '%s' produced '%s', expected '%s'", template, actual, expected)
}
}
func TestRegexParserExpand(t *testing.T) {
fooval := uniuri.New()
barval := uniuri.New()
os.Setenv("FOO", fooval)
os.Setenv("BAR", barval)
assertThat("${FOO}", fooval, t)
assertThat("$FOO", fooval, t)
assertThat("foo ${FOO} bar", "foo " + fooval + " bar", t)
assertThat("${FOO} foo bar", fooval + " foo bar", t)
assertThat("$", "$", t)
assertThat("${}", "${}", t)
assertThat("foo ${BAR", "foo ${BAR", t)
assertThat("} BAZ", "} BAZ", t)
assertThat("${FOO}bar", fooval + "bar", t)
assertThat("$FOObar", "$FOObar", t)
assertThat("bar$FOO", "bar" + fooval, t)
assertThat("bar${FOO}", "bar" + fooval, t)
assertThat("foo${FOO}bar${BAR}", "foo" + fooval + "bar" + barval, t)
}
func BenchmarkRegexParserExpand(b *testing.B) {
fooval := uniuri.New()
barval := uniuri.New()
os.Setenv("FOO", fooval)
os.Setenv("BAR", barval)
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf(RegexParserExpand("foo${FOO}bar${BAR}"))
}
}
func BenchmarkExpandEnv(b *testing.B) {
fooval := uniuri.New()
barval := uniuri.New()
os.Setenv("FOO", fooval)
os.Setenv("BAR", barval)
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf(os.ExpandEnv("foo${FOO}bar${BAR}"))
}
}