2018-10-19 20:51:21 +02:00
|
|
|
package zplgfa
|
|
|
|
|
|
|
|
|
|
import (
|
2018-10-20 18:31:30 +02:00
|
|
|
"encoding/base64"
|
2018-10-20 01:18:00 +02:00
|
|
|
"encoding/json"
|
2018-10-19 21:51:55 +02:00
|
|
|
"image"
|
|
|
|
|
_ "image/gif"
|
|
|
|
|
_ "image/jpeg"
|
|
|
|
|
_ "image/png"
|
2018-10-20 01:18:00 +02:00
|
|
|
"io/ioutil"
|
2018-10-19 21:51:55 +02:00
|
|
|
"log"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
2018-10-20 01:18:00 +02:00
|
|
|
"testing"
|
2018-10-19 20:51:21 +02:00
|
|
|
)
|
|
|
|
|
|
2018-10-19 23:07:53 +02:00
|
|
|
type zplTest struct {
|
2018-10-20 01:18:00 +02:00
|
|
|
Filename string `json:"filename"`
|
|
|
|
|
Zplstring string `json:"zplstring"`
|
|
|
|
|
Graphictype string `json:"graphictype"`
|
2018-10-19 20:51:21 +02:00
|
|
|
}
|
2018-10-19 21:51:55 +02:00
|
|
|
|
2018-10-19 23:07:53 +02:00
|
|
|
var zplTests []zplTest
|
2018-10-19 21:51:55 +02:00
|
|
|
|
2018-10-19 23:07:53 +02:00
|
|
|
func init() {
|
2024-06-16 15:35:26 +02:00
|
|
|
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)
|
|
|
|
|
}
|
2018-10-19 23:07:53 +02:00
|
|
|
}
|
2018-10-19 21:51:55 +02:00
|
|
|
|
2018-10-19 23:07:53 +02:00
|
|
|
func Test_CompressASCII(t *testing.T) {
|
|
|
|
|
if str := CompressASCII("FFFFFFFF000000"); str != "NFL0" {
|
2024-06-16 15:35:26 +02:00
|
|
|
t.Fatalf("CompressASCII failed: got %s, want NFL0", str)
|
2018-10-19 21:51:55 +02:00
|
|
|
}
|
2018-10-19 23:07:53 +02:00
|
|
|
}
|
2018-10-19 21:51:55 +02:00
|
|
|
|
2018-10-19 23:07:53 +02:00
|
|
|
func Test_ConvertToZPL(t *testing.T) {
|
|
|
|
|
for i, testcase := range zplTests {
|
2024-06-16 15:35:26 +02:00
|
|
|
t.Run(testcase.Filename, func(t *testing.T) {
|
|
|
|
|
testConvertToZPL(t, testcase, i)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-20 01:18:00 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
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()
|
2018-10-20 01:18:00 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
_, _, err = image.DecodeConfig(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to decode config for file %s: %s", testcase.Filename, err)
|
|
|
|
|
}
|
2018-10-20 01:18:00 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
if _, err := file.Seek(0, 0); err != nil {
|
|
|
|
|
t.Fatalf("Failed to reset file pointer for %s: %s", testcase.Filename, err)
|
|
|
|
|
}
|
2018-10-20 01:18:00 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
img, _, err := image.Decode(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Failed to decode image for file %s: %s", testcase.Filename, err)
|
|
|
|
|
}
|
2018-10-20 01:18:00 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
flat := FlattenImage(img)
|
|
|
|
|
graphicType := parseGraphicType(testcase.Graphictype)
|
|
|
|
|
gfimg := ConvertToZPL(flat, graphicType)
|
2018-10-20 01:18:00 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
if graphicType == Binary {
|
|
|
|
|
gfimg = base64.StdEncoding.EncodeToString([]byte(gfimg))
|
|
|
|
|
} else {
|
|
|
|
|
gfimg = cleanZPLString(gfimg)
|
|
|
|
|
}
|
2018-10-20 01:18:00 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-20 18:31:30 +02:00
|
|
|
|
2024-06-16 15:35:26 +02:00
|
|
|
func parseGraphicType(graphicTypeStr string) GraphicType {
|
|
|
|
|
switch graphicTypeStr {
|
|
|
|
|
case "ASCII":
|
|
|
|
|
return ASCII
|
|
|
|
|
case "Binary":
|
|
|
|
|
return Binary
|
|
|
|
|
case "CompressedASCII":
|
|
|
|
|
return CompressedASCII
|
|
|
|
|
default:
|
|
|
|
|
return CompressedASCII
|
2018-10-19 21:51:55 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-16 15:35:26 +02:00
|
|
|
|
|
|
|
|
func cleanZPLString(zpl string) string {
|
|
|
|
|
zpl = strings.ReplaceAll(zpl, " ", "")
|
|
|
|
|
zpl = strings.ReplaceAll(zpl, "\n", "")
|
|
|
|
|
return zpl
|
|
|
|
|
}
|