Skip to content

Instantly share code, notes, and snippets.

@johnelliott
Created November 7, 2014 14:48
Show Gist options
  • Select an option

  • Save johnelliott/bef503cb9039a2776d25 to your computer and use it in GitHub Desktop.

Select an option

Save johnelliott/bef503cb9039a2776d25 to your computer and use it in GitHub Desktop.
Node-Jasmine setup using module.exports
function Rectangle(length, width) {
this.length = length;
this.width = width;
this.isSquare = function function_name(argument) {
return this.length === this.width;
};
this.area = function function_name(argument) {
return this.length * this.width;
};
this.perimeter = function function_name(argument) {
return (this.length * 2) + (this.width * 2);
};
}
function Triangle(sideA, sideB, sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
function LineSegment(x1, y1, x2, y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.length = function(x1, y1, x2, y2) {
xDistance = this.x2 - this.x1;
yDistance = this.y2 - this.y1;
return Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));
};
}
module.exports = {
rectangle: Rectangle,
triangle: Triangle,
lineSegment: LineSegment
};
var geometry = require('../geom');
describe("Rectangle tests", function() {
it("should be square for a 1x1 rectangle", function() {
var rect = new geometry.rectangle(1,1);
var result = rect.isSquare();
expect(result).toBe(true);
});
it("should NOT be square for a 1x2 rectangle", function() {
var rect = new geometry.rectangle(1,2);
var result = rect.isSquare();
expect(result).toBe(false);
});
it("should have a perimeter of 4 for a 1x1 rectangle", function() {
var rect = new geometry.rectangle(1,1);
var result = rect.perimeter();
expect(result).toBe(4);
});
it("should have a area of 100 for a 10x10 rectangle", function() {
var rect = new geometry.rectangle(10,10);
var result = rect.area();
expect(result).toBe(100);
});
});
describe("Line segments tests", function() {
it("should return line length 1 for a 0,0 to 0,1 line", function() {
var line = new geometry.lineSegment(0,0,0,1);
var result = line.length();
expect(result).toBe(1);
});
it("should return line length 0 for a 0,1 to 0,1 line", function() {
var line = new geometry.lineSegment(0,1,0,1);
var result = line.length();
expect(result).toBe(0);
});
it("should return line length 0 for a 0,0 to 3,2 line", function() {
var line = new geometry.lineSegment(0,0,3,2);
var result = line.length();
expect(result).toBe(3.605551275463989);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment