Skip to content
This repository was archived by the owner on Sep 28, 2022. It is now read-only.

Commit afa0db0

Browse files
committed
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]>
1 parent b80a793 commit afa0db0

File tree

13 files changed

+563
-2
lines changed

13 files changed

+563
-2
lines changed

build.gradle

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ repositories {
3232
ext {
3333
goloCliMain = 'org.eclipse.golo.cli.Main'
3434
goloSources = fileTree('src/main/golo').include('**/*.golo')
35+
goloTests = fileTree('src/test/golo').include('**/*.golo')
3536
goloDocs = file("$buildDir/docs/golodoc")
3637
}
3738

@@ -107,6 +108,17 @@ test {
107108

108109
// .................................................................................................................. //
109110

111+
task golotest(type: JavaExec, dependsOn: [testClasses]) {
112+
main = goloCliMain
113+
args = ['test', '--files'] + goloTests
114+
classpath = sourceSets.main.runtimeClasspath
115+
inputs.files goloTests
116+
description = 'Run Golo Tests'
117+
group = 'Test'
118+
}
119+
120+
// .................................................................................................................. //
121+
110122
processResources {
111123
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
112124
version: version,
@@ -331,4 +343,4 @@ task wrapper(type: Wrapper) {
331343
description 'Generates the Gradle wrapper scripts.'
332344
}
333345

334-
// .................................................................................................................. //
346+
// .................................................................................................................. //

src/main/golo/testing.golo

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
module gololang.Testing
2+
3+
import gololang.testing.Runner
4+
import gololang.testing.Suite
5+
import gololang.testing.Test
6+
import gololang.testing.Reporter
7+
8+
augment gololang.testing.Runner.types.Runner {
9+
10+
function describe = |this, description, fn| {
11+
let parent = this: currentSuite()
12+
let suite = Suite(
13+
description,
14+
parent,
15+
this: reporter(): onSuiteStarted(),
16+
this: reporter(): onSuiteDone()
17+
)
18+
parent: add(suite)
19+
this: currentSuite(suite)
20+
fn()
21+
this: currentSuite(parent)
22+
}
23+
24+
function it = |this, description, fn| {
25+
let parent = this: currentSuite()
26+
parent: add(Test(
27+
description,
28+
fn,
29+
parent,
30+
this: reporter(): onTestStarted(),
31+
this: reporter(): onTestDone()
32+
))
33+
}
34+
35+
function beforeEach = |runner, fn| -> runner: currentSuite(): addBeforeEach(fn)
36+
function afterEach = |runner, fn| -> runner: currentSuite(): addAfterEach(fn)
37+
}

src/main/golo/testing/console.golo

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
module gololang.testing.presenters.Console
2+
3+
import gololang.testing.Presenter
4+
import gololang.AnsiCodes
5+
6+
7+
function Console = {
8+
let level = DynamicObject(): position(-1): indentSize(2)
9+
10+
let fwd = -> level: position(level: position() + 1)
11+
let bwd = -> level: position(level: position() - 1)
12+
let spaces = -> (level: position() * level: indentSize()): times({ print(" ")})
13+
14+
return Presenter()
15+
:onTestStarted(|test| {
16+
fwd()
17+
spaces()
18+
_onTestStarted(test)
19+
})
20+
:onTestDone(|test| {
21+
spaces()
22+
_onTestDone(test)
23+
bwd()
24+
})
25+
:onSuiteStarted(|suite| {
26+
fwd()
27+
spaces()
28+
_onSuiteStarted(suite)
29+
})
30+
:onSuiteDone(|suite| {
31+
spaces()
32+
_onSuiteDone(suite)
33+
bwd()
34+
})
35+
:onGlobalStarted(|runner| {
36+
_onGlobalStarted(runner)
37+
})
38+
:onGlobalDone(|runner| {
39+
_onGlobalDone(runner)
40+
})
41+
}
42+
43+
local function success = |msg| {
44+
fg_green()
45+
println(msg)
46+
reset()
47+
}
48+
49+
local function error = |msg| {
50+
fg_red()
51+
println(msg)
52+
reset()
53+
}
54+
55+
local function warning = |msg| {
56+
fg_yellow()
57+
println(msg)
58+
reset()
59+
}
60+
61+
local function info = |msg| {
62+
fg_blue()
63+
println(msg)
64+
reset()
65+
}
66+
67+
local function _onTestStarted = |test| {
68+
# info(test: description())
69+
}
70+
71+
local function _onTestDone = |test| {
72+
if (test: failed()) {
73+
error(test: description())
74+
} else {
75+
success(test: description())
76+
}
77+
}
78+
79+
local function _onSuiteStarted = |suite| {
80+
# success(suite: description())
81+
println(suite: description())
82+
}
83+
84+
local function _onSuiteDone = |suite| {
85+
# error(suite: description())
86+
}
87+
88+
local function _onGlobalStarted = |runner| {
89+
# println("Global started...")
90+
}
91+
92+
local function _onGlobalDone = |runner| {
93+
info("Total " + runner: currentSuite(): report(): total() + " tests ran. Failures " + runner: currentSuite(): report(): failures())
94+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module gololang.testing.Presenter
2+
3+
struct Presenter = {
4+
onGlobalStarted,
5+
onGlobalDone,
6+
onSuiteStarted,
7+
onSuiteDone,
8+
onTestStarted,
9+
onTestDone
10+
}
11+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module gololang.testing.Reporter
2+
3+
struct Reporter = {
4+
presenters
5+
}
6+
7+
function Reporter = -> gololang.testing.Reporter.types.Reporter(list[])
8+
9+
augment gololang.testing.Reporter.types.Reporter {
10+
11+
function addPresenter = |this, presenter| { this: presenters(): add(presenter) }
12+
13+
function onTestStarted = |this| -> |test| -> this: presenters(): each(|p| -> p: onTestStarted()(test))
14+
15+
function onTestDone = |this| -> |test| -> this: presenters(): each(|p| -> p: onTestDone()(test))
16+
17+
function onSuiteStarted = |this| -> |suite| -> this: presenters(): each(|p| -> p: onSuiteStarted()(suite))
18+
19+
function onSuiteDone = |this| -> |suite| -> this: presenters(): each(|p| -> p: onSuiteDone()(suite))
20+
21+
function onGlobalStarted = |this, runner| -> this: presenters(): each(|p| -> p: onGlobalStarted()(runner))
22+
23+
function onGlobalDone = |this, runner| -> this: presenters(): each(|p| -> p: onGlobalDone()(runner))
24+
}

src/main/golo/testing/runner.golo

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module gololang.testing.Runner
2+
3+
import gololang.testing.Utils
4+
5+
import gololang.testing.Suite
6+
import gololang.testing.Test
7+
import gololang.testing.Reporter
8+
import gololang.testing.presenters.Console
9+
10+
struct Runner = {
11+
currentSuite,
12+
reporter
13+
}
14+
15+
function build = {
16+
let reporter = Reporter()
17+
let top_level_suite = Suite("TOP_LEVEL_SUITE", null, NO_OP_1(), NO_OP_1())
18+
let runner = Runner(top_level_suite, reporter)
19+
runner: addPresenter(Console())
20+
return runner
21+
}
22+
23+
augment gololang.testing.Runner.types.Runner {
24+
function run = |this| {
25+
this: reporter(): onGlobalStarted(this)
26+
this: currentSuite(): run()
27+
this: reporter(): onGlobalDone(this)
28+
}
29+
30+
function addPresenter = |this, presenter| -> this: reporter(): addPresenter(presenter)
31+
}

src/main/golo/testing/suite.golo

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module gololang.testing.Suite
2+
3+
import gololang.testing.Test
4+
5+
struct Suite = {
6+
description,
7+
parent,
8+
children,
9+
befores,
10+
afters,
11+
onStart,
12+
onDone,
13+
report
14+
}
15+
16+
struct SuiteReport = {
17+
total,
18+
failures
19+
}
20+
21+
function Suite = |description, parent, onStart, onDone| -> gololang.testing.Suite.types.Suite()
22+
:description(description)
23+
:parent(parent)
24+
:children(list[])
25+
:befores(list[])
26+
:afters(list[])
27+
:onStart(onStart)
28+
:onDone(onDone)
29+
:report(SuiteReport(0, 0))
30+
31+
augment gololang.testing.Suite.types.SuiteReport {
32+
function addExcutedTest = |this, child| {
33+
if child oftype gololang.testing.Suite.types.Suite.class {
34+
this: total( this: total() + child: report(): total() )
35+
} else {
36+
this: total( this: total() + 1)
37+
}
38+
}
39+
function addFailures = |this, child| {
40+
if child oftype gololang.testing.Suite.types.Suite.class {
41+
this: failures( this: failures() + child: report(): failures() )
42+
} else {
43+
this: failures( this: failures() + 1)
44+
}
45+
}
46+
}
47+
48+
augment gololang.testing.Suite.types.Suite {
49+
50+
function run = |this| {
51+
this: onStart()(this)
52+
this: children(): each(|child| {
53+
this: befores(): each(|before| -> before())
54+
child: run()
55+
this: report(): addExcutedTest(child)
56+
if child: failed() {
57+
this: report(): addFailures(child)
58+
}
59+
this: afters(): each(|after| -> after())
60+
})
61+
this: onDone()(this)
62+
}
63+
64+
function add = |this, it| {
65+
it: parent(this)
66+
this: children(): add(it)
67+
}
68+
69+
function failed = |this| -> this: report(): failures() isnt 0
70+
71+
function addBeforeEach = |this, before| -> this: befores(): add(before)
72+
function addAfterEach = |this, after| -> this: afters(): add(after)
73+
}

src/main/golo/testing/test.golo

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module gololang.testing.Test
2+
3+
struct Test = {
4+
description,
5+
fn,
6+
parent,
7+
onStart,
8+
onDone,
9+
status
10+
}
11+
12+
function Test = |description, fn, parent, onStart, onDone| -> gololang.testing.Test.types.Test()
13+
:description(description)
14+
:fn(fn)
15+
:parent(parent)
16+
:onStart(onStart)
17+
:onDone(onDone)
18+
:status("not_started")
19+
20+
local function _failed = -> "failed"
21+
22+
augment gololang.testing.Test.types.Test {
23+
function run = |this| {
24+
this: onStart()(this)
25+
try {
26+
this: fn()()
27+
} catch (e) {
28+
this: status(_failed())
29+
}
30+
this: onDone()(this)
31+
}
32+
33+
function failed = |this| -> this: status() is _failed()
34+
}

src/main/golo/testing/utils.golo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module gololang.testing.Utils
2+
3+
function NO_OP_1 = -> |x|{}

0 commit comments

Comments
 (0)