1
- cw-2 :
1
+ xctest :
2
2
algorithms :
3
3
initial : |-
4
4
// return the two oldest/oldest ages within the array of ages passed in.
5
5
// 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
8
7
}
9
8
10
9
answer : |-
@@ -21,102 +20,102 @@ cw-2:
21
20
return [nextOldest, oldest]
22
21
}
23
22
24
-
25
23
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
41
25
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
+ }
44
44
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
+ ])
47
48
48
49
bug fixes :
49
50
initial : |-
50
- func add(a: Int, b: Int) -> Int {
51
+ func add(_ a: Int, _ b: Int) -> Int {
51
52
a + b
52
53
}
53
54
54
55
answer : |-
55
- func add(a: Int, b: Int) -> Int {
56
+ func add(_ a: Int, _ b: Int) -> Int {
56
57
return a + b
57
58
}
58
59
59
60
fixture : |-
60
- // Use "describe" to define the test suite
61
- Test.describe("add method")
61
+ import XCTest
62
62
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
+ }
65
71
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
+ ])
70
75
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")
76
76
77
77
refactoring :
78
78
initial : |-
79
79
// 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"
80
83
func greet(other: String, name: String) -> String {
81
84
return "Hello \(other), my name is \(name)"
82
85
}
83
86
84
87
answer : |-
85
88
class Person {
86
89
let name: String
87
- init(name: String) {
90
+ init(_ name: String) {
88
91
self.name = name
89
92
}
90
- func greet(other: String) -> String {
93
+ func greet(_ other: String) -> String {
91
94
return "Hello \(other), my name is \(self.name)"
92
95
}
93
96
}
94
97
95
98
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
+ }
114
115
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
+ ])
120
119
121
120
reference :
122
121
initial : |-
@@ -125,62 +124,18 @@ cw-2:
125
124
answer : |-
126
125
let websites = ["codewars"]
127
126
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
-
172
127
fixture : |-
173
128
import XCTest
174
129
175
- class PersonTest : XCTestCase {
130
+ class WebsitesTest : XCTestCase {
176
131
static var allTests = [
177
- ("testGreet ", testGreet ),
132
+ ("testDefined ", testDefined ),
178
133
]
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")
182
136
}
183
- }
184
- XCTMain([
185
- testCase(PersonTest.allTests)
186
- ])
137
+ }
138
+
139
+ _XCTMain([
140
+ testCase(WebsitesTest.allTests)
141
+ ])
0 commit comments