-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibraryTest.php
More file actions
234 lines (191 loc) · 6.18 KB
/
libraryTest.php
File metadata and controls
234 lines (191 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
define('LIBRARY_FILE', __DIR__ . '/library.php');
require LIBRARY_FILE;
class PHPAssessmentTests extends PHPUnit_Framework_TestCase
{
public function test_is_even()
{
$this->assertTrue(
function_exists('isEven'),
"Expected isEven() function to be present."
);
// ensure the funciton returns the correct data type
$this->assertTrue(
is_bool(isEven(3)),
"Expected isEven() function to return a boolean."
);
$this->assertTrue(
is_bool(isEven(4)),
"Expected isEven() function to return a boolean."
);
$this->assertTrue(
isEven(10),
"Expected isEven(10) to return true."
);
$this->assertTrue(
isEven('10'),
"Expected isEven('10') to return true."
);
$this->assertFalse(
isEven(5),
"Expected isEven(5) to return false."
);
$this->assertFalse(
isEven("Bob"),
"Expected isEven('Bob') to return false."
);
}
public function test_is_vowel()
{
$this->assertTrue(
function_exists('isVowel'),
"Expected isVowel() function to be present"
);
$this->assertFalse(
isVowel([1, 2, 3]),
"Expected isVowel([1, 2, 3]) to return false");
$this->assertTrue(
is_bool(isVowel('a')),
"Expected isVowel() function to return a boolean."
);
$this->assertTrue(
isVowel('a'),
"Expected isVowel('a') to return true."
);
$this->assertTrue(
isVowel('e'),
"Expected isVowel('e') to return true."
);
$this->assertTrue(
isVowel('I'),
"Expected isVowel('I') to return true."
);
$this->assertTrue(
isVowel('o'),
"Expected isVowel('o') to return true."
);
$this->assertTrue(
isVowel('U'),
"Expected isVowel('U') to return true."
);
$this->assertFalse(
isVowel('B'),
"Expected isVowel('B') to return false."
);
$this->assertFalse(
isVowel(5),
"Expected isVowel(5) to return false."
);
}
public function test_first()
{
// make sure the function exists
$this->assertTrue(
function_exists('first'),
"Expected first() function to be defined."
);
// should return first character of a string.
$this->assertEquals(
"B", first('Bob'),
"Expected first('Bob') to return 'B'."
);
// should return first element of an array.
$this->assertEquals(
1, first([1, 2, 3]),
"Expected first([1, 2, 3]) to return 1."
);
// should return first character of a string.
$this->assertEquals(
'P', first('Programming'),
"Expected first(Programming) to return 'P'."
);
}
public function test_second()
{
$this->assertTrue(
function_exists('second'),
"Expected second() function to be defined."
);
$this->assertEquals(
"o", second('Bob'),
"Expected second('Bob') to return 'o'."
);
$this->assertEquals(
2, second([1, 2, 3]),
"Expected second([1, 2, 3]) to return 2."
);
$this->assertEquals(
"r", second('Programming'),
"Expected last('Programming') to return 'r'."
);
}
public function test_last()
{
$this->assertTrue(
function_exists('last'),
"Expected last() function to be defined."
);
$this->assertEquals(
"b", last('Bob'),
"Expected last('Bob') to return 'b'."
);
$this->assertEquals(
"g", last('Programming'),
"Expected last('Programming') to return 'g'."
);
$this->assertEquals(
3, last([1, 2, 3]),
"Expected last([1, 2, 3]) to return 3."
);
}
public function test_reverse()
{
$this->assertTrue(
function_exists('reverse'),
"Expected reverse() function to be defined."
);
$this->assertEquals(
"boB", reverse('Bob'),
"Expected reverse('Bob') to return 'boB'."
);
$this->assertEquals(
"gnimmargorP", reverse('Programming'),
"Expected reverse('Programming') to return 'gnimmargorP'."
);
$this->assertEquals(
[3, 2, 1], reverse([1, 2, 3]),
"Expected reverse([1, 2, 3]) to return [3, 2, 1]."
);
}
public function test_random()
{
// The primary challenge to testing a random() function is that it lacks referential transparency.
// See http://blog.agiledeveloper.com/2016/01/benefits-of-pure-functions-offer.html
$this->assertTrue(
function_exists('random'),
"Expected random() function to be defined."
);
// expect a string when passed a string
$this->assertTrue(
is_string(random("Banana")),
"Expected random() to return a random character represented as a string data type when provided a string."
);
// expect a number when passed an array of numbers
$this->assertTrue(
is_int(random([1, 2, 3])),
"Expected random([1, 2, 3]) to return an integer since a random element of the array [1, 2, 3] is an integer."
);
$this->assertEquals(
'a', random('aaaaaaaaaaaaaaaaaa'),
"Expected random('aaaaaaaaaaaaaaaaaa') to return 'a'."
);
$this->assertEquals(
4, random([4, 4, 4, 4, 4]),
"Expected random([4, 4, 4, 4, 4]) to return 4."
);
$this->assertTrue(
in_array(random([1, 2, 3, 4, 5]), [1, 2, 3, 4, 5]),
"Expected random([1, 2, 3, 4, 5]) to return 1 or 2 or 3 or 4 or 5."
);
}
}