Created
January 18, 2012 16:49
-
-
Save weisjohn/1633980 to your computer and use it in GitHub Desktop.
code from my first visit to Dayton Clean Coders, an implementation of the kata at http://onestepback.org/vital_testing/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// implementation of the challenge found here: http://onestepback.org/vital_testing/ | |
var TriangleClassifier = { | |
valid : function (args) { | |
var sides = [ args[0], args[1], args[2] ]; | |
if (args.length != 3) { | |
return false; | |
} | |
for (var i = 0; i < sides.length; i++){ | |
if (sides[i] <= 0 || typeof sides[i] != 'number') { | |
return false; | |
} | |
} | |
sides.sort(); | |
if (sides[0] + sides[1] <= sides[2] ) { | |
return false; | |
} | |
return true; | |
}, | |
classify : function(len1, len2, len3) { | |
if ( !TriangleClassifier.valid(arguments) ) { | |
return "ArgumentError"; | |
} | |
if (len1 == len2 && len2 == len3 ) { | |
return ":equilateral"; | |
} | |
if (len1 == len2 || len2 == len3 || len3 == len1) { | |
return ":isosceles"; | |
} | |
return ":scalene"; | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('equilateral has 3 equal sides',function(){ | |
it('has equal sides', function() { | |
expect( | |
TriangleClassifier.classify(3, 3, 3) | |
).toBe(':equilateral'); | |
}); | |
}); | |
describe('isosceles has two equals sides',function(){ | |
it('has two equal sides', function() { | |
expect( | |
TriangleClassifier.classify(3,3,2) | |
).toBe(':isosceles'); | |
}); | |
}); | |
describe('scalene has no equals sides',function(){ | |
it('has no equal sides', function() { | |
expect( | |
TriangleClassifier.classify(2,3,4) | |
).toBe(':scalene'); | |
}); | |
}); | |
describe('sides are not of a triangle',function(){ | |
it('raises ArgumentError for a negative length side', function() { | |
expect( | |
TriangleClassifier.classify(-1,1,5) | |
).toBe('ArgumentError'); | |
}); | |
it('raises ArgumentError for a zero length side', function() { | |
expect( | |
TriangleClassifier.classify(0,1,5) | |
).toBe('ArgumentError'); | |
}); | |
it('raises ArgumentError for invalid triangle', function() { | |
expect( | |
TriangleClassifier.classify(2,2,4) | |
).toBe('ArgumentError'); | |
}); | |
it('raises ArgumentError for less than 3 sides', function() { | |
expect( | |
TriangleClassifier.classify(2,2) | |
).toBe('ArgumentError'); | |
}); | |
it('raises ArgumentError for more than 3 sides', function() { | |
expect( | |
TriangleClassifier.classify(3,4,5,6) | |
).toBe('ArgumentError'); | |
}); | |
it('raises ArgumentError for invalid data type for a sides', function() { | |
expect( | |
TriangleClassifier.classify(3,4,'5') | |
).toBe('ArgumentError'); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment