var SourceMapIndexGenerator = require('../lib/source-map-index-generator.js'),
    SourceMapGenerator = require('source-map').SourceMapGenerator;

// Generate test data for mapping
var input = [
      '// First line comment',
      'var test = {',
      '  a: "b"',
      '};'
    ].join('\n'),
    output = 'var test={a:"b"};',
    srcFile = 'input.js';
// Generated by node-jsmin2
var coordmap = {"22":0,"23":1,"24":2,"25":3,"26":4,"27":5,"28":6,"29":7,"31":8,"33":9,"37":10,"38":11,"40":12,"41":13,"42":14,"44":15,"45":16};

// Generated by hand ;_;
var propmap = [{
      generated: {'line': 1, 'column': 0}, // v
      original:  {'line': 2, 'column': 0},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 1}, // a
      original:  {'line': 2, 'column': 1},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 2}, // r
      original:  {'line': 2, 'column': 2},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 3}, //
      original:  {'line': 2, 'column': 3},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 4}, // t
      original:  {'line': 2, 'column': 4},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 5}, // e
      original:  {'line': 2, 'column': 5},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 6}, // s
      original:  {'line': 2, 'column': 6},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 7}, // t
      original:  {'line': 2, 'column': 7},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 8}, // =
      original:  {'line': 2, 'column': 9},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 9}, // {
      original:  {'line': 2, 'column': 11},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 10}, // a
      original:  {'line': 3, 'column': 2},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 11}, // :
      original:  {'line': 3, 'column': 3},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 12}, // "
      original:  {'line': 3, 'column': 5},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 13}, // b
      original:  {'line': 3, 'column': 6},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 14}, // "
      original:  {'line': 3, 'column': 7},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 15}, // }
      original:  {'line': 4, 'column': 0},
      source: srcFile
    }, {
      generated: {'line': 1, 'column': 16}, // ;
      original:  {'line': 4, 'column': 1},
      source: srcFile
    }];

exports['test some simple stuff'] = function (test) {
  test.expect(2);

  var map;
  test.doesNotThrow(function () {
    map = new SourceMapIndexGenerator({
      file: 'foo.js',
      sourceRoot: '.'
    });
  });

  test.ok(map);

  test.done();
};

exports['test generates the same mapping as SourceMapGenerator'] = function (test) {
  test.expect(1);

  var generatorProps = {
        file: 'min.js'
      },
      generator = new SourceMapGenerator(generatorProps);

  // Loop over the property map and add it to the SourceMapGenerator
  var i = 0,
      len = propmap.length;
  for (; i < len; i++) {
    generator.addMapping(propmap[i]);
  }

  // Collect the source map
  var genSourceMap = generator.toString();

  // Generate another source map via SourceMapIndexGenerator
  var indexGenerator = new SourceMapIndexGenerator(generatorProps);

  // Add the index coordinate mapping
  indexGenerator.addIndexMapping({
    src: srcFile,
    input: input,
    output: output,
    map: coordmap
  });

  // Collect the second source map
  var indexSourceMap = indexGenerator.toString();

  // Assert that they generate the same map
  test.strictEqual(genSourceMap, indexSourceMap, 'The sourcemap from SourceMapGenerator does not match the source map from SourceMapIndexGenerator');
  test.done();
};
