Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Swift Style Guide

Lucas Fonseca edited this page Aug 10, 2020 · 2 revisions

Swift Test Style Guide

This style guide is based on Apple’s excellent Swift standard library style and describes the preferred style for code written as part of the Beagle project (the framework itself and all our sample code).

Table of Contents

Goals

Following this style guide should:

  • Make it easier to read and begin understanding unfamiliar code.
  • Make code easier to maintain.
  • Reduce simple programmer errors.
  • Reduce cognitive load while coding.
  • Keep discussions on diffs focused on the code's logic rather than its style.

Tests

To keep our tests more readable, we adopted some rules and guides, so please read this document end implement your tests following this guideline.

  • Ever adopt camelcase to tests names, the chosen name must agree with this naming convention.
  • Whenever you can, use the specific XCTAssert to your assertpion, like XCTAssertTrue, XCTAssertNil, etc.
  • Use "//given //when //then" coments to guide your test whenever you can.

Memory leaks test could be a big help for us, so use them whenever necessary.

Bad way❌

func test_whenAnUnknownTypeIsDecoded_thenItShouldReturnNil() { 
     //Given
     let jsonData = """
      {
           "_beagleComponent_": "beagle:unknown"
      }
      """.data(using: .utf8)!
      
     // When
     let unknown = try sut.decodeComponent(from: jsonData) as? UnknownComponent
    
     // Then
     XCTAssert(unknown?.type == "beagle:unknown")
}

Good way✅

func testUnknownTypeIsDecodedWithNil() { 
     //Given
     let jsonData = """
      {
           "_beagleComponent_": "beagle:unknown"
      }
      """.data(using: .utf8)!
      
     // When
     let unknown = try sut.decodeComponent(from: jsonData) as? UnknownComponent
    
     // Then
     XCTAssertEqual(unknown?.type, "beagle:unknown")
}