const isError = require('util').isError

const test = require('tape')
const isPromise = require('is-promise')

const swear = require('../lib/')

test('always returns a promise', t => {
  t.plan(1)

  t.true(isPromise(swear({})))
})

test('resolves promises in a provided object', t => {
  t.plan(1)

  swear({a: promisify('b')})
    .then(x => t.deepEqual(x, {a: 'b'}))
    .catch(t.fail)
})

test('rejects if any promise is rejected', t => {
  t.plan(2)

  swear({a: rejectify('error')})
    .then(() => t.fail())
    .catch(err => {
      t.true(isError(err))
      t.equal(err.message, 'error')
    })
})

test('resolves all the way down', t => {
  t.plan(1)

  swear({a: promisify({b: promisify('c')})})
    .then(x => t.deepEqual(x, {a: {b: 'c'}}))
    .catch(t.fail)
})

test('works with arrays', t => {
  t.plan(1)

  swear([promisify(1)])
    .then(x => t.deepEqual(x, [1]))
    .catch(t.fail)
})

test('works with arrays chained', t => {
  t.plan(1)

  swear([promisify({a: promisify(1)})])
    .then(x => t.deepEqual(x, [{a: 1}]))
    .catch(t.fail)
})

test('works with a promise', t => {
  t.plan(3)

  swear(promisify('x'))
    .then(x => t.equal(x, 'x'))
    .catch(t.fail)

  swear(rejectify('x'))
    .then(t.fail)
    .catch(t.pass)

  swear(promisify({a: promisify('b')}))
    .then(x => t.deepEqual(x, {a: 'b'}))
    .catch(t.fail)
})

test('works with strings and numbers', t => {
  t.plan(2)

  swear(4)
    .then(x => t.equal(x, 4))
    .catch(t.fail)

  swear('lol')
    .then(x => t.equal(x, 'lol'))
    .catch(t.fail)
})

test('works with a promise that resolves to a string or number', t => {
  t.plan(2)

  swear(promisify(4))
    .then(x => t.equal(x, 4))
    .catch(t.fail)

  swear(promisify('lol'))
    .then(x => t.equal(x, 'lol'))
    .catch(t.fail)
})

test('safe to wrap', t => {
  t.plan(2)

  swear(swear(4))
    .then(x => t.equal(x, 4))
    .catch(t.fail)

  swear(swear(swear(4)))
    .then(x => t.equal(x, 4))
    .catch(t.fail)
})

function promisify (x) {
  return new Promise(resolve => resolve(x))
}

function rejectify (x) {
  return new Promise((resolve, reject) => reject(new Error(x)))
}

@@ -301,33 +301,39 @@
   swear(%7Ba: 
-p
+P
 romis
-ify
+e.resolve
 ('b')%7D)%0A    
@@ -467,25 +467,40 @@
 ear(%7Ba: 
-rejectify
+Promise.reject(new Error
 ('error'
@@ -500,16 +500,17 @@
 'error')
+)
 %7D)%0A    .
@@ -694,31 +694,43 @@
 %7Ba: 
-p
+P
 romis
-ify
+e.resolve
 (%7Bb: 
-p
+P
 romis
-ify
+e.resolve
 ('c'
@@ -859,25 +859,31 @@
  swear(%5B
-p
+P
 romis
-ify
+e.resolve
 (1)%5D)%0A  
@@ -1002,39 +1002,51 @@
  swear(%5B
-p
+P
 romis
-ify
+e.resolve
 (%7Ba: 
-p
+P
 romis
-ify
+e.resolve
 (1)%7D)%5D)%0A
@@ -1166,25 +1166,31 @@
   swear(
-p
+P
 romis
-ify
+e.resolve
 ('x'))%0A 
@@ -1252,23 +1252,39 @@
 ear(
-rejectify
+Promise.reject(new Error
 ('x'))
+)
 %0A   
@@ -1330,31 +1330,43 @@
 ear(
-p
+P
 romis
-ify
+e.resolve
 (%7Ba: 
-p
+P
 romis
-ify
+e.resolve
 ('b'
@@ -1719,25 +1719,31 @@
   swear(
-p
+P
 romis
-ify
+e.resolve
 (4))%0A   
@@ -1797,25 +1797,31 @@
   swear(
-p
+P
 romis
-ify
+e.resolve
 ('lol'))
@@ -2068,168 +2068,4 @@
 %0A%7D)%0A
-%0Afunction promisify (x) %7B%0A  return new Promise(resolve =%3E resolve(x))%0A%7D%0A%0Afunction rejectify (x) %7B%0A  return new Promise((resolve, reject) =%3E reject(new Error(x)))%0A%7D%0A

kill unnecessary test helper functions