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

Assertion helpers #360

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions doc/misc.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,54 @@ let err, val = trying({
The error is the left value, the correct one the right (mnemonic: “right” also means “correct”). This allows to deal with error in the same way as Go does for instance.

The decorators `nullify` and `raising` are the duals of `option` or `result`

=== Assertions (`gololang.Assertions`)

It allows to declare (and test) an expected boolean condition in a program.

==== `assert`

Ensure that a boolean expression is true at runtime:

[source,golo]
----
assert(-> 5: equals(5))

# If you don't provide callback to assert(), an AssertionError is thrown if the predicate is false
assert(-> 5: equals(42))

assert(-> "Golo" oftype Integer.class, |error| {
println("Huston?")
})

assert(predicate= -> 5 < 0, onSuccess=|res|{
println("You win!")
}, onError=|err|{
println("5 is not less than 0!")
})
----

==== `assertEqual`

Ensure that two expressions are equal to each other:

[source,golo]
----
# be careful, if no callback and assertion is false, then the program is aborted
assertEqual(42, 42)

assertEqual(42, 69, |error| {
println(error)
})

let point = Point(5,5)
assertEqual(point: frozenCopy(), point: frozenCopy(), |res| { # success
println(":)")
},
|error| { # fail
println(":(")
})

# display report of results
gololang.Assertions.displayTestsReport()
----
58 changes: 58 additions & 0 deletions samples/some-assertions.golo
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) 2012-2016 Institut National des Sciences Appliquées de Lyon (INSA-Lyon)
#
# All rights reserved. This Example Content is intended to demonstrate
# usage of Eclipse technology. It is provided to you under the terms and
# conditions of the Eclipse Distribution License v1.0 which is available
# at http://www.eclipse.org/org/documents/edl-v10.php

module golo.samples.Assertions

import gololang.Assertions

struct Point = {
x, y
}

function main = |args| {

assert(-> 5: equals(5))

assert(-> "BoB" oftype String.class, |error| {
println("Huston?")
})

assert(predicate= -> "BoB" oftype Integer.class,
successMessage= "This is an Integer",
onSuccess= |res| {
println("You Win")
},
errorMessage= "This isn't an Integer",
onError = |error| {
println("Huston?")
}
)

assert(predicate= -> 5 < 0, onSuccess=|res| {}, onError=|err| {
println("5 is not less than 0!")
})

assertEqual(42, 42)

assertEqual(42, 69, |error| {
println(error)
})

assertEqual(Point(5,5), Point(5,5), |error| {
println("???")
})

let point = Point(5,5)
assertEqual(point: frozenCopy(), point: frozenCopy(), |res| {
println(":)")
},
|error| {
println(":(")
})

gololang.Assertions.displayTestsReport()
}
Loading