miércoles, 26 de noviembre de 2014

Remove numbers in an array using gulp, mocha and expect

This last weekend, Iwas at codemotion 2014. In one  of the sessions, the speaker spoke about gulp. He said that it will be the standard tool for web developer. So I´ve done a little kata (remove a number in an array) with expect.js, mocha and gulp in javascript. The code lives here

The gulpfile.js

var gulp = require('gulp'),
   jshint = require('gulp-jshint'),
   mocha = require('gulp-mocha');

gulp.task('jshint', function(){
 gulp.src('source/*.js')
    jshint.reporter('default');
});

gulp.task('mocha', function () {
    gulp.src('test/*.js')
        .pipe(mocha({
            reporter: 'list'
        }))
        .on('error', function(err){
         console.log(err.message);
      this.emit('end'); 
        });
});

gulp.task('watch', function () {
 gulp.watch('source/*.js', function(){
  gulp.start('mocha');
 });
 gulp.watch('test/*.js', function(){
  gulp.start('mocha');
 });
});

gulp.task('default', function() {
    gulp.start('jshint', 'watch');
});


The System Under Test:

function removeElementInArray(number, vector){
 if(vector == []){
  return vector;
 }

 var result = [];
 var head;

 for(var position = 0; position < vector.length; position++){
  head = vector[position];
  if(head != number){
   result.push(head);
  }
 }
 return result;
}

exports.remove = removeElementInArray;

And the tests:

var sut = require("../source/sut.js"),
 expect = require("expect.js");

describe("Remove a number in an array thought functional way using TDD", function(){
  it("Given empty array then return empty array", function(){
    expect(sut.remove(2, [])).to.eql([]);
  });
  
  it("Given array with one element and 0 positions then return the same array", function(){
    expect(sut.remove(2, [2])).to.eql([]);
  });
  it("Given array with two numbers and number then return array without the number in the array", function(){
    expect(sut.remove(1, [1,2])).to.eql([2]);
  });
  it("Given array and positions then return rotated array without the number", function(){
    expect(sut.remove(3, [3,2,3,4])).to.eql([2,4]);
  });
});

No hay comentarios:

Publicar un comentario