@appling/codegen-go 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@appling/codegen-go",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "Appling code generation extension for Go",
5
5
  "homepage": "https://www.appling.tools/docs/",
6
6
  "repository": {
@@ -16,18 +16,19 @@
16
16
  }
17
17
  },
18
18
  "files": [
19
- "out"
19
+ "out",
20
+ "templates"
20
21
  ],
21
22
  "dependencies": {
22
- "@appling/model": "0.1.5"
23
+ "@appling/model": "0.1.6"
23
24
  },
24
25
  "peerDependencies": {
25
- "@appling/codegen": "0.2.1"
26
+ "@appling/codegen": "0.2.2"
26
27
  },
27
28
  "devDependencies": {
28
- "@appling/codegen": "0.2.1",
29
29
  "@types/node": "^24.19.2",
30
- "typescript": "7.0.2"
30
+ "typescript": "7.0.2",
31
+ "@appling/codegen": "0.2.2"
31
32
  },
32
33
  "license": "MIT",
33
34
  "publishConfig": {
@@ -0,0 +1,142 @@
1
+ {# The access package, in internal/access/access.go (internal/accessctl when a module is named access): the actor,
2
+ the role and actor constants, one Is<Role> check per role, and the tenant helpers. #}
3
+ {% block header %}
4
+ // Package {{ pkg }} holds the actors, roles and tenant of the model and the checks its policies call.
5
+ package {{ pkg }}
6
+
7
+ import (
8
+ {% for path in imports %}
9
+ "{{ path }}"
10
+ {% endfor %}
11
+ )
12
+
13
+ // ErrForbidden is returned by an action when its policies do not grant it to the actor.
14
+ var ErrForbidden = errors.New("forbidden")
15
+ {% endblock %}
16
+ {% block constants %}
17
+
18
+ // Role is a role of the model, by qualified name.
19
+ type Role string
20
+ {% if roles.length %}
21
+
22
+ const (
23
+ {% for role in roles %}
24
+ // Role{{ role.name }} is the role {{ role.qn }}.
25
+ Role{{ role.name }} Role = {{ role.qn | dump }}
26
+ {% endfor %}
27
+ )
28
+ {% endif %}
29
+
30
+ // ActorType is a declared actor of the model, by qualified name.
31
+ type ActorType string
32
+ {% if actors.length %}
33
+
34
+ const (
35
+ {% for actor in actors %}
36
+ // Actor{{ actor.name }} is the {{ actor.kind }} actor {{ actor.qn }}.
37
+ Actor{{ actor.name }} ActorType = {{ actor.qn | dump }}
38
+ {% endfor %}
39
+ )
40
+ {% endif %}
41
+ {% endblock %}
42
+ {% block actor %}
43
+
44
+ // Actor is who acts: a declared actor with the roles it carries, the tenant it acts in, and the fields
45
+ // the actors declare. A field is nil for actors that do not declare it; fields that hold records hold their ID.
46
+ type Actor struct {
47
+ Type ActorType `json:"type"`
48
+ Roles []Role `json:"roles"`
49
+ // ID is the record the actor is stored as, for actors declared with `is`.
50
+ ID *int64 `json:"id"`
51
+ {% if tenant %}
52
+ // Tenant is the ID of the {{ tenant.entity }} the actor acts in (the tenant {{ tenant.qn }}); nil when it acts across tenants.
53
+ Tenant *int64 `json:"tenant"`
54
+ {% endif %}
55
+ {% for field in fields %}
56
+ // {{ field.name }} is the field {{ field.field }} of {{ field.owner }}{{ (", the ID of a " + field.record) if field.record }}.
57
+ {{ field.name }} {{ field.type }} `json:"{{ field.field }}"`
58
+ {% endfor %}
59
+ // system marks a call the system makes; only System sets it.
60
+ system bool
61
+ }
62
+
63
+ // System returns the actor of a call the system makes, such as a job, an event handler or a scenario without
64
+ // an actor: the actions check no policy and no tenant for it. The zero Actor is not the system.
65
+ func System() Actor {
66
+ return Actor{system: true}
67
+ }
68
+
69
+ // IsSystem reports whether the call is one the system makes, which the actions do not check.
70
+ func (a Actor) IsSystem() bool {
71
+ return a.system
72
+ }
73
+ {% endblock %}
74
+ {% block actors %}
75
+ {% for actor in actors %}
76
+
77
+ // New{{ actor.name }} returns the actor {{ actor.qn }} with the roles it declares.
78
+ func New{{ actor.name }}() Actor {
79
+ return Actor{Type: Actor{{ actor.name }}, Roles: []Role{ {{- actor.roles | join(", ") }}}}
80
+ }
81
+ {% endfor %}
82
+ {% endblock %}
83
+ {% block roles %}
84
+
85
+ // Has reports whether the actor carries the role, without checking the role's condition.
86
+ func (a Actor) Has(role Role) bool {
87
+ return slices.Contains(a.Roles, role)
88
+ }
89
+ {% for role in roles %}
90
+
91
+ // Is{{ role.name }} reports whether the actor is {{ role.qn }}: it carries the role{{ " and the role's condition holds" if role.conditional }}.
92
+ func Is{{ role.name }}(actor Actor) bool {
93
+ {% if not role.conditional %}
94
+ return actor.Has(Role{{ role.name }})
95
+ {% elif role.condition %}
96
+ return actor.Has(Role{{ role.name }}) && ({{ role.condition }})
97
+ {% else %}
98
+ return false
99
+ {% endif %}
100
+ }
101
+ {% endfor %}
102
+ {% endblock %}
103
+ {% block helpers %}
104
+
105
+ // Value returns what an optional actor field points to, or the zero value when the actor does not have it.
106
+ func Value[T any](p *T) T {
107
+ if p == nil {
108
+ var zero T
109
+ return zero
110
+ }
111
+ return *p
112
+ }
113
+
114
+ // Same reports whether a record ID of the actor refers to the record with the given ID.
115
+ func Same(ref *int64, id int64) bool {
116
+ return ref != nil && *ref == id
117
+ }
118
+
119
+ // SameRef reports whether two record IDs of the actor refer to the same record.
120
+ func SameRef(a, b *int64) bool {
121
+ return a != nil && b != nil && *a == *b
122
+ }
123
+ {% endblock %}
124
+ {% block tenant %}
125
+ {% if tenant %}
126
+
127
+ // Tenanted is a record that belongs to a {{ tenant.entity }}, the entity of the tenant {{ tenant.qn }}.
128
+ type Tenanted interface {
129
+ // TenantID returns the ID of the {{ tenant.entity }}, and false when the record does not reach one.
130
+ TenantID() (int64, bool)
131
+ }
132
+
133
+ // InTenant reports whether the record belongs to the tenant the actor acts in. An actor without a tenant acts across tenants.
134
+ func InTenant(actor Actor, record Tenanted) bool {
135
+ if actor.Tenant == nil {
136
+ return true
137
+ }
138
+ id, ok := record.TenantID()
139
+ return ok && id == *actor.Tenant
140
+ }
141
+ {% endif %}
142
+ {% endblock %}
@@ -0,0 +1,6 @@
1
+ {# The check of an action, a query or a use case, in internal/<module>/policies.go: its policies and, for an
2
+ action or a query, the update or read check of its entity (or the tenant), combined with &&. #}
3
+ // {{ name }} reports whether the actor may {{ what }}{% if inherited %}, including the {{ inherited }} check of {{ entity }}{% endif %}{% if not parts.length %}; no policy grants it{% endif %}.
4
+ func {{ name }}({{ signature }}) bool {
5
+ return {{ parts | join(" && ") if parts.length else "false" }}
6
+ }
@@ -0,0 +1,18 @@
1
+ {# The checks of an entity, in internal/<module>/policies.go: one per operation, false where no policy grants it,
2
+ and one per operation and field that a field policy narrows. #}
3
+ {% for op in operations %}
4
+ {% if not loop.first %}
5
+
6
+ {% endif %}
7
+ // Can{{ op.name }}{{ entity }} reports whether the policies of {{ qn }} let the actor {{ op.verb }} the record{% if not op.granted %}; no policy grants {{ op.verb }}{% endif %}.
8
+ func Can{{ op.name }}{{ entity }}({{ signature }}) bool {
9
+ return {{ op.parts | join(" && ") }}
10
+ }
11
+ {% for field in op.fields %}
12
+
13
+ // Can{{ op.name }}{{ entity }}{{ field.name }} reports whether the actor may {{ op.verb }} the field {{ field.field }} of the record.
14
+ func Can{{ op.name }}{{ entity }}{{ field.name }}({{ signature }}) bool {
15
+ return {{ field.parts | join(" && ") }}
16
+ }
17
+ {% endfor %}
18
+ {% endfor %}
@@ -0,0 +1,5 @@
1
+ {# A policy, in internal/<module>/policies.go: its condition over the actor and what its element's check receives. #}
2
+ // {{ name }} is the policy {{ qn }}: {{ text }}
3
+ func {{ name }}({{ signature }}) bool {
4
+ return {{ condition or "false" }}
5
+ }
@@ -0,0 +1,9 @@
1
+ {# The filter of a query, in internal/<module>/queries.go: whether a record is among its results. #}
2
+ // In{{ name }} reports whether a record is among the results of the query {{ qn }}{% if params.length > 1 %} for the inputs{% endif %}.
3
+ {% if about %}
4
+ //
5
+ {{ about }}
6
+ {% endif %}
7
+ func In{{ name }}({{ params | join(", ") }}) bool {
8
+ return {{ condition }}
9
+ }
@@ -0,0 +1,35 @@
1
+ {# An action, in internal/<module>/actions.go: a function that checks the actor and the preconditions, runs the
2
+ transitions and effects (precomputed lines of behavior/transitions.go.njk and behavior/effects/) and returns the events. #}
3
+ {% import "go/macros.njk" as go %}
4
+ {% block signature %}
5
+ // {{ name }} is the action {{ qn }}. It returns the events it emits.{{ " The records it creates, updates or deletes go to the store functions, which write them." if stores }}
6
+ {% if about %}
7
+ //
8
+ {{ about }}
9
+ {% endif %}
10
+ func {{ name }}({{ params | join(", ") }}) ([]any, error) {
11
+ var events []any
12
+ {% endblock %}
13
+ {% block guard %}
14
+ {% if guard %}
15
+ {{ go.denied("\t", guard, name, forbidden) -}}
16
+ {% endif %}
17
+ {% endblock %}
18
+ {% block preconditions %}
19
+ {% for precondition in preconditions %}
20
+ if !({{ precondition.condition }}) {
21
+ {% if precondition.error %}
22
+ return nil, {{ precondition.error }}
23
+ {% else %}
24
+ return nil, errors.New({{ precondition.text | dump }})
25
+ {% endif %}
26
+ }
27
+ {% endfor %}
28
+ {% endblock %}
29
+ {% block body %}
30
+ {% for line in body %}
31
+ {{ line }}
32
+ {% endfor %}
33
+ return events, nil
34
+ }
35
+ {% endblock %}
@@ -0,0 +1,20 @@
1
+ {# A create effect: builds the record with New<Entity> and the given fields, checks CanCreate<Entity> once the
2
+ model has policies, and hands it to the create<Entity> store function. A record without a name lives in a block. #}
3
+ {% import "go/macros.njk" as go %}
4
+ {% set pad = "" if variable else "\t" %}
5
+ {% if not variable %}
6
+ {
7
+ {% endif %}
8
+ {{ pad }}{{ record }} := {{ constructor }}()
9
+ {% for field in fields %}
10
+ {{ pad }}{{ record }}.{{ field.name }} = {{ field.value }}
11
+ {% endfor %}
12
+ {% if check %}
13
+ {{ go.denied(pad, check, action, forbidden) -}}
14
+ {% endif %}
15
+ {{ pad }}if err := create{{ entity }}({{ record }}); err != nil {
16
+ {{ pad }} return nil, err
17
+ {{ pad }}}
18
+ {% if not variable %}
19
+ }
20
+ {% endif %}
@@ -0,0 +1,2 @@
1
+ {# An emit effect: appends the event with its fields to the events the function returns. #}
2
+ events = append(events, {{ event }}{ {{- values | join(", ") }}})
@@ -0,0 +1,6 @@
1
+ {# A for effect: runs the effects below it for every item of a list of the record. #}
2
+ for {% if used %}_, {{ variable }} := {% endif %}range {{ collection }} {
3
+ {% for line in body %}
4
+ {{ line }}
5
+ {% endfor %}
6
+ }
@@ -0,0 +1,20 @@
1
+ {# An update or delete effect on a related record (or delete self): sets the fields, checks Can<Operation><Entity>
2
+ once the model has policies, and hands the record to the update<Entity> or delete<Entity> store function.
3
+ An optional path changes nothing when it is nil. #}
4
+ {% import "go/macros.njk" as go %}
5
+ {% set pad = "\t" if optional else "" %}
6
+ {% if optional %}
7
+ if {{ path }} != nil {
8
+ {% endif %}
9
+ {% if check %}
10
+ {{ go.denied(pad, check, action, forbidden) -}}
11
+ {% endif %}
12
+ {% for assignment in sets %}
13
+ {{ pad }}{{ path }}.{{ assignment.name }} = {{ assignment.value }}
14
+ {% endfor %}
15
+ {{ pad }}if err := {{ operation }}{{ entity }}({{ path }}); err != nil {
16
+ {{ pad }} return nil, err
17
+ {{ pad }}}
18
+ {% if optional %}
19
+ }
20
+ {% endif %}
@@ -0,0 +1,2 @@
1
+ {# A set effect: assigns a field of the record. #}
2
+ {{ target }} = {{ value }}
@@ -0,0 +1,3 @@
1
+ {# The error of a refused transition, once per module with a machine, in internal/<module>/errors.go. #}
2
+ // ErrInvalidTransition is returned when the state does not allow an action.
3
+ var ErrInvalidTransition = errors.New("invalid transition")
@@ -0,0 +1,13 @@
1
+ {# The transitions of a machine that one event triggers, in internal/<module>/machines.go. #}
2
+ // {{ name }} applies the transitions of {{ machine }} that {{ event }} triggers.
3
+ {% if about %}
4
+ //
5
+ {{ about }}
6
+ {% endif %}
7
+ func {{ name }}(event {{ type }}) ([]any, error) {
8
+ var events []any
9
+ {% for line in transitions %}
10
+ {{ line }}
11
+ {% endfor %}
12
+ return events, nil
13
+ }
@@ -0,0 +1,13 @@
1
+ {# The transitions an action or an event runs: a switch over the state with one case per transition, in the
2
+ function of the action (behavior/action.go.njk) or of the machine (behavior/machine.go.njk). #}
3
+ switch {
4
+ {% for branch in cases %}
5
+ case {{ branch.condition }}:
6
+ {% for line in branch.effects %}
7
+ {{ line }}
8
+ {% endfor %}
9
+ {{ state }} = {{ branch.to }}
10
+ {% endfor %}
11
+ default:
12
+ return nil, fmt.Errorf("%w: {{ label }} from %s", {{ invalid }}, {{ state }})
13
+ }
@@ -0,0 +1,2 @@
1
+ {# The package comment of a module, in internal/<module>/doc.go. #}
2
+ // Package {{ pkg }} holds the {{ qn }} module of the model.
@@ -0,0 +1,62 @@
1
+ {# An entity, in internal/<module>/types.go: a struct with an ID, a constructor with the defaults, a method per
2
+ derived field, Validate for the invariants and, when it belongs to the tenant, TenantID. #}
3
+ {% block struct %}
4
+ // {{ name }} is the entity {{ qn }}.
5
+ {% if about %}
6
+ //
7
+ {{ about }}
8
+ {% endif %}
9
+ type {{ name }} struct {
10
+ ID int64 `json:"id"`
11
+ {% for field in fields %}
12
+ {{ field.name }} {{ field.type }} `json:"{{ field.json }}"`
13
+ {% endfor %}
14
+ }
15
+ {% endblock %}
16
+ {% block new %}
17
+
18
+ // New{{ name }} returns a new {{ name }} with the defaults of the model.
19
+ func New{{ name }}() *{{ name }} {
20
+ return &{{ name }}{
21
+ {% for field in defaults %}
22
+ {{ field.name }}: {{ field.value }},
23
+ {% endfor %}
24
+ }
25
+ }
26
+ {% endblock %}
27
+ {% block derived %}
28
+ {% for field in derived %}
29
+
30
+ // {{ field.name }} is the derived field {{ field.qn }}.
31
+ func ({{ receiver }} *{{ name }}) {{ field.name }}() {{ field.type }} {
32
+ return {{ field.code }}
33
+ }
34
+ {% endfor %}
35
+ {% endblock %}
36
+ {% block validate %}
37
+
38
+ // Validate checks the invariants of {{ name }}.
39
+ func ({{ receiver }} *{{ name }}) Validate() error {
40
+ var errs []error
41
+ {% for check in checks %}
42
+ if !({{ check.condition }}) {
43
+ errs = append(errs, errors.New({{ check.label | dump }}))
44
+ }
45
+ {% endfor %}
46
+ return errors.Join(errs...)
47
+ }
48
+ {% endblock %}
49
+ {% block tenant %}
50
+ {% if tenant %}
51
+
52
+ // TenantID returns the ID of the {{ tenant.entity }} this {{ name }} belongs to, for the tenant {{ tenant.qn }}.
53
+ func ({{ receiver }} *{{ name }}) TenantID() (int64, bool) {
54
+ {% if tenant.hops.length %}
55
+ if {{ tenant.hops | join(" == nil || ") }} == nil {
56
+ return 0, false
57
+ }
58
+ {% endif %}
59
+ return {{ tenant.record }}.ID, true
60
+ }
61
+ {% endif %}
62
+ {% endblock %}
@@ -0,0 +1,13 @@
1
+ {# An enum: a string type with one constant per literal, in internal/<module>/types.go. #}
2
+ // {{ name }} is the enum {{ qn }}.
3
+ {% if about %}
4
+ //
5
+ {{ about }}
6
+ {% endif %}
7
+ type {{ name }} string
8
+
9
+ const (
10
+ {% for literal in literals %}
11
+ {{ name }}{{ literal.constant }} {{ name }} = {{ literal.value | dump }}
12
+ {% endfor %}
13
+ )
@@ -0,0 +1,3 @@
1
+ {# A declared error, in internal/<module>/errors.go. #}
2
+ // {{ name }} is the declared error {{ qn }}.
3
+ var {{ name }} = errors.New({{ label | dump }})
@@ -0,0 +1,13 @@
1
+ {# A value type (in internal/<module>/types.go) or an event (in internal/<module>/events.go): a struct of its stored fields. #}
2
+ // {{ name }} is the {{ what }} {{ qn }}.
3
+ {% if about %}
4
+ //
5
+ {{ about }}
6
+ {% endif %}
7
+ type {{ name }} struct {
8
+ {% for field in fields %}
9
+ {{ field.name }} {{ field.type }} `json:"{{ field.json }}"`
10
+ {% else %}
11
+
12
+ {% endfor %}
13
+ }
@@ -0,0 +1,7 @@
1
+ {# The actor check shared by actions and their effects. `indent` is the tabs every line starts with. #}
2
+ {% macro denied(indent, check, action, forbidden) %}
3
+ {{ indent }}if !actor.IsSystem() && !{{ check }} {
4
+ {{ indent }} return nil, fmt.Errorf("%w: {{ action }}", {{ forbidden }})
5
+ {{ indent }}}
6
+ {% endmacro %}
7
+
@@ -0,0 +1,3 @@
1
+ {# The ptr helper, in internal/<module>/support.go and in each module's scenarios_test.go. #}
2
+ // ptr returns a pointer to a copy of v, for optional fields.
3
+ func ptr[T any](v T) *T { return &v }
@@ -0,0 +1,24 @@
1
+ {# The test of an invariant, in internal/<module>/<module>_test.go: a valid record passes Validate, and one field
2
+ the simulator proves to break the invariant fails with its label. #}
3
+ func {{ name }}(t *testing.T) {
4
+ {% block holds %}
5
+ t.Run("holds for a valid record", func(t *testing.T) {
6
+ if err := {{ valid }}().Validate(); err != nil {
7
+ t.Fatalf("a valid {{ entity }}: %v", err)
8
+ }
9
+ })
10
+ {% endblock %}
11
+ {% block rejects %}
12
+ t.Run("rejects a violation", func(t *testing.T) {
13
+ {% if violation %}
14
+ x := {{ valid }}()
15
+ {{ violation }}
16
+ if err := x.Validate(); err == nil || !strings.Contains(err.Error(), {{ label | dump }}) {
17
+ t.Fatalf({{ (violation + ": want the error %q, got %v") | dump }}, {{ label | dump }}, err)
18
+ }
19
+ {% else %}
20
+ t.Skip({{ ("registry task: " + qn) | dump }})
21
+ {% endif %}
22
+ })
23
+ {% endblock %}
24
+ }
@@ -0,0 +1,19 @@
1
+ {# The test of a policy on a whole entity, in internal/<module>/<module>_test.go: an actor the simulator proves the
2
+ policy lets through passes Can<Op><Entity>, and one it keeps out fails it. A case without such an actor is skipped. #}
3
+ func {{ name }}(t *testing.T) {
4
+ {% for run in runs %}
5
+ t.Run({{ run.label | dump }}, func(t *testing.T) {
6
+ {% if run.actor %}
7
+ actor := {{ run.actor }}()
8
+ {{ record }} := {{ valid }}()
9
+ {% for check in run.checks %}
10
+ if {{ "!" if run.want else "" }}{{ check }}(actor, {{ record }}) {
11
+ t.Error({{ (check + ": want " + run.want + " for " + run.qn) | dump }})
12
+ }
13
+ {% endfor %}
14
+ {% else %}
15
+ t.Skip({{ ("registry task: " + qn) | dump }})
16
+ {% endif %}
17
+ })
18
+ {% endfor %}
19
+ }
@@ -0,0 +1,65 @@
1
+ {# A scenario, in internal/<module>/scenarios_test.go (package <module>_test): the given rows, the actor, one call
2
+ of the action and the checks of `then`. #}
3
+ // {{ name }} runs the scenario {{ qn }}{{ does }}
4
+ func {{ name }}(t *testing.T) {
5
+ {% block given %}
6
+ {% for row in rows %}
7
+ {{ row.variable }} := {{ row.constructor }}()
8
+ {{ row.variable }}.ID = {{ row.id }}
9
+ {% for field in row.fields %}
10
+ {{ row.variable }}.{{ field.name }} = {{ field.value }}
11
+ {% endfor %}
12
+ {% endfor %}
13
+ {% endblock %}
14
+ {% block actor %}
15
+ {% if actor %}
16
+ actor := {{ actor.constructor }}()
17
+ {% for field in actor.fields %}
18
+ actor.{{ field.name }} = {{ field.value }}
19
+ {% endfor %}
20
+ {% if actor.unused %}
21
+ _ = actor
22
+ {% endif %}
23
+ {% endif %}
24
+ {% endblock %}
25
+ {% block when %}
26
+ {{ result }}, err := {{ call }}
27
+ {% endblock %}
28
+ {% block then %}
29
+ {% if refused %}
30
+ if !errors.Is(err, {{ refused }}) {
31
+ t.Fatalf("{{ label }}: want {{ refused }}, got %v", err)
32
+ }
33
+ {% elif error %}
34
+ if !errors.Is(err, {{ error }}) {
35
+ t.Fatalf("{{ label }}: want {{ error }}, got %v", err)
36
+ }
37
+ {% elif forbidden %}
38
+ if err == nil || errors.Is(err, {{ forbidden }}) {
39
+ t.Fatalf("{{ label }}: want an error, got %v", err)
40
+ }
41
+ {% elif fails %}
42
+ if err == nil {
43
+ t.Fatalf("{{ label }}: want an error, got none")
44
+ }
45
+ {% else %}
46
+ if err != nil {
47
+ t.Fatalf("{{ label }}: %v", err)
48
+ }
49
+ {% for check in checks %}
50
+ {% if check.condition %}
51
+ if !({{ check.condition }}) {
52
+ t.Error({{ (check.shown + " is false") | dump }})
53
+ }
54
+ {% else %}
55
+ if !slices.ContainsFunc(events, func(event any) bool {
56
+ {{ "got" if check.compared.length else "_" }}, ok := event.({{ check.event }})
57
+ return ok{{ " && " if check.compared.length }}{{ check.compared | join(" && ") }}
58
+ }) {
59
+ t.Error({{ (check.shown + " did not happen") | dump }})
60
+ }
61
+ {% endif %}
62
+ {% endfor %}
63
+ {% endif %}
64
+ {% endblock %}
65
+ }
@@ -0,0 +1,4 @@
1
+ {# A skipped test whose body is a registry task, in internal/<module>/<module>_test.go or scenarios_test.go. #}
2
+ func {{ name }}(t *testing.T) {
3
+ t.Skip({{ ("registry task: " + qn) | dump }})
4
+ }
@@ -0,0 +1,27 @@
1
+ {# The test of a transition an action runs, in internal/<module>/<module>_test.go: a valid record goes from a
2
+ `from` state to the `to` state, and a state no transition leaves is refused with ErrInvalidTransition. #}
3
+ func {{ name }}(t *testing.T) {
4
+ {% block goes %}
5
+ t.Run("goes from {{ from.label }} to {{ to.label }}", func(t *testing.T) {
6
+ x := {{ valid }}()
7
+ x.{{ state }} = {{ from.go }}
8
+ if _, err := {{ call }}; err != nil {
9
+ t.Fatalf("{{ action }} from {{ from.label }}: %v", err)
10
+ }
11
+ if x.{{ state }} != {{ to.go }} {
12
+ t.Fatalf("{{ action }} from {{ from.label }}: want {{ to.label }}, got %v", x.{{ state }})
13
+ }
14
+ })
15
+ {% endblock %}
16
+ {% block refuses %}
17
+ {% if invalid %}
18
+ t.Run("refuses {{ invalid.label }}", func(t *testing.T) {
19
+ x := {{ valid }}()
20
+ x.{{ state }} = {{ invalid.go }}
21
+ if _, err := {{ call }}; !errors.Is(err, ErrInvalidTransition) {
22
+ t.Fatalf("{{ action }} from {{ invalid.label }}: want ErrInvalidTransition, got %v", err)
23
+ }
24
+ })
25
+ {% endif %}
26
+ {% endblock %}
27
+ }
@@ -0,0 +1,10 @@
1
+ {# The test helper valid<Entity>, in internal/<module>/<module>_test.go: New<Entity> with a sample value for every
2
+ required stored field without a default. #}
3
+ // {{ name }} returns {{ article }} {{ entity }} with the defaults of the model and a sample value for every other required field.
4
+ func {{ name }}() *{{ entity }} {
5
+ x := New{{ entity }}()
6
+ {% for sample in samples %}
7
+ x.{{ sample.field }} = {{ sample.value }}
8
+ {% endfor %}
9
+ return x
10
+ }