...
1
15
16 package engine
17
18 import (
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 )
23
24 var cases = []struct {
25 path, data string
26 }{
27 {"ship/captain.txt", "The Captain"},
28 {"ship/stowaway.txt", "Legatt"},
29 {"story/name.txt", "The Secret Sharer"},
30 {"story/author.txt", "Joseph Conrad"},
31 {"multiline/test.txt", "bar\nfoo\n"},
32 {"multiline/test_with_blank_lines.txt", "bar\nfoo\n\n\n"},
33 }
34
35 func getTestFiles() files {
36 a := make(files, len(cases))
37 for _, c := range cases {
38 a[c.path] = []byte(c.data)
39 }
40 return a
41 }
42
43 func TestNewFiles(t *testing.T) {
44 files := getTestFiles()
45 if len(files) != len(cases) {
46 t.Errorf("Expected len() = %d, got %d", len(cases), len(files))
47 }
48
49 for i, f := range cases {
50 if got := string(files.GetBytes(f.path)); got != f.data {
51 t.Errorf("%d: expected %q, got %q", i, f.data, got)
52 }
53 if got := files.Get(f.path); got != f.data {
54 t.Errorf("%d: expected %q, got %q", i, f.data, got)
55 }
56 }
57 }
58
59 func TestFileGlob(t *testing.T) {
60 as := assert.New(t)
61
62 f := getTestFiles()
63
64 matched := f.Glob("story/**")
65
66 as.Len(matched, 2, "Should be two files in glob story/**")
67 as.Equal("Joseph Conrad", matched.Get("story/author.txt"))
68 }
69
70 func TestToConfig(t *testing.T) {
71 as := assert.New(t)
72
73 f := getTestFiles()
74 out := f.Glob("**/captain.txt").AsConfig()
75 as.Equal("captain.txt: The Captain", out)
76
77 out = f.Glob("ship/**").AsConfig()
78 as.Equal("captain.txt: The Captain\nstowaway.txt: Legatt", out)
79 }
80
81 func TestToSecret(t *testing.T) {
82 as := assert.New(t)
83
84 f := getTestFiles()
85
86 out := f.Glob("ship/**").AsSecrets()
87 as.Equal("captain.txt: VGhlIENhcHRhaW4=\nstowaway.txt: TGVnYXR0", out)
88 }
89
90 func TestLines(t *testing.T) {
91 as := assert.New(t)
92
93 f := getTestFiles()
94
95 out := f.Lines("multiline/test.txt")
96 as.Len(out, 2)
97
98 as.Equal("bar", out[0])
99 }
100
101 func TestBlankLines(t *testing.T) {
102 as := assert.New(t)
103
104 f := getTestFiles()
105
106 out := f.Lines("multiline/test_with_blank_lines.txt")
107 as.Len(out, 4)
108
109 as.Equal("bar", out[0])
110 as.Equal("", out[3])
111 }
112
View as plain text