From 4aad23ab305423d9240eac2210f2c254bec97323 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Mon, 30 Sep 2024 13:21:17 +0300 Subject: [PATCH] update tests --- driver/testdata.go | 55 +++++++++++++++++++++++ enginetest/engine.go | 2 +- menu_traversal_test.go | 100 +++++++++++++++++++++++------------------ 3 files changed, 112 insertions(+), 45 deletions(-) create mode 100644 driver/testdata.go diff --git a/driver/testdata.go b/driver/testdata.go new file mode 100644 index 0000000..e8d9575 --- /dev/null +++ b/driver/testdata.go @@ -0,0 +1,55 @@ +package driver + +import ( + "encoding/json" + "log" + "os" +) + +type Step struct { + Input string `json:"input"` + ExpectedContent string `json:"expectedContent"` +} + +type Group struct { + Name string `json:"name"` + Steps []Step `json:"steps"` +} + +type Session struct { + Name string `json:"name"` + Groups []Group `json:"groups"` +} + +func ReadData() []Session { + data, err := os.ReadFile("test_data_two.json") + if err != nil { + log.Fatalf("Failed to read file: %v", err) + } + // Unmarshal JSON data + var sessions []Session + err = json.Unmarshal(data, &sessions) + if err != nil { + log.Fatalf("Failed to unmarshal JSON: %v", err) + } + + return sessions +} + +func FilterGroupsByName(groups []Group, name string) []Group { + var filteredGroups []Group + for _, group := range groups { + if group.Name == name { + filteredGroups = append(filteredGroups, group) + } + } + return filteredGroups +} + +func Map[T any, U any](input []T, fn func(T) U) []U { + result := make([]U, len(input)) + for i, v := range input { + result[i] = fn(v) + } + return result +} diff --git a/enginetest/engine.go b/enginetest/engine.go index 4eed863..7ea57cb 100644 --- a/enginetest/engine.go +++ b/enginetest/engine.go @@ -27,7 +27,7 @@ func TestEngine(sessionId string) (engine.Engine,*persist.Persister){ Root: "root", SessionId: sessionId, OutputSize: uint32(160), - //FlagCount: uint32(16), + FlagCount: uint32(16), } dbDir := ".test_state" diff --git a/menu_traversal_test.go b/menu_traversal_test.go index c674e50..2483bd5 100644 --- a/menu_traversal_test.go +++ b/menu_traversal_test.go @@ -1,58 +1,70 @@ package main import ( - "bufio" + "bytes" "context" - "encoding/json" - "fmt" - "os" - "strings" "testing" - "git.defalsify.org/vise.git/engine" - + "git.grassecon.net/urdt/ussd/driver" "git.grassecon.net/urdt/ussd/enginetest" ) -type TestCase struct { - Input []string `json:"input"` - Expected string `json:"expected"` -} - -type UserRegistration struct { - UserRegistration []TestCase `json:"user_registration"` -} - -type TestData struct { - UserRegistration []TestCase `json:"user_registration"` - PinCheck []TestCase `json:"pincheck"` -} +var ( + testData = driver.ReadData() +) func TestUserRegistration(t *testing.T) { - en, pe := enginetest.TestEngine("session12341122") - //w := bytes.NewBuffer(nil) - file, err := os.Open("test_data.json") - if err != nil { - fmt.Println("Error opening file:", err) - return - } - defer file.Close() + en, _ := enginetest.TestEngine("session1234112") + var err error + ctx := context.Background() + sessions := testData + for _, session := range sessions { + groups := driver.FilterGroupsByName(session.Groups, "account_creation_successful") + for _, group := range groups { + for _, step := range group.Steps { + cont, _ := en.Exec(ctx, []byte(step.Input)) + if cont { + w := bytes.NewBuffer(nil) + _, err = en.Flush(ctx, w) + if err != nil { + t.Fatal(err) + } + b := w.Bytes() + if !bytes.Equal(b, []byte(step.ExpectedContent)) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + } - var testData TestData - decoder := json.NewDecoder(file) - if err := decoder.Decode(&testData); err != nil { - fmt.Println("Error decoding JSON:", err) - return + } + } + } + } +} + +func TestAcceptTerms(t *testing.T) { + en, _ := enginetest.TestEngine("session12341123") + var err error + ctx := context.Background() + sessions := testData + + for _, session := range sessions { + groups := driver.FilterGroupsByName(session.Groups, "account_creation_accept_terms") + for _, group := range groups { + for _, step := range group.Steps { + cont, _ := en.Exec(ctx, []byte(step.Input)) + if cont { + w := bytes.NewBuffer(nil) + _, err = en.Flush(ctx, w) + if err != nil { + t.Fatal(err) + } + b := w.Bytes() + if !bytes.Equal(b, []byte(step.ExpectedContent)) { + t.Fatalf("expected:\n\t%s\ngot:\n\t%s\n", step.ExpectedContent, b) + } + + } + + } + } } - - var inputBuilder strings.Builder - for _, testCase := range testData.UserRegistration { - inputBuilder.WriteString(strings.Join(testCase.Input, "\n") + "\n") - } - readers := bufio.NewReader(strings.NewReader(inputBuilder.String())) - engine.Loop(context.Background(), en, readers, os.Stdout, nil) - st := pe.GetState() - sym, _ := st.Where() - fmt.Println("Rendering symbol:", sym) - }