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

Commit

Permalink
feat(testing): Init of the testing module
Browse files Browse the repository at this point in the history
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
danielpetisme committed Dec 30, 2015
1 parent b80a793 commit afa0db0
Show file tree
Hide file tree
Showing 13 changed files with 563 additions and 2 deletions.
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ repositories {
ext {
goloCliMain = 'org.eclipse.golo.cli.Main'
goloSources = fileTree('src/main/golo').include('**/*.golo')
goloTests = fileTree('src/test/golo').include('**/*.golo')
goloDocs = file("$buildDir/docs/golodoc")
}

Expand Down Expand Up @@ -107,6 +108,17 @@ test {

// .................................................................................................................. //

task golotest(type: JavaExec, dependsOn: [testClasses]) {
main = goloCliMain
args = ['test', '--files'] + goloTests
classpath = sourceSets.main.runtimeClasspath
inputs.files goloTests
description = 'Run Golo Tests'
group = 'Test'
}

// .................................................................................................................. //

processResources {
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [
version: version,
Expand Down Expand Up @@ -331,4 +343,4 @@ task wrapper(type: Wrapper) {
description 'Generates the Gradle wrapper scripts.'
}

// .................................................................................................................. //
// .................................................................................................................. //
37 changes: 37 additions & 0 deletions src/main/golo/testing.golo
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)
}
94 changes: 94 additions & 0 deletions src/main/golo/testing/console.golo
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())
}
11 changes: 11 additions & 0 deletions src/main/golo/testing/presenter.golo
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
}

24 changes: 24 additions & 0 deletions src/main/golo/testing/reporter.golo
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))
}
31 changes: 31 additions & 0 deletions src/main/golo/testing/runner.golo
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)
}
73 changes: 73 additions & 0 deletions src/main/golo/testing/suite.golo
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)
}
34 changes: 34 additions & 0 deletions src/main/golo/testing/test.golo
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()
}
3 changes: 3 additions & 0 deletions src/main/golo/testing/utils.golo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module gololang.testing.Utils

function NO_OP_1 = -> |x|{}
Loading

0 comments on commit afa0db0

Please sign in to comment.