Skip to content

Commit

Permalink
add should module
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Nov 30, 2023
1 parent 9faf699 commit 9472e7c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 28 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

## Unpublished

## v0.2.0 - November 30 2023

- Changed API completely (whoops)
- Use erlang.rescue instead of otp constructs
- Add JSON example to show how this actually gets used
- Add a `should` module

## v0.1.1 - September 5 2023
- added `named` convenience function ( thx Jak! )
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import gleam/dynamic
pub fn main() {
// Lets do a more real world example. Lets say you have some test
// Lets say you have some test
// cases in a json file. Here's an example of building a test suite
// from them
Expand Down
8 changes: 7 additions & 1 deletion src/testbldr.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,26 @@ pub fn demonstrate(with input: List(pieces.Test), that name: String) {
{ "Test \"" <> name <> "\" passed" }
|> ansi.green
|> io.println

acc + 1
}
Ok(pieces.Fail(msg)) -> {
io.print(int.to_string(index + 1) <> ". ")
{ "Test \"" <> name <> "\" failed: " <> msg }
{ "Test \"" <> name <> "\" failed: " }
|> ansi.red
|> io.println

msg
|> io.println

acc
}
Error(_) -> {
io.print(int.to_string(index + 1) <> ". ")
{ "Test \"" <> name <> "\" panicked!" }
|> ansi.red
|> io.println

acc
}
}
Expand Down
38 changes: 36 additions & 2 deletions src/testbldr/should.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@ import gleam/string
pub fn equal(a, b) -> pieces.TestOutcome {
case a == b {
True -> pieces.Pass
False -> pieces.Fail(string.inspect(a) <> " != " <> string.inspect(b))
False -> pieces.Fail(bin_op_msg(a, "==", b))
}
}

pub fn not_equal(a, b) -> pieces.TestOutcome {
case a != b {
True -> pieces.Pass
False -> pieces.Fail(string.inspect(a) <> " == " <> string.inspect(b))
False -> pieces.Fail(bin_op_msg(a, "!=", b))
}
}

pub fn be_true(a: Bool) -> pieces.TestOutcome {
case a {
True -> pieces.Pass
False -> pieces.Fail("\nExpected True, got False\n")
}
}

pub fn be_false(a: Bool) -> pieces.TestOutcome {
case a {
False -> pieces.Pass
True -> pieces.Fail("\nExpected False, got True\n")
}
}

pub fn be_ok(a: Result(a, b)) -> pieces.TestOutcome {
case a {
Ok(_) -> pieces.Pass
Error(_) ->
pieces.Fail("\nExpected Ok, Got Error: " <> string.inspect(a) <> "\n")
}
}

pub fn be_error(a: Result(a, b)) -> pieces.TestOutcome {
case a {
Ok(_) ->
pieces.Fail("\nExpected Error, Got Ok: " <> string.inspect(a) <> "\n")
Error(_) -> pieces.Pass
}
}

fn bin_op_msg(lhs: a, op: String, rhs: b) -> String {
"\nlhs: " <> string.inspect(lhs) <> "\nrhs: " <> string.inspect(rhs) <> "\nassertion lhs " <> op <> " rhs failed\n"
}
12 changes: 0 additions & 12 deletions test/test_cases.json

This file was deleted.

45 changes: 33 additions & 12 deletions test/testbldr_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,15 @@ import gleam/list
import gleam/int
import gleam/json
import gleam/dynamic
import gleam/string

pub fn main() {
// Basic example of programatically building a test case
testbldr.demonstrate(
// Give this grouping of tests a name
that: "Some numbers are odd",
// The list of tests
with: // We map over the input and use it to produce tests
{
with: {
use n <- list.map([1, 3, 5, 7, 9])

// `named` will let you dynamically name your test
// cases for printing
use <- testbldr.named(int.to_string(n) <> " is odd")

// You can `should` just like gleeunit
n % 2
|> should.equal(1)
},
Expand Down Expand Up @@ -49,9 +42,7 @@ pub fn main() {

testbldr.demonstrate(
that: "Our doubling function works",
with: // Decode our tests cases from the JSON. If it doesn't decode
// correctly we crash the whole thing because it's a test suite
{
with: {
let assert Ok(test_cases) = test_cases_from_json(test_cases)

// Map over our tests cases to start transforming them
Expand All @@ -66,6 +57,36 @@ pub fn main() {
|> should.equal(test_case.expected_output)
},
)

testbldr.demonstrate(
that: "Boolean messages look right",
with: {
use val <- list.map([True, False])
use <- testbldr.named(string.inspect(val) <> " should be true")
val
|> should.be_true
},
)

testbldr.demonstrate(
that: "Boolean messages look right",
with: {
use val <- list.map([True, False])
use <- testbldr.named(string.inspect(val) <> " should be false")
val
|> should.be_false
},
)

testbldr.demonstrate(
that: "Result messages look right",
with: {
use val <- list.map([Ok(Nil), Error(Nil)])
use <- testbldr.named(string.inspect(val) <> " should be ok")
val
|> should.be_ok
},
)
}

/// Something to parse our JSON test cases into
Expand Down

0 comments on commit 9472e7c

Please sign in to comment.