Improve testing infrastructure with dynamic discovery and validation #20

Merged
mvhutz merged 4 commits from style/lambda-iterator into main 2026-01-13 01:20:47 +00:00
Showing only changes of commit e17a85e0a3 - Show all commits

View File

@@ -15,17 +15,17 @@ import (
) )
// Helper function to run a single sample through the lambda interpreter. // Helper function to run a single sample through the lambda interpreter.
func runSample(samplePath string) error { func runSample(samplePath string) (string, error) {
// Read the sample file. // Read the sample file.
input, err := os.ReadFile(samplePath) input, err := os.ReadFile(samplePath)
if err != nil { if err != nil {
return err return "", err
} }
// Parse code into syntax tree. // Parse code into syntax tree.
ast, err := saccharine.Parse(string(input)) ast, err := saccharine.Parse(string(input))
if err != nil { if err != nil {
return err return "", err
} }
// Compile expression to lambda calculus. // Compile expression to lambda calculus.
@@ -45,46 +45,7 @@ func runSample(samplePath string) error {
process := engine.New(cfg, &compiled) process := engine.New(cfg, &compiled)
process.Run() process.Run()
// Get final result (to ensure it's not optimized away). return lambda.Stringify(compiled) + "\n", nil
_ = lambda.Stringify(compiled)
return nil
}
// Helper function to run a sample and return its output.
func runSampleWithOutput(samplePath string) (string, error) {
// Read the sample file.
input, err := os.ReadFile(samplePath)
if err != nil {
return "", err
}
// Parse code into syntax tree.
ast, err := saccharine.Parse(string(input))
if err != nil {
return "", err
}
// Compile expression to lambda calculus.
compiled := convert.SaccharineToLambda(ast)
// Create minimal config for testing.
cfg := &config.Config{
Source: config.StringSource{Data: ""},
Destination: config.StdoutDestination{},
Profile: "",
Explanation: false,
Statistics: false,
Verbose: false,
}
// Create and run the engine.
process := engine.New(cfg, &compiled)
process.Run()
// Get final result.
output := lambda.Stringify(compiled)
return output + "\n", nil
} }
// Test that all samples produce expected output. // Test that all samples produce expected output.
@@ -98,16 +59,11 @@ func TestSamplesValidity(t *testing.T) {
// Build expected file path. // Build expected file path.
expectedPath := strings.TrimSuffix(testPath, filepath.Ext(testPath)) + ".expected" expectedPath := strings.TrimSuffix(testPath, filepath.Ext(testPath)) + ".expected"
// Check if expected file exists.
if _, err := os.Stat(expectedPath); os.IsNotExist(err) {
continue
}
name := strings.TrimSuffix(filepath.Base(testPath), filepath.Ext(testPath)) name := strings.TrimSuffix(filepath.Base(testPath), filepath.Ext(testPath))
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
// Run the sample and capture output. // Run the sample and capture output.
actual, err := runSampleWithOutput(testPath) actual, err := runSample(testPath)
assert.NoError(t, err, "Failed to run sample.") assert.NoError(t, err, "Failed to run sample.")
// Read expected output. // Read expected output.
@@ -133,9 +89,8 @@ func BenchmarkSamples(b *testing.B) {
b.Run(name, func(b *testing.B) { b.Run(name, func(b *testing.B) {
for b.Loop() { for b.Loop() {
if err := runSample(path); err != nil { _, err := runSample(path)
b.Fatal(err) assert.NoError(b, err, "Failed to run sample.")
}
} }
}) })
} }