Last active
January 2, 2017 06:21
-
-
Save stujo/ad3a48a64199b2d63dbe82cad46defb2 to your computer and use it in GitHub Desktop.
code-wars-test-stubs.js
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
var Test = { | |
describe: Test_describe, | |
before: Test_before, | |
expect: Test_expect, | |
it: Test_it, | |
context: Test_context, | |
contexts: [], | |
assertEquals: Test_assertEquals, | |
logError: Test_logError, | |
logSuccess: Test_logSuccess, | |
currentIt: { | |
label: 'No It', | |
func: { | |
toString: function() {} | |
} | |
} | |
} | |
//// Helpers | |
function Test_describe(label, func) { | |
Test.contexts.push(new TestContext(label)) | |
Test_printLabel(); | |
func(); | |
Test.contexts.pop(); | |
} | |
function Test_it(label, func) { | |
Test.contexts.forEach(function(context) { | |
context.runBefores(); | |
}); | |
Test.currentIt = { | |
label, | |
func | |
}; | |
func.call(this); | |
Test.currentIt = null; | |
} | |
function Test_before(func) { | |
Test.context().addBefore(func); | |
} | |
function Test_context() { | |
return Test.contexts[Test.contexts.length - 1] | |
} | |
function Test_printLabel() { | |
var output = Test.contexts.map(function(context) { | |
return context.label; | |
}).join(" : "); | |
console.log(output); | |
} | |
function Test_assertEquals(actual, expected, message) { | |
if (actual === expected) { | |
Test.logSuccess("OK : " + (message || "No Description")); | |
Test.logSuccess("GOT : " + JSON.stringify(actual)); | |
} else { | |
Test.logError('########################################'); | |
Test.logError("FAILED : " + Test.currentIt.label || "No Label"); | |
Test.logError("FAILED : " + (message || "No Description")); | |
Test.logError("GOT : " + JSON.stringify(actual)); | |
Test.logError("EXPECTED : " + JSON.stringify(expected)); | |
Test.logError(Test.currentIt.func.toString()); | |
Test.logError('########################################'); | |
} | |
} | |
function Test_expect(ok, message) { | |
if (ok) { | |
Test.logSuccess("OK : " + (message || "No Description")); | |
} else { | |
Test.logError('########################################'); | |
Test.logError("FAILED : " + Test.currentIt.label || "No Label"); | |
Test.logError("FAILED : " + (message || "No Description")); | |
Test.logError(Test.currentIt.func.toString()); | |
Test.logError('########################################'); | |
} | |
} | |
function Test_logError(message) { | |
console.log("\x1b[31m", message, "\x1b[0m"); | |
} | |
function Test_logSuccess(message) { | |
console.log("\x1b[32m", message, "\x1b[0m"); | |
} | |
function TestContext(label) { | |
return { | |
label, | |
befores: [], | |
addBefore, | |
runBefores | |
}; | |
function addBefore(func) { | |
this.befores.push(func); | |
} | |
function runBefores() { | |
this.befores.forEach(function(before) { | |
before.call(this); | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment