Skip to content

Commit 53b0d3c

Browse files
Multi-line test input/output
1 parent d41f2bb commit 53b0d3c

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

Sources/AtCoderLibrary/Command/New/Generator/Test.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,23 @@ struct Test: Generator {
1313
1414
func testExample() throws {
1515
let cases: [TestCase] = [
16-
\(problem.tests.map { """
17-
(#filePath, #line, \"\($0.input.replacingOccurrences(of: "\n", with: "\\n"))\", \"\($0.output.replacingOccurrences(of: "\n", with: "\\n"))\"),
18-
""" }.joined(separator: "\n"))
16+
\(problem.tests.map(Self.testCase).joined(separator: "\n"))
1917
]
2018
try cases.forEach(solve)
2119
}
2220
}
2321
"""
2422
}
23+
24+
private static func testCase(_ test: Problem.Test) -> String {
25+
let inputs = test.input.split(separator: "\n")
26+
let outputs = test.output.split(separator: "\n")
27+
return """
28+
(#filePath, #line, \"""
29+
\(inputs.map { " \($0)" }.joined(separator: "\n"))
30+
\""", \"""
31+
\(outputs.map { " \($0)" }.joined(separator: "\n"))
32+
\"""),
33+
"""
34+
}
2535
}

Sources/AtCoderLibrary/Command/New/Generator/TestLibrary.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
struct TestLibrary: Generator {
22
let directory: String? = "Tests/TestLibrary"
3-
let fileName = "Library.swift"
3+
let fileName = "TestLibrary.swift"
44
var source: String {
55
"""
66
import XCTest
@@ -18,6 +18,7 @@ struct TestLibrary: Generator {
1818
guard #available(macOS 10.13, *) else {
1919
return
2020
}
21+
let expected = expected.last == "\\n" ? expected : expected + "\\n"
2122
var error = ""
2223
let exp = expectation(description: "")
2324
let testTarget = String(describing: type(of: self)).replacingOccurrences(of: "Tests", with: "")

Tests/AtCoderLibraryTests/Command/New/Generator/TestTests.swift

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ final class TestTests: XCTestCase {
77
let problem = Problem(
88
url: "https://example.com/p_1".url,
99
tests: [
10-
.init(input: "input1", output: "output1"),
11-
.init(input: "input2", output: "output2"),
12-
.init(input: "input3", output: "output3"),
10+
.init(input: "10 2\n20 3\n30 4\n", output: "5\n20\n3\n"),
11+
.init(input: "199 2\n", output: "1992\n"),
1312
],
1413
name: "",
1514
context: .init(contest: .empty, alphabet: "A"),
@@ -26,9 +25,20 @@ final class TestTests: XCTestCase {
2625
2726
func testExample() throws {
2827
let cases: [TestCase] = [
29-
(#filePath, #line, "input1", "output1"),
30-
(#filePath, #line, "input2", "output2"),
31-
(#filePath, #line, "input3", "output3"),
28+
(#filePath, #line, \"""
29+
10 2
30+
20 3
31+
30 4
32+
\""", \"""
33+
5
34+
20
35+
3
36+
\"""),
37+
(#filePath, #line, \"""
38+
199 2
39+
\""", \"""
40+
1992
41+
\"""),
3242
]
3343
try cases.forEach(solve)
3444
}

0 commit comments

Comments
 (0)