Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. import XCTest
  2. @testable import FunctionalKata
  3.  
  4. class FunctionalKataTests: XCTestCase {
  5.  
  6. // Create a function to count the elements contained in a list
  7. func count(list: [Any?]) -> Int {
  8. if let _ = list.first {
  9. let tail = Array(list.dropFirst())
  10. return 1 + count(list: tail)
  11. }
  12. return 0
  13. }
  14.  
  15. func sum(_ list: [Int]) -> Int {
  16. return list.reduce(0, +)
  17. }
  18.  
  19. // Sample implementation of +
  20. func plus(_ left: Int, _ right: Int) -> Int { return left + right }
  21.  
  22. func testListOfNilValues_Count_ReturnsNumberOfNilValues() {
  23. let list: [String?] = [nil, nil]
  24. XCTAssertEqual(count(list: list), 2)
  25. }
  26.  
  27. func testEmptyList_Count_ReturnsZero() {
  28. XCTAssertEqual(count(list: [String]()), 0)
  29. }
  30.  
  31. func testListOneElement_Count_ReturnsOne() {
  32. XCTAssertEqual(count(list: ["element"]), 1)
  33. }
  34.  
  35. func testListTwoElements_Count_ReturnsTwo() {
  36. XCTAssertEqual(count(list: ["element", "anotherElement"]), 2)
  37. }
  38.  
  39. func testSumZeroToZero_ReturnsZero() {
  40. XCTAssertEqual(sum([]), 0)
  41. }
  42.  
  43. func testSum123Returns6() {
  44. XCTAssertEqual(sum([1,2,3]), 6)
  45. }
  46.  
  47. func testSumMinusOneToOne_Returns0() {
  48. XCTAssertEqual(sum([-1,1]), 0)
  49. }
  50.  
  51. func test1Plus2_Returns3() {
  52. XCTAssertEqual(plus(1,2), 3)
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement