Skip to content

Commit

Permalink
Merge pull request #1538 from hylo-lang/refactor-test-generation
Browse files Browse the repository at this point in the history
Gather test runners in the same file
  • Loading branch information
kyouko-taiga authored Jul 28, 2024
2 parents aadefc2 + b5bfc66 commit fe67119
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 89 deletions.
62 changes: 60 additions & 2 deletions Sources/TestUtils/AnnotatedHyloFileTest.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Driver
import FrontEnd
import IR
import Utils
import XCTest

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}

}
36 changes: 0 additions & 36 deletions Sources/TestUtils/LoweringTests.swift

This file was deleted.

23 changes: 0 additions & 23 deletions Sources/TestUtils/ParseTest.swift

This file was deleted.

28 changes: 0 additions & 28 deletions Sources/TestUtils/TypeCheckerTests.swift

This file was deleted.

0 comments on commit fe67119

Please sign in to comment.