-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathzplgfa_test.go
102 lines (87 loc) · 2.3 KB
/
zplgfa_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
93
94
95
96
97
98
99
100
101
102
package zplgfa
import (
"encoding/base64"
"encoding/json"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"os"
"strings"
"testing"
)
type zplTest struct {
Filename string `json:"filename"`
Zplstring string `json:"zplstring"`
Graphictype string `json:"graphictype"`
}
var zplTests []zplTest
func init() {
jsonstr, err := ioutil.ReadFile("./tests/tests.json")
if err != nil {
log.Fatalf("Failed to read test cases: %s", err)
}
if err := json.Unmarshal(jsonstr, &zplTests); err != nil {
log.Fatalf("Failed to unmarshal test cases: %s", err)
}
}
func Test_CompressASCII(t *testing.T) {
if str := CompressASCII("FFFFFFFF000000"); str != "NFL0" {
t.Fatalf("CompressASCII failed: got %s, want NFL0", str)
}
}
func Test_ConvertToZPL(t *testing.T) {
for i, testcase := range zplTests {
t.Run(testcase.Filename, func(t *testing.T) {
testConvertToZPL(t, testcase, i)
})
}
}
func testConvertToZPL(t *testing.T, testcase zplTest, index int) {
file, err := os.Open(testcase.Filename)
if err != nil {
t.Fatalf("Failed to open file %s: %s", testcase.Filename, err)
}
defer file.Close()
_, _, err = image.DecodeConfig(file)
if err != nil {
t.Fatalf("Failed to decode config for file %s: %s", testcase.Filename, err)
}
if _, err := file.Seek(0, 0); err != nil {
t.Fatalf("Failed to reset file pointer for %s: %s", testcase.Filename, err)
}
img, _, err := image.Decode(file)
if err != nil {
t.Fatalf("Failed to decode image for file %s: %s", testcase.Filename, err)
}
flat := FlattenImage(img)
graphicType := parseGraphicType(testcase.Graphictype)
gfimg := ConvertToZPL(flat, graphicType)
if graphicType == Binary {
gfimg = base64.StdEncoding.EncodeToString([]byte(gfimg))
} else {
gfimg = cleanZPLString(gfimg)
}
if gfimg != testcase.Zplstring {
t.Fatalf("Testcase %d ConvertToZPL failed for file %s: \nExpected: \n%s\nGot: \n%s\n", index, testcase.Filename, testcase.Zplstring, gfimg)
}
}
func parseGraphicType(graphicTypeStr string) GraphicType {
switch graphicTypeStr {
case "ASCII":
return ASCII
case "Binary":
return Binary
case "CompressedASCII":
return CompressedASCII
default:
return CompressedASCII
}
}
func cleanZPLString(zpl string) string {
zpl = strings.ReplaceAll(zpl, " ", "")
zpl = strings.ReplaceAll(zpl, "\n", "")
return zpl
}