The seat generator gives every value, event and answer the wire field form (left out while 1) and a tolerant decoder answering the form it read; each contract carries its forms and a digest of its fields, and the compare fails a definition whose fields changed while its form did not. Every delivery seat is regenerated, and the readers take the form. Builds: design 47 H2 §780; H-A-352
168 lines
6.2 KiB
Go
168 lines
6.2 KiB
Go
package seatgen
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
re "regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestAStateHasItsNamesKeyFormsAndTolerantValues is ADR 0634 §6's generator check for `states`: a seat's states give a
|
|
// constant per local name, per key form a builder, a parser and a tolerant reader of the value that answers its form
|
|
// (design 47 H2 §780), an event and an answer have such a reader too, and the fixtures of `state:` decode into those
|
|
// types. The generated seat package is written out and run with `go vet` and `go test`, so
|
|
// what the generator writes is compiled, not only read.
|
|
func TestAStateHasItsNamesKeyFormsAndTolerantValues(t *testing.T) {
|
|
m, err := Load("testdata/contracts", "example.org/contracts/go", "@example/contracts")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files, err := Generate(m)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
src := string(files["go/seats/aregistry/contract.go"])
|
|
for _, want := range []string{
|
|
"func AsksByAskerIDKey(asker, id string) string", "func ParseAsksByAskerIDKey(key string) (string, string, bool)",
|
|
"func DecodeAsksByAskerIDValue(raw []byte)", "type AsksByAskerIDValue struct",
|
|
"func AsksByAskerIDDoneKey(", "func AsksByDefaultsKey() string",
|
|
"func LocalNameBySeenIDOfAskKey(idOfAsk string) string",
|
|
} {
|
|
if !strings.Contains(src, want) {
|
|
t.Errorf("the seat's package lacks %q:\n%s", want, src)
|
|
}
|
|
}
|
|
for _, c := range []string{`StateAsks\s+= "asks"`, `StateLocalName\s+= "local-name"`} {
|
|
if !re.MustCompile(c).MatchString(src) {
|
|
t.Errorf("the seat's package lacks a constant %s", c)
|
|
}
|
|
}
|
|
if strings.Contains(src, "Record") {
|
|
t.Errorf("the seat's package still names a record:\n%s", src)
|
|
}
|
|
js := string(files["ts/seats/aregistry/index.js"])
|
|
dts := string(files["ts/seats/aregistry/index.d.ts"])
|
|
for file, wants := range map[string][]string{
|
|
js: {"export const states = {", "export function asksByAskerIDKey(asker, id) {", "export function parseAsksByAskerIDKey(_key) {",
|
|
"export function parseAsksByNodeModuleKeyKey(_key) {", "const key = _rest;"},
|
|
dts: {"export declare const states: {", "export declare function asksByAskerIDKey(asker: string, id: string): string;",
|
|
"export declare function parseAsksByAskerIDKey(_key: string)", "export interface AsksByAskerIDValue",
|
|
// a key form with a part named key: the parser's own parameter and locals are named apart from the parts
|
|
"export declare function parseAsksByNodeModuleKeyKey(_key: string): { node: string; module: string; key: string } | undefined;"},
|
|
} {
|
|
for _, want := range wants {
|
|
if !strings.Contains(file, want) {
|
|
t.Errorf("the seat's npm package lacks %q:\n%s", want, file)
|
|
}
|
|
}
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
for p, b := range files {
|
|
if !strings.HasPrefix(p, "go/seats/aregistry/") {
|
|
continue
|
|
}
|
|
dst := filepath.Join(dir, strings.TrimPrefix(p, "go/seats/aregistry/"))
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(dst, b, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
extra := `package aregistry
|
|
|
|
import "testing"
|
|
|
|
func TestKeysAndValues(t *testing.T) {
|
|
if k := AsksByAskerIDKey("a", "7"); k != "a.7" {
|
|
t.Fatal(k)
|
|
}
|
|
if a, id, ok := ParseAsksByAskerIDKey("a.7"); !ok || a != "a" || id != "7" {
|
|
t.Fatal(a, id, ok)
|
|
}
|
|
for _, bad := range []string{"", "a", ".7", "a.", "a.7.done"} {
|
|
if bad == "a.7.done" {
|
|
continue // the id takes the rest: another form is told apart by its own parser
|
|
}
|
|
if _, _, ok := ParseAsksByAskerIDKey(bad); ok {
|
|
t.Errorf("%q parsed", bad)
|
|
}
|
|
}
|
|
if a, id, ok := ParseAsksByAskerIDDoneKey("a.7.done"); !ok || a != "a" || id != "7" {
|
|
t.Fatal(a, id, ok)
|
|
}
|
|
if _, _, ok := ParseAsksByAskerIDDoneKey("a.7"); ok {
|
|
t.Error("a key without its literal parsed")
|
|
}
|
|
if !ParseAsksByDefaultsKey("defaults") || ParseAsksByDefaultsKey("defaultsx") {
|
|
t.Error("a literal key")
|
|
}
|
|
// design 47 H2 §780: a value, an event and an answer with a field the reader does not know decode and answer their
|
|
// form; an absent form reads as 1.
|
|
if v, form, err := DecodeAsksByAskerIDValue([]byte(` + "`" + `{"state":"open","extra":1}` + "`" + `)); err != nil || v.State != "open" || form != 1 {
|
|
t.Error("a value with a field the type does not know:", v, form, err)
|
|
}
|
|
if v, form, err := DecodeAsksByAskerIDValue([]byte(` + "`" + `{"state":"open","since":3,"form":2}` + "`" + `)); err != nil || v.State != "open" || form != 2 || v.Form != 2 {
|
|
t.Error(v, form, err)
|
|
}
|
|
if v, form, err := DecodePeekAnswer([]byte(` + "`" + `{"id":"f1","new":true,"form":3}` + "`" + `)); err != nil || v.ID != "f1" || form != 3 {
|
|
t.Error("an answer:", v, form, err)
|
|
}
|
|
if v, form, err := DecodeOwedEvent([]byte(` + "`" + `{"any":"thing","new":1}` + "`" + `)); err != nil || v["any"] != "thing" || form != 1 {
|
|
t.Error("an event:", v, form, err)
|
|
}
|
|
if Form != 1 || FormRegistry != 1 {
|
|
t.Error("forms", Form, FormRegistry)
|
|
}
|
|
}
|
|
`
|
|
if err := os.WriteFile(filepath.Join(dir, "extra_test.go"), []byte(extra), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, args := range [][]string{{"vet", "./..."}, {"test", "./..."}} {
|
|
cmd := exec.Command("go", args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "GOWORK=off", "GOFLAGS=-mod=mod")
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("go %s on the generated package: %v\n%s", strings.Join(args, " "), err, out)
|
|
}
|
|
}
|
|
|
|
// design 47 H2 §780: the generator's check fails a definition whose fields changed with its form unchanged.
|
|
tree := t.TempDir()
|
|
for p, b := range files {
|
|
if err := os.MkdirAll(filepath.Dir(filepath.Join(tree, p)), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(tree, p), b, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
d := m.Seats["a-registry"]
|
|
d.States[0].KeyForms[0].Value["properties"].(map[string]any)["added"] = map[string]any{"type": "string"}
|
|
m.Seats["a-registry"] = d
|
|
unraised := func() bool {
|
|
t.Helper()
|
|
again, err := Generate(m)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := Compare(tree, again)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return strings.Contains(strings.Join(out, "\n"), "contract.go: its fields changed and its form did not")
|
|
}
|
|
if !unraised() {
|
|
t.Error("a field added with the form unchanged passed the compare")
|
|
}
|
|
d.Form = 2
|
|
m.Seats["a-registry"] = d
|
|
if unraised() {
|
|
t.Error("a field added with the form raised failed the compare")
|
|
}
|
|
}
|