Skip to content

Commit

Permalink
Add CI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tesujimath committed Oct 16, 2024
1 parent 7506f98 commit 465924f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 64 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: test suite
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install Nix
uses: cachix/install-nix-action@v30
- name: Run tests
run: |
nix develop '.#ci' --command ./tests.elv
26 changes: 16 additions & 10 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,24 @@
};
in
{
devShells.default = with pkgs;
mkShell {
buildInputs = [
bashInteractive
devShells =
let
inherit (pkgs) bashInteractive elvish yq python3Packages mkShell;
ci-packages =
[
elvish
yq

# CLI tap consumer
python3Packages.tappy
python3Packages.pyyaml
python3Packages.more-itertools
# CLI tap consumer
python3Packages.tappy
python3Packages.pyyaml
python3Packages.more-itertools
];
in
{
default = mkShell { buildInputs = ci-packages ++ [ bashInteractive ]; };

yq
];
ci = mkShell { buildInputs = ci-packages; };
};
}
);
Expand Down
108 changes: 54 additions & 54 deletions tap.elv
Original file line number Diff line number Diff line change
@@ -1,71 +1,71 @@
fn -validate {|tests|
var tests-kind = (kind-of $tests)
if (not-eq $tests-kind list) {
fail 'tests must be list, found '$tests-kind
}
# Run all the tests in a tests suite.
#
# `tests` is a list of maps, where each map is a test.
# A test comprises a map with the following keys:
# - `d` - a string, the test name or description
# - `f` - a function of no arguments, outputing one or two results.
# The first result is a boolean, $true for success
# The optional second result is a map, which is converted to YAML and included as a TAP YAML block.
fn run {|tests|
fn -validate {|tests|
var tests-kind = (kind-of $tests)
if (not-eq $tests-kind list) {
fail 'tests must be list, found '$tests-kind
}

var i = 0
for test $tests {
# TAP numbers tests from 1
set i = (+ $i 1)
var i = 0
for test $tests {
# TAP numbers tests from 1
set i = (+ $i 1)

var test-kind = (kind-of $test)
if (not-eq $test-kind map) {
fail 'test '$i' must be map, found '$test-kind': '$test
}
var test-kind = (kind-of $test)
if (not-eq $test-kind map) {
fail 'test '$i' must be map, found '$test-kind': '$test
}

if (not (has-key $test d)) {
fail 'test '$i' missing `d`'
}
var d-kind = (kind-of $test[d])
if (not-eq $d-kind string) {
fail 'test '$i' `d` must be string, found '$d-kind
}
if (not (has-key $test d)) {
fail 'test '$i' missing `d`'
}
var d-kind = (kind-of $test[d])
if (not-eq $d-kind string) {
fail 'test '$i' `d` must be string, found '$d-kind
}

if (not (has-key $test f)) {
fail 'test '$i' missing `f`'
}
var f-kind = (kind-of $test[f])
if (not-eq $f-kind fn) {
fail 'test '$i' `f` must be fn, found '$f-kind
if (not (has-key $test f)) {
fail 'test '$i' missing `f`'
}
var f-kind = (kind-of $test[f])
if (not-eq $f-kind fn) {
fail 'test '$i' `f` must be fn, found '$f-kind
}
}
}
}

fn -tap-yaml {|doc|
echo ' ---'
put $doc | to-json | yq --yaml-output --sort-keys | from-lines | each {|line|
echo ' '$line
fn -tap-yaml {|doc|
echo ' ---'
put $doc | to-json | yq --yaml-output --sort-keys | from-lines | each {|line|
echo ' '$line
}
echo ' ...'
}
echo ' ...'
}

fn -tap-result {|i d ok &doc=[&]|
var status = (if $ok { put 'ok' } else { put 'not ok' })
echo $status' '$i' - '$d
fn -tap-result {|i d ok &doc=[&]|
var status = (if $ok { put 'ok' } else { put 'not ok' })
echo $status' '$i' - '$d

if (not-eq $doc [&]) {
-tap-yaml $doc
if (not-eq $doc [&]) {
-tap-yaml $doc
}
}
}

fn -tap-fail {|i d &doc=[&]|
-tap-result $i $d $false &doc=$doc
}
fn -tap-fail {|i d &doc=[&]|
-tap-result $i $d $false &doc=$doc
}

fn -tap-pass {|i d|
-tap-result $i $true $d
}
fn -tap-pass {|i d|
-tap-result $i $true $d
}

# Run all the tests in a tests suite.
#
# `tests` is a list of maps, where each map is a test.
# A test comprises a map with the following keys:
# - `d` - a string, the test name or description
# - `f` - a function of no arguments, outputing one or two results.
# The first result is a boolean, $true for success
# The optional second result is a map, which is converted to YAML and included as a TAP YAML block.
fn run {|tests|
-validate $tests

echo 'TAP version 13'
Expand Down
24 changes: 24 additions & 0 deletions tests.elv
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env elvish

use ./tap

# just test against canned output
var actual = (tap:run [[&d='simple fail' &f={ put $false [&expected=[&A=a] &actual=[&A=b]]}] [&d='easy pass' &f={ put $true }]] | slurp)
var expected = 'TAP version 13
1..2
not ok 1 - simple fail
---
actual:
A: b
expected:
A: a
...
ok 2 - easy pass
'

if (eq $actual $expected) {
echo "all tests passed"
} else {
put actual $actual expected $expected
fail "tests failed"
}

0 comments on commit 465924f

Please sign in to comment.