/*!
 * value2stream <https://github.com/hybridables/value2stream>
 *
 * Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
 * Released under the MIT license.
 */

'use strict'

var test = require('mukla')
var isBuffer = require('is-buffer')
var value2stream = require('./index')
var Promize = require('pinkie-promise')

function toStream (input, ondata, done) {
  return value2stream(input)
    .on('data', function (val) {
      try {
        ondata(val)
      } catch (err) {
        /* istanbul ignore next */
        done(err)
      }
    })
    .once('error', done)
    .once('end', done)
}

test('should create stream from number value', function (done) {
  toStream(123, function (val) {
    test.strictEqual(typeof val, 'number')
    test.strictEqual(val, 123)
  }, done)
})

test('should create stream from string value', function (done) {
  toStream('foo bar', function (val) {
    test.strictEqual(typeof val, 'string')
    test.strictEqual(val, 'foo bar')
  }, done)
})

test('should create stream from buffer value', function (done) {
  toStream(new Buffer('buf str'), function (val) {
    test.strictEqual(isBuffer(val), true)
    test.strictEqual(val.toString(), 'buf str')
  }, done)
})

test('should create stream from array value', function (done) {
  toStream([1, 'foo', 2], function (val) {
    test.deepEqual(val, [1, 'foo', 2])
  }, done)
})

test('should create stream from resolved promise', function (done) {
  var promise = Promize.resolve('abc')
  toStream(promise, function (val) {
    test.strictEqual(typeof val, 'string')
    test.strictEqual(val, 'abc')
  }, done)
})

test('should fire `error` event on rejected promise', function (done) {
  var rejected = Promize.reject(new Error('foo msg'))
  value2stream(rejected).once('error', function (err) {
    test.ifError(!err)
    test.strictEqual(err instanceof Error, true)
    test.strictEqual(err.message, 'foo msg')
    done()
  })
})

test('should fire `data` event even if value is Error object', function (done) {
  var error = new Error('some err here')
  toStream(error, function (val) {
    test.strictEqual(val instanceof Error, true)
    test.strictEqual(val.message, 'some err here')
  }, done)
})

test('should allow custom promise constructor to be used when no native promise', function (done) {
  value2stream(123, Promize)
    .on('data', function (val) {
      test.strictEqual(val, 123)
    })
    .once('error', done)
    .once('end', done)
})

test('should fire `error` event on `opts.objectMode: false` (default) and object value', function (done) {
  value2stream({ a: 'b' }).once('error', function (err) {
    test.ifError(!err)
    test.strictEqual(err instanceof Error, true)
    done()
  })
})

test('should fire `data` event on `opts.objectMode: true` and object value', function (done) {
  value2stream({ a: 'b' }, { objectMode: true })
    .on('data', function (val) {
      test.deepEqual(val, { a: 'b' })
    })
    .once('error', done)
    .once('end', done)
})

@@ -2483,37 +2483,36 @@
 t('should fire %60
-error
+data
 %60 event on %60opts
@@ -2528,12 +2528,11 @@
 de: 
-fals
+tru
 e%60 (
@@ -2570,32 +2570,45 @@
 ction (done) %7B%0A 
+ var stream =
  value2stream(%7B 
@@ -2616,28 +2616,39 @@
 : 'b' %7D)
-.once('error
+%0A  stream%0A    .on('data
 ', funct
@@ -2644,39 +2644,41 @@
 ata', function (
-err
+val
 ) %7B%0A
+  
     test.ifError
@@ -2674,85 +2674,88 @@
 est.
-ifError(!err)%0A    test.strictEqual(err instanceof E
+deepEqual(val, %7B a: 'b' %7D)%0A    %7D)%0A    .once('e
 rror
+'
 , 
-tru
+don
 e)%0A    
-d
+.
 on
+c
 e(
-)%0A  %7D
+'end', done
 )%0A%7D)
@@ -2767,36 +2767,37 @@
 t('should fire %60
-data
+error
 %60 event on %60opts
@@ -2809,19 +2809,20 @@
 ctMode: 
-tru
+fals
 e%60 and o
@@ -2846,32 +2846,45 @@
 ction (done) %7B%0A 
+ var stream =
  value2stream(%7B 
@@ -2911,29 +2911,37 @@
 de: 
-tru
+fals
 e %7D)%0A  
- 
- .on('data
+stream.once('error
 ', f
@@ -2945,35 +2945,35 @@
 , function (
-val
+err
 ) %7B%0A    
   test.deepE
@@ -2964,95 +2964,171 @@
     
-  
 test.
-deepEqual(val, %7B a: 'b' %7D)%0A    %7D)%0A    .once('error', don
+ifError(!err)%0A    test.strictEqual(err instanceof Error, true)%0A    test.strictEqual(/Invalid non-string%5C/buffer chunk/.test(err.message), tru
 e)%0A    
-.
+d
 on
-c
 e(
-'end', done
+)%0A  %7D
 )%0A%7D)

fix tests