Method / function names with spaces

Hi !

I love the way F# supports spaces in function names, here is an example :

// function definition
let ``add two to`` x = x + 2
//  function call
``add two to`` 4 // 6

It would be great if this can be supported in swift too !

My primary use case would be in unit tests which could read like well written specification, here is an example :

func ``test it should add two to the given number``() {
    XCTAssertEqual(6, addTwo(to: 4))
}

What do you think ? :smile:

1 Like

I don’t find the proposed use case very compelling. In my case the test would look a bit like this:

func testArithmetics() {
    XCTAssertEqual(1+1, 2, "Addition works correctly")
    XCTAssertEqual(2-1, 1, "Subtraction works correctly")
}
2 Likes

I find your example a bit contrived. Mine is a bit too I concede.

What about a more "real-world" one like this :

func test_Should_display_correct_information_when_request_fails() {
    givenRequestWillFail(withError: "Error")
    whenSendingInvoice()
    thenShouldDisplayError(withMessage: "Error")
}

Would give :

func ``test It should display correct information when request fails``() {
    givenRequestWillFail(withError: "Error")
    whenSendingInvoice()
    thenShouldDisplayError(withMessage: "Error")
}

Getting rid of test in the beginning of the method name would also be great but probably the subject of an other topic.

Instead of having to allow spaces in function names in order to be able to write more concise tests, I'd rather take a look at e.g. Quick.

Personally, I find describe("it should add two to the given number") { ... } more readable than func ``it should add two to the given number``() { ... }.

2 Likes

Yep Quick is awesome, but not everybody can use it for different reasons. Improving the standard way to tests is also important in my humble opinion.

3 Likes

Perhaps this can be done via Markdown, this way we can pack a lot more information.

/**
 - testDescription: It should display correct information when request fails.
 - failBehaviour:
    - If it fails with `Error1`, look into class `Foo`
    - If it fails with `Error2`, ...
 */
func testUIOnFailRequest() { ... }

And we can have the tester pick up these information rather than using function name.

4 Likes

I believe tests should be self documenting. Using markdown in this way usually leads to developers questioning whether the content of the markdown still holds true. Also, I would say needing that much content is probably a smell that your test should be broken up into smaller chunks anyway.

Not every team is so lucky as to have a separate "tester" - some of us write all of our own tests :)

1 Like

+1 for a feature like this

I am quite fond of how kotlin allows test functions to start and end with a single "`"

fun `ensure function returns true when this condition`() { ... }`

This syntax allows for really easy to read/write tests and takes out any need to markdown or comments. Especially in the context of a test report, having to read through test_functionName_conditions_expectedOutcome() is horrendous by comparison.

2 Likes

It was rejected: SE-0275 Allow more characters (like whitespaces and punctuations) for escaped identifiers.

1 Like