At the beginning of the FizzBuzz code challenge , we deleted the @testable line of code. What does this @testable do?
When we import a module in our test class, all public methods and properties in this module become visible to our test class. As we know in Swift, default access control is internal and not public. That means we have to use the public access control in our implementation code to make our methods and properties available for testing.
If we want to use the implementation code as a framework, then it is fine to keep the public access control. Otherwise, it seems a lot of unnecessary and boilerplate code for just making our methods visible to our test class; @testable can be handy in this case, as it makes all the internal methods visible in our test class. That means we can omit all the public keywords from implementation, and our FizzBuzzCalculator will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
struct FizzBuzzCalculator { func fizzBuzz(number: Int) -> String { if isDivisibleBy(numerator: number, denominator: 3) && isDivisibleBy(numerator: number, denominator: 5) { return "FizzBuzz" } else if isDivisibleBy(numerator: number, denominator: 5) { return "Buzz" } else if isDivisibleBy(numerator: number, denominator: 3) { return "Fizz" } return "\(number)" } func isDivisibleBy(numerator: Int, denominator: Int) -> Bool { return numerator % denominator == 0 } } |