-
Notifications
You must be signed in to change notification settings - Fork 0
/
table-tests.js
130 lines (120 loc) · 3.24 KB
/
table-tests.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
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
/* global describe beforeEach before */
const expect = require('chai').expect
const mochaTable = require('.')
const describeTable = mochaTable.describeTable
const tableIt = mochaTable.tableIt
const entryIt = mochaTable.entryIt
const xentryIt = mochaTable.xentryIt
describeTable('Primality tests', function () {
tableIt('is %d prime? (should be %t)', function (number, result) {
expect(isPrime(number)).to.equal(result)
})
entryIt(3, true)
entryIt(4, false)
entryIt.skip(15, false)
xentryIt(21, false)
entryIt(1847, true)
entryIt(1848, false)
entryIt(685242563, false) // This fails on purpose
})
describeTable('Multitable tests', function () {
tableIt('testing 1 %d', function (number) {})
entryIt(10)
tableIt('testing 2 %d', function (number) {})
entryIt(20)
})
describeTable('Skip tests', function () {
tableIt.skip('testing 3 %d', function (number) {})
entryIt(30)
tableIt('testing 4 %d', function (number) {})
entryIt(40)
})
describeTable('Asynchronous primality tests', function () {
tableIt('is %d (asynchronously) prime? (should be %t)', function (number, result, done) {
expect(isPrime(number)).to.equal(result)
done()
})
entryIt(3, true)
entryIt(4, false)
entryIt.skip(15, false)
xentryIt(21, false)
entryIt(1847, true)
entryIt(1848, false)
entryIt(685242563, false) // This fails on purpose
})
describeTable('Timeouts', function () {
tableIt('should continue to time out', function (done) {
this.timeout(100)
})
entryIt()
})
describe('Handling this', function () {
beforeEach(function () {
this.me = 'right one'
})
describeTable('This handling', function () {
tableIt('this object should be the right one', function () {
expect(this.me).to.equal('right one')
})
entryIt()
})
describeTable('This handling', function () {
tableIt('this object should be the right one in async', function (done) {
expect(this.me).to.equal('right one')
done()
})
entryIt()
})
})
describe('Proper before usage', function () {
describeTable('first before works', function () {
before(function () {
this.me = 'first'
})
tableIt('should end up with %s', function (item) {
expect(this.me).to.equal(item)
})
entryIt('first')
})
describeTable('second before works', function () {
before(function () {
this.me = 'second'
})
tableIt('should also end up with %s', function (item) {
expect(this.me).to.equal(item)
})
entryIt('second')
})
})
describeTable('Promises', function () {
tableIt('make sure the promise is %s', function (which) {
switch (which) {
case 'accepted':
return {
then: function (success, reject) {
success()
}
}
case 'rejected':
return {
then: function (success, reject) {
reject()
}
}
case 'timed out':
this.timeout(100)
return {
then: function (success, reject) {}
}
}
})
entryIt('accepted')
entryIt('rejected')
entryIt('timed out')
})
// My amazing O(1) primality tester.
// Works for the majority of integers!!
function isPrime (n) {
return (n === 2 || n / 2 !== Math.floor(n / 2)) &&
(n === 3 || n / 3 !== Math.floor(n / 3))
}