Skip to content

Commit

Permalink
CanaryTest API tweaks
Browse files Browse the repository at this point in the history
- Rename durationSeconds/durationMilliseconds to getDurationSeconds/getDurationMilliseconds and make them not getters
- Add notSilent and notVerbose methods
- Add getLogFunction and setLogFunction methods, use the log function instead of always only using console.log
  • Loading branch information
pineapplemachine committed Mar 29, 2018
1 parent 851e5d2 commit 37eeeaa
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
59 changes: 45 additions & 14 deletions canary.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ class CanaryTest{
this.onEachEndCallbacks = [];
// A dictionary of tags assigned to this test in particular.
this.tagDictionary = {};
// Logging function determining what should happen to logged messages
this.logFunction = console.log;
// The location where this test was defined.
const location = this.getCallerLocation();
if(location){
Expand Down Expand Up @@ -224,6 +226,13 @@ class CanaryTest{
child.silent();
}
}
// Mark the test and all its children as not silent.
notSilent(){
this.isSilent = false;
for(let child of this.children){
child.notSilent();
}
}
// Mark the test and all its children as verbose. They will log a lot of
// information about the test process.
// It also un-silences silenced tests.
Expand All @@ -234,6 +243,13 @@ class CanaryTest{
child.verbose();
}
}
// Mark the test and all its children as not silent.
notVerbose(){
this.isVerbose = false;
for(let child of this.children){
child.notVerbose();
}
}
// Assign some tags to this test, which will be inherited by its children
// and grandchildren and etc.
tags(...tags){
Expand Down Expand Up @@ -286,16 +302,25 @@ class CanaryTest{
getName(){
return this.name;
}
getLogFunction(){
return this.logFunction;
}
setLogFunction(logFunction){
this.logFunction = logFunction;
for(let child of this.children){
child.setLogFunction(logFunction);
}
}
// Log a message. (Except if the test was marked as silent.)
log(...message){
log(message){
if(!this.isSilent){
return console.log(...message);
return this.logFunction(message);
}
}
// Log a verbose message.
logVerbose(...message){
logVerbose(message){
if(this.isVerbose && !this.isSilent){
return console.log(...message);
return this.logFunction(message);
}
}
// Helper function to retrieve the current time in milliseconds.
Expand All @@ -307,15 +332,15 @@ class CanaryTest{
}
}
// Get how long the test took to run, in seconds
get durationSeconds(){
getDurationSeconds(){
if(this.startTime === undefined || this.endTime === undefined){
return undefined;
}else{
return 0.001 * (this.endTime - this.startTime);
}
}
// Get how long the test took to run, in milliseconds
get durationMilliseconds(){
getDurationMilliseconds(){
if(this.startTime === undefined || this.endTime === undefined){
return undefined;
}else{
Expand Down Expand Up @@ -557,7 +582,7 @@ class CanaryTest{
// If no errors were encountered during this completion process, log a
// message explaining that the test was completed.
}else{
const duration = this.durationSeconds.toFixed(3);
const duration = this.getDurationSeconds().toFixed(3);
if(this.isGroup){
this.log(`Completed test group "${this.getTitle()}". (${duration}s)`);
}else{
Expand Down Expand Up @@ -618,8 +643,7 @@ class CanaryTest{
`"${this.parent.name}".`
);
if(this.parent.removeTest){
this.parent.removeTest(this);
return true;
return this.parent.removeTest(this);
}
this.parent = undefined;
return false;
Expand All @@ -635,6 +659,12 @@ class CanaryTest{
this.children[index].parent = undefined;
this.children.splice(index, 1);
return true;
}else{
for(let searchChild of this.children){
if(searchChild.removeTest(child)){
return true;
}
}
}
return false;
}
Expand Down Expand Up @@ -677,6 +707,7 @@ class CanaryTest{
test.isIgnored = this.isIgnored;
test.isSilent = this.isSilent;
test.isVerbose = this.isVerbose;
test.logFunction = this.logFunction;
// All done! Return the produced CanaryTest instance.
return test;
}
Expand Down Expand Up @@ -768,7 +799,6 @@ class CanaryTest{
}else{
let anyChildSatisfies = false;
for(let child of this.children){
this.logVerbose("Checking child: ", child.name);
if(child.applyFilter(filter)){
anyChildSatisfies = true;
}
Expand Down Expand Up @@ -951,10 +981,11 @@ class CanaryTest{
text += yellow(`- ${this.name} (TODO)`);
// The test ran successfully.
}else if(this.success){
if(this.durationSeconds === undefined){
const seconds = this.getDurationSeconds();
if(seconds === undefined){
text += green(`✓ ${this.name}`);
}else{
text += green(`✓ ${this.name} (${this.durationSeconds.toFixed(3)}s)`);
text += green(`✓ ${this.name} (${seconds.toFixed(3)}s)`);
}
// The test encountered an error or errors.
}else if(this.anyErrors()){
Expand Down Expand Up @@ -1056,9 +1087,9 @@ class CanaryTest{
try{
// Set a default empty options object when none was specified.
options = options || {};
function log(...message){
function log(message){
if(!options.silent){
console.log(...message);
return this.getLogFunction()(message);
}
}
// Indicate that tests are about to be run!
Expand Down
22 changes: 18 additions & 4 deletions docs/api-advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const test = new canary.Test("Example test", function(){

# silent

Set a test and all its children to run silently. This means that they will not output any log messages.
Recursively set a test and all its children to run silently. This means that they will not output any log messages.

The [**log**](api-intermediate-usage.md#log) method of a [**CanaryTest**](api-introduction.md) instance can be used to log a message only if the test has not been set to run silently.

Expand All @@ -32,6 +32,12 @@ canary.test("Example silent test", function(){
});
```

# notSilent

Recursively set a test and all its children to not [run silently](api-advanced-usage.md#silent). (This is the default behavior.)

The [**log**](api-intermediate-usage.md#log) method of a [**CanaryTest**](api-introduction.md) instance can be used to log a message only if the test has not been set to run silently.

# verbose

Set a test and all its children to run verbosely. This means they will output even more detailed logs than usual.
Expand All @@ -50,6 +56,12 @@ canary.test("Example verbose test", function(){
});
```

# notVerbose

Recursively set a test and all its children to not [run verbosely](api-advanced-usage.md#verbose). (This is the default behavior.)

The [**logVerbose**](api-intermediate-usage.md#logverbose) method of a [**CanaryTest**](api-introduction.md) instance can be used to log a message only when the test is set to run verbosely.

# unignore

Un-ignore a test that was previously marked as being ignored.
Expand Down Expand Up @@ -128,13 +140,13 @@ Get a string representing the status of this test.

**Returns:** `"skipped"` if the test was marked as skipped or was never attempted, `"passed"` if the test completed successfully, or `"failed"` if the test was unsuccessful.

# durationSeconds
# getDurationSeconds

Get the length of time taken to run this test, in seconds.

**Returns:** The number of seconds it took to run the test, or **undefined** if the test has not yet been completed.

# durationMilliseconds
# getDurationMilliseconds

Get the length of time taken to run this test, in milliseconds.

Expand Down Expand Up @@ -222,7 +234,9 @@ Note that if the test was previously added to another test group, it will be rem

# removeTest

Remove a child test from a parent group.
Remove a child test from a test tree.

This method searches recursively for the given test, so it will be removed regardless of whether it is an immediate child of this test, or if it is a child of a child, etc.

**Arguments:** `({CanaryTest} test)`

Expand Down
39 changes: 31 additions & 8 deletions docs/api-intermediate-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ canary.group("Example test group", function(){

# log

Log a message to the console, except for if this test has been set to run silently.
Log a message, except for if this test has been set to run silently.

**Arguments:** `(...{anything} message)`

The arguments to the function are treated the same as with [**console.log**](https://developer.mozilla.org/en-US/docs/Web/API/Console/log).
**Arguments:** `({object} message)`

**Examples:**

Expand All @@ -76,11 +74,9 @@ canary.test("Example test", function(){

# logVerbose

Log a message to the console, but only if this test has been set to run verbosely.

**Arguments:** `(...{anything} message)`
Log a message, but only if this test has been set to run verbosely.

The arguments to the function are treated the same as with [**console.log**](https://developer.mozilla.org/en-US/docs/Web/API/Console/log).
**Arguments:** `({object} message)`

**Examples:**

Expand All @@ -90,3 +86,30 @@ canary.test("Example test", function(){
});
```

# getLogFunction

Get the logging function that is being used by this test. By default, Canary uses [**console.log**](https://developer.mozilla.org/en-US/docs/Web/API/Console/log) when logging messages.

**Returns:** The function which is invoked to log messages.

**Examples:**

``` js
const logFunction = canary.getLogFunction();
assert(logFunction === console.log);
```

# setLogFunction

Set the logging function that is being used by this test and all children tests. By default, Canary uses [**console.log**](https://developer.mozilla.org/en-US/docs/Web/API/Console/log) when logging messages.

**Arguments:** `({function} logFunction)`

**Examples:**

``` js
// Add a timestamp in front of every message that Canary outputs
canary.setLogFunction(message => {
return console.log((new Date()).toISOString() + ": " + message);
});
```

0 comments on commit 37eeeaa

Please sign in to comment.