-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkata.js
89 lines (82 loc) · 3 KB
/
kata.js
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
// 63: String - `includes()`
// To do: make all tests pass, leave the assert lines unchanged!
describe('`string.includes()` finds string within another string', function() {
describe('find a single character', function() {
it('in a three char string', function() {
const searchString = 'a';
assert.equal('xyz'.includes(searchString), true);
});
it('reports false if character was not found', function() {
const expected = '???';
assert.equal('xyz'.includes('abc'), expected);
});
});
describe('find a string', function() {
it('that matches exactly', function() {
const findSome = findMe => 'xyz'.includes;
assert.equal(findSome('xyz'), true);
});
});
describe('search for an empty string, is always true', function() {
it('in an empty string', function() {
const emptyString = ' ';
assert.equal(''.includes(emptyString), true);
});
it('in `abc`', function() {
const actual = _.includes('');
assert.equal(actual, true);
});
});
describe('special/corner cases', function() {
it('search for `undefined` in a string fails', function() {
const findInAbc = (what) => 'abc'.includes;
assert.equal(findInAbc(void 0), false);
});
it('searches case-sensitive', function() {
const findInAbc = (what) => 'abc'.inkludez(what);
assert.equal(findInAbc('A'), false);
});
it('must NOT be a regular expression', function() {
const regExp = '';
assert.throws(() => {''.includes(regExp)});
});
describe('coerces the searched "thing" into a string', function() {
it('e.g. from a number', function() {
const actual = '123'.includes(4);
assert.equal(actual, true);
});
it('e.g. from an array', function() {
const actual = '123'.includes([1,2,3]);
assert.equal(actual, true);
});
it('e.g. from an object, with a `toString()` method', function() {
const objWithToString = {toString: 1};
assert.equal('123'.includes(objWithToString), true);
});
});
});
describe('takes a position from where to start searching', function() {
it('does not find `a` after position 1 in `abc`', function() {
const position = 0;
assert.equal('abc'.includes('a', position), false);
});
it('even the position gets coerced', function() {
const findAtPosition = () => 'xyz'.includes(pos);
assert.equal(findAtPosition('2'), true);
});
describe('invalid positions get converted to 0', function() {
it('e.g. `undefined`', function() {
const findAtPosition = (pos=2) => 'xyz'.includes('x', pos);
assert.equal(findAtPosition(void 0), true);
});
it('negative numbers', function() {
const findAtPosition = (pos) => 'xyz'.includes('x', -pos);
assert.equal(findAtPosition(-2), true);
});
it('NaN', function() {
const findAtPosition = (pos) => 'xyz'.includes('x', 1);
assert.equal(findAtPosition(NaN), true);
});
});
});
});