Method / function names with spaces


(Nicolas Verinaud) #1

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:


(Tomáš Znamenáček) #2

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")
}

(Nicolas Verinaud) #3

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.


(Pierpaolo Frasa) #4

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``() { ... }.


(Nicolas Verinaud) #5

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.


#6

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.