| [9af201e] | 1 | 'use strict'
|
|---|
| 2 |
|
|---|
| 3 | const assert = require('chai').assert
|
|---|
| 4 | const axios = require('axios')
|
|---|
| 5 | const fs = require('fs')
|
|---|
| 6 | const path = require('path')
|
|---|
| 7 | const Promise = require('bluebird')
|
|---|
| 8 | const stream = require('stream')
|
|---|
| 9 |
|
|---|
| 10 | const modulePath = '../src'
|
|---|
| 11 |
|
|---|
| 12 | suite('integration:', () => {
|
|---|
| 13 | let log
|
|---|
| 14 |
|
|---|
| 15 | setup(() => {
|
|---|
| 16 | log = {}
|
|---|
| 17 | })
|
|---|
| 18 |
|
|---|
| 19 | test('require does not throw', () => {
|
|---|
| 20 | assert.doesNotThrow(() => {
|
|---|
| 21 | require(modulePath)
|
|---|
| 22 | })
|
|---|
| 23 | })
|
|---|
| 24 |
|
|---|
| 25 | test('require returns object', () => {
|
|---|
| 26 | assert.isObject(require(modulePath))
|
|---|
| 27 | })
|
|---|
| 28 |
|
|---|
| 29 | suite('require:', () => {
|
|---|
| 30 | let bfj
|
|---|
| 31 |
|
|---|
| 32 | setup(() => {
|
|---|
| 33 | bfj = require(modulePath)
|
|---|
| 34 | })
|
|---|
| 35 |
|
|---|
| 36 | test('walk function is exported', () => {
|
|---|
| 37 | assert.isFunction(bfj.walk)
|
|---|
| 38 | })
|
|---|
| 39 |
|
|---|
| 40 | test('walk expects one argument', () => {
|
|---|
| 41 | assert.lengthOf(bfj.walk, 1)
|
|---|
| 42 | })
|
|---|
| 43 |
|
|---|
| 44 | test('match function is exported', () => {
|
|---|
| 45 | assert.isFunction(bfj.match)
|
|---|
| 46 | })
|
|---|
| 47 |
|
|---|
| 48 | test('match expects two arguments', () => {
|
|---|
| 49 | assert.lengthOf(bfj.match, 2)
|
|---|
| 50 | })
|
|---|
| 51 |
|
|---|
| 52 | test('parse function is exported', () => {
|
|---|
| 53 | assert.isFunction(bfj.parse)
|
|---|
| 54 | })
|
|---|
| 55 |
|
|---|
| 56 | test('parse expects one argument', () => {
|
|---|
| 57 | assert.lengthOf(bfj.parse, 1)
|
|---|
| 58 | })
|
|---|
| 59 |
|
|---|
| 60 | test('read function is exported', () => {
|
|---|
| 61 | assert.isFunction(bfj.read)
|
|---|
| 62 | })
|
|---|
| 63 |
|
|---|
| 64 | test('read expects two arguments', () => {
|
|---|
| 65 | assert.lengthOf(bfj.read, 2)
|
|---|
| 66 | })
|
|---|
| 67 |
|
|---|
| 68 | test('eventify function is exported', () => {
|
|---|
| 69 | assert.isFunction(bfj.eventify)
|
|---|
| 70 | })
|
|---|
| 71 |
|
|---|
| 72 | test('eventify expects one argument', () => {
|
|---|
| 73 | assert.lengthOf(bfj.eventify, 1)
|
|---|
| 74 | })
|
|---|
| 75 |
|
|---|
| 76 | test('streamify function is exported', () => {
|
|---|
| 77 | assert.isFunction(bfj.streamify)
|
|---|
| 78 | })
|
|---|
| 79 |
|
|---|
| 80 | test('streamify expects one argument', () => {
|
|---|
| 81 | assert.lengthOf(bfj.streamify, 1)
|
|---|
| 82 | })
|
|---|
| 83 |
|
|---|
| 84 | test('stringify function is exported', () => {
|
|---|
| 85 | assert.isFunction(bfj.stringify)
|
|---|
| 86 | })
|
|---|
| 87 |
|
|---|
| 88 | test('stringify expects two arguments', () => {
|
|---|
| 89 | assert.lengthOf(bfj.stringify, 2)
|
|---|
| 90 | })
|
|---|
| 91 |
|
|---|
| 92 | test('write function is exported', () => {
|
|---|
| 93 | assert.isFunction(bfj.write)
|
|---|
| 94 | })
|
|---|
| 95 |
|
|---|
| 96 | test('write expects two arguments', () => {
|
|---|
| 97 | assert.lengthOf(bfj.write, 3)
|
|---|
| 98 | })
|
|---|
| 99 |
|
|---|
| 100 | test('events are exported', () => {
|
|---|
| 101 | assert.deepEqual(bfj.events, require('../src/events'))
|
|---|
| 102 | })
|
|---|
| 103 |
|
|---|
| 104 | suite('read object:', () => {
|
|---|
| 105 | let failed, file, result, error
|
|---|
| 106 |
|
|---|
| 107 | setup(() => {
|
|---|
| 108 | failed = false
|
|---|
| 109 | file = path.join(__dirname, 'data.json')
|
|---|
| 110 | fs.writeFileSync(file, JSON.stringify({
|
|---|
| 111 | foo: [ 'b', 'a', 'r' ],
|
|---|
| 112 | baz: null,
|
|---|
| 113 | qux: 3.14159265359e42
|
|---|
| 114 | }, null, '\t'))
|
|---|
| 115 | return bfj.read(file)
|
|---|
| 116 | .then(res => {
|
|---|
| 117 | result = res
|
|---|
| 118 | })
|
|---|
| 119 | .catch(err => {
|
|---|
| 120 | failed = true
|
|---|
| 121 | error = err
|
|---|
| 122 | })
|
|---|
| 123 | })
|
|---|
| 124 |
|
|---|
| 125 | teardown(() => {
|
|---|
| 126 | fs.unlinkSync(file)
|
|---|
| 127 | })
|
|---|
| 128 |
|
|---|
| 129 | test('result was correct', () => {
|
|---|
| 130 | assert.isFalse(failed)
|
|---|
| 131 | assert.isUndefined(error)
|
|---|
| 132 | assert.isObject(result)
|
|---|
| 133 | assert.lengthOf(Object.keys(result), 3)
|
|---|
| 134 | assert.isArray(result.foo)
|
|---|
| 135 | assert.lengthOf(result.foo, 3)
|
|---|
| 136 | assert.strictEqual(result.foo[0], 'b')
|
|---|
| 137 | assert.strictEqual(result.foo[1], 'a')
|
|---|
| 138 | assert.strictEqual(result.foo[2], 'r')
|
|---|
| 139 | assert.isNull(result.baz)
|
|---|
| 140 | assert.strictEqual(result.qux, 3.14159265359e42)
|
|---|
| 141 | })
|
|---|
| 142 | })
|
|---|
| 143 |
|
|---|
| 144 | suite('read value:', () => {
|
|---|
| 145 | let failed, file, result, error
|
|---|
| 146 |
|
|---|
| 147 | setup(() => {
|
|---|
| 148 | failed = false
|
|---|
| 149 | file = path.join(__dirname, 'data.json')
|
|---|
| 150 | fs.writeFileSync(file, '"foo"')
|
|---|
| 151 | return bfj.read(file)
|
|---|
| 152 | .then(res => {
|
|---|
| 153 | result = res
|
|---|
| 154 | })
|
|---|
| 155 | .catch(err => {
|
|---|
| 156 | failed = true
|
|---|
| 157 | error = err
|
|---|
| 158 | })
|
|---|
| 159 | })
|
|---|
| 160 |
|
|---|
| 161 | teardown(() => {
|
|---|
| 162 | fs.unlinkSync(file)
|
|---|
| 163 | })
|
|---|
| 164 |
|
|---|
| 165 | test('result was correct', () => {
|
|---|
| 166 | assert.isFalse(failed)
|
|---|
| 167 | assert.isUndefined(error)
|
|---|
| 168 | assert.strictEqual(result, 'foo')
|
|---|
| 169 | })
|
|---|
| 170 | })
|
|---|
| 171 |
|
|---|
| 172 | suite('read error:', () => {
|
|---|
| 173 | let failed, file, result, error
|
|---|
| 174 |
|
|---|
| 175 | setup(() => {
|
|---|
| 176 | failed = false
|
|---|
| 177 | file = path.join(__dirname, 'data.json')
|
|---|
| 178 | fs.writeFileSync(file, '"foo" "bar"')
|
|---|
| 179 | return bfj.read(file)
|
|---|
| 180 | .then(res => result = res)
|
|---|
| 181 | .catch(err => {
|
|---|
| 182 | failed = true
|
|---|
| 183 | error = err
|
|---|
| 184 | })
|
|---|
| 185 | })
|
|---|
| 186 |
|
|---|
| 187 | teardown(() => {
|
|---|
| 188 | fs.unlinkSync(file)
|
|---|
| 189 | })
|
|---|
| 190 |
|
|---|
| 191 | test('result was correct', () => {
|
|---|
| 192 | assert.isTrue(failed)
|
|---|
| 193 | assert.isUndefined(result)
|
|---|
| 194 | assert.instanceOf(error, Error)
|
|---|
| 195 | })
|
|---|
| 196 | })
|
|---|
| 197 |
|
|---|
| 198 | suite('read missing file:', () => {
|
|---|
| 199 | let failed, file, result, error
|
|---|
| 200 |
|
|---|
| 201 | setup(() => {
|
|---|
| 202 | failed = false
|
|---|
| 203 | file = path.join(__dirname, 'missing.json')
|
|---|
| 204 | assert.isFalse(fs.existsSync(file))
|
|---|
| 205 | return bfj.read(file)
|
|---|
| 206 | .then(res => result = res)
|
|---|
| 207 | .catch(err => {
|
|---|
| 208 | failed = true
|
|---|
| 209 | error = err
|
|---|
| 210 | })
|
|---|
| 211 | })
|
|---|
| 212 |
|
|---|
| 213 | test('result was correct', () => {
|
|---|
| 214 | assert.isTrue(failed)
|
|---|
| 215 | assert.isUndefined(result)
|
|---|
| 216 | assert.instanceOf(error, Error)
|
|---|
| 217 | })
|
|---|
| 218 | })
|
|---|
| 219 |
|
|---|
| 220 | suite('match predicate:', () => {
|
|---|
| 221 | let file, results, errors
|
|---|
| 222 |
|
|---|
| 223 | setup(done => {
|
|---|
| 224 | file = path.join(__dirname, 'data.json')
|
|---|
| 225 | fs.writeFileSync(file, JSON.stringify({
|
|---|
| 226 | foo: 'bar',
|
|---|
| 227 | baz: 'qux',
|
|---|
| 228 | wibble: 'blee'
|
|---|
| 229 | }))
|
|---|
| 230 | results = []
|
|---|
| 231 | errors = []
|
|---|
| 232 | const datastream = bfj.match(
|
|---|
| 233 | fs.createReadStream(file),
|
|---|
| 234 | (k, v) => k === 'baz' || v === 'blee',
|
|---|
| 235 | { minDepth: 1 }
|
|---|
| 236 | )
|
|---|
| 237 | datastream.on('data', item => results.push(item))
|
|---|
| 238 | datastream.on('error', error => errors.push(error))
|
|---|
| 239 | datastream.on('end', done)
|
|---|
| 240 | })
|
|---|
| 241 |
|
|---|
| 242 | test('the correct properties were matched', () => {
|
|---|
| 243 | assert.deepEqual([ 'qux', 'blee' ], results)
|
|---|
| 244 | })
|
|---|
| 245 |
|
|---|
| 246 | test('no errors occurred', () => {
|
|---|
| 247 | assert.deepEqual(errors, [])
|
|---|
| 248 | })
|
|---|
| 249 | })
|
|---|
| 250 |
|
|---|
| 251 | suite('match nested:', () => {
|
|---|
| 252 | let file, results, errors
|
|---|
| 253 |
|
|---|
| 254 | setup(done => {
|
|---|
| 255 | file = path.join(__dirname, 'data.json')
|
|---|
| 256 | fs.writeFileSync(file, JSON.stringify({
|
|---|
| 257 | foo: {
|
|---|
| 258 | bar: 'baz'
|
|---|
| 259 | }
|
|---|
| 260 | }))
|
|---|
| 261 | results = []
|
|---|
| 262 | errors = []
|
|---|
| 263 | const datastream = bfj.match(fs.createReadStream(file), () => true)
|
|---|
| 264 | datastream.on('data', item => results.push(item))
|
|---|
| 265 | datastream.on('error', error => errors.push(error))
|
|---|
| 266 | datastream.on('end', done)
|
|---|
| 267 | })
|
|---|
| 268 |
|
|---|
| 269 | test('the correct properties were matched', () => {
|
|---|
| 270 | assert.deepEqual([ 'baz', { bar: 'baz' }, { foo: { bar: 'baz' } } ], results)
|
|---|
| 271 | })
|
|---|
| 272 |
|
|---|
| 273 | test('no errors occurred', () => {
|
|---|
| 274 | assert.deepEqual(errors, [])
|
|---|
| 275 | })
|
|---|
| 276 | })
|
|---|
| 277 |
|
|---|
| 278 | suite('match ndjson:', () => {
|
|---|
| 279 | let file, results, errors
|
|---|
| 280 |
|
|---|
| 281 | setup(done => {
|
|---|
| 282 | file = path.join(__dirname, 'data.ndjson')
|
|---|
| 283 | fs.writeFileSync(file, [
|
|---|
| 284 | JSON.stringify([ 'a', 'b' ]),
|
|---|
| 285 | JSON.stringify(null),
|
|---|
| 286 | '',
|
|---|
| 287 | '',
|
|---|
| 288 | JSON.stringify('wibble')
|
|---|
| 289 | ].join('\n'))
|
|---|
| 290 | results = []
|
|---|
| 291 | errors = []
|
|---|
| 292 | const datastream = bfj.match(fs.createReadStream(file), () => true, { ndjson: true })
|
|---|
| 293 | datastream.on('data', item => results.push(item))
|
|---|
| 294 | datastream.on('error', error => errors.push(error))
|
|---|
| 295 | datastream.on('end', done)
|
|---|
| 296 | })
|
|---|
| 297 |
|
|---|
| 298 | test('the correct properties were matched', () => {
|
|---|
| 299 | assert.deepEqual([ 'a', 'b', [ 'a', 'b' ], 'wibble' ], results)
|
|---|
| 300 | })
|
|---|
| 301 |
|
|---|
| 302 | test('no errors occurred', () => {
|
|---|
| 303 | assert.deepEqual(errors, [])
|
|---|
| 304 | })
|
|---|
| 305 | })
|
|---|
| 306 |
|
|---|
| 307 | suite('parse request:', () => {
|
|---|
| 308 | let error, result
|
|---|
| 309 |
|
|---|
| 310 | setup((done) => {
|
|---|
| 311 | const jsonstream = new stream.PassThrough()
|
|---|
| 312 | axios({
|
|---|
| 313 | responseType: 'stream',
|
|---|
| 314 | url: 'https://gitlab.com/philbooth/bfj/raw/master/package.json',
|
|---|
| 315 | }).then((response) => response.data.pipe(bfj.unpipe((err, res) => {
|
|---|
| 316 | error = err
|
|---|
| 317 | result = res
|
|---|
| 318 | done()
|
|---|
| 319 | })))
|
|---|
| 320 | })
|
|---|
| 321 |
|
|---|
| 322 | test('result was correct', () => {
|
|---|
| 323 | assert.isNull(error)
|
|---|
| 324 | assert.deepEqual(result, require('../package.json'))
|
|---|
| 325 | })
|
|---|
| 326 | })
|
|---|
| 327 |
|
|---|
| 328 | suite('parse NDJSON:', () => {
|
|---|
| 329 | let failed, file, results
|
|---|
| 330 |
|
|---|
| 331 | setup(() => {
|
|---|
| 332 | failed = false
|
|---|
| 333 | file = path.join(__dirname, 'data.ndjson')
|
|---|
| 334 | results = []
|
|---|
| 335 | fs.writeFileSync(file, [
|
|---|
| 336 | JSON.stringify([ 'b', 'a', 'r' ]),
|
|---|
| 337 | JSON.stringify(null),
|
|---|
| 338 | '',
|
|---|
| 339 | '',
|
|---|
| 340 | JSON.stringify('wibble')
|
|---|
| 341 | ].join('\n'))
|
|---|
| 342 | const stream = fs.createReadStream(file)
|
|---|
| 343 | return bfj.parse(stream, { ndjson: true })
|
|---|
| 344 | .then(result => {
|
|---|
| 345 | results.push(result)
|
|---|
| 346 | return bfj.parse(stream, { ndjson: true })
|
|---|
| 347 | })
|
|---|
| 348 | .then(result => {
|
|---|
| 349 | results.push(result)
|
|---|
| 350 | return bfj.parse(stream, { ndjson: true })
|
|---|
| 351 | })
|
|---|
| 352 | .then(result => {
|
|---|
| 353 | results.push(result)
|
|---|
| 354 | return bfj.parse(stream, { ndjson: true })
|
|---|
| 355 | })
|
|---|
| 356 | .then(result => {
|
|---|
| 357 | results.push(result)
|
|---|
| 358 | return bfj.parse(stream, { ndjson: true })
|
|---|
| 359 | })
|
|---|
| 360 | .then(result => results.push(result))
|
|---|
| 361 | .catch(e => {
|
|---|
| 362 | failed = true
|
|---|
| 363 | })
|
|---|
| 364 | })
|
|---|
| 365 |
|
|---|
| 366 | teardown(() => {
|
|---|
| 367 | fs.unlinkSync(file)
|
|---|
| 368 | })
|
|---|
| 369 |
|
|---|
| 370 | test('results were correct', () => {
|
|---|
| 371 | assert.isFalse(failed)
|
|---|
| 372 | assert.lengthOf(results, 5)
|
|---|
| 373 | assert.deepEqual(results, [
|
|---|
| 374 | [ 'b', 'a', 'r' ],
|
|---|
| 375 | null,
|
|---|
| 376 | 'wibble',
|
|---|
| 377 | undefined,
|
|---|
| 378 | undefined
|
|---|
| 379 | ])
|
|---|
| 380 | })
|
|---|
| 381 | })
|
|---|
| 382 |
|
|---|
| 383 | suite('stringify value:', () => {
|
|---|
| 384 | let result
|
|---|
| 385 |
|
|---|
| 386 | setup(() => {
|
|---|
| 387 | return bfj.stringify(new Promise(resolve => {
|
|---|
| 388 | setTimeout(resolve.bind(null, 'foo\t"\nbar'), 20)
|
|---|
| 389 | }))
|
|---|
| 390 | .then(res => result = res)
|
|---|
| 391 | })
|
|---|
| 392 |
|
|---|
| 393 | test('result was correct', () => {
|
|---|
| 394 | assert.strictEqual(result, '"foo\\t\\"\\nbar"')
|
|---|
| 395 | })
|
|---|
| 396 | })
|
|---|
| 397 |
|
|---|
| 398 | suite('write object:', () => {
|
|---|
| 399 | let failed, file, result
|
|---|
| 400 |
|
|---|
| 401 | setup(() => {
|
|---|
| 402 | failed = false
|
|---|
| 403 | file = path.join(__dirname, 'data.json')
|
|---|
| 404 | return bfj.write(
|
|---|
| 405 | file,
|
|---|
| 406 | { foo: [ 'b', 'a', 'r' ], baz: null, qux: 3.14159265359e42 }
|
|---|
| 407 | )
|
|---|
| 408 | .then(() => {
|
|---|
| 409 | result = fs.readFileSync(file, { encoding: 'utf8' })
|
|---|
| 410 | })
|
|---|
| 411 | .catch(error => {
|
|---|
| 412 | failed = true
|
|---|
| 413 | result = error
|
|---|
| 414 | })
|
|---|
| 415 | })
|
|---|
| 416 |
|
|---|
| 417 | teardown(() => {
|
|---|
| 418 | fs.unlinkSync(file)
|
|---|
| 419 | })
|
|---|
| 420 |
|
|---|
| 421 | test('did not fail', () => {
|
|---|
| 422 | assert.isFalse(failed)
|
|---|
| 423 | })
|
|---|
| 424 |
|
|---|
| 425 | test('result was correct', () => {
|
|---|
| 426 | assert.strictEqual(result, '{"foo":["b","a","r"],"baz":null,"qux":3.14159265359e+42}')
|
|---|
| 427 | })
|
|---|
| 428 | })
|
|---|
| 429 | })
|
|---|
| 430 | })
|
|---|
| 431 |
|
|---|