let assert = require('chai').assert;

import {add, into, map} from '../../src/index.es6';

let listXf = {
  '@@transducer/init': () => [],
  '@@transducer/step': (acc, x) => [...acc, x],
  '@@transducer/result': x => x
};

describe('map', () => {
  it('should map arrays', () => {
    const result = map(add(2), [0,1,2]);
    assert.deepEqual(result, [2,3,4]);
  });

  it('should be curried', () => {
    const result = map(add(2))([0,1,2]);
    assert.deepEqual(result, [2,3,4]);
  });

  let times2 = x => x * 2;
  let add1 = x => x + 1;
  let dec = x =>  x - 1;
  let intoArray = into([]);

  it('maps simple functions over arrays', function() {
    assert.deepEqual(map(times2, [1, 2, 3, 4]), [2, 4, 6, 8]);
  });

  it('dispatches to objects that implement `map`', function() {
    let obj = {x: 100, map: function(f) { return f(this.x); }};
    assert.strictEqual(map(add1, obj), 101);
  });

  it('maps simple functions into arrays', function() {
    assert.deepEqual(intoArray(map(times2), [1, 2, 3, 4]), [2, 4, 6, 8]);
  });

  it('composes', function() {
    let mdouble = map(times2);
    let mdec = map(dec);
    assert.deepEqual(mdec(mdouble([10, 20, 30])), [19, 39, 59]);
  });


  it('can compose transducer-style', function() {
    let mdouble = map(times2);
    let mdec = map(dec);
    let xcomp = mdec(mdouble(listXf));
    assert.deepEqual(xcomp.xf, {xf: listXf, f: times2});
    assert.strictEqual(xcomp.f, dec);
  });

  it('is curried', function() {
    let inc = map(add1);
    assert.deepEqual(inc([1, 2, 3]), [2, 3, 4]);
  });

  it('dispatches to transformer objects', function() {
    assert.deepEqual(map(add1, listXf), {
      f: add1,
      xf: listXf
    });
  });

  it('correctly reports the arity of curried versions', function() {
    let inc = map(add1);
    assert.strictEqual(inc.length, 1);
  });
});

@@ -1,12 +1,13 @@
+%0A
 let assert =
@@ -564,36 +564,9 @@
  1;%0A
-  let intoArray = into(%5B%5D);
 %0A
+
 %0A  i
@@ -951,14 +951,13 @@
 into
-Array(
+(%5B%5D, 
 map(
@@ -994,32 +994,276 @@
  6, 8%5D);%0A  %7D);%0A%0A
+  it('maps simple functions over generator', function() %7B%0A    function* numbers()%7B%0A      let i = 1;%0A      while(i %3C 5)%7B%0A        yield i;%0A        i +=1;%0A      %7D%0A    %7D%0A%0A    assert.deepEqual(into(%5B%5D, map(times2), numbers()), %5B2, 4, 6, 8%5D);%0A  %7D);%0A%0A
   it('composes',
@@ -1401,16 +1401,15 @@
 %5D);%0A
+
   %7D);%0A%0A
-%0A
   it

test iterator support