Skip to content

Commit b5a4964

Browse files
author
Jaakko Heusala
committed
Initial gndtest
1 parent f5a4ddd commit b5a4964

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

cmd/gndtest/main.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
// TestCase represents a single test case
10+
type TestCase struct {
11+
Name string
12+
Input string
13+
Expected string
14+
Description string
15+
}
16+
17+
// TestResult represents the result of running a test
18+
type TestResult struct {
19+
TestCase TestCase
20+
Passed bool
21+
Output string
22+
Error string
23+
}
24+
25+
func parseTestFile(content string) (TestCase, error) {
26+
lines := strings.Split(content, "\n")
27+
if len(lines) < 4 {
28+
return TestCase{}, fmt.Errorf("invalid test file format")
29+
}
30+
31+
// Format:
32+
// name: Test Name
33+
// description: Test Description
34+
// input: |
35+
// test input
36+
// expected: |
37+
// expected output
38+
var testCase TestCase
39+
var currentSection string
40+
var currentContent strings.Builder
41+
42+
for _, line := range lines {
43+
if strings.HasPrefix(line, "name: ") {
44+
testCase.Name = strings.TrimPrefix(line, "name: ")
45+
} else if strings.HasPrefix(line, "description: ") {
46+
testCase.Description = strings.TrimPrefix(line, "description: ")
47+
} else if strings.HasPrefix(line, "input: |") {
48+
currentSection = "input"
49+
currentContent.Reset()
50+
} else if strings.HasPrefix(line, "expected: |") {
51+
currentSection = "expected"
52+
currentContent.Reset()
53+
} else if currentSection != "" {
54+
if line == "" && currentContent.Len() == 0 {
55+
continue
56+
}
57+
currentContent.WriteString(line)
58+
currentContent.WriteString("\n")
59+
}
60+
}
61+
62+
testCase.Input = strings.TrimSpace(currentContent.String())
63+
return testCase, nil
64+
}
65+
66+
func main() {
67+
if len(os.Args) != 2 {
68+
fmt.Println("Usage: gndtest <test-file>")
69+
os.Exit(1)
70+
}
71+
72+
testFile := os.Args[1]
73+
74+
// Read test file
75+
content, err := os.ReadFile(testFile)
76+
if err != nil {
77+
fmt.Printf("Error reading test file: %v\n", err)
78+
os.Exit(1)
79+
}
80+
81+
// Parse test case
82+
testCase, err := parseTestFile(string(content))
83+
if err != nil {
84+
fmt.Printf("Error parsing test case: %v\n", err)
85+
os.Exit(1)
86+
}
87+
88+
// Run test
89+
result := runTest(testCase)
90+
91+
// Print result
92+
if result.Passed {
93+
fmt.Printf("✓ Test '%s' passed\n", testCase.Name)
94+
} else {
95+
fmt.Printf("✗ Test '%s' failed\n", testCase.Name)
96+
if result.Error != "" {
97+
fmt.Printf("Error: %s\n", result.Error)
98+
}
99+
if result.Output != "" {
100+
fmt.Printf("Output: %s\n", result.Output)
101+
}
102+
os.Exit(1)
103+
}
104+
}
105+
106+
func runTest(testCase TestCase) TestResult {
107+
// TODO: Implement actual test execution using gnd
108+
// For now, just return a placeholder result
109+
return TestResult{
110+
TestCase: testCase,
111+
Passed: true,
112+
}
113+
}

0 commit comments

Comments
 (0)