-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestutils.go
127 lines (105 loc) · 2.77 KB
/
testutils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bytes"
"io"
"io/ioutil"
"strings"
"github.com/spf13/afero"
)
// CompareReaders returns true if they contain the same data.
func CompareReaders(r1 io.Reader, r2 io.Reader) bool {
b1, err := ioutil.ReadAll(r1)
if err != nil {
panic("Error reading reader")
}
b2, err := ioutil.ReadAll(r2)
if err != nil {
panic("Error reading reader")
}
return bytes.Compare(b1, b2) == 0
}
// CompareBufferString returns true if the contents of buffer matches those in the string.
func CompareBufferString(buffer *Buffer, s string) bool {
r := strings.NewReader(s)
return CompareReaders(r, buffer.NewReader())
}
// CompareBufferBytes returns true if the contents of buffer matches those in the string.
func CompareBufferBytes(buffer *Buffer, b []byte) bool {
s := string(b)
return CompareBufferString(buffer, s)
}
// A Unicode box
const UnicodeBox = `
╔═╗
║ ║
╚═╝
`
// A 3x3 matrix of .'s
const Empty3x3 = `
...
...
...
`
// StringToRunes converts ASCII art to a RuneGrid for testing.
func StringToRunes(s string, replaceWithNul rune) [][]rune {
s = strings.Trim(s, "\n\t ")
s = strings.Replace(s, string(replaceWithNul), string(0), 9999)
s = strings.Replace(s, "\t", "", 9999)
// Get width based on number of characters in the first line
// (strings.Index doesn't seem to work with the unicode box example)
runes := []rune(s)
i := 0
for runes[i] != '\n' {
i++
}
width := i
// Get height based on line numbers
height := strings.Count(s, "\n") + 1
// Strip newlines
runes = []rune(strings.Replace(s, "\n", "", 9999))
i = 0
grid := make([][]rune, height)
for y := 0; y < height; y++ {
grid[y] = make([]rune, width)
for x := 0; x < width; x++ {
r := runes[i]
i++
grid[y][x] = r
}
}
return grid
}
// StringToRuneGrid converts ASCII art to a RuneGrid for testing.
func StringToRuneGrid(s string, replaceWithNul rune) RuneGrid {
cells := StringToRunes(s, replaceWithNul)
height := len(cells)
if height == 1 {
return NewRuneGrid(0, 0)
}
width := len(cells[0])
grid := NewRuneGrid(width, height)
grid.cells = cells
return grid
}
var fakeFileName = "fakefile.txt"
var fakeFileContents = []byte(`Hello, this is a test`)
var fakeFileContents2 = []byte(`This is the 2nd file!`)
var fakeFileSystem = map[string][]byte{
"file.txt": []byte(`!`),
"fakefile.txt": fakeFileContents,
"fakefile2.txt": fakeFileContents2,
}
// GetTestFs returns an inmemory filesystem with test data.
func GetTestFs() afero.Fs {
return GetCustomTestFs(fakeFileSystem)
}
// GetCustomTestFs returns an inmemory filesystem with the given test files.
func GetCustomTestFs(filemap map[string][]byte) afero.Fs {
fs := afero.MemMapFs{}
for filename, data := range filemap {
file, _ := fs.Create(filename)
defer file.Close()
file.Write(data)
}
return &fs
}