2018-04-12 20:00:09 -04:00
|
|
|
package bootcommand
|
|
|
|
|
2018-04-13 17:41:17 -04:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2018-08-23 16:16:01 -04:00
|
|
|
"time"
|
2018-04-13 17:41:17 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2018-04-12 20:00:09 -04:00
|
|
|
|
|
|
|
func Test_chunkScanCodes(t *testing.T) {
|
|
|
|
|
|
|
|
var chunktests = []struct {
|
|
|
|
size int
|
|
|
|
in [][]string
|
|
|
|
out [][]string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
3,
|
|
|
|
[][]string{
|
|
|
|
{"a", "b"},
|
|
|
|
{"c"},
|
|
|
|
{"d"},
|
|
|
|
{"e", "f"},
|
|
|
|
{"g", "h"},
|
|
|
|
{"i", "j"},
|
|
|
|
{"k"},
|
|
|
|
{"l", "m"},
|
|
|
|
},
|
|
|
|
[][]string{
|
|
|
|
{"a", "b", "c"},
|
|
|
|
{"d", "e", "f"},
|
|
|
|
{"g", "h"},
|
|
|
|
{"i", "j", "k"},
|
|
|
|
{"l", "m"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
-1,
|
|
|
|
[][]string{
|
|
|
|
{"a", "b"},
|
|
|
|
{"c"},
|
|
|
|
{"d"},
|
|
|
|
{"e", "f"},
|
|
|
|
{"g", "h"},
|
|
|
|
{"i", "j"},
|
|
|
|
{"k"},
|
|
|
|
{"l", "m"},
|
|
|
|
},
|
|
|
|
[][]string{
|
|
|
|
{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range chunktests {
|
|
|
|
out, err := chunkScanCodes(tt.in, tt.size)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equalf(t, tt.out, out, "expecting chunks of %d.", tt.size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_chunkScanCodeError(t *testing.T) {
|
|
|
|
// can't go from wider to thinner
|
|
|
|
in := [][]string{
|
|
|
|
{"a", "b", "c"},
|
|
|
|
{"d", "e", "f"},
|
|
|
|
{"g", "h"},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := chunkScanCodes(in, 2)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
2018-04-13 17:41:17 -04:00
|
|
|
|
2018-04-19 20:02:39 -04:00
|
|
|
func Test_pcxtSpecialOnOff(t *testing.T) {
|
2018-04-13 17:41:17 -04:00
|
|
|
in := "<rightShift><rightshiftoff><RIGHTSHIFTON>"
|
|
|
|
expected := []string{"36", "b6", "b6", "36"}
|
|
|
|
var codes []string
|
|
|
|
sendCodes := func(c []string) error {
|
|
|
|
codes = c
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-23 16:16:01 -04:00
|
|
|
d := NewPCXTDriver(sendCodes, -1, time.Duration(0))
|
2018-04-13 17:41:17 -04:00
|
|
|
seq, err := GenerateExpressionSequence(in)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = seq.Do(context.Background(), d)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, codes)
|
|
|
|
}
|
2018-04-19 19:44:50 -04:00
|
|
|
|
2018-04-19 20:02:39 -04:00
|
|
|
func Test_pcxtSpecial(t *testing.T) {
|
|
|
|
in := "<left>"
|
2018-04-19 20:07:58 -04:00
|
|
|
expected := []string{"e0", "4b", "e0", "cb"}
|
2018-04-19 20:02:39 -04:00
|
|
|
var codes []string
|
|
|
|
sendCodes := func(c []string) error {
|
|
|
|
codes = c
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-23 16:16:01 -04:00
|
|
|
d := NewPCXTDriver(sendCodes, -1, time.Duration(0))
|
2018-04-19 20:02:39 -04:00
|
|
|
seq, err := GenerateExpressionSequence(in)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = seq.Do(context.Background(), d)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, codes)
|
|
|
|
}
|
|
|
|
|
2018-04-19 19:44:50 -04:00
|
|
|
func Test_flushes(t *testing.T) {
|
|
|
|
in := "abc123<wait>098"
|
|
|
|
expected := [][]string{
|
|
|
|
{"1e", "9e", "30", "b0", "2e", "ae", "02", "82", "03", "83", "04", "84"},
|
|
|
|
{"0b", "8b", "0a", "8a", "09", "89"},
|
|
|
|
}
|
|
|
|
var actual [][]string
|
|
|
|
sendCodes := func(c []string) error {
|
|
|
|
actual = append(actual, c)
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-23 16:16:01 -04:00
|
|
|
d := NewPCXTDriver(sendCodes, -1, time.Duration(0))
|
2018-04-19 19:44:50 -04:00
|
|
|
seq, err := GenerateExpressionSequence(in)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = seq.Do(context.Background(), d)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, actual)
|
|
|
|
}
|
2018-08-23 16:16:01 -04:00
|
|
|
|
|
|
|
func Test_KeyIntervalNotGiven(t *testing.T) {
|
2018-08-24 23:43:51 -04:00
|
|
|
d := NewPCXTDriver(nil, -1, time.Duration(0))
|
2018-08-23 16:16:01 -04:00
|
|
|
assert.Equal(t, d.interval, time.Duration(100)*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_KeyIntervalGiven(t *testing.T) {
|
2018-08-24 23:43:51 -04:00
|
|
|
d := NewPCXTDriver(nil, -1, time.Duration(5000)*time.Millisecond)
|
2018-08-23 16:16:01 -04:00
|
|
|
assert.Equal(t, d.interval, time.Duration(5000)*time.Millisecond)
|
|
|
|
}
|