82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
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!
|
|
|
|
const std = @import("std");
|
|
|
|
fn main() void {
|
|
std.debug.print("Hello, world!\n", .{});
|
|
}
|
|
`
|
|
|
|
root := &zig.Root{
|
|
ContainerDocComment: "Hello, world!",
|
|
ContainerMembers: []*zig.ContainerMember{
|
|
{
|
|
Decl: &zig.GlobalVarDecl{
|
|
Const: true,
|
|
Name: "std",
|
|
Value: &zig.CallExpr{
|
|
Fun: &zig.Identifier{Name: "@import"},
|
|
Args: []zig.Expr{
|
|
&zig.Literal{Kind: "string", Value: "std"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Decl: &zig.FnDecl{
|
|
Name: "main",
|
|
ReturnType: &zig.Identifier{Name: "void"},
|
|
Body: &zig.Block{
|
|
Stmts: []zig.Stmt{
|
|
&zig.ExprStmt{
|
|
Expr: &zig.CallExpr{
|
|
Fun: &zig.FieldAccessExpr{
|
|
Receiver: &zig.FieldAccessExpr{
|
|
Receiver: &zig.Identifier{Name: "std"},
|
|
Field: "debug",
|
|
},
|
|
Field: "print",
|
|
},
|
|
Args: []zig.Expr{
|
|
&zig.Literal{Kind: "string", Value: "Hello, world!\n"},
|
|
&zig.InitListExpr{Empty: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
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)
|
|
}
|
|
}
|