From 5edefdff5f5eee26f2652294ce8d0c60731a1ea9 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 2 Mar 2023 09:49:43 -0700 Subject: [PATCH] test $INPUT_RANDOM_WEIGHT_FILE --- src/device/input/input.go | 8 +++++- src/device/input/input_exported_test.go | 36 ++++++++++++++++++++++++ src/device/input/random_exported_test.go | 16 ----------- 3 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 src/device/input/input_exported_test.go delete mode 100644 src/device/input/random_exported_test.go diff --git a/src/device/input/input.go b/src/device/input/input.go index 840dbbb..c9a2ed2 100644 --- a/src/device/input/input.go +++ b/src/device/input/input.go @@ -1,9 +1,15 @@ package input +import "os" + type Input interface { Read() []Button } func New() Input { - return NewRandom(randomCharFromRange('a', 'g')) + foo := randomCharFromRange('a', 'g') + if p, ok := os.LookupEnv("INPUT_RANDOM_WEIGHT_FILE"); ok { + foo = randomCharFromWeightFile(p) + } + return NewRandom(foo) } diff --git a/src/device/input/input_exported_test.go b/src/device/input/input_exported_test.go new file mode 100644 index 0000000..91a0a3a --- /dev/null +++ b/src/device/input/input_exported_test.go @@ -0,0 +1,36 @@ +package input_test + +import ( + "mayhem-party/src/device/input" + "os" + "path" + "testing" +) + +func TestNew(t *testing.T) { + r := input.New() + for i := 0; i < 15; i++ { + batch := r.Read() + for _, got := range batch { + t.Logf("[%d] %c:%v", i, got.Char, got.Down) + } + } +} + +func TestNewRandomWeightFile(t *testing.T) { + d := t.TempDir() + p := path.Join(d, "f") + if err := os.WriteFile(p, []byte(`{"a":1}`), os.ModePerm); err != nil { + t.Fatal(err) + } + os.Setenv("INPUT_RANDOM_WEIGHT_FILE", p) + r := input.New() + for i := 0; i < 15; i++ { + batch := r.Read() + for _, got := range batch { + if got.Char != 'a' { + t.Error(got) + } + } + } +} diff --git a/src/device/input/random_exported_test.go b/src/device/input/random_exported_test.go deleted file mode 100644 index ff5bcd1..0000000 --- a/src/device/input/random_exported_test.go +++ /dev/null @@ -1,16 +0,0 @@ -package input_test - -import ( - "mayhem-party/src/device/input" - "testing" -) - -func TestNew(t *testing.T) { - r := input.New() - for i := 0; i < 15; i++ { - batch := r.Read() - for _, got := range batch { - t.Logf("[%d] %c:%v", i, got.Char, got.Down) - } - } -}