Sha256: 5235e21d3778883ef14266d422d47de9fc05cf5efd8d00ed6935d451964b888d

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

var test = require('tape')
var ndj = require('./')
var os = require('os')
var concat = require('concat-stream')

test('.parse', function(t) {
  var parser = ndj.parse()
  parser.on('data', function(obj) {
    t.equal(obj.hello, 'world')
    t.end()
  })

  parser.write('{"hello": "world"}\n')
})

test('.parse twice', function(t) {
  var parser = ndj.parse()
  parser.once('data', function(obj) {
    t.equal(obj.hello, 'world')
    parser.once('data', function(obj) {
      t.equal(obj.hola, 'mundo')
      t.end()
    })
  })

  parser.write('{"hello": "world"}\n{"hola": "mundo"}\n')
})

test('.parse - strict:true error', function (t) {
  var parser = ndj.parse({strict: true})
  try {
    parser.write('{"no":"json"\n')
  } catch(e) {
    t.pass('error thrown')
    t.end()
  }
})

test('.parse - strict:true error event', function (t) {
  var parser = ndj.parse({strict: true})
  parser.on('error', function (err) {
    t.pass('error event called')
    t.end()
  })
  try {
    parser.write('{"no":"json"\n')
  } catch(e) {
    t.fail('should not throw')
  }
})

test('.parse - strict:false error', function (t) {
  var parser = ndj.parse({strict: false})
  parser.once('data', function (data) {
    t.ok(data.json, 'parse second one')
    t.end()
  })
  try {
    parser.write('{"json":false\n{"json":true}\n')
  } catch(e) {
    t.fail('should not call an error')
  }
})

test('.serialize', function(t) {
  var serializer = ndj.serialize()
  serializer.pipe(concat(function(data) {
    t.equal(data, '{"hello":"world"}' + os.EOL)
    t.end()
  }))
  serializer.write({hello: 'world'})
  serializer.end()
})

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
node_task-0.3.5 lib/node_task/node_modules/ndjson/test.js
node_task-0.3.4 lib/node_task/node_modules/ndjson/test.js
node_task-0.3.3 lib/node_task/node_modules/ndjson/test.js
node_task-0.3.2 lib/node_task/node_modules/ndjson/test.js
node_task-0.3.1 lib/node_task/node_modules/ndjson/test.js
node_task-0.3.0 lib/node_task/node_modules/ndjson/test.js
node_task-0.2.0 lib/node_task/node_modules/ndjson/test.js