Starting Zig code generator with one test
This commit is contained in:
55
internal/zig/zig_test.go
Normal file
55
internal/zig/zig_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package zig_test
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.frop.prof/luke/go-zig-compiler/internal/zig"
|
||||
)
|
||||
|
||||
func Expect[T cmp.Ordered](expected, actual T) error {
|
||||
if expected != actual {
|
||||
return fmt.Errorf("\nExpected: %v\nActual: %v", expected, actual)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestHelloWorld(t *testing.T) {
|
||||
expected := `//! Hello, world!
|
||||
|
||||
fn main() void {
|
||||
return;
|
||||
}
|
||||
`
|
||||
|
||||
root := &zig.Root{
|
||||
ContainerDocComment: "Hello, world!",
|
||||
ContainerMembers: []*zig.ContainerMember{
|
||||
{
|
||||
Decls: []zig.Decl{
|
||||
&zig.FnDecl{
|
||||
Name: "main",
|
||||
ReturnType: "void",
|
||||
Body: &zig.Block{
|
||||
Stmts: []zig.Stmt{
|
||||
&zig.ReturnStmt{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
sb := new(strings.Builder)
|
||||
err := zig.Write(sb, root)
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
actual := sb.String()
|
||||
|
||||
if err = Expect(expected, actual); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user