test: add dynamic test discovery and validity checks.
Modified benchmark_test.go to dynamically discover all .test files in the tests directory instead of using hardcoded paths. Added TestSamplesValidity integration test that validates each test file against its corresponding .expected file. Added runSampleWithOutput helper function to capture interpreter output. Added new test cases with expected outputs for validation.
This commit is contained in:
@@ -2,6 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/config"
|
||||
@@ -9,6 +11,7 @@ import (
|
||||
"git.maximhutz.com/max/lambda/pkg/convert"
|
||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// Helper function to run a single sample through the lambda interpreter.
|
||||
@@ -48,17 +51,86 @@ func runSample(samplePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Benchmark all samples using sub-benchmarks.
|
||||
func BenchmarkSamples(b *testing.B) {
|
||||
samples := map[string]string{
|
||||
"Church": "../../tests/church.test",
|
||||
"Fast": "../../tests/fast.test",
|
||||
"Saccharine": "../../tests/saccharine.test",
|
||||
"Simple": "../../tests/simple.test",
|
||||
"Thunk": "../../tests/thunk.test",
|
||||
// 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
|
||||
}
|
||||
|
||||
for name, path := range samples {
|
||||
// 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.
|
||||
func TestSamplesValidity(t *testing.T) {
|
||||
// Discover all .test files in the tests directory.
|
||||
testFiles, err := filepath.Glob("../../tests/*.test")
|
||||
assert.NoError(t, err, "Failed to read tests directory.")
|
||||
assert.NotEmpty(t, testFiles, "No '*.test' files found in directory.")
|
||||
|
||||
for _, testPath := range testFiles {
|
||||
// Build expected file path.
|
||||
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))
|
||||
|
||||
t.Run(name, func(t *testing.T) {
|
||||
// Run the sample and capture output.
|
||||
actual, err := runSampleWithOutput(testPath)
|
||||
assert.NoError(t, err, "Failed to run sample.")
|
||||
|
||||
// Read expected output.
|
||||
expectedBytes, err := os.ReadFile(expectedPath)
|
||||
assert.NoError(t, err, "Failed to read expected output.")
|
||||
expected := string(expectedBytes)
|
||||
|
||||
// Compare outputs.
|
||||
assert.Equal(t, expected, actual, "Output does not match expected.")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark all samples using sub-benchmarks.
|
||||
func BenchmarkSamples(b *testing.B) {
|
||||
// Discover all .test files in the tests directory.
|
||||
testFiles, err := filepath.Glob("../../tests/*.test")
|
||||
assert.NoError(b, err, "Failed to read tests directory.")
|
||||
assert.NotEmpty(b, testFiles, "No '*.test' files found in directory.")
|
||||
|
||||
for _, path := range testFiles {
|
||||
name := strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
|
||||
|
||||
b.Run(name, func(b *testing.B) {
|
||||
for b.Loop() {
|
||||
if err := runSample(path); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user