go-vise

Constrained Size Output Virtual Machine
Info | Log | Files | Refs | README | LICENSE

commit d8a7dcec31129c6708914dbee3eae2f94a92416f
parent cd9478f3b3893090aab15305e948ea875f8e9649
Author: lash <dev@holbrook.no>
Date:   Sun,  1 Sep 2024 18:47:36 +0100

Stabilize language set in db test driver

Diffstat:
Mdb/db.go | 27++++++++++++++++++++-------
Mdb/db_test.go | 78+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
2 files changed, 85 insertions(+), 20 deletions(-)

diff --git a/db/db.go b/db/db.go @@ -54,6 +54,8 @@ type Db interface { SetLock(typ uint8, locked bool) error // Safe returns true if db is safe for use with a vm. Safe() bool + // SetLanguage sets the language context to use on consecutive gets or puts + SetLanguage(*lang.Language) } type lookupKey struct { @@ -98,6 +100,10 @@ func(db *baseDb) SetPrefix(pfx uint8) { db.pfx = pfx } +// SetLanguage implements the Db interface. +func(db *baseDb) SetLanguage(ln *lang.Language) { + db.lang = ln +} // SetSession implements the Db interface. func(db *baseDb) SetSession(sessionId string) { db.sid = append([]byte(sessionId), 0x2E) @@ -125,12 +131,11 @@ func(db *baseDb) checkPut() bool { return db.pfx & db.lock == 0 } -func(db *baseDb) SetLanguage(ln *lang.Language) { - db.lang = ln -} - // ToKey creates a DbKey within the current session context. +// +// TODO: hard to read, clean up func(db *baseDb) ToKey(ctx context.Context, key []byte) (lookupKey, error) { + var ln *lang.Language var lk lookupKey var b []byte if db.pfx == DATATYPE_UNKNOWN { @@ -143,9 +148,17 @@ func(db *baseDb) ToKey(ctx context.Context, key []byte) (lookupKey, error) { } lk.Default = ToDbKey(db.pfx, b, nil) if db.pfx & (DATATYPE_MENU | DATATYPE_TEMPLATE | DATATYPE_STATICLOAD) > 0 { - ln, ok := ctx.Value("Language").(lang.Language) - if ok { - lk.Translation = ToDbKey(db.pfx, b, &ln) + if db.lang != nil { + ln = db.lang + } else { + lo, ok := ctx.Value("Language").(lang.Language) + if ok { + ln = &lo + } + } + logg.TraceCtxf(ctx, "language using", "ln", ln) + if ln != nil { + lk.Translation = ToDbKey(db.pfx, b, ln) } } return lk, nil diff --git a/db/db_test.go b/db/db_test.go @@ -7,12 +7,12 @@ import ( "errors" "fmt" "path" - "strconv" "testing" "git.defalsify.org/vise.git/lang" ) + type testCase struct { typ uint8 s string @@ -20,6 +20,7 @@ type testCase struct { v []byte x []byte l *lang.Language + t string } type testVector struct { @@ -29,6 +30,23 @@ type testVector struct { s string } +type testFunc func() testVector + +var ( + tests = []testFunc{ +// generateSessionTestVectors, + generateLanguageTestVectors, + } + dataTypeDebug = map[uint8]string{ + DATATYPE_BIN: "bytecode", + DATATYPE_TEMPLATE: "template", + DATATYPE_MENU: "menu", + DATATYPE_STATICLOAD: "staticload", + DATATYPE_STATE: "state", + DATATYPE_USERDATA: "udata", + } +) + func(tc *testCase) Key() []byte { return tc.k } @@ -45,10 +63,21 @@ func(tc *testCase) Session() string { return tc.s } +func(tc *testCase) Lang() string { + if tc.l == nil { + return "" + } + return tc.l.Code +} + func(tc *testCase) Expect() []byte { return tc.x } +func(tc *testCase) Label() string { + return tc.t +} + func(tv *testVector) add(typ uint8, k string, v string, session string, expect string, language string) { var b []byte var x []byte @@ -76,7 +105,12 @@ func(tv *testVector) add(typ uint8, k string, v string, session string, expect s } ln = &lo } - + s := dataTypeDebug[typ] + s = path.Join(s, session) + s = path.Join(s, k) + if ln != nil { + s = path.Join(s, language) + } o := &testCase { typ: typ, k: []byte(k), @@ -84,9 +118,8 @@ func(tv *testVector) add(typ uint8, k string, v string, session string, expect s s: session, x: x, l: ln, + t: s, } - s := path.Join(strconv.Itoa(int(typ)), session) - s = path.Join(s, k) tv.c[s] = o i := len(tv.v) tv.v = append(tv.v, s) @@ -116,9 +149,18 @@ func(tv *testVector) put(ctx context.Context, db Db) error { if i == -1 { break } + logg.TraceCtxf(ctx, "running put for test", "vector", tv.label(), "case", tc.Label()) db.SetPrefix(tc.Typ()) db.SetSession(tc.Session()) db.SetLock(tc.Typ(), false) + db.SetLanguage(nil) + if tc.Lang() != "" { + ln, err := lang.LanguageFromCode(tc.Lang()) + if err != nil { + return err + } + db.SetLanguage(&ln) + } err := db.Put(ctx, tc.Key(), tc.Val()) if err != nil { return err @@ -152,7 +194,9 @@ func generateSessionTestVectors() testVector { func generateLanguageTestVectors() testVector { tv := testVector{c: make(map[string]*testCase), s: "language"} tv.add(DATATYPE_BIN, "foo", "deadbeef", "", "beeffeed", "") - tv.add(DATATYPE_BIN, "foo", "deadbeef", "", "deadbeef", "nor") + tv.add(DATATYPE_BIN, "foo", "beeffeed", "", "beeffeed", "nor") + tv.add(DATATYPE_TEMPLATE, "foo", "tinkywinky", "", "tinkywinky", "") + tv.add(DATATYPE_TEMPLATE, "foo", "dipsy", "", "dipsy", "nor") return tv } @@ -172,6 +216,17 @@ func runTest(t *testing.T, ctx context.Context, db Db, vs testVector) error { } else { s += "NoSession" } + if tc.Lang() != "" { + ln, err := lang.LanguageFromCode(tc.Lang()) + if err != nil { + return err + } + s += "Lang" + ln.Code + db.SetLanguage(&ln) + } else { + s += "DefaultLang" + db.SetLanguage(nil) + } r := t.Run(s, func(t *testing.T) { db.SetPrefix(tc.Typ()) db.SetSession(tc.Session()) @@ -191,14 +246,11 @@ func runTest(t *testing.T, ctx context.Context, db Db, vs testVector) error { } func runTests(t *testing.T, ctx context.Context, db Db) error { - err := runTest(t, ctx, db, generateSessionTestVectors()) - if err != nil { - return err - } - - err = runTest(t, ctx, db, generateLanguageTestVectors()) - if err != nil { - return err + for _, fn := range tests { + err := runTest(t, ctx, db, fn()) + if err != nil { + return err + } } return nil