From b5bfc662879b37ef33e21e9d5ba3947ae5364832 Mon Sep 17 00:00:00 2001 From: Dimi Racordon Date: Sun, 28 Jul 2024 16:52:25 +0200 Subject: [PATCH] Gather test runners in the same file --- Sources/TestUtils/AnnotatedHyloFileTest.swift | 62 ++++++++++++++++++- Sources/TestUtils/LoweringTests.swift | 36 ----------- Sources/TestUtils/ParseTest.swift | 23 ------- Sources/TestUtils/TypeCheckerTests.swift | 28 --------- 4 files changed, 60 insertions(+), 89 deletions(-) delete mode 100644 Sources/TestUtils/LoweringTests.swift delete mode 100644 Sources/TestUtils/ParseTest.swift delete mode 100644 Sources/TestUtils/TypeCheckerTests.swift diff --git a/Sources/TestUtils/AnnotatedHyloFileTest.swift b/Sources/TestUtils/AnnotatedHyloFileTest.swift index 778692fd8..c63208cc4 100644 --- a/Sources/TestUtils/AnnotatedHyloFileTest.swift +++ b/Sources/TestUtils/AnnotatedHyloFileTest.swift @@ -1,5 +1,6 @@ import Driver import FrontEnd +import IR import Utils import XCTest @@ -108,8 +109,7 @@ extension XCTestCase { /// - expectSuccess: true if an error from `process` represents a test failure, false if the /// lack of an error represents a test failure; nil if that information is to be derived /// from the contents of the file. - @nonobjc - public func checkAnnotatedHyloFileDiagnostics( + private func checkAnnotatedHyloFileDiagnostics( inFileAt hyloFilePath: String, expecting expectation: ExpectedTestOutcome, _ process: (_ file: SourceFile, _ diagnostics: inout DiagnosticSet) throws -> Void @@ -240,4 +240,62 @@ extension XCTestCase { } } + /// Lowers the hylo file at `hyloFilePath` to IR, applying any mandatory passes, and `XCTAssert`s + /// that diagnostics and thrown errors match annotated expectations. + @nonobjc + public func lowerToFinishedIR( + _ hyloFilePath: String, extending p: TypedProgram, expecting expectation: ExpectedTestOutcome + ) throws { + try checkAnnotatedHyloFileDiagnostics( + inFileAt: hyloFilePath, expecting: expectation + ) { (hyloSource, log) in + + let (p, m) = try p.loadModule(reportingDiagnosticsTo: &log) { (ast, log, space) in + // Note: built-in module is visible so that we can test built-in function calls. + try ast.loadModule( + hyloSource.baseName, parsing: [hyloSource], inNodeSpace: space, + withBuiltinModuleAccess: true, + reportingDiagnosticsTo: &log) + } + + // Emit Hylo IR. + var ir = try Module(lowering: m, in: p, reportingDiagnosticsTo: &log) + // Run mandatory IR analysis and transformation passes. + try ir.applyMandatoryPasses(reportingDiagnosticsTo: &log) + } + } + + /// Parses the hylo file at `hyloFilePath`, `XCTAssert`ing that diagnostics and thrown + /// errors match annotated expectations. + @nonobjc + public func parse( + _ hyloFilePath: String, extending p: TypedProgram, expecting expectation: ExpectedTestOutcome + ) throws { + try checkAnnotatedHyloFileDiagnostics( + inFileAt: hyloFilePath, expecting: expectation + ) { (hyloSource, log) in + var ast = AST() + _ = try ast.loadModule( + hyloSource.baseName, parsing: [hyloSource], reportingDiagnosticsTo: &log) + } + } + + /// Type-checks the Hylo file at `hyloFilePath`, `XCTAssert`ing that diagnostics and thrown + /// errors match annotated expectations. + @nonobjc + public func typeCheck( + _ hyloFilePath: String, extending p: TypedProgram, expecting expectation: ExpectedTestOutcome + ) throws { + try checkAnnotatedHyloFileDiagnostics( + inFileAt: hyloFilePath, expecting: expectation + ) { (hyloSource, log) in + _ = try p.loadModule(reportingDiagnosticsTo: &log) { (ast, log, space) in + try ast.loadModule( + hyloSource.baseName, parsing: [hyloSource], inNodeSpace: space, + withBuiltinModuleAccess: true, + reportingDiagnosticsTo: &log) + } + } + } + } diff --git a/Sources/TestUtils/LoweringTests.swift b/Sources/TestUtils/LoweringTests.swift deleted file mode 100644 index ad9c69782..000000000 --- a/Sources/TestUtils/LoweringTests.swift +++ /dev/null @@ -1,36 +0,0 @@ -import FrontEnd -import IR -import StandardLibrary -import Utils -import XCTest - -extension XCTestCase { - - /// Lowers the hylo file at `hyloFilePath` to IR, applying any mandatory passes, and `XCTAssert`s - /// that diagnostics and thrown errors match annotated expectations. - @nonobjc - public func lowerToFinishedIR( - _ hyloFilePath: String, extending p: TypedProgram, expecting expectation: ExpectedTestOutcome - ) throws { - - try checkAnnotatedHyloFileDiagnostics( - inFileAt: hyloFilePath, expecting: expectation - ) { (hyloSource, log) in - - let (p, m) = try p.loadModule(reportingDiagnosticsTo: &log) { (ast, log, space) in - // Note: built-in module is visible so that we can test built-in function calls. - try ast.loadModule( - hyloSource.baseName, parsing: [hyloSource], inNodeSpace: space, - withBuiltinModuleAccess: true, - reportingDiagnosticsTo: &log) - } - - // Emit Hylo IR. - var ir = try Module(lowering: m, in: p, reportingDiagnosticsTo: &log) - // Run mandatory IR analysis and transformation passes. - try ir.applyMandatoryPasses(reportingDiagnosticsTo: &log) - } - - } - -} diff --git a/Sources/TestUtils/ParseTest.swift b/Sources/TestUtils/ParseTest.swift deleted file mode 100644 index eecc6a2fa..000000000 --- a/Sources/TestUtils/ParseTest.swift +++ /dev/null @@ -1,23 +0,0 @@ -import FrontEnd -import XCTest - -extension XCTestCase { - - /// Parses the hylo file at `hyloFilePath`, `XCTAssert`ing that diagnostics and thrown - /// errors match annotated expectations. - @nonobjc - public func parse( - _ hyloFilePath: String, extending p: TypedProgram, expecting expectation: ExpectedTestOutcome - ) throws { - - try checkAnnotatedHyloFileDiagnostics( - inFileAt: hyloFilePath, expecting: expectation - ) { (hyloSource, log) in - var ast = AST() - _ = try ast.loadModule( - hyloSource.baseName, parsing: [hyloSource], reportingDiagnosticsTo: &log) - } - - } - -} diff --git a/Sources/TestUtils/TypeCheckerTests.swift b/Sources/TestUtils/TypeCheckerTests.swift deleted file mode 100644 index 76ad6deba..000000000 --- a/Sources/TestUtils/TypeCheckerTests.swift +++ /dev/null @@ -1,28 +0,0 @@ -import FrontEnd -import StandardLibrary -import Utils -import XCTest - -extension XCTestCase { - - /// Type-checks the Hylo file at `hyloFilePath`, `XCTAssert`ing that diagnostics and thrown - /// errors match annotated expectations. - @nonobjc - public func typeCheck( - _ hyloFilePath: String, extending p: TypedProgram, expecting expectation: ExpectedTestOutcome - ) throws { - - try checkAnnotatedHyloFileDiagnostics( - inFileAt: hyloFilePath, expecting: expectation - ) { (hyloSource, log) in - _ = try p.loadModule(reportingDiagnosticsTo: &log) { (ast, log, space) in - try ast.loadModule( - hyloSource.baseName, parsing: [hyloSource], inNodeSpace: space, - withBuiltinModuleAccess: true, - reportingDiagnosticsTo: &log) - } - } - - } - -}