Skip to content

Instantly share code, notes, and snippets.

@doekman
Last active October 20, 2015 17:51
Show Gist options
  • Save doekman/51aede23278a73ec1b3e to your computer and use it in GitHub Desktop.
Save doekman/51aede23278a73ec1b3e to your computer and use it in GitHub Desktop.
To refactor, or not to refactor
// Refactor step 7, still don't like it, f*ck it, just use native function
function isLeapYear(year) {
return new Date(year, 2-1, 29).getDate() == 29;
}
// Create a Jasmine fake
function describe(testName, tests) {
console.clear();
console.info('Name: %s - Start: %s', testName, new Date);
expect.nr=0;
expect.errors=0;
tests();
if (expect.errors == 0) console.info('All %s tests OK', expect.nr);
else console.error('%s of %s tests failed', expect.errors, expect.nr);
}
function expect(x) {
return {
toBe: function(y) {
if (x===y) console.info("- %s: OK", expect.nr++);
else console.error("- %s: Error", expect.nr++), expect.errors++;
}
};
}
// Unit test
function unitTests() {
describe("leapYear tests", function() {
expect(isLeapYear(1900)).toBe(false);
expect(isLeapYear(1971)).toBe(false);
expect(isLeapYear(1972)).toBe(true);
expect(isLeapYear(2000)).toBe(true);
expect(isLeapYear(2003)).toBe(false);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment