⭐️ A friendly language for building type-safe, scalable systems!

Add `assert` integration tests

authored by gearsco.de and committed by Louis Pilfold 3daf675a dd67180b

+1
test/assert/.gitignore
··· 1 + build
+4
test/assert/Makefile
··· 1 + .PHONY: test-all 2 + test-all: 3 + @echo test/assert 4 + @./run_tests.sh
+6
test/assert/gleam.toml
··· 1 + name = "project" 2 + version = "1.0.0" 3 + description = "A Gleam project" 4 + 5 + [dependencies] 6 + gleam_stdlib = ">= 0.58.0 and < 2.0.0"
+9
test/assert/manifest.toml
··· 1 + # This file was generated by Gleam 2 + # You typically do not need to edit this file 3 + 4 + packages = [ 5 + { name = "gleam_stdlib", version = "0.58.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "091F2D2C4A3A4E2047986C47E2C2C9D728A4E068ABB31FDA17B0D347E6248467" }, 6 + ] 7 + 8 + [requirements] 9 + gleam_stdlib = { version = ">= 0.58.0 and < 2.0.0" }
+54
test/assert/run_tests.sh
··· 1 + #/usr/bin/env sh 2 + 3 + set -eu 4 + 5 + should_succeed() { 6 + echo 7 + echo Running: "$@" 8 + 9 + EXIT_CODE=0 10 + cargo run -- $@ > /dev/null 2>&1 || EXIT_CODE=$? 11 + if [ $EXIT_CODE -ne 0 ] 12 + then 13 + echo ERROR: Command should have succeeded 14 + exit 1 15 + else 16 + echo Test Passed '(command run successfully)' 17 + fi 18 + } 19 + 20 + should_fail() { 21 + echo 22 + echo Running: "$@" 23 + 24 + EXIT_CODE=0 25 + cargo run -- $@ > /dev/null 2>&1 || EXIT_CODE=$? 26 + 27 + if [ $EXIT_CODE -eq 0 ] 28 + then 29 + echo ERROR: Command should have failed 30 + exit 1 31 + else 32 + echo Test Passed '(command errored as expected)' 33 + fi 34 + } 35 + 36 + all_targets() { 37 + $@ --target erlang 38 + $@ --target javascript --runtime nodejs 39 + $@ --target javascript --runtime deno 40 + $@ --target javascript --runtime bun 41 + } 42 + 43 + # Ensure the project builds correctly 44 + should_succeed build --target erlang 45 + should_succeed build --target javascript 46 + 47 + all_targets should_succeed run --module passing 48 + 49 + # Since a single failing `assert` will exit immediately, we must test each 50 + # failing case individually as separate modules. 51 + all_targets should_fail run --module failing1 52 + all_targets should_fail run --module failing2 53 + all_targets should_fail run --module failing3 54 + all_targets should_fail run --module failing4
+4
test/assert/src/failing1.gleam
··· 1 + pub fn main() { 2 + let x = True 3 + assert !x 4 + }
+5
test/assert/src/failing2.gleam
··· 1 + import gleam/int 2 + 3 + pub fn main() { 4 + assert int.is_even(47) 5 + }
+6
test/assert/src/failing3.gleam
··· 1 + import gleam/bool 2 + import gleam/result 3 + 4 + pub fn main() { 5 + assert bool.negate(False) && result.is_ok(Error(81)) 6 + }
+5
test/assert/src/failing4.gleam
··· 1 + import gleam/int 2 + 3 + pub fn main() { 4 + assert int.add(4, 5) > int.absolute_value(-11) 5 + }
+25
test/assert/src/passing.gleam
··· 1 + import gleam/bool 2 + import gleam/int 3 + import gleam/result 4 + 5 + /// All these assertions should succeed 6 + pub fn main() { 7 + let x = True 8 + assert x 9 + assert case x && False { 10 + True -> False 11 + False -> True 12 + } 13 + 14 + assert result.is_ok(Ok(10)) 15 + assert result.is_error(Error("Hello")) 16 + 17 + assert int.add(5, 6) == 11 18 + assert int.add(1, 4) < 9 19 + assert int.add(8, 12) >= 20 20 + assert bool.negate(False) && result.is_ok(Ok(42)) 21 + assert int.is_even(3) || int.is_even(4) 22 + 23 + // This should short-circuit so we don't panic here 24 + assert True || panic 25 + }