-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_util_test.go
More file actions
66 lines (62 loc) · 2.57 KB
/
syntax_util_test.go
File metadata and controls
66 lines (62 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2023-2026 thorsphere.
// All Rights Reserved. Use is governed with GNU Affero General Public License v3.0
// that can be found in the LICENSE file.
package lpcode_test
// Import Go packages lpcode, tserr and tsfio
import (
// testing
"github.com/thorsphere/lpcode" // lpcode
"github.com/thorsphere/tserr" // tserr
"github.com/thorsphere/tsfio" // tsfio
)
// evalCode evaluates the source code of c by comparing the source code
// with the associated golden file for test case tc. The function returns
// an error if the source code does not match the contents of the golden file.
func evalCode(
c *lpcode.Code,
tc string,
) error {
// Return an error if c is nil
if c == nil {
return tserr.NilPtr()
}
// Format the retrieved code
if e := c.Format(); e != nil {
// The test fails if Format returns an error
return e
}
// Evaluate the retrieved code with the golden file
if e := tsfio.EvalGoldenFile(&tsfio.Testcase{Name: tc, Data: c.String()}); e != nil {
// The test fails if the retrieved code does not match the contents of the golden file
return e
}
// Return nil
return nil
}
// testIfErr generates a code snippet that demonstrates the usage of the IfErr method in lpcode.
// It returns the generated code snippet as a pointer to lpcode.Code.
// The generated code snippet includes a struct declaration, variable specifications,
// a function declaration, an if statement for error handling, another if statement,
// a composite literal with keyed elements, and return statements.
func testIfErr() *lpcode.Code {
// Retrieve a new code.
return lpcode.NewCode().
// type struct declaration with TypeStruct
TypeStruct("foo").
// variable specification with VarSpec
VarSpec(&lpcode.VarSpecArgs{Ident: "A", Type: "int"}).
VarSpec(&lpcode.VarSpecArgs{Ident: "B", Type: "string"}).BlockEnd().
// function declaration with Func1
Func1(&lpcode.Func1Args{Name: "Example", Var: "x", Type: "int", Return: "error"}).
// if statement for error handling with IfErr
IfErr(&lpcode.IfErrArgs{Method: "doSomething()", Operator: "!="}).Return().Ident("err").BlockEnd().
// if statement with If
If(&lpcode.IfArgs{ExprLeft: "x", Operator: ">", ExprRight: "10"}).
// composite literal with CompositeLit
CompositeLit("foo").
// keyed element with KeyedElement
KeyedElement(&lpcode.KeyedElementArgs{Key: "A", Elem: "1"}).
KeyedElement(&lpcode.KeyedElementArgs{Key: `B`, Elem: `"hello"`}).
// return statement with Return, an identifier with Ident, a block ending with BlockEnd and a function ending with FuncEnd
BlockEnd().BlockEnd().Return().Ident("nil").FuncEnd()
}