Skip to content

Commit

Permalink
Format tests as well (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeLyon authored Dec 10, 2021
1 parent d660d0e commit 8076673
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/buildAndTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
--mode=format \
--in-place \
--ignore-unparsable-files \
--recursive Sources
--recursive Sources Tests
git diff
git diff-index --quiet HEAD --
Expand Down
58 changes: 32 additions & 26 deletions Tests/ShwiftTests/Shwift Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,36 @@ final class ShwiftCoreTests: XCTestCase {
is: """
Echo
""")

try XCTAssertOutput(
of: { context, standardOutput in
try await Process.run("cat", Self.supportFilePath, standardOutput: standardOutput, in: context)
try await Process.run(
"cat", Self.supportFilePath, standardOutput: standardOutput, in: context)
},
is: """
Cat
""")
Cat
""")

try XCTAssertOutput(
of: { context, standardOutput in
try await Process.run("echo", "Echo", standardOutput: standardOutput, in: context)
try await Process.run("cat", Self.supportFilePath, standardOutput: standardOutput, in: context)
try await Process.run(
"cat", Self.supportFilePath, standardOutput: standardOutput, in: context)
},
is: """
Echo
Cat
""")
Echo
Cat
""")
}

func testFailure() throws {
try XCTAssertOutput(
of: { context, _ in
try await Process.run("false", in: context)
},
is: .failure)
}

func textExecutablePipe() throws {
try XCTAssertOutput(
of: { context, output in
Expand All @@ -48,15 +50,17 @@ final class ShwiftCoreTests: XCTestCase {
try await Process.run("echo", "Input", standardOutput: output, in: context)
},
to: { input in
try await Process.run("sed", "s/Input/Output/", standardInput: input, standardOutput: output, in: context)
}).destination
try await Process.run(
"sed", "s/Input/Output/", standardInput: input, standardOutput: output, in: context)
}
).destination
},
is: """
Echo
Cat
""")
Echo
Cat
""")
}

func testBuiltinOutput() throws {
try XCTAssertOutput(
of: { context, output in
Expand All @@ -69,8 +73,8 @@ final class ShwiftCoreTests: XCTestCase {
}
},
is: """
Builtin (interpolated)
""")
Builtin (interpolated)
""")
}

func testReadFromFile() throws {
Expand All @@ -82,15 +86,15 @@ final class ShwiftCoreTests: XCTestCase {
Cat
""")
}

private enum Outcome: ExpressibleByStringInterpolation {
init(stringLiteral value: String) {
self = .success(value)
}
case success(String)
case failure
}

private func XCTAssertOutput(
of operation: @escaping (Context, FileDescriptor) async throws -> Void,
is expectedOutcome: Outcome,
Expand All @@ -111,8 +115,9 @@ final class ShwiftCoreTests: XCTestCase {
defer { e2.fulfill() }
do {
return try await Output.nullDevice.withFileDescriptor(in: context) { output in
try await Builtin.withChannel(input: input, output: output, in: context) { channel in
return try await channel.input.lines
try await Builtin.withChannel(input: input, output: output, in: context) {
channel in
return try await channel.input.lines
.reduce(into: [], { $0.append($1) })
.joined(separator: "\n")
}
Expand All @@ -121,8 +126,9 @@ final class ShwiftCoreTests: XCTestCase {
XCTFail(file: file, line: line)
throw error
}
})
.destination
}
)
.destination
switch expectedOutcome {
case .success(let expected):
XCTAssertEqual(
Expand All @@ -141,11 +147,11 @@ final class ShwiftCoreTests: XCTestCase {
break
}
}

}
wait(for: [e1, e2], timeout: 2)
}

private static let supportFilePath = Bundle.module.path(forResource: "Cat", ofType: "txt")!
}

Expand Down

0 comments on commit 8076673

Please sign in to comment.