Files
ZPLify/zplgfa_test.go

96 lines
2.1 KiB
Go
Raw Normal View History

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() {
2018-10-20 01:18:00 +02:00
jsonstr, _ := ioutil.ReadFile("./tests/tests.json")
json.Unmarshal(jsonstr, &zplTests)
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" {
t.Fatalf("CompressASCII failed")
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) {
2018-10-20 01:18:00 +02:00
var graphicType GraphicType
2018-10-19 23:07:53 +02:00
for i, testcase := range zplTests {
2018-10-20 01:18:00 +02:00
filename, zplstring, graphictype := testcase.Filename, testcase.Zplstring, testcase.Graphictype
2018-10-19 23:07:53 +02:00
// open file
file, err := os.Open(filename)
if err != nil {
2018-10-20 01:18:00 +02:00
log.Printf("Warning: could not open the file \"%s\": %s\n", filename, err)
2018-10-19 23:07:53 +02:00
return
}
2018-10-20 01:18:00 +02:00
2018-10-19 23:07:53 +02:00
defer file.Close()
2018-10-20 01:18:00 +02:00
2018-10-19 23:07:53 +02:00
// load image head information
config, format, err := image.DecodeConfig(file)
if err != nil {
log.Printf("Warning: image not compatible, format: %s, config: %v, error: %s\n", format, config, err)
}
2018-10-20 01:18:00 +02:00
2018-10-19 23:07:53 +02:00
// reset file pointer to the beginning of the file
file.Seek(0, 0)
2018-10-20 01:18:00 +02:00
2018-10-19 23:07:53 +02:00
// load and decode image
img, _, err := image.Decode(file)
if err != nil {
log.Printf("Warning: could not decode the file, %s\n", err)
return
}
2018-10-20 01:18:00 +02:00
2018-10-19 23:07:53 +02:00
// flatten image
flat := FlattenImage(img)
2018-10-20 01:18:00 +02:00
2018-10-19 23:07:53 +02:00
// convert image to zpl compatible type
2018-10-20 01:18:00 +02:00
switch graphictype {
case "ASCII":
graphicType = ASCII
case "Binary":
graphicType = Binary
case "CompressedASCII":
graphicType = CompressedASCII
default:
graphicType = CompressedASCII
}
gfimg := ConvertToZPL(flat, graphicType)
2018-10-20 01:18:00 +02:00
if graphictype == "Binary" {
2018-10-20 18:31:30 +02:00
gfimg = base64.StdEncoding.EncodeToString([]byte(gfimg))
} else {
// remove whitespace - only for the test
gfimg = strings.Replace(gfimg, " ", "", -1)
gfimg = strings.Replace(gfimg, "\n", "", -1)
2018-10-20 18:31:30 +02:00
}
2018-10-19 23:07:53 +02:00
if gfimg != zplstring {
2018-10-20 18:31:30 +02:00
log.Printf("ConvertToZPL Test for file \"%s\" failed, wanted: \n%s\ngot: \n%s\n", filename, zplstring, gfimg)
2018-10-19 23:07:53 +02:00
t.Fatalf("Testcase %d ConvertToZPL failed", i)
}
2018-10-19 21:51:55 +02:00
}
}