-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
146 lines (124 loc) · 3.1 KB
/
test.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// --------------------------------------------------------------------------------------------------------------------
'use strict'
// npm
var test = require('tape')
// local
var zid = require('./')
// --------------------------------------------------------------------------------------------------------------------
// setup
const lookalikeRegex = /[OISZbl]/
// --------------------------------------------------------------------------------------------------------------------
test('test lengths', function(t) {
t.plan(12)
var id
for(var i = 1; i <= 12; i++) {
id = zid(i)
t.equal(id.length, i, 'Test length of id is ' + i)
}
t.end()
})
test('test contents', function(t) {
t.plan(12)
var id
for(var i = 1; i <= 12; i++) {
id = zid(i)
t.ok(id.match(/^[A-Za-z0-9]+$/), 'Test contents')
}
t.end()
})
test('test throw', function(t) {
t.plan(6)
try {
zid()
t.fail('Should have thrown')
}
catch(err) {
t.ok('Yes, this throws', 'Throws with no input')
}
try {
zid(0)
t.fail('Should have thrown')
}
catch(err) {
t.ok('Yes, this throws', 'Throws with zero')
}
try {
zid(-1)
t.fail('Should have thrown')
}
catch(err) {
t.ok('Yes, this throws', 'Throws with negative numbers')
}
try {
zid(NaN)
t.fail('Should have thrown')
}
catch(err) {
t.ok('Yes, this throws', 'Throws with zero')
}
try {
zid(Infinity)
t.fail('Should have thrown')
}
catch(err) {
t.ok('Yes, this throws', 'Throws with zero')
}
try {
zid('hello')
t.fail('Should have thrown')
}
catch(err) {
t.ok('Yes, this throws', 'Throws with negative numbers')
}
t.end()
})
test('test lookalikes: false', function(t) {
t.plan(1)
for(let i = 1; i <= 1000; i++) {
const id = zid(12, { lookalikes: false })
if (lookalikeRegex.test(id)) {
t.fail(`Test lookalikes=false doesn't have invalid chars: ${id}`)
t.end()
return
}
}
t.pass('No invalid chars found')
t.end()
})
test('test lookalikes: true', function(t) {
t.plan(1)
let count = 0
for(let i = 1; i <= 1000; i++) {
const id = zid(12, { lookalikes: true })
if (lookalikeRegex.test(id)) {
count = count + 1
}
}
// check we found at least one id with an invalid char as expected
if ( count === 0 ) {
t.fail("We expect to find at least one invalid char in 1,000 tests!")
}
else {
t.pass(`We did have at least one id with an invalid char, as expected (count=${count}).`)
}
t.end()
})
test('test no options', function(t) {
t.plan(1)
let count = 0
for(let i = 1; i <= 1000; i++) {
const id = zid(12, { /* lookalikes: true */ })
if (lookalikeRegex.test(id)) {
count = count + 1
}
}
// check we found at least one id with an invalid char as expected
if ( count === 0 ) {
t.fail("We expect to find at least one invalid char in 1,000 tests!")
}
else {
t.pass(`We did have at least one id with an invalid char, as expected (count=${count}).`)
}
t.end()
})
// --------------------------------------------------------------------------------------------------------------------