Skip to content

Commit 0ac2aeb

Browse files
committed
chore: switch to assert lib with support for excluding fields by type
1 parent fe3d41e commit 0ac2aeb

File tree

5 files changed

+9
-28
lines changed

5 files changed

+9
-28
lines changed

engine/internal/memoise.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ func Memoise[T any](f func() (T, error)) *MemoisedFunction[T] {
99
return &MemoisedFunction[T]{f: f}
1010
}
1111

12+
// MemoisedFunction lazily caches the result of a function on first use.
1213
type MemoisedFunction[T any] struct {
1314
once sync.Once
1415
f func() (T, error)
1516
val T
1617
err error
1718
}
1819

20+
// Get calls the function or returns the result of the previous call.
1921
func (o *MemoisedFunction[T]) Get() (T, error) {
2022
o.once.Do(func() { o.val, o.err = o.f() })
2123
return o.val, o.err

engine/logging/logger.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func (l *Logger) writerScanner(wg *sync.WaitGroup, r *io.PipeReader, level LogLe
256256
os.Stdout.Write(newline)
257257
return
258258
} else if err != nil {
259+
os.Stdout.Write(newline)
259260
l.Warnf("error reading CSI sequence: %s", err)
260261
return
261262
}

parser/parser.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ func (r *RefList) Strings() []string {
307307
type Ref struct {
308308
Pos lexer.Position
309309

310+
// Text is the text body of a Ref.
311+
//
310312
// This is a bit hairy because we need to explicitly match WS
311313
// to "un"-elide it, but we don't want to capture it.
312314
Text string `WS? ((?!WS) @(Var | Cmd | Ident | Number | "-" | "/" | "." | "*" | "@" | "[" | "]" | "{" | "}" | "!" | ","))+ | @(String | StringLiteral | MultilineString)`

parser/parser_test.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"embed"
55
"io/fs"
66
"os"
7-
"reflect"
87
"strings"
98
"testing"
109

@@ -232,30 +231,11 @@ func TestParser(t *testing.T) {
232231
}
233232
bitfile, err := parser.ParseString("", input, options...)
234233
assert.NoError(t, err, "%s\n%s", repr.String(tokens, repr.Indent(" ")), repr.String(bitfile, repr.Indent(" ")))
235-
normaliseAllNodes(bitfile)
236-
assert.Equal(t, test.expected, bitfile, repr.String(tokens, repr.Indent(" ")))
234+
assert.Equal(t, test.expected, bitfile, repr.String(tokens, repr.Indent(" ")), assert.Exclude[lexer.Position]())
237235
})
238236
}
239237
}
240238

241-
func normaliseAllNodes[T Node](node T) T {
242-
_ = Visit(node, func(node Node, next func() error) error {
243-
normaliseNode(node)
244-
return next()
245-
})
246-
return node
247-
}
248-
249-
func normaliseNode[T any](node T) T {
250-
v := reflect.Indirect(reflect.ValueOf(node))
251-
f := v.FieldByName("Pos")
252-
if !f.CanAddr() {
253-
panic(node)
254-
}
255-
f.Set(reflect.Zero(f.Type()))
256-
return node
257-
}
258-
259239
func tokenise(t *testing.T, input string) []lexer.Token {
260240
t.Helper()
261241
lex, err := lex.Lex("", strings.NewReader(input))
@@ -284,6 +264,5 @@ func TestParseSamples(t *testing.T) {
284264
func TestParseRefList(t *testing.T) {
285265
refs, err := ParseRefList(lexer.Position{}, `a b c`)
286266
assert.NoError(t, err)
287-
normaliseAllNodes(refs)
288-
assert.Equal(t, &RefList{Refs: []*Ref{{Text: "a"}, {Text: "b"}, {Text: "c"}}}, refs)
267+
assert.Equal(t, &RefList{Refs: []*Ref{{Text: "a"}, {Text: "b"}, {Text: "c"}}}, refs, assert.Exclude[lexer.Position]())
289268
}

parser/text_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ import (
44
"testing"
55

66
"github.com/alecthomas/assert/v2"
7+
"github.com/alecthomas/participle/v2/lexer"
78
)
89

910
func TestParseText(t *testing.T) {
1011
text, err := ParseTextString(`Hello, %{world}! What's happening %(echo "today")%?`)
1112
assert.NoError(t, err)
12-
text = normaliseNode(text)
13-
for i, fragment := range text.Fragments {
14-
text.Fragments[i] = normaliseNode(fragment)
15-
}
1613
assert.Equal(t, &Text{
1714
Fragments: []Fragment{
1815
&TextFragment{Text: "Hello, "},
@@ -21,5 +18,5 @@ func TestParseText(t *testing.T) {
2118
&CmdFragment{Cmd: `echo "today"`},
2219
&TextFragment{Text: "?"},
2320
},
24-
}, text)
21+
}, text, assert.Exclude[lexer.Position]())
2522
}

0 commit comments

Comments
 (0)