|
| 1 | +using System.Reflection; |
| 2 | +using Xunit; |
| 3 | +using Xunit.Abstractions; |
| 4 | + |
| 5 | +namespace Sharpy.Compiler.Tests.Integration; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// File-based integration tests that discover .spy test files and their expected outputs. |
| 9 | +/// |
| 10 | +/// Test files are organized in the TestFixtures directory: |
| 11 | +/// TestFixtures/ |
| 12 | +/// feature_name/ |
| 13 | +/// test_name.spy - Sharpy source code |
| 14 | +/// test_name.expected - Expected stdout output |
| 15 | +/// test_name.error - (optional) Expected compilation error substring |
| 16 | +/// |
| 17 | +/// A test passes if: |
| 18 | +/// - The .spy file compiles and executes successfully |
| 19 | +/// - The stdout matches the contents of .expected exactly |
| 20 | +/// |
| 21 | +/// Error tests (when .error file exists): |
| 22 | +/// - The .spy file should fail to compile |
| 23 | +/// - The error message should contain the text in .error |
| 24 | +/// </summary> |
| 25 | +public class FileBasedIntegrationTests : IntegrationTestBase |
| 26 | +{ |
| 27 | + private static readonly string FixturesPath; |
| 28 | + |
| 29 | + static FileBasedIntegrationTests() |
| 30 | + { |
| 31 | + // Find the TestFixtures directory relative to the test assembly |
| 32 | + var assemblyLocation = Assembly.GetExecutingAssembly().Location; |
| 33 | + var assemblyDir = Path.GetDirectoryName(assemblyLocation)!; |
| 34 | + |
| 35 | + // Navigate from bin/Debug/net10.0 to the source directory |
| 36 | + FixturesPath = Path.GetFullPath(Path.Combine( |
| 37 | + assemblyDir, "..", "..", "..", "Integration", "TestFixtures")); |
| 38 | + } |
| 39 | + |
| 40 | + public FileBasedIntegrationTests(ITestOutputHelper output) : base(output) |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Discovers all test fixtures by scanning the TestFixtures directory. |
| 46 | + /// </summary> |
| 47 | + public static IEnumerable<object[]> GetTestFixtures() |
| 48 | + { |
| 49 | + if (!Directory.Exists(FixturesPath)) |
| 50 | + { |
| 51 | + yield break; |
| 52 | + } |
| 53 | + |
| 54 | + // Find all .spy files recursively |
| 55 | + foreach (var spyFile in Directory.EnumerateFiles(FixturesPath, "*.spy", SearchOption.AllDirectories)) |
| 56 | + { |
| 57 | + var relativePath = Path.GetRelativePath(FixturesPath, spyFile); |
| 58 | + var testName = Path.ChangeExtension(relativePath, null).Replace(Path.DirectorySeparatorChar, '/'); |
| 59 | + yield return new object[] { testName, spyFile }; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + [Theory] |
| 64 | + [MemberData(nameof(GetTestFixtures))] |
| 65 | + public void RunTestFixture(string testName, string spyFilePath) |
| 66 | + { |
| 67 | + Output.WriteLine($"Running test: {testName}"); |
| 68 | + Output.WriteLine($"Source file: {spyFilePath}"); |
| 69 | + |
| 70 | + // Read the source file |
| 71 | + var source = File.ReadAllText(spyFilePath); |
| 72 | + Output.WriteLine("=== Sharpy Source ==="); |
| 73 | + Output.WriteLine(source); |
| 74 | + Output.WriteLine("====================="); |
| 75 | + |
| 76 | + // Check if this is an error test |
| 77 | + var errorFilePath = Path.ChangeExtension(spyFilePath, ".error"); |
| 78 | + var isErrorTest = File.Exists(errorFilePath); |
| 79 | + |
| 80 | + // Execute the test |
| 81 | + var result = CompileAndExecute(source, Path.GetFileName(spyFilePath)); |
| 82 | + |
| 83 | + if (isErrorTest) |
| 84 | + { |
| 85 | + // Error test: compilation should fail |
| 86 | + var expectedError = File.ReadAllText(errorFilePath).Trim(); |
| 87 | + Output.WriteLine($"Expected error: {expectedError}"); |
| 88 | + |
| 89 | + Assert.False(result.Success, |
| 90 | + $"Expected compilation to fail, but it succeeded. Output: {result.StandardOutput}"); |
| 91 | + |
| 92 | + var actualErrors = string.Join("\n", result.CompilationErrors); |
| 93 | + Assert.Contains(expectedError, actualErrors, StringComparison.OrdinalIgnoreCase); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + // Success test: compilation should succeed and output should match |
| 98 | + var expectedFilePath = Path.ChangeExtension(spyFilePath, ".expected"); |
| 99 | + |
| 100 | + Assert.True(File.Exists(expectedFilePath), |
| 101 | + $"Missing expected output file: {expectedFilePath}"); |
| 102 | + |
| 103 | + var expectedOutput = File.ReadAllText(expectedFilePath); |
| 104 | + |
| 105 | + Assert.True(result.Success, |
| 106 | + $"Compilation failed: {string.Join("\n", result.CompilationErrors)}"); |
| 107 | + |
| 108 | + Assert.Equal(expectedOutput, result.StandardOutput); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Verifies that the test fixtures directory exists and contains at least one test. |
| 114 | + /// This is a sanity check to ensure the test discovery is working. |
| 115 | + /// </summary> |
| 116 | + [Fact] |
| 117 | + public void TestFixturesDirectory_Exists() |
| 118 | + { |
| 119 | + Output.WriteLine($"Looking for fixtures in: {FixturesPath}"); |
| 120 | + Assert.True(Directory.Exists(FixturesPath), |
| 121 | + $"TestFixtures directory not found at: {FixturesPath}"); |
| 122 | + } |
| 123 | +} |
0 commit comments