1- package codegen
1+ package codegen_test
22
33import (
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
2122var 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-
11424func 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\n Output:\n %s" , err , output )
278- }
279-
280- // Print the output for visibility
281- t .Logf ("Test harness output:\n %s" , output )
282- }
0 commit comments