Skip to content

Commit

Permalink
v1
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Dec 29, 2023
1 parent 3f59fc5 commit 2474526
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 251 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unpublished

## v1.0.0 - December 28 2023
- Completely change the api again and then do v1 (woohoo)

## v0.3.0 - December 14 2023

- Added `Silent` variant to test outcome so you can optionally
Expand Down
80 changes: 16 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,77 +9,29 @@ automatically discovered and run.
Other times you may want to read test cases in from files, json responses, etc.
This library is for the second case.

# Example

```gleam
import testbldr
import testbldr/should
import gleam/list
import gleam/int
import gleam/json
import gleam/dynamic
pub fn main() {
// Lets say you have some test
// cases in a json file. Here's an example of building a test suite
// from them
// The function we're testing
let double = fn(x) { x * 2 }
// Test cases given as json
let test_cases =
"[
{
\"name\": \"1 doubled is 2\",
\"input\": 1,
\"expected_output\": 2
},
{
\"name\": \"3 doubled is 6\",
\"input\": 3,
\"expected_output\": 6
let test_runner =
testbldr.test_runner_default()
|> testbldr.include_passing_tests_in_output(True)
|> testbldr.output_results_to_stdout()
let tests = {
use n <- list.map([1, 3, 5, 8, 9])
use <- testbldr.named(int.to_string(n) <> " is odd")
case n % 2 == 1 {
True -> testbldr.Pass
False -> testbldr.Fail(int.to_string(n) <> " is even, not odd")
}
]"
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
let assert Ok(test_cases) = test_cases_from_json(test_cases)
// Map over our tests cases to start transforming them
use test_case <- list.map(test_cases)
// Give each test the name specified in the json, to be printed
// properly on test run
use <- testbldr.named(test_case.name)
// The thing we're actually testing
double(test_case.input)
|> should.equal(test_case.expected_output)
},
)
}
/// Something to parse our JSON test cases into
type TestCase {
TestCase(name: String, input: Int, expected_output: Int)
}
/// Decode our list of test cases
fn test_cases_from_json(
input: String,
) -> Result(List(TestCase), json.DecodeError) {
let test_decoder =
dynamic.decode3(
TestCase,
dynamic.field("name", of: dynamic.string),
dynamic.field("input", of: dynamic.int),
dynamic.field("expected_output", of: dynamic.int),
)
}
json.decode(from: input, using: dynamic.list(test_decoder))
test_runner
|> testbldr.run(tests)
}
```
4 changes: 2 additions & 2 deletions gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "testbldr"
version = "0.3.0"
version = "1.0.0"
description = "A library for programatically building and running test cases"

# Fill out these fields if you intend to generate HTML documentation or publish
Expand All @@ -13,5 +13,5 @@ dev-dependencies = { gleeunit = "~> 1.0", gleam_json = "~> 0.7"}

[dependencies]
gleam_stdlib = "~> 0.32"
gleam_community_ansi = "~> 1.2"
gleam_erlang = "~> 0.23"
simplifile = "~> 1.0"
5 changes: 2 additions & 3 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
# You typically do not need to edit this file

packages = [
{ name = "gleam_community_ansi", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "8B5A9677BC5A2738712BBAF2BA289B1D8195FDF962BBC769569976AD5E9794E1" },
{ name = "gleam_community_colour", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "036C206886AFB9F153C552700A7A0B4D2864E3BC96A20C77E5F34A013C051BE3" },
{ name = "gleam_erlang", version = "0.23.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "C21CFB816C114784E669FFF4BBF433535EEA9960FA2F216209B8691E87156B96" },
{ name = "gleam_json", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "CB405BD93A8828BCD870463DE29375E7B2D252D9D124C109E5B618AAC00B86FC" },
{ name = "gleam_stdlib", version = "0.32.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "07D64C26D014CF570F8ACADCE602761EA2E74C842D26F2FD49B0D61973D9966F" },
{ name = "gleeunit", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "D3682ED8C5F9CAE1C928F2506DE91625588CC752495988CBE0F5653A42A6F334" },
{ name = "simplifile", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0BD6F0E7DA1A7E11D18B8AD48453225CAFCA4C8CFB4513D217B372D2866C501C" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

[requirements]
gleam_community_ansi = { version = "~> 1.2" }
gleam_erlang = { version = "~> 0.23" }
gleam_json = { version = "~> 0.7" }
gleam_stdlib = { version = "~> 0.32" }
gleeunit = { version = "~> 1.0" }
simplifile = { version = "~> 1.0" }
Loading

0 comments on commit 2474526

Please sign in to comment.