Skip to content

Commit f65333d

Browse files
committed
Wire up main test properly
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent ecdfc28 commit f65333d

2 files changed

Lines changed: 87 additions & 125 deletions

File tree

pkg/codegen/codegen_test.go

Lines changed: 3 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package codegen
1+
package codegen_test
22

33
import (
44
"bytes"
@@ -12,6 +12,7 @@ import (
1212
"strings"
1313
"testing"
1414

15+
"github.com/glojurelang/glojure/pkg/codegen"
1516
"github.com/glojurelang/glojure/pkg/glj"
1617
"github.com/glojurelang/glojure/pkg/lang"
1718
"github.com/glojurelang/glojure/pkg/reader"
@@ -20,97 +21,6 @@ import (
2021

2122
var updateGolden = flag.Bool("update", false, "update golden files")
2223

23-
const testHarnessCode = `package main
24-
25-
import (
26-
"fmt"
27-
"io/ioutil"
28-
"os"
29-
"path/filepath"
30-
"strings"
31-
32-
_ "github.com/glojurelang/glojure/pkg/codegen/testdata/codegen/test"
33-
"github.com/glojurelang/glojure/pkg/lang"
34-
)
35-
36-
func main() {
37-
// Find all .glj files in testdata directory
38-
// Get the testdata path relative to GOPATH or module root
39-
testdataDir := os.Args[1]
40-
var namespaces []string
41-
42-
err := filepath.Walk(testdataDir, func(path string, info os.FileInfo, err error) error {
43-
if err != nil {
44-
return err
45-
}
46-
if strings.HasSuffix(path, ".glj") {
47-
// Read first line to get namespace
48-
content, err := ioutil.ReadFile(path)
49-
if err != nil {
50-
return err
51-
}
52-
lines := strings.Split(string(content), "\n")
53-
if len(lines) > 0 && strings.HasPrefix(lines[0], "(ns ") {
54-
// Extract namespace name
55-
nsLine := lines[0]
56-
nsLine = strings.TrimPrefix(nsLine, "(ns ")
57-
nsLine = strings.TrimSuffix(nsLine, ")")
58-
parts := strings.Fields(nsLine)
59-
if len(parts) > 0 {
60-
namespaces = append(namespaces, parts[0])
61-
}
62-
}
63-
}
64-
return nil
65-
})
66-
if err != nil {
67-
fmt.Printf("Error walking testdata: %v\n", err)
68-
os.Exit(1)
69-
}
70-
71-
failed := false
72-
for _, nsName := range namespaces {
73-
ns := lang.FindNamespace(lang.NewSymbol(nsName))
74-
if ns == nil {
75-
fmt.Printf("SKIP: namespace %s not found\n", nsName)
76-
continue
77-
}
78-
79-
mainVar := ns.FindInternedVar(lang.NewSymbol("-main"))
80-
if mainVar == nil {
81-
fmt.Printf("SKIP: %s/-main not found\n", nsName)
82-
continue
83-
}
84-
85-
// Check if -main has :expected-output metadata
86-
meta := mainVar.Meta()
87-
if meta == nil {
88-
fmt.Printf("SKIP: %s/-main has no metadata\n", nsName)
89-
continue
90-
}
91-
92-
expected := meta.ValAt(lang.NewKeyword("expected-output"))
93-
if expected == nil {
94-
fmt.Printf("SKIP: %s/-main has no :expected-output\n", nsName)
95-
continue
96-
}
97-
98-
// Run -main and check the result
99-
result := mainVar.Invoke()
100-
if !lang.Equals(result, expected) {
101-
fmt.Printf("FAIL: %s/-main returned %v, expected %v\n", nsName, result, expected)
102-
failed = true
103-
} else {
104-
fmt.Printf("PASS: %s/-main\n", nsName)
105-
}
106-
}
107-
108-
if failed {
109-
os.Exit(1)
110-
}
111-
}
112-
`
113-
11424
func TestCodegen(t *testing.T) {
11525
var testFiles []string
11626
err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
@@ -151,7 +61,7 @@ func TestCodegen(t *testing.T) {
15161

15262
// Generate code for the namespace
15363
var buf bytes.Buffer
154-
gen := New(&buf)
64+
gen := codegen.New(&buf)
15565
if err := gen.Generate(ns); err != nil {
15666
t.Fatalf("failed to generate code: %v", err)
15767
}
@@ -248,35 +158,3 @@ func testMainFunction(t *testing.T, ns *lang.Namespace) {
248158
t.Errorf("-main returned %v, expected %v", result, expectedOutput)
249159
}
250160
}
251-
252-
// TestGeneratedCode compiles and runs the generated code to verify behavior
253-
func TestGeneratedCode(t *testing.T) {
254-
// Write the test harness in a temporary directory
255-
tmpDir, err := ioutil.TempDir("", "glojure_test_harness")
256-
if err != nil {
257-
t.Fatal(err)
258-
}
259-
defer os.RemoveAll(tmpDir)
260-
261-
harnessPath := filepath.Join(tmpDir, "harness_main.go")
262-
if err := ioutil.WriteFile(harnessPath, []byte(testHarnessCode), 0644); err != nil {
263-
t.Fatal(err)
264-
}
265-
266-
// Get absolute path to testdata directory
267-
testdataPath, err := filepath.Abs("testdata")
268-
if err != nil {
269-
t.Fatal(err)
270-
}
271-
272-
// Compile and run the test harness
273-
cmd := exec.Command("go", "run", harnessPath, testdataPath)
274-
cmd.Dir = "."
275-
output, err := cmd.CombinedOutput()
276-
if err != nil {
277-
t.Fatalf("Test harness failed: %v\nOutput:\n%s", err, output)
278-
}
279-
280-
// Print the output for visibility
281-
t.Logf("Test harness output:\n%s", output)
282-
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package codegentest
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
10+
_ "github.com/glojurelang/glojure/pkg/codegen/testdata/codegen/test"
11+
"github.com/glojurelang/glojure/pkg/lang"
12+
)
13+
14+
// TestMain is the entry point for running tests. We use it to ensure
15+
// that this test runs in a separate process.
16+
func TestMain(m *testing.M) {
17+
os.Exit(m.Run())
18+
}
19+
20+
func TestGeneratedGo(t *testing.T) {
21+
// Find all .glj files in testdata directory
22+
testdataDir := "../testdata"
23+
var namespaces []string
24+
25+
err := filepath.Walk(testdataDir, func(path string, info os.FileInfo, err error) error {
26+
if err != nil {
27+
return err
28+
}
29+
if strings.HasSuffix(path, ".glj") {
30+
// Read first line to get namespace
31+
content, err := ioutil.ReadFile(path)
32+
if err != nil {
33+
return err
34+
}
35+
lines := strings.Split(string(content), "\n")
36+
if len(lines) > 0 && strings.HasPrefix(lines[0], "(ns ") {
37+
// Extract namespace name
38+
nsLine := lines[0]
39+
nsLine = strings.TrimPrefix(nsLine, "(ns ")
40+
nsLine = strings.TrimSuffix(nsLine, ")")
41+
parts := strings.Fields(nsLine)
42+
if len(parts) > 0 {
43+
namespaces = append(namespaces, parts[0])
44+
}
45+
}
46+
}
47+
return nil
48+
})
49+
if err != nil {
50+
t.Fatalf("Error walking testdata: %v", err)
51+
}
52+
53+
for _, nsName := range namespaces {
54+
nsName := nsName // Capture range variable
55+
t.Run(nsName, func(t *testing.T) {
56+
ns := lang.FindNamespace(lang.NewSymbol(nsName))
57+
if ns == nil {
58+
t.Fatalf("namespace %s not found", nsName)
59+
}
60+
61+
mainVar := ns.FindInternedVar(lang.NewSymbol("-main"))
62+
if mainVar == nil {
63+
t.Skip()
64+
}
65+
66+
// Check if -main has :expected-output metadata
67+
meta := mainVar.Meta()
68+
if meta == nil {
69+
t.Fatalf("metadata for %s/-main is nil", nsName)
70+
}
71+
72+
expected := meta.ValAt(lang.NewKeyword("expected-output"))
73+
if expected == nil {
74+
t.Fatalf("no :expected-output metadata for %s/-main", nsName)
75+
}
76+
77+
// Run -main and check the result
78+
result := mainVar.Invoke()
79+
if !lang.Equals(result, expected) {
80+
t.Errorf("%s/-main returned %v, expected %v", nsName, result, expected)
81+
}
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)