diff --git a/cmd/lambda/benchmark_test.go b/cmd/lambda/lambda_test.go similarity index 67% rename from cmd/lambda/benchmark_test.go rename to cmd/lambda/lambda_test.go index aaeab0d..9f827f8 100644 --- a/cmd/lambda/benchmark_test.go +++ b/cmd/lambda/lambda_test.go @@ -15,17 +15,17 @@ import ( ) // 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. input, err := os.ReadFile(samplePath) if err != nil { - return err + return "", err } // Parse code into syntax tree. ast, err := saccharine.Parse(string(input)) if err != nil { - return err + return "", err } // Compile expression to lambda calculus. @@ -45,46 +45,7 @@ func runSample(samplePath string) error { process := engine.New(cfg, &compiled) process.Run() - // Get final result (to ensure it's not optimized away). - _ = 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 + return lambda.Stringify(compiled) + "\n", nil } // Test that all samples produce expected output. @@ -98,16 +59,11 @@ func TestSamplesValidity(t *testing.T) { // 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) + actual, err := runSample(testPath) assert.NoError(t, err, "Failed to run sample.") // Read expected output. @@ -133,9 +89,8 @@ func BenchmarkSamples(b *testing.B) { b.Run(name, func(b *testing.B) { for b.Loop() { - if err := runSample(path); err != nil { - b.Fatal(err) - } + _, err := runSample(path) + assert.NoError(b, err, "Failed to run sample.") } }) }