| 1 | /* eslint-env mocha */
|
|---|
| 2 | /* eslint-disable max-nested-callbacks */
|
|---|
| 3 |
|
|---|
| 4 | 'use strict'
|
|---|
| 5 |
|
|---|
| 6 | const { assert } = require('chai')
|
|---|
| 7 | const Hoopy = require('.')
|
|---|
| 8 |
|
|---|
| 9 | test('interface is correct', () => {
|
|---|
| 10 | assert.isFunction(Hoopy)
|
|---|
| 11 | assert.lengthOf(Hoopy, 1)
|
|---|
| 12 | assert.throws(() => new Hoopy(0))
|
|---|
| 13 | assert.doesNotThrow(() => new Hoopy(1))
|
|---|
| 14 | assert.throws(() => new Hoopy(-1))
|
|---|
| 15 | assert.throws(() => new Hoopy(1).push())
|
|---|
| 16 | assert.throws(() => new Hoopy(1).pop())
|
|---|
| 17 | assert.throws(() => new Hoopy(1).shift())
|
|---|
| 18 | assert.throws(() => new Hoopy(1).unshift())
|
|---|
| 19 | })
|
|---|
| 20 |
|
|---|
| 21 | suite('instantiate, size=1:', () => {
|
|---|
| 22 | let hoopy
|
|---|
| 23 |
|
|---|
| 24 | setup(() => {
|
|---|
| 25 | hoopy = new Hoopy(1)
|
|---|
| 26 | })
|
|---|
| 27 |
|
|---|
| 28 | test('instance is array', () => {
|
|---|
| 29 | assert.isTrue(Array.isArray(hoopy))
|
|---|
| 30 | })
|
|---|
| 31 |
|
|---|
| 32 | test('length is correct', () => {
|
|---|
| 33 | assert.equal(hoopy.length, 1)
|
|---|
| 34 | })
|
|---|
| 35 |
|
|---|
| 36 | test('[0] is undefined', () => {
|
|---|
| 37 | assert.isUndefined(hoopy[0])
|
|---|
| 38 | })
|
|---|
| 39 |
|
|---|
| 40 | test('[1] is undefined', () => {
|
|---|
| 41 | assert.isUndefined(hoopy[1])
|
|---|
| 42 | })
|
|---|
| 43 |
|
|---|
| 44 | test('[-1] is undefined', () => {
|
|---|
| 45 | assert.isUndefined(hoopy[-1])
|
|---|
| 46 | })
|
|---|
| 47 |
|
|---|
| 48 | test('grow method is implemented', () => {
|
|---|
| 49 | assert.isFunction(hoopy.grow)
|
|---|
| 50 | assert.lengthOf(hoopy.grow, 1)
|
|---|
| 51 | })
|
|---|
| 52 |
|
|---|
| 53 | test('grow throws if by=0', () => {
|
|---|
| 54 | assert.throws(() => hoopy.grow(0))
|
|---|
| 55 | })
|
|---|
| 56 |
|
|---|
| 57 | suite('assign to [0]:', () => {
|
|---|
| 58 | setup(() => {
|
|---|
| 59 | hoopy[0] = 'foo'
|
|---|
| 60 | })
|
|---|
| 61 |
|
|---|
| 62 | test('[0] is set correctly', () => {
|
|---|
| 63 | assert.equal(hoopy[0], 'foo')
|
|---|
| 64 | })
|
|---|
| 65 |
|
|---|
| 66 | test('[1] is set correctly', () => {
|
|---|
| 67 | assert.equal(hoopy[1], 'foo')
|
|---|
| 68 | })
|
|---|
| 69 |
|
|---|
| 70 | test('[-1] is set correctly', () => {
|
|---|
| 71 | assert.equal(hoopy[-1], 'foo')
|
|---|
| 72 | })
|
|---|
| 73 |
|
|---|
| 74 | suite('assign to [1]:', () => {
|
|---|
| 75 | setup(() => {
|
|---|
| 76 | hoopy[1] = 'bar'
|
|---|
| 77 | })
|
|---|
| 78 |
|
|---|
| 79 | test('[0] is set correctly', () => {
|
|---|
| 80 | assert.equal(hoopy[0], 'bar')
|
|---|
| 81 | })
|
|---|
| 82 |
|
|---|
| 83 | test('[1] is set correctly', () => {
|
|---|
| 84 | assert.equal(hoopy[1], 'bar')
|
|---|
| 85 | })
|
|---|
| 86 |
|
|---|
| 87 | test('[-1] is set correctly', () => {
|
|---|
| 88 | assert.equal(hoopy[-1], 'bar')
|
|---|
| 89 | })
|
|---|
| 90 | })
|
|---|
| 91 |
|
|---|
| 92 | suite('grow, by=1:', () => {
|
|---|
| 93 | setup(() => {
|
|---|
| 94 | hoopy.grow(1)
|
|---|
| 95 | })
|
|---|
| 96 |
|
|---|
| 97 | test('length is correct', () => {
|
|---|
| 98 | assert.equal(hoopy.length, 2)
|
|---|
| 99 | })
|
|---|
| 100 |
|
|---|
| 101 | test('[0] is set correctly', () => {
|
|---|
| 102 | assert.equal(hoopy[0], 'foo')
|
|---|
| 103 | })
|
|---|
| 104 |
|
|---|
| 105 | test('[1] is undefined', () => {
|
|---|
| 106 | assert.isUndefined(hoopy[1])
|
|---|
| 107 | })
|
|---|
| 108 |
|
|---|
| 109 | test('[-1] is undefined', () => {
|
|---|
| 110 | assert.isUndefined(hoopy[-1])
|
|---|
| 111 | })
|
|---|
| 112 | })
|
|---|
| 113 | })
|
|---|
| 114 | })
|
|---|
| 115 |
|
|---|
| 116 | suite('instantiate, size=2:', () => {
|
|---|
| 117 | let hoopy
|
|---|
| 118 |
|
|---|
| 119 | setup(() => {
|
|---|
| 120 | hoopy = new Hoopy(2)
|
|---|
| 121 | })
|
|---|
| 122 |
|
|---|
| 123 | test('length is correct', () => {
|
|---|
| 124 | assert.equal(hoopy.length, 2)
|
|---|
| 125 | })
|
|---|
| 126 |
|
|---|
| 127 | suite('assign to [0]:', () => {
|
|---|
| 128 | setup(() => {
|
|---|
| 129 | hoopy[0] = 'foo'
|
|---|
| 130 | })
|
|---|
| 131 |
|
|---|
| 132 | test('[0] is set correctly', () => {
|
|---|
| 133 | assert.equal(hoopy[0], 'foo')
|
|---|
| 134 | })
|
|---|
| 135 |
|
|---|
| 136 | test('[1] is undefined', () => {
|
|---|
| 137 | assert.isUndefined(hoopy[1])
|
|---|
| 138 | })
|
|---|
| 139 |
|
|---|
| 140 | test('[2] is set correctly', () => {
|
|---|
| 141 | assert.equal(hoopy[2], 'foo')
|
|---|
| 142 | })
|
|---|
| 143 |
|
|---|
| 144 | test('[3] is undefined', () => {
|
|---|
| 145 | assert.isUndefined(hoopy[3])
|
|---|
| 146 | })
|
|---|
| 147 |
|
|---|
| 148 | test('[-1] is undefined', () => {
|
|---|
| 149 | assert.isUndefined(hoopy[-1])
|
|---|
| 150 | })
|
|---|
| 151 |
|
|---|
| 152 | suite('assign to [1]:', () => {
|
|---|
| 153 | setup(() => {
|
|---|
| 154 | hoopy[1] = 'bar'
|
|---|
| 155 | })
|
|---|
| 156 |
|
|---|
| 157 | test('[0] is set correctly', () => {
|
|---|
| 158 | assert.equal(hoopy[0], 'foo')
|
|---|
| 159 | })
|
|---|
| 160 |
|
|---|
| 161 | test('[1] is set correctly', () => {
|
|---|
| 162 | assert.equal(hoopy[1], 'bar')
|
|---|
| 163 | })
|
|---|
| 164 |
|
|---|
| 165 | test('[2] is set correctly', () => {
|
|---|
| 166 | assert.equal(hoopy[2], 'foo')
|
|---|
| 167 | })
|
|---|
| 168 |
|
|---|
| 169 | test('[-1] is set correctly', () => {
|
|---|
| 170 | assert.equal(hoopy[-1], 'bar')
|
|---|
| 171 | })
|
|---|
| 172 |
|
|---|
| 173 | suite('assign to [2]:', () => {
|
|---|
| 174 | setup(() => {
|
|---|
| 175 | hoopy[2] = 'baz'
|
|---|
| 176 | })
|
|---|
| 177 |
|
|---|
| 178 | test('[0] is set correctly', () => {
|
|---|
| 179 | assert.equal(hoopy[0], 'baz')
|
|---|
| 180 | })
|
|---|
| 181 |
|
|---|
| 182 | test('[1] is set correctly', () => {
|
|---|
| 183 | assert.equal(hoopy[1], 'bar')
|
|---|
| 184 | })
|
|---|
| 185 |
|
|---|
| 186 | test('[2] is set correctly', () => {
|
|---|
| 187 | assert.equal(hoopy[2], 'baz')
|
|---|
| 188 | })
|
|---|
| 189 |
|
|---|
| 190 | test('[-1] is set correctly', () => {
|
|---|
| 191 | assert.equal(hoopy[-1], 'bar')
|
|---|
| 192 | })
|
|---|
| 193 |
|
|---|
| 194 | suite('grow, by=1:', () => {
|
|---|
| 195 | setup(() => {
|
|---|
| 196 | hoopy.grow(1)
|
|---|
| 197 | })
|
|---|
| 198 |
|
|---|
| 199 | test('length is correct', () => {
|
|---|
| 200 | assert.equal(hoopy.length, 3)
|
|---|
| 201 | })
|
|---|
| 202 |
|
|---|
| 203 | test('[0] is undefined', () => {
|
|---|
| 204 | assert.isUndefined(hoopy[0])
|
|---|
| 205 | })
|
|---|
| 206 |
|
|---|
| 207 | test('[1] is set correctly', () => {
|
|---|
| 208 | assert.equal(hoopy[1], 'bar')
|
|---|
| 209 | })
|
|---|
| 210 |
|
|---|
| 211 | test('[2] is set correctly', () => {
|
|---|
| 212 | assert.equal(hoopy[2], 'baz')
|
|---|
| 213 | })
|
|---|
| 214 |
|
|---|
| 215 | test('[3] is undefined', () => {
|
|---|
| 216 | assert.isUndefined(hoopy[3])
|
|---|
| 217 | })
|
|---|
| 218 | })
|
|---|
| 219 |
|
|---|
| 220 | suite('grow, by=2:', () => {
|
|---|
| 221 | setup(() => {
|
|---|
| 222 | hoopy.grow(2)
|
|---|
| 223 | })
|
|---|
| 224 |
|
|---|
| 225 | test('length is correct', () => {
|
|---|
| 226 | assert.equal(hoopy.length, 4)
|
|---|
| 227 | })
|
|---|
| 228 |
|
|---|
| 229 | test('[0] is undefined', () => {
|
|---|
| 230 | assert.isUndefined(hoopy[0])
|
|---|
| 231 | })
|
|---|
| 232 |
|
|---|
| 233 | test('[1] is set correctly', () => {
|
|---|
| 234 | assert.equal(hoopy[1], 'bar')
|
|---|
| 235 | })
|
|---|
| 236 |
|
|---|
| 237 | test('[2] is set correctly', () => {
|
|---|
| 238 | assert.equal(hoopy[2], 'baz')
|
|---|
| 239 | })
|
|---|
| 240 |
|
|---|
| 241 | test('[3] is undefined', () => {
|
|---|
| 242 | assert.isUndefined(hoopy[3])
|
|---|
| 243 | })
|
|---|
| 244 |
|
|---|
| 245 | test('[4] is undefined', () => {
|
|---|
| 246 | assert.isUndefined(hoopy[4])
|
|---|
| 247 | })
|
|---|
| 248 |
|
|---|
| 249 | test('[5] is set correctly', () => {
|
|---|
| 250 | assert.equal(hoopy[5], 'bar')
|
|---|
| 251 | })
|
|---|
| 252 | })
|
|---|
| 253 | })
|
|---|
| 254 | })
|
|---|
| 255 | })
|
|---|
| 256 | })
|
|---|
| 257 |
|
|---|
| 258 | suite('instantiate and overflow, size=3:', () => {
|
|---|
| 259 | let hoopy
|
|---|
| 260 |
|
|---|
| 261 | setup(() => {
|
|---|
| 262 | hoopy = new Hoopy(3)
|
|---|
| 263 | hoopy[2] = 'foo'
|
|---|
| 264 | hoopy[3] = 'bar'
|
|---|
| 265 | hoopy[4] = 'baz'
|
|---|
| 266 | })
|
|---|
| 267 |
|
|---|
| 268 | test('data is correct', () => {
|
|---|
| 269 | assert.equal(hoopy.length, 3)
|
|---|
| 270 | assert.equal(hoopy[2], 'foo')
|
|---|
| 271 | assert.equal(hoopy[3], 'bar')
|
|---|
| 272 | assert.equal(hoopy[4], 'baz')
|
|---|
| 273 | assert.equal(hoopy[0], hoopy[3])
|
|---|
| 274 | assert.equal(hoopy[1], hoopy[4])
|
|---|
| 275 | })
|
|---|
| 276 |
|
|---|
| 277 | test('slice works correctly', () => {
|
|---|
| 278 | assert.equal(hoopy.slice(0, 3)[2], hoopy[2])
|
|---|
| 279 | })
|
|---|
| 280 |
|
|---|
| 281 | suite('grow, by=1:', () => {
|
|---|
| 282 | setup(() => {
|
|---|
| 283 | hoopy.grow(1)
|
|---|
| 284 | })
|
|---|
| 285 |
|
|---|
| 286 | test('data is correct', () => {
|
|---|
| 287 | assert.equal(hoopy.length, 4)
|
|---|
| 288 | assert.equal(hoopy[2], 'foo')
|
|---|
| 289 | assert.equal(hoopy[3], 'bar')
|
|---|
| 290 | assert.equal(hoopy[4], 'baz')
|
|---|
| 291 | assert.equal(hoopy[0], hoopy[4])
|
|---|
| 292 | assert.isUndefined(hoopy[1])
|
|---|
| 293 | })
|
|---|
| 294 | })
|
|---|
| 295 |
|
|---|
| 296 | suite('grow, by=2:', () => {
|
|---|
| 297 | setup(() => {
|
|---|
| 298 | hoopy.grow(2)
|
|---|
| 299 | })
|
|---|
| 300 |
|
|---|
| 301 | test('data is correct', () => {
|
|---|
| 302 | assert.equal(hoopy.length, 5)
|
|---|
| 303 | assert.equal(hoopy[2], 'foo')
|
|---|
| 304 | assert.equal(hoopy[3], 'bar')
|
|---|
| 305 | assert.equal(hoopy[4], 'baz')
|
|---|
| 306 | assert.isUndefined(hoopy[0])
|
|---|
| 307 | assert.isUndefined(hoopy[1])
|
|---|
| 308 | })
|
|---|
| 309 | })
|
|---|
| 310 | })
|
|---|
| 311 |
|
|---|