Skip to content

Commit

Permalink
Adapt to standard library traits (#1)
Browse files Browse the repository at this point in the history
* adapt to numeric traits

* Use Package.juvix

* Run formatter

* Use release ref for juvix-stdlib

* Format project

* Test should call Debug.failWith only at the end of a failing suite

* Use == instead of Eq.eq

* Add a test suite

* Run tests on CI

* Print output on failure

* Add line buffering

* Remove Example.juvix

* Expand Example.juvix

* Run the Example as part of the test suite

* Add examples to the README

---------

Co-authored-by: Jonathan Cubides <[email protected]>
Co-authored-by: Paul Cadman <[email protected]>
  • Loading branch information
3 people authored Nov 21, 2023
1 parent a7ac74c commit 3ed8a39
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 21 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test Test

on:
push:
branches: [ main ]
pull_request:
workflow_dispatch:

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: checkout code
uses: actions/checkout@v3

- name: Download latest nightly Juvix binary
uses: jaxxstorm/[email protected]
with:
repo: anoma/juvix-nightly-builds
cache: enable

- name: Run tests
run: |
make test
26 changes: 17 additions & 9 deletions Example.juvix
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ module Example;

import Stdlib.Prelude open;
import Test.JuvixUnit open;
import Stdlib.Data.Nat.Ord open;

headMay {A} : List A -> Maybe A := head nothing ∘ map just;

tests : List Test :=
[testCase
"1 == 1"
(assertEqual "1 /= 1" 1 1); testCase
"not (1 == 1)"
(assertFalse "1 == 1" (1 == 1))];
[ testCase "1 == 1" (assertEqual "1 /= 1" 1 1)
; testCase "[1] == [1]" (assertEqual "[1] /= [1]" [1] [1])
; testCase
"length [1] == 1"
(assertTrue "length [1] /= 1" (length [1] == 1))
; testCase
"headMay [] is nothing"
(assertNothing
λ {xs := "expected nothing, got: " ++str Show.show xs}
(headMay {Nat} []))
; testCase
"headMay [1] is just"
(assertJust "expected just, got nothing" (headMay [1]))
];

main : IO :=
runTestSuite
(testSuite (name := "Example"; tests := tests));
main : IO := runTestSuite (testSuite "Example" tests);
18 changes: 16 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
all: test

build/TestFail: tests/TestFail.juvix $(wildcard ./**/*.juvix)
@mkdir -p build/
juvix compile -o build/TestFail tests/TestFail.juvix

build/TestPass: tests/TestPass.juvix $(wildcard ./**/*.juvix)
@mkdir -p build/
juvix compile -o build/TestPass tests/TestPass.juvix

build/Example: Example.juvix $(wildcard ./**/*.juvix)
@mkdir -p build/
juvix compile -o build/Example Example.juvix

.PHONY : test
test: build/Example
.PHONY : example
example: build/Example
./build/Example

.PHONY : test
test: build/TestFail build/TestPass build/Example
tests/check_output.sh "./build/TestPass" expect_success "OK,Suite passed"
tests/check_output.sh "./build/TestFail" expect_fail "FAIL,OK,Suite failed"
tests/check_output.sh "./build/Example" expect_success "OK,Suite passed"

.PHONY: clean-build
clean-build:
@rm -rf build/
Expand Down
9 changes: 9 additions & 0 deletions Package.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Package;

import PackageDescription.V1 open;

package : Package :=
defaultPackage
{name := "test";
version := mkVersion 0 7 0;
dependencies := [github "anoma" "juvix-stdlib" "v0.0.1"]};
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ $ ./Example
Test suite 'Example'
1 == 1 OK
All tests from test suite 'Example' complete
Suite passed
```

For more examples see [Example.juvix](Example.juvix) or the [test suite](./tests).
6 changes: 3 additions & 3 deletions Test/JuvixUnit.juvix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Test.JuvixUnit;

import Stdlib.Prelude open;
import Stdlib.Prelude open hiding {fail};

import Stdlib.Debug.Fail as Fail;

Expand Down Expand Up @@ -49,7 +49,7 @@ runTestSuite (suite : TestSuite) : IO :=
++str "' complete")
>> if
(anyFail suite)
(Fail.fail "Suite failed")
(Fail.failwith "Suite failed")
(printStringLn "Suite passed");

failWhen (msg : String) (b : Bool) : Assertion :=
Expand All @@ -69,4 +69,4 @@ assertNothing {A} (mkMsg : A -> String)
: Maybe A -> Assertion := maybe pass (fail ∘ mkMsg);

assertEqual {A} {{Eq A}} (msg : String) (a1 a2 : A)
: Assertion := failUnless msg (Eq.eq a1 a2);
: Assertion := failUnless msg (a1 == a2);
9 changes: 9 additions & 0 deletions juvix.lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was autogenerated by Juvix version 0.5.4.
# Do not edit this file manually.

dependencies:
- git:
name: anoma_juvix-stdlib
ref: f68b0614ad695eaa13ead42f3466e0a78219f826
url: https://github.com/anoma/juvix-stdlib
dependencies: []
7 changes: 0 additions & 7 deletions juvix.yaml

This file was deleted.

4 changes: 4 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.juvix-build
.history
deps/
build/
8 changes: 8 additions & 0 deletions tests/Package.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Package;

import PackageDescription.V1 open;

package : Package :=
defaultPackage
{name := "tests";
dependencies := [defaultStdlib; path "../"]};
12 changes: 12 additions & 0 deletions tests/TestFail.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module TestFail;

import Stdlib.Prelude open;
import Test.JuvixUnit open;

main : IO :=
runTestSuite
(testSuite
(name := "TestFail";
tests := [ testCase "2 == 1" (assertEqual "2 /= 1" 2 1)
; testCase "1 == 1" (assertEqual "1 /= 1" 1 1)
]));
10 changes: 10 additions & 0 deletions tests/TestPass.juvix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module TestPass;

import Stdlib.Prelude open;
import Test.JuvixUnit open;

main : IO :=
runTestSuite
(testSuite
(name := "TestPass";
tests := [testCase "1 == 1" (assertEqual "1 /= 1" 1 1)]));
44 changes: 44 additions & 0 deletions tests/check_output.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

if [ "$#" -lt 3 ]; then
echo "Usage: $0 <command> <expect_success> <regex_list>"
exit 1
fi

# The command under test
COMMAND="$1"

# Whether a success (0 exit code) is expected
EXPECT_SUCCESS="$2"

# Regular expressions passed as a single string, split by a delimiter (e.g., comma)
IFS=',' read -r -a regex_list <<< "$3"

# Execute the command and store both stdout and stderr in the output
output=$(stdbuf -oL $COMMAND 2>&1)
exit_code=$?

# Check the exit code
if [[ "$EXPECT_SUCCESS" == "expect_success" && $exit_code -ne 0 ]]; then
echo "Expected $COMMAND to succeed but it failed with exit code $exit_code."
exit 1
elif [[ ! "$EXPECT_SUCCESS" == "expect_success" && $exit_code -eq 0 ]]; then
echo "Expected $COMMAND to fail but it succeeded."
exit 1
fi


failed=0

for regex in "${regex_list[@]}"; do
if [[ ! $output =~ $regex ]]; then
echo "$COMMAND: No match found for regex: $regex"
echo "output:"
echo "$output"
failed=1
fi
done

if [ $failed -eq 1 ]; then
exit 1
fi
13 changes: 13 additions & 0 deletions tests/juvix.lock.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This file was autogenerated by Juvix version 0.5.4.
# Do not edit this file manually.

dependencies:
- path: .juvix-build/stdlib/
dependencies: []
- path: ../
dependencies:
- git:
name: anoma_juvix-stdlib
ref: f68b0614ad695eaa13ead42f3466e0a78219f826
url: https://github.com/anoma/juvix-stdlib
dependencies: []

0 comments on commit 3ed8a39

Please sign in to comment.