This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): Init of the testing module
Add a CLI Add a Gradle integration Add beforeEach/afterEach fixtures Add bassic console presentation Signed-off-by: Daniel Petisme <[email protected]>
- Loading branch information
1 parent
b80a793
commit afa0db0
Showing
13 changed files
with
563 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module gololang.Testing | ||
|
||
import gololang.testing.Runner | ||
import gololang.testing.Suite | ||
import gololang.testing.Test | ||
import gololang.testing.Reporter | ||
|
||
augment gololang.testing.Runner.types.Runner { | ||
|
||
function describe = |this, description, fn| { | ||
let parent = this: currentSuite() | ||
let suite = Suite( | ||
description, | ||
parent, | ||
this: reporter(): onSuiteStarted(), | ||
this: reporter(): onSuiteDone() | ||
) | ||
parent: add(suite) | ||
this: currentSuite(suite) | ||
fn() | ||
this: currentSuite(parent) | ||
} | ||
|
||
function it = |this, description, fn| { | ||
let parent = this: currentSuite() | ||
parent: add(Test( | ||
description, | ||
fn, | ||
parent, | ||
this: reporter(): onTestStarted(), | ||
this: reporter(): onTestDone() | ||
)) | ||
} | ||
|
||
function beforeEach = |runner, fn| -> runner: currentSuite(): addBeforeEach(fn) | ||
function afterEach = |runner, fn| -> runner: currentSuite(): addAfterEach(fn) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
module gololang.testing.presenters.Console | ||
|
||
import gololang.testing.Presenter | ||
import gololang.AnsiCodes | ||
|
||
|
||
function Console = { | ||
let level = DynamicObject(): position(-1): indentSize(2) | ||
|
||
let fwd = -> level: position(level: position() + 1) | ||
let bwd = -> level: position(level: position() - 1) | ||
let spaces = -> (level: position() * level: indentSize()): times({ print(" ")}) | ||
|
||
return Presenter() | ||
:onTestStarted(|test| { | ||
fwd() | ||
spaces() | ||
_onTestStarted(test) | ||
}) | ||
:onTestDone(|test| { | ||
spaces() | ||
_onTestDone(test) | ||
bwd() | ||
}) | ||
:onSuiteStarted(|suite| { | ||
fwd() | ||
spaces() | ||
_onSuiteStarted(suite) | ||
}) | ||
:onSuiteDone(|suite| { | ||
spaces() | ||
_onSuiteDone(suite) | ||
bwd() | ||
}) | ||
:onGlobalStarted(|runner| { | ||
_onGlobalStarted(runner) | ||
}) | ||
:onGlobalDone(|runner| { | ||
_onGlobalDone(runner) | ||
}) | ||
} | ||
|
||
local function success = |msg| { | ||
fg_green() | ||
println(msg) | ||
reset() | ||
} | ||
|
||
local function error = |msg| { | ||
fg_red() | ||
println(msg) | ||
reset() | ||
} | ||
|
||
local function warning = |msg| { | ||
fg_yellow() | ||
println(msg) | ||
reset() | ||
} | ||
|
||
local function info = |msg| { | ||
fg_blue() | ||
println(msg) | ||
reset() | ||
} | ||
|
||
local function _onTestStarted = |test| { | ||
# info(test: description()) | ||
} | ||
|
||
local function _onTestDone = |test| { | ||
if (test: failed()) { | ||
error(test: description()) | ||
} else { | ||
success(test: description()) | ||
} | ||
} | ||
|
||
local function _onSuiteStarted = |suite| { | ||
# success(suite: description()) | ||
println(suite: description()) | ||
} | ||
|
||
local function _onSuiteDone = |suite| { | ||
# error(suite: description()) | ||
} | ||
|
||
local function _onGlobalStarted = |runner| { | ||
# println("Global started...") | ||
} | ||
|
||
local function _onGlobalDone = |runner| { | ||
info("Total " + runner: currentSuite(): report(): total() + " tests ran. Failures " + runner: currentSuite(): report(): failures()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module gololang.testing.Presenter | ||
|
||
struct Presenter = { | ||
onGlobalStarted, | ||
onGlobalDone, | ||
onSuiteStarted, | ||
onSuiteDone, | ||
onTestStarted, | ||
onTestDone | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module gololang.testing.Reporter | ||
|
||
struct Reporter = { | ||
presenters | ||
} | ||
|
||
function Reporter = -> gololang.testing.Reporter.types.Reporter(list[]) | ||
|
||
augment gololang.testing.Reporter.types.Reporter { | ||
|
||
function addPresenter = |this, presenter| { this: presenters(): add(presenter) } | ||
|
||
function onTestStarted = |this| -> |test| -> this: presenters(): each(|p| -> p: onTestStarted()(test)) | ||
|
||
function onTestDone = |this| -> |test| -> this: presenters(): each(|p| -> p: onTestDone()(test)) | ||
|
||
function onSuiteStarted = |this| -> |suite| -> this: presenters(): each(|p| -> p: onSuiteStarted()(suite)) | ||
|
||
function onSuiteDone = |this| -> |suite| -> this: presenters(): each(|p| -> p: onSuiteDone()(suite)) | ||
|
||
function onGlobalStarted = |this, runner| -> this: presenters(): each(|p| -> p: onGlobalStarted()(runner)) | ||
|
||
function onGlobalDone = |this, runner| -> this: presenters(): each(|p| -> p: onGlobalDone()(runner)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module gololang.testing.Runner | ||
|
||
import gololang.testing.Utils | ||
|
||
import gololang.testing.Suite | ||
import gololang.testing.Test | ||
import gololang.testing.Reporter | ||
import gololang.testing.presenters.Console | ||
|
||
struct Runner = { | ||
currentSuite, | ||
reporter | ||
} | ||
|
||
function build = { | ||
let reporter = Reporter() | ||
let top_level_suite = Suite("TOP_LEVEL_SUITE", null, NO_OP_1(), NO_OP_1()) | ||
let runner = Runner(top_level_suite, reporter) | ||
runner: addPresenter(Console()) | ||
return runner | ||
} | ||
|
||
augment gololang.testing.Runner.types.Runner { | ||
function run = |this| { | ||
this: reporter(): onGlobalStarted(this) | ||
this: currentSuite(): run() | ||
this: reporter(): onGlobalDone(this) | ||
} | ||
|
||
function addPresenter = |this, presenter| -> this: reporter(): addPresenter(presenter) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
module gololang.testing.Suite | ||
|
||
import gololang.testing.Test | ||
|
||
struct Suite = { | ||
description, | ||
parent, | ||
children, | ||
befores, | ||
afters, | ||
onStart, | ||
onDone, | ||
report | ||
} | ||
|
||
struct SuiteReport = { | ||
total, | ||
failures | ||
} | ||
|
||
function Suite = |description, parent, onStart, onDone| -> gololang.testing.Suite.types.Suite() | ||
:description(description) | ||
:parent(parent) | ||
:children(list[]) | ||
:befores(list[]) | ||
:afters(list[]) | ||
:onStart(onStart) | ||
:onDone(onDone) | ||
:report(SuiteReport(0, 0)) | ||
|
||
augment gololang.testing.Suite.types.SuiteReport { | ||
function addExcutedTest = |this, child| { | ||
if child oftype gololang.testing.Suite.types.Suite.class { | ||
this: total( this: total() + child: report(): total() ) | ||
} else { | ||
this: total( this: total() + 1) | ||
} | ||
} | ||
function addFailures = |this, child| { | ||
if child oftype gololang.testing.Suite.types.Suite.class { | ||
this: failures( this: failures() + child: report(): failures() ) | ||
} else { | ||
this: failures( this: failures() + 1) | ||
} | ||
} | ||
} | ||
|
||
augment gololang.testing.Suite.types.Suite { | ||
|
||
function run = |this| { | ||
this: onStart()(this) | ||
this: children(): each(|child| { | ||
this: befores(): each(|before| -> before()) | ||
child: run() | ||
this: report(): addExcutedTest(child) | ||
if child: failed() { | ||
this: report(): addFailures(child) | ||
} | ||
this: afters(): each(|after| -> after()) | ||
}) | ||
this: onDone()(this) | ||
} | ||
|
||
function add = |this, it| { | ||
it: parent(this) | ||
this: children(): add(it) | ||
} | ||
|
||
function failed = |this| -> this: report(): failures() isnt 0 | ||
|
||
function addBeforeEach = |this, before| -> this: befores(): add(before) | ||
function addAfterEach = |this, after| -> this: afters(): add(after) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module gololang.testing.Test | ||
|
||
struct Test = { | ||
description, | ||
fn, | ||
parent, | ||
onStart, | ||
onDone, | ||
status | ||
} | ||
|
||
function Test = |description, fn, parent, onStart, onDone| -> gololang.testing.Test.types.Test() | ||
:description(description) | ||
:fn(fn) | ||
:parent(parent) | ||
:onStart(onStart) | ||
:onDone(onDone) | ||
:status("not_started") | ||
|
||
local function _failed = -> "failed" | ||
|
||
augment gololang.testing.Test.types.Test { | ||
function run = |this| { | ||
this: onStart()(this) | ||
try { | ||
this: fn()() | ||
} catch (e) { | ||
this: status(_failed()) | ||
} | ||
this: onDone()(this) | ||
} | ||
|
||
function failed = |this| -> this: status() is _failed() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module gololang.testing.Utils | ||
|
||
function NO_OP_1 = -> |x|{} |
Oops, something went wrong.