-
Notifications
You must be signed in to change notification settings - Fork 0
/
dots_test.go
92 lines (83 loc) · 1.78 KB
/
dots_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
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
package dots
import (
"bytes"
"image"
"os"
"path/filepath"
"strings"
"testing"
)
func TestShapes(t *testing.T) {
tcs := []struct {
name string
file string
expected string
}{
{
file: "cross.png",
expected: `
⠑⢄⠀⠀⠀⠀⡠⠊
⠀⠀⠑⢄⡠⠊⠀⠀
⠀⠀⡠⠊⠑⢄⠀⠀
⡠⠊⠀⠀⠀⠀⠑⢄`,
},
{
file: "square.png",
expected: `
⡏⠉⠉⠉⠉⠉⠉⢹
⡇⠀⠀⠀⠀⠀⠀⢸
⡇⠀⠀⠀⠀⠀⠀⢸
⣇⣀⣀⣀⣀⣀⣀⣸`,
},
{
file: "checker.png",
expected: `
⠛⣤⠛⣤⠛⣤⠛⣤
⠛⣤⠛⣤⠛⣤⠛⣤
⠛⣤⠛⣤⠛⣤⠛⣤
⠛⣤⠛⣤⠛⣤⠛⣤`,
},
}
var buf bytes.Buffer
for _, tc := range tcs {
buf.Reset()
img := loadImg(t, tc.file)
if err := Write(img, Writer(&buf), Width(8)); err != nil {
t.Fatal("failed to write image bytes", err)
}
if strings.TrimSpace(buf.String()) != strings.TrimSpace(tc.expected) {
t.Fail()
t.Logf("expected:%s\ngot:\n%s\n", tc.expected, buf.String())
}
}
}
func TestOptions(t *testing.T) {
t.Run("invert", func(t *testing.T) {
var buf bytes.Buffer
img := loadImg(t, "cross.png")
if err := Write(img, Writer(&buf), Width(8), Invert()); err != nil {
t.Fatal("failed to write image bytes", err)
}
expected := `
⣮⡻⣿⣿⣿⣿⢟⣵
⣿⣿⣮⡻⢟⣵⣿⣿
⣿⣿⢟⣵⣮⡻⣿⣿
⢟⣵⣿⣿⣿⣿⣮⡻`
got := buf.String()
if strings.TrimSpace(expected) != strings.TrimSpace(got) {
t.Fatalf("expected\n%s\ngot\n%s\n", expected, got)
}
})
}
func loadImg(t *testing.T, path string) image.Image {
f, err := os.Open(filepath.Join("testdata", path))
if err != nil {
t.Fatal("failed to read image file", err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
t.Fatal("failed to decode image", err)
}
return img
}