123 lines
2.5 KiB
Go
123 lines
2.5 KiB
Go
// Copyright (C) 2019 ston1th
|
|
|
|
package encoder
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"testing"
|
|
)
|
|
|
|
func TestComment(t *testing.T) {
|
|
var (
|
|
in = bytes.NewBufferString(`
|
|
REM Hello World
|
|
`)
|
|
out = new(bytes.Buffer)
|
|
)
|
|
e, err := NewEncoder(out, "de")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = e.Encode(in)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !bytes.Equal(out.Bytes(), nil) {
|
|
t.Error("bytes not equal")
|
|
}
|
|
}
|
|
|
|
func TestString(t *testing.T) {
|
|
var (
|
|
in = bytes.NewBufferString(`
|
|
STRING Hello World
|
|
`)
|
|
out = new(bytes.Buffer)
|
|
want = []byte{0xb, 0x2, 0x8, 0x0, 0xf, 0x0, 0xf, 0x0,
|
|
0x12, 0x0, 0x2c, 0x0, 0x1a, 0x2, 0x12, 0x0,
|
|
0x15, 0x0, 0xf, 0x0, 0x7, 0x0}
|
|
)
|
|
e, err := NewEncoder(out, "de")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = e.Encode(in)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !bytes.Equal(out.Bytes(), want) {
|
|
t.Error("bytes not equal")
|
|
}
|
|
}
|
|
|
|
func TestExample(t *testing.T) {
|
|
var (
|
|
in = bytes.NewBufferString(`
|
|
REM Type Hello World into Windows notepad. Target: Windows 95 and beyond. Author: Darren
|
|
DELAY 1000
|
|
GUI r
|
|
DELAY 100
|
|
STRING c:\windows\notepad.exe
|
|
ENTER
|
|
DELAY 1000
|
|
STRING Hello World
|
|
`)
|
|
out = new(bytes.Buffer)
|
|
want = []byte{0x0, 0xff, 0x0, 0xff, 0x0, 0xff, 0x0, 0xeb,
|
|
0x15, 0x8, 0x0, 0x64, 0x6, 0x0, 0x37, 0x2,
|
|
0x2d, 0x40, 0x1a, 0x0, 0xc, 0x0, 0x11, 0x0,
|
|
0x7, 0x0, 0x12, 0x0, 0x1a, 0x0, 0x16, 0x0,
|
|
0x2d, 0x40, 0x11, 0x0, 0x12, 0x0, 0x17, 0x0,
|
|
0x8, 0x0, 0x13, 0x0, 0x4, 0x0, 0x7, 0x0,
|
|
0x37, 0x0, 0x8, 0x0, 0x1b, 0x0, 0x8, 0x0,
|
|
0x28, 0x0, 0x0, 0xff, 0x0, 0xff, 0x0, 0xff,
|
|
0x0, 0xeb, 0xb, 0x2, 0x8, 0x0, 0xf, 0x0,
|
|
0xf, 0x0, 0x12, 0x0, 0x2c, 0x0, 0x1a, 0x2,
|
|
0x12, 0x0, 0x15, 0x0, 0xf, 0x0, 0x7, 0x0}
|
|
)
|
|
e, err := NewEncoder(out, "de")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
err = e.Encode(in)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !bytes.Equal(out.Bytes(), want) {
|
|
t.Error("bytes not equal")
|
|
}
|
|
}
|
|
|
|
func TestError(t *testing.T) {
|
|
out := new(bytes.Buffer)
|
|
_, err := NewEncoder(out, "")
|
|
if err == nil {
|
|
t.Error("language '' should not be supported")
|
|
}
|
|
_, err = NewEncoder(out, "fizzbuzz")
|
|
if err == nil {
|
|
t.Error("language 'fizzbuzz' should not be supported")
|
|
}
|
|
}
|
|
|
|
func BenchmarkExample(b *testing.B) {
|
|
var in = bytes.NewBufferString(`
|
|
REM Type Hello World into Windows notepad. Target: Windows 95 and beyond. Author: Darren
|
|
DELAY 1000
|
|
GUI r
|
|
DELAY 100
|
|
STRING c:\windows\notepad.exe
|
|
ENTER
|
|
DELAY 1000
|
|
STRING Hello World
|
|
`)
|
|
e, err := NewEncoder(ioutil.Discard, "de")
|
|
if err != nil {
|
|
b.Error(err)
|
|
}
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
e.Encode(in)
|
|
}
|
|
}
|