Skip to content

Instantly share code, notes, and snippets.

@BruceHubbard
Created January 17, 2012 18:14
Show Gist options
  • Save BruceHubbard/1627915 to your computer and use it in GitHub Desktop.
Save BruceHubbard/1627915 to your computer and use it in GitHub Desktop.
Dayton Clean Coders triangle problems specs and source
////Specs
describe('Triangle Classify',function(){
describe('Classify', function() {
it('On passing 3 different lengths returns scalene', function() {
expect(classify(3, 4, 5)).toBe(types.s);
});
it('On passing 2 equal lenghts returns isosceles', function() {
expect(classify(3,3,2)).toBe(types.i);
expect(classify(3,2,3)).toBe(types.i);
expect(classify(2,3,3)).toBe(types.i);
});
it('On passing 3 equal lenghts return equil-', function() {
expect(classify(1,1,1)).toBe(types.e);
});
it('On passing an invalid leg length should throw Exception', function() {
expect(function() {classify(0, 1, 1);}).toThrow(new Error('Invalid leg length'));
});
it('On passing an impossible leg length combination throw an Error', function() {
expect(function() {classify(1, 4, 5);}).toThrow(new Error('Impossible leg length combination'));
expect(function() {classify(1, 2, 5);}).toThrow(new Error('Impossible leg length combination'));
});
it('On passing non number it should throw an error', function() {
expect(function() { classify(1, 4, 'b');}).toThrow(new Error('"b" is not a number'));
});
});
});
describe('Triangle validator', function() {
it('testing spies', function() {
var spy = jasmine.createSpy('validator')
});
});
//source
var types = {
s: 'scalene',
i: 'isosceles',
e: 'equilateral'
};
var validator = {
isValidLeg: function(leg) {
return leg > 0;
}
};
function classify(len1, len2, len3) {
for(var i = 0, sides = [len1, len2, len3]; i < sides.length; i++) {
if(isNaN(sides[i])) {
throw new Error('"' + sides[i] + '" is not a number');
}
}
if(len1 <= 0 || len2 <= 0 || len3 <= 0) {
throw new Error('Invalid leg length');
}
if(len1 + len2 <= len3
|| len1 + len3 <= len2
|| len2 + len3 <= len1) {
throw new Error('Impossible leg length combination');
}
if(len1 === len2 && len1 === len3) {
return types.e;
}
if(len1 === len2 || len2 === len3 || len1 === len3) {
return types.i;
}
return types.s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment