const babel = require('babel-core');

const customMatchers = {
  toBeTransformedTo: () => {
    const transformAmdToCommonJS = (code) => {
      return babel.transform(code, { plugins: ['./index'] }).code;
    };

    const transformTrivial = (code) => {
      return babel.transform(code).code;
    };

    const removeBlankLines = (string) => {
      return string.split('\n').filter(line => !!line.trim().length).join('\n');
    };

    return {
      compare(actual, expected) {
        const transformed = transformAmdToCommonJS(actual);
        actual = removeBlankLines(transformTrivial(actual));
        expected = removeBlankLines(transformTrivial(expected));
        const result = {
          pass: removeBlankLines(transformed) === expected
        };
        if(result.pass) {
          result.message = `Expected\n\n${actual}\n\nnot to be transformed ` +
            `to\n\n${expected}\n\nbut instead they were the same.\n`;
        } else {
          result.message = `Expected\n\n${actual}\n\nto be transformed ` +
            `to\n\n${expected}\n\nbut instead got\n\n${transformed}\n`;
        }
        return result;
      }
    };
  }
};

describe('Plugin', () => {
  beforeEach(() => {
    jasmine.addMatchers(customMatchers);
  });

  it('transforms anonymous define blocks with one dependency', () => {
    expect(`
      define(['stuff'], function(donkeys) {
        return {
          llamas: donkeys.version
        };
      });
    `).toBeTransformedTo(`
      var donkeys = require('stuff');
      module.exports = function() {
        return {
          llamas: donkeys.version
        };
      }();
    `);
  });

  it('transforms anonymous define blocks with multiple dependencies', () => {
    expect(`
      define(['stuff', 'here'], function(donkeys, aruba) {
        return {
           llamas: donkeys.version,
           cows: aruba.hi
        };
      });
    `).toBeTransformedTo(`
      var donkeys = require('stuff');
      var aruba = require('here');
      module.exports = function() {
        return {
          llamas: donkeys.version,
          cows: aruba.hi
        };
      }();
    `);
  });

  it('only transforms define blocks at the top level', () => {
    const program = `
      if(someDumbCondition) {
        define(['stuff'], function(stuff) {
          return { hi: 'world' };
        });
      }
    `;
    expect(program).toBeTransformedTo(program);
  });

  it('transforms require blocks with one dependency', () => {
    expect(`
      require(['llamas'], function(llama) {
        llama.doSomeStuff();
      });
    `).toBeTransformedTo(`
      var llama = require('llamas');
      (function() {
        llama.doSomeStuff();
      })();
    `);
  });
});

@@ -2133,24 +2133,450 @@
  %60);%0A  %7D);%0A%0A
+  it('transforms anonymous define blocks with unused dependencies', () =%3E %7B%0A    expect(%60%0A      define(%5B'stuff', 'here'%5D, function(donkeys) %7B%0A        return %7B%0A           llamas: donkeys.version%0A        %7D;%0A      %7D);%0A    %60).toBeTransformedTo(%60%0A      var donkeys = require('stuff');%0A      require('here');%0A      module.exports = function() %7B%0A        return %7B%0A          llamas: donkeys.version%0A        %7D;%0A      %7D();%0A    %60);%0A  %7D);%0A%0A
   it('only t
@@ -3134,13 +3134,756 @@
 );%0A  %7D);
+%0A%0A  it('transforms require blocks with multiple dependencies', () =%3E %7B%0A    expect(%60%0A      require(%5B'llamas', 'frogs'%5D, function(llama, frog) %7B%0A        llama.doSomeStuff();%0A        frog.sayRibbit();%0A      %7D);%0A    %60).toBeTransformedTo(%60%0A      var llama = require('llamas');%0A      var frog = require('frogs');%0A      (function() %7B%0A        llama.doSomeStuff();%0A        frog.sayRibbit();%0A      %7D)();%0A    %60);%0A  %7D);%0A%0A  it('transforms require blocks with unused dependencies', () =%3E %7B%0A    expect(%60%0A      require(%5B'llamas', 'frogs'%5D, function(llama) %7B%0A        llama.doSomeStuff();%0A      %7D);%0A    %60).toBeTransformedTo(%60%0A      var llama = require('llamas');%0A      require('frogs');%0A      (function() %7B%0A        llama.doSomeStuff();%0A      %7D)();%0A    %60);%0A  %7D);
 %0A%7D);%0A

Add tests for unused dependencies