-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A better way to deal with tabular test cases #862
Comments
Sorry, but it's not clear what you're asking for here. Please clarify—if there's something actionable here, we can reopen this issue. |
Might #840 help? |
I think the feature you're looking for is what we call Parameterized Tests. See https://swiftpackageindex.com/swiftlang/swift-testing/6.0.3/documentation/testing |
Oh hey yeah. I think my mind filled in |
OK, I tried that, but it's not really satisfactory. There are two problems:
|
If you're using If you don't want all combinations of inputs, you can use |
( |
Exactly how could that help me? |
BTW, I read the documentation. I don't think you are understanding the problem here. The looping is unavoidable in this case. Clone the project yourself and see if it isn't obvious from GitHub. Once you start looping over things in a test case, the feedback you get about what failed and why is poor, so you end up writing large "comment" parameters to the |
If I'm understanding you correctly, you have this: let regularCases: [String: [(input: String, expected: Bool)]] = [
"": [("", true), ("x", false), ("xy", false)],
"x": [("", false), ("x", true), ("xy", false)],
"x+": [("", false), ("x", true), ("xy", false), ("xx", true)],
"x*": [("", true), ("x", true), ("xy", false), ("xx", true)],
"x?": [("", true), ("x", true), ("xy", false), ("xx", false)],
"x|y": [("", false), ("x", true), ("y", true), ("xx", false)],
] And you want to transform it into something like let regularCases: [(pattern: String, input: String, expected: Bool)] = [
"": [("", true), ("x", false), ("xy", false)],
...
].flatMap { pattern, inputAndExpected in
inputAndExpected.map { input, expected in
(pattern, input, expected)
}
}
@Test(arguments: regularCases) func myTest(pattern: String, input: String, expected: Bool) {
...
} Feel free to tweak the exact spelling to your liking, of course. |
I understand what you're suggesting here. The problem is that for each pattern, there's a bunch of preliminary work (and potentially, tests) that shouldn't be repeated for each input/expectation combination. I can imagine it might be possible to solve this by nesting test functions. Is that supported? |
That pattern would be covered by @stmontgomery's work on scoped traits here. |
Reading that proposal, I don't see how to apply it. I'd assume the general idea is:
But I don't see how to express it. Can you show how that would be done. This is how I imagine expressing the problem if nested tests were supported: @Test(arguments: regularCases) func nfaToDfa(pattern: String, expectations: [(input: String, expected: Bool)]) async throws {
let n = TestNFA(pattern)
let d = SmallDFA(EquivalentDFA(n)) // small makes it easier to read.
let m = MinimizedDFA(d)
#expect(m.states.count <= d.states.count)
@Test(arguments: expectations) func matching(x: (input: String, expectedMatch: Bool)) async throws {
#expect(n.recognizes(x.input) == x.expectedMatch)
#expect(d.recognizes(x.input) == x.expectedMatch)
#expect(m.recognizes(x.input) == x.expectedMatch)
}
} |
Description
Here's an example I'm working with:
When one of these fails, it of course points to one of the two expect lines; that's not really what I want at all. Plus, I have to add these comments to get useful information out. Maybe there's a different idiom that would work better, in which case document it? Or maybe different library features are needed.
Expected behavior
No response
Actual behavior
No response
Steps to reproduce
No response
swift-testing version/commit hash
No response
Swift & OS version (output of
swift --version && uname -a
)No response
The text was updated successfully, but these errors were encountered: