Packages

A library for programatically building and running test cases

Current section

6 Versions

Jump to

Compare versions

15 files changed
+522 additions
-248 deletions
  @@ -9,77 +9,29 @@ automatically discovered and run.
9 9 Other times you may want to read test cases in from files, json responses, etc.
10 10 This library is for the second case.
11 11
12 + # Example
13 +
12 14 ```gleam
13 15 import testbldr
14 - import testbldr/should
15 16 import gleam/list
16 17 import gleam/int
17 - import gleam/json
18 - import gleam/dynamic
19 18
20 19 pub fn main() {
20 + let test_runner =
21 + testbldr.test_runner_default()
22 + |> testbldr.include_passing_tests_in_output(True)
23 + |> testbldr.output_results_to_stdout()
21 24
22 - // Lets say you have some test
23 - // cases in a json file. Here's an example of building a test suite
24 - // from them
25 -
26 - // The function we're testing
27 - let double = fn(x) { x * 2 }
28 -
29 - // Test cases given as json
30 - let test_cases =
31 - "[
32 - {
33 - \"name\": \"1 doubled is 2\",
34 - \"input\": 1,
35 - \"expected_output\": 2
36 - },
37 - {
38 - \"name\": \"3 doubled is 6\",
39 - \"input\": 3,
40 - \"expected_output\": 6
25 + let tests = {
26 + use n <- list.map([1, 3, 5, 8, 9])
27 + use <- testbldr.named(int.to_string(n) <> " is odd")
28 + case n % 2 == 1 {
29 + True -> testbldr.Pass
30 + False -> testbldr.Fail(int.to_string(n) <> " is even, not odd")
41 31 }
42 - ]"
32 + }
43 33
44 - testbldr.demonstrate(
45 - that: "Our doubling function works",
46 - with:
47 - {
48 - // Decode our tests cases from the JSON. If it doesn't decode
49 - // correctly we crash the whole thing because it's a test suite
50 - let assert Ok(test_cases) = test_cases_from_json(test_cases)
51 -
52 - // Map over our tests cases to start transforming them
53 - use test_case <- list.map(test_cases)
54 -
55 - // Give each test the name specified in the json, to be printed
56 - // properly on test run
57 - use <- testbldr.named(test_case.name)
58 -
59 - // The thing we're actually testing
60 - double(test_case.input)
61 - |> should.equal(test_case.expected_output)
62 - },
63 - )
64 - }
65 -
66 - /// Something to parse our JSON test cases into
67 - type TestCase {
68 - TestCase(name: String, input: Int, expected_output: Int)
69 - }
70 -
71 - /// Decode our list of test cases
72 - fn test_cases_from_json(
73 - input: String,
74 - ) -> Result(List(TestCase), json.DecodeError) {
75 - let test_decoder =
76 - dynamic.decode3(
77 - TestCase,
78 - dynamic.field("name", of: dynamic.string),
79 - dynamic.field("input", of: dynamic.int),
80 - dynamic.field("expected_output", of: dynamic.int),
81 - )
82 -
83 - json.decode(from: input, using: dynamic.list(test_decoder))
34 + test_runner
35 + |> testbldr.run(tests)
84 36 }
85 37 ```
  @@ -1,5 +1,5 @@
1 1 name = "testbldr"
2 - version = "0.3.0"
2 + version = "1.0.0"
3 3 description = "A library for programatically building and running test cases"
4 4
5 5 # Fill out these fields if you intend to generate HTML documentation or publish
  @@ -13,5 +13,5 @@ dev-dependencies = { gleeunit = "~> 1.0", gleam_json = "~> 0.7"}
13 13
14 14 [dependencies]
15 15 gleam_stdlib = "~> 0.32"
16 - gleam_community_ansi = "~> 1.2"
17 16 gleam_erlang = "~> 0.23"
17 + simplifile = "~> 1.0"
  @@ -1,6 +1,6 @@
1 1 {<<"name">>, <<"testbldr">>}.
2 2 {<<"app">>, <<"testbldr">>}.
3 - {<<"version">>, <<"0.3.0">>}.
3 + {<<"version">>, <<"1.0.0">>}.
4 4 {<<"description">>, <<"A library for programatically building and running test cases">>}.
5 5 {<<"licenses">>, [<<"Apache-2.0">>]}.
6 6 {<<"build_tools">>, [<<"gleam">>]}.
  @@ -13,27 +13,26 @@
13 13 {<<"optional">>, false},
14 14 {<<"requirement">>, <<"~> 0.23">>}
15 15 ]},
16 + {<<"simplifile">>, [
17 + {<<"app">>, <<"simplifile">>},
18 + {<<"optional">>, false},
19 + {<<"requirement">>, <<"~> 1.0">>}
20 + ]},
16 21 {<<"gleam_stdlib">>, [
17 22 {<<"app">>, <<"gleam_stdlib">>},
18 23 {<<"optional">>, false},
19 24 {<<"requirement">>, <<"~> 0.32">>}
20 - ]},
21 - {<<"gleam_community_ansi">>, [
22 - {<<"app">>, <<"gleam_community_ansi">>},
23 - {<<"optional">>, false},
24 - {<<"requirement">>, <<"~> 1.2">>}
25 25 ]}
26 26 ]}.
27 27 {<<"files">>, [
28 28 <<"README.md">>,
29 29 <<"gleam.toml">>,
30 - <<"include/testbldr@pieces_Fail.hrl">>,
31 - <<"include/testbldr@pieces_Test.hrl">>,
30 + <<"include/testbldr_Fail.hrl">>,
31 + <<"include/testbldr_Test.hrl">>,
32 + <<"include/testbldr_TestRunner.hrl">>,
32 33 <<"src/testbldr.app.src">>,
33 34 <<"src/testbldr.erl">>,
34 35 <<"src/testbldr.gleam">>,
35 - <<"src/testbldr/pieces.gleam">>,
36 36 <<"src/testbldr/should.gleam">>,
37 - <<"src/testbldr@pieces.erl">>,
38 37 <<"src/testbldr@should.erl">>
39 38 ]}.
  @@ -1 +0,0 @@
1 - -record(fail, {msg :: binary()}).
  @@ -1,4 +0,0 @@
1 - -record(test, {
2 - name :: binary(),
3 - test_function :: fun(() -> testbldr@pieces:test_outcome())
4 - }).
Loading more files…