'use strict';

var gutil = require('gulp-util');
var should = require('should');
var bump = require('..');

require('mocha');

describe('gulp-bump: JSON comparison fixtures', function() {

  it('should bump patch version by default', function(done) {
    var fakeFile = new gutil.File({
      contents: new Buffer('{ "version": "0.0.9" }'),
      path: 'test/fixtures/test.json'
    });

    var bumpS = bump();

    bumpS.once('data', function(newFile) {
      should.exist(newFile);
      should.exist(newFile.contents);
      JSON.parse(newFile.contents.toString()).version.should.equal('0.0.10');
      return done();
    });
    bumpS.write(fakeFile);
    bumpS.end();
  });

  it('should bump patch version as default and a key=appversion', function(done) {
    var fakeFile = new gutil.File({
      contents: new Buffer('{ "appversion": "0.0.1" }'),
      path: 'test/fixtures/test.json'
    });

    var bumpS = bump({key: 'appversion'});

    bumpS.once('data', function(newFile) {
      should.exist(newFile);
      should.exist(newFile.contents);
      JSON.parse(newFile.contents.toString()).appversion.should.equal('0.0.2');
      return done();
    });
    bumpS.write(fakeFile);
    bumpS.end();
  });

  it('should ignore invalid type and use type=patch', function(done) {
    var fakeFile = new gutil.File({
      contents: new Buffer('{ "version": "0.0.1" }'),
      path: 'test/fixtures/test.json'
    });

    var bumpS = bump({type: 'invalidType'});

    bumpS.once('data', function(newFile) {
      should.exist(newFile);
      should.exist(newFile.contents);
      JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
      return done();
    });

    bumpS.write(fakeFile);
    bumpS.end();
  });

  it('should set the correct version when supplied', function(done) {
    var fakeFile = new gutil.File({
      contents: new Buffer('{ "version": "0.0.1" }'),
      path: 'test/fixtures/test.json'
    });

    var bumpS = bump({version: '0.0.2'});

    bumpS.once('data', function(newFile) {
      should.exist(newFile);
      should.exist(newFile.contents);
      JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
      return done();
    });
    bumpS.write(fakeFile);
    bumpS.end();
  });

  it('should set the correct version when supplied even if key did not exist', function(done) {
    var fakeFile = new gutil.File({
      contents: new Buffer('{}'),
      path: 'test/fixtures/test.json'
    });

    var bumpS = bump({version: '0.0.2'});

    bumpS.once('data', function(newFile) {
      should.exist(newFile);
      should.exist(newFile.contents);
      JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2');
      return done();
    });
    bumpS.write(fakeFile);
    bumpS.end();
  });

  it('should bump prerelease version', function(done) {
    var fakeFile = new gutil.File({
      contents: new Buffer('{ "version": "0.0.1-0"}'),
      path: 'test/fixtures/test.json'
    });

    var bumpS = bump({type: 'prerelease'});

    bumpS.once('data', function(newFile) {
      should.exist(newFile);
      should.exist(newFile.contents);
      JSON.parse(newFile.contents.toString()).version.should.equal('0.0.1-1');
      return done();
    });
    bumpS.write(fakeFile);
    bumpS.end();
  });
});

@@ -3279,13 +3279,593 @@
 );%0A  %7D);
+%0A%0A   it('should bump to a prerelease version with a preid', function(done) %7B%0A    var fakeFile = new gutil.File(%7B%0A      contents: new Buffer('%7B %22version%22: %220.0.1%22%7D'),%0A      path: 'test/fixtures/test.json'%0A    %7D);%0A%0A    var bumpS = bump(%7Btype: 'prerelease', preid : 'alphaWhateverTheYWant'%7D);%0A%0A    bumpS.once('data', function(newFile) %7B%0A      should.exist(newFile);%0A      should.exist(newFile.contents);%0A      JSON.parse(newFile.contents.toString()).version.should.equal('0.0.2-alphaWhateverTheYWant.0');%0A      return done();%0A    %7D);%0A    bumpS.write(fakeFile);%0A    bumpS.end();%0A  %7D);
 %0A%7D);%0A

add basic test for preid