unit tests are good and v01cfg transforms input if players and user in players

master
bel 2023-03-25 09:06:43 -06:00
parent 0ee3a8b6e8
commit db69f76aa0
3 changed files with 103 additions and 36 deletions

View File

@ -30,20 +30,10 @@ type (
Message string Message string
} }
Players []struct { Players []struct {
Buttons struct { Transformation v01Transformation
Up string
Down string
Left string
Right string
L string
R string
A string
B string
X string
Y string
}
} }
} }
v01Transformation map[string]string
) )
func NewV01(src raw.Raw) V01 { func NewV01(src raw.Raw) V01 {
@ -67,7 +57,33 @@ func (v01 V01) Read() []Button {
log.Printf("%v: %s", err, line) log.Printf("%v: %s", err, line)
} }
v01.telemetry(msg) v01.telemetry(msg)
return msg.buttons()
return v01.cfg.transform(msg).buttons()
}
func (cfg v01Cfg) transform(msg v01Msg) v01Msg {
if len(cfg.Players) == 0 {
return msg
}
user := cfg.Users[msg.U]
if user.Player < 1 {
msg.Y = ""
msg.N = ""
return msg
}
player := cfg.Players[user.Player-1]
msg.Y = player.Transformation.pipe(msg.Y)
msg.N = player.Transformation.pipe(msg.N)
return msg
}
func (t v01Transformation) pipe(s string) string {
for i := range s {
if v := t[s[i:i+1]]; v != "" {
s = s[:i] + v[:1] + s[i+1:]
}
}
return s
} }
func (v01 V01) telemetry(msg v01Msg) { func (v01 V01) telemetry(msg v01Msg) {

View File

@ -0,0 +1,31 @@
package button_test
import (
"fmt"
"mayhem-party/src/device/input/button"
"testing"
"time"
)
func TestV01(t *testing.T) {
src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50))
t.Logf("(%v) %s", len(src), src.Read())
v01 := button.NewV01(src)
got := v01.Read()
want := []button.Button{
{Down: true, Char: 'a'},
{Down: true, Char: 'b'},
{Down: true, Char: 'c'},
{Down: false, Char: 'c'},
{Down: false, Char: 'd'},
{Down: false, Char: 'e'},
}
if len(got) != len(want) {
t.Fatal(len(want), len(got))
}
for i := range got {
if got[i] != want[i] {
t.Errorf("[%d] want %+v got %+v", i, want[i], got[i])
}
}
}

View File

@ -1,31 +1,51 @@
package button_test package button
import ( import (
"fmt"
"mayhem-party/src/device/input/button"
"testing" "testing"
"time"
) )
func TestV01(t *testing.T) { func TestV01TransformationPipe(t *testing.T) {
src := constSrc(fmt.Sprintf(`{"T":%v,"U":"bel","Y":"abc","N":"cde"}`, time.Now().UnixNano()/int64(time.Millisecond)-50)) cases := map[string]struct {
t.Logf("(%v) %s", len(src), src.Read()) input string
v01 := button.NewV01(src) xform map[string]string
got := v01.Read() want string
want := []button.Button{ }{
{Down: true, Char: 'a'}, "empty input": {
{Down: true, Char: 'b'}, xform: map[string]string{"a": "bc"},
{Down: true, Char: 'c'}, },
{Down: false, Char: 'c'}, "empty xform": {
{Down: false, Char: 'd'}, input: "aa",
{Down: false, Char: 'e'}, want: "aa",
},
"all": {
input: "aa",
xform: map[string]string{"a": "cc"},
want: "cc",
},
"last": {
input: "ba",
xform: map[string]string{"a": "cc"},
want: "bc",
},
"first": {
input: "ab",
xform: map[string]string{"a": "cc"},
want: "cb",
},
"noop": {
input: "bb",
xform: map[string]string{"a": "bc"},
want: "bb",
},
} }
if len(got) != len(want) {
t.Fatal(len(want), len(got)) for name, d := range cases {
} c := d
for i := range got { t.Run(name, func(t *testing.T) {
if got[i] != want[i] { got := v01Transformation(c.xform).pipe(c.input)
t.Errorf("[%d] want %+v got %+v", i, want[i], got[i]) if got != c.want {
t.Errorf("%+v(%s) want %s got %s", c.xform, c.input, c.want, got)
} }
})
} }
} }