Test Driven Development Exercise

Running jasmine test inside jsFiddle to demonstrate test driven development (TDD) with a simple exercise to build a calculator application in Javascript.

by kamui

HTML

<link rel="stylesheet" href="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css">
<script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="//cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<title>Jasmine Spec Runner</title><meta name="description" value="
Test Driven Development - Build a calulator
* it needs be able to sum multiple values
* it needs be able to able to subtract multiple values
* it needs to be able to multiply multiple values
* it needs to be able to divide one value from another
* it needs to be able to work with floating point numbers
* it needs to be able to calculate the valie of an input number to the power of another number.
* it needs to be able to calulate the square root of a value
* it should have the ability to take a string of calculation values and compute the correct result.
* it should be able to handle multiple operators at the same time.
* it should output values to 5 decimal places
* it would be good if it could have a text field and button that when pressed would validate and calculate a user input calculation.
* it would be good if it could generate the calculations from calculator buttons
" />
<h3>In the beginning there was nothing, but one failing test.</h3>
<ul>
<li>First you must write a failing test</li>
<li>Then write only enough code to make it pass</li>
<li>Refactor and repeat</li>
</ul>
<!--<input name="calculation" type="text" /><button id="calculationtrigger">
caclulate</button>-->

JavaScript

/*
var calculator = 
{
  //sum:function(a,b){},
  //sum:function(a,b){ return a + b; },
  //sumreducer: (accumulator, currentValue) => accumulator + currentValue,
  //sum: function() { return Object.values(arguments).reduce(this.sumreducer); },
  //subtract:function(a,b){},
  //subtract: function(a, b){ return a - b;},
  //subtractReducer: (accumulator, currentValue) => accumulator - currentValue, 
  //subtract: function() { return Object.values(arguments).reduce(this.subtractReducer); },
};
*/

// specs code
describe("calculator", function() {
  it("calculator should be implemented", function() {
    expect(calculator).toBeDefined();
  });
  
  /*
  it("sum method should be implemented", function() {
    expect(calculator.sum).toBeDefined();
  });
 
  it("sum method should sum 2 values resulting in 3", function() {
    expect(calculator.sum(1,2)).toEqual(3);
  });
 
  it("sum method should sum 3 values resulting in 6", function() {
    expect(calculator.sum(1,2,3)).toEqual(6);
  });  
  
  it("subtract method should be implemented", function() {
    expect(calculator.subtract).toBeDefined();
  });
 
  it("subtract method should subtract one value 1 from another 2 resulting in 1", function() {
    expect(calculator.subtract(2,1)).toEqual(1);
  });
   
  it("subtract method should one value from another sequentially resulting in 1", function() {
    expect(calculator.subtract(10,2,7)).toEqual(1);
  })
  */
});

// load jasmine htmlReporter
(function() {
  var env = jasmine.getEnv();
  env.addReporter(new jasmine.HtmlReporter());
  env.execute();
}());