Skip to content

Commit

Permalink
Tweak code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pineapplemachine committed Mar 27, 2018
1 parent a0d9bb5 commit 27a0fe5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
9 changes: 4 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ Canary can be installed and added to a JavaScript project with `npm install cana
A simple example of JavaScript code tested using Canary:

``` js
const canary = require("canary-test"); // This library
const assert = require("assert"); // Node's built-in assertion library
// This library
const canary = require("canary-test");
// Node's built-in assertion library
const assert = require("assert");

// A function that ought to be tested
function leftPad(value, length){
if(value.length >= length){
return String(value);
}
let text = String(value);
while(text.length < length){
text = " " + text;
Expand Down
31 changes: 31 additions & 0 deletions leftPad.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This is an example of using Canary to test some code!
// Try it out with `node leftPad.js`

// This library
const canary = require("./canary.js");
// Node's built-in assertion library
const assert = require("assert");

// A function that ought to be tested
function leftPad(value, length){
let text = String(value);
while(text.length < length){
text = " " + text;
}
return text;
}

// Tests written using Canary
canary.group("leftPad", function(){
this.test("returns the input when it's as long as or longer than the input length", () => {
assert.equal(leftPad("hello", 3), "hello");
assert.equal(leftPad("world", 5), "world");
});
this.test("pads shorter inputs with spaces to match the desired length", () => {
assert.equal(leftPad("hi", 4), " hi");
assert.equal(leftPad("123", 6), " 123");
});
});

// Run the tests!
canary.doReport();
11 changes: 6 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ Canary works best when used in combination with an assertion library such as [ch
Here's a simple example of JavaScript code tested using Canary:

``` js
const canary = require("canary-test"); // This library
const assert = require("assert"); // Node's built-in assertion library
// This library
const canary = require("canary-test");
// Node's built-in assertion library
const assert = require("assert");

// A function that ought to be tested
function leftPad(value, length){
if(value.length >= length){
return String(value);
}
let text = String(value);
while(text.length < length){
text = " " + text;
}
return text;
}

// Tests written using Canary
canary.group("leftPad", function(){
this.test("returns the input when it's as long as or longer than the input length", () => {
assert.equal(leftPad("hello", 3), "hello");
Expand Down

0 comments on commit 27a0fe5

Please sign in to comment.