1- cw-2 :
1+ xctest :
22 algorithms :
33 initial : |-
44 // return the two oldest/oldest ages within the array of ages passed in.
55 // it should return the two ages as a sorted array, youngest age first
6- func twoOldestAges(ages: [Int]) -> [Int] {
7- // TODO: complete
6+ func twoOldestAges(ages: [Int]) -> [Int] { // TODO: complete
87 }
98
109 answer : |-
@@ -21,102 +20,102 @@ cw-2:
2120 return [nextOldest, oldest]
2221 }
2322
24-
2523 fixture : |-
26- // some example data
27- let results1 = twoOldestAges(ages: [1,5,87,45,8,8])
28- let results2 = twoOldestAges(ages: [6,5,83,5,3,18])
29-
30- // NOTE: You can use Test or test, whichever you prefer.
31-
32- // Use "describe" to label your test suite.
33- Test.describe("twoOldestAges")
34-
35- // Use "it" to identify the conditions you are testing for
36- Test.it("should return the second oldest age first")
37- // using assert_equals will report the invalid values to the user
38- Test.assert_equals(results1[0], 45)
39- // using expect will just give a user a generic error message, unless you provide a message
40- Test.expect(results2[0] == 18, "Number is not the second oldest")
24+ import XCTest
4125
42- // its best practice to test for multiple groups of tests, using it calls.
43- Test.it("should return the oldest age last")
26+ class TwoOldestAgesTest: XCTestCase {
27+ static var allTests = [
28+ ("testReturnSecondOldestFirst", testReturnSecondOldestFirst),
29+ ("testReturnOldestSecond", testReturnOldestSecond),
30+ ]
31+ func testReturnSecondOldestFirst() {
32+ let r1 = twoOldestAges(ages: [1,5,87,45,8,8])
33+ XCTAssertEqual(r1[0], 45)
34+ let r2 = twoOldestAges(ages: [6,5,83,5,3,18])
35+ XCTAssertEqual(r2[0], 18)
36+ }
37+ func testReturnOldestSecond() {
38+ let r1 = twoOldestAges(ages: [1,5,87,45,8,8])
39+ XCTAssertEqual(r1[1], 87)
40+ let r2 = twoOldestAges(ages: [6,5,83,5,3,18])
41+ XCTAssertEqual(r2[1], 83)
42+ }
43+ }
4444
45- Test.assert_equals(results1[1], 87)
46- Test.expect(results2[1] == 83, "Number is not the oldest")
45+ _XCTMain([
46+ testCase(TwoOldestAgesTest.allTests)
47+ ])
4748
4849 bug fixes :
4950 initial : |-
50- func add(a: Int, b: Int) -> Int {
51+ func add(_ a: Int, _ b: Int) -> Int {
5152 a + b
5253 }
5354
5455 answer : |-
55- func add(a: Int, b: Int) -> Int {
56+ func add(_ a: Int, _ b: Int) -> Int {
5657 return a + b
5758 }
5859
5960 fixture : |-
60- // Use "describe" to define the test suite
61- Test.describe("add method")
61+ import XCTest
6262
63- // Use "it" to indicate a condition you are testing for
64- Test.it("should add both arguments and return")
63+ class AddTest: XCTestCase {
64+ static var allTests = [
65+ ("testAdd", testAdd),
66+ ]
67+ func testAdd() {
68+ XCTAssertEqual(add(1, 1), 2)
69+ }
70+ }
6571
66- // "assert_equals" will return information about what values were
67- // expect if the assertion fails. This can be very useful to other
68- // users trying to pass the kata.
69- Test.assert_equals(add(a:1,b:2), 3)
72+ _XCTMain([
73+ testCase(AddTest.allTests)
74+ ])
7075
71- // "expect" is a lower level assertion that will allow you to test
72- // anything. It just needs a boolean result. You should pass a message
73- // as the second parameter so that if the assertion fails the user
74- // will be giving some useful information.
75- Test.expect(add(a:1,b:1) == 2, "add(a:1,b:1) should == 2")
7676
7777 refactoring :
7878 initial : |-
7979 // refactor this method into a Person class with its own greet method
80+ // let p = Person("foo")
81+ // p.name == "foo"
82+ // p.greet("bar") == "Hello bar, my name is foo"
8083 func greet(other: String, name: String) -> String {
8184 return "Hello \(other), my name is \(name)"
8285 }
8386
8487 answer : |-
8588 class Person {
8689 let name: String
87- init(name: String) {
90+ init(_ name: String) {
8891 self.name = name
8992 }
90- func greet(other: String) -> String {
93+ func greet(_ other: String) -> String {
9194 return "Hello \(other), my name is \(self.name)"
9295 }
9396 }
9497
9598 fixture : |-
96- // Use "describe" to define the test suite
97- Test.describe("Person")
98-
99- let jack = Person(name: "Jack")
100-
101- // Use "it" to indicate a condition you are testing for
102- Test.it("should have a name")
103-
104- // "assert_equals" will return information about what values were
105- // expected if the assertion fails. This can be very useful to other
106- // users trying to pass the kata.
107- Test.assert_equals(jack.name, "Jack")
108-
109- Test.it("should greet Jill")
110-
111- Test.assert_equals(jack.greet(other: "Jill"), "Hello Jill, my name is Jack")
112-
113- Test.it("should greet other people as well")
99+ import XCTest
100+ class PersonTest: XCTestCase {
101+ static var allTests = [
102+ ("testName", testName),
103+ ("testGreet", testGreet),
104+ ]
105+ func testName() {
106+ let jack = Person("Jack")
107+ XCTAssertEqual(jack.name, "Jack")
108+ }
109+ func testGreet() {
110+ let jack = Person("Jack")
111+ XCTAssertEqual(jack.greet("Jill"), "Hello Jill, my name is Jack")
112+ XCTAssertEqual(jack.greet("Jane"), "Hello Jane, my name is Jack")
113+ }
114+ }
114115
115- // unlike "assert_equals", "expect" is a lower level assertion that
116- // takes a boolean to determine if it passes. If it fails it will
117- // output the message that you give it, or a generic one. It is a good
118- // idea to provide a custom error message to help users pass the kata
119- Test.expect(jack.greet(other: "Jane") == "Hello Jane, my name is Jack", "Jack apparently is only able to greet Jane")
116+ _XCTMain([
117+ testCase(PersonTest.allTests)
118+ ])
120119
121120 reference :
122121 initial : |-
@@ -125,62 +124,18 @@ cw-2:
125124 answer : |-
126125 let websites = ["codewars"]
127126
128- fixture : |-
129- // Use test.describe (or Test.describe) to describe your test suite
130- Test.describe("websites")
131-
132- // Use "it" calls to describe the specific test case
133- Test.it("should have the value 'codewars' inside of it")
134-
135- // assert equals will pass if both items equal each other (using ==). If
136- // the test fails, assert_equals will output a descriptive message indicating
137- // what the values were expected to be.
138- Test.assert_equals(["codewars"], websites)
139-
140- // you can also use the lower level test.expect. If you use test.expect directly then
141- // you should provide a custom error message, as the default one will be pretty useless
142- // to users trying to pass the kata.
143- Test.expect(["codewars"] == websites, "Array does not have correct value")
144- xctest :
145- reference :
146- initial : |-
147- class Person {
148- let name: String
149-
150- init(_ name: String) {
151- // TODO: Program Constructor
152- }
153-
154- func greet(_ other: String) -> String {
155- // TODO: Write a greeting string
156- }
157- }
158-
159- answer : |-
160- class Person {
161- let name: String
162-
163- init(_ name: String) {
164- self.name = name
165- }
166-
167- func greet(_ other: String) -> String {
168- return "Hello, \(other), I am \(name), it's nice to meet you!"
169- }
170- }
171-
172127 fixture : |-
173128 import XCTest
174129
175- class PersonTest : XCTestCase {
130+ class WebsitesTest : XCTestCase {
176131 static var allTests = [
177- ("testGreet ", testGreet ),
132+ ("testDefined ", testDefined ),
178133 ]
179- func testGreet() {
180- let person = Person("Jorge")
181- XCTAssertEqual(person.greet("Aditya"), "Hello, Aditya, I am Jorge, it's nice to meet you!")
134+ func testDefined() {
135+ XCTAssertEqual(websites[0], "codewars")
182136 }
183- }
184- XCTMain([
185- testCase(PersonTest.allTests)
186- ])
137+ }
138+
139+ _XCTMain([
140+ testCase(WebsitesTest.allTests)
141+ ])
0 commit comments