Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic tests #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Automatic tests
on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
pull_request:
branches: ["main"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:


jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: imagemagick cargo
version: 1.0
- name: Install just from crates.io
uses: baptiste0928/cargo-install@v2
with:
crate: just
- uses: yusancky/setup-typst@v2
id: setup-typst
with:
version: 'v0.6.0'
- run: |
just test
29 changes: 29 additions & 0 deletions .github/workflows/update-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Update Tests images
on:
workflow_dispatch:

jobs:
update-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: imagemagick cargo
version: 1.0
- name: Install just from crates.io
uses: baptiste0928/cargo-install@v2
with:
crate: just
- uses: yusancky/setup-typst@v2
id: setup-typst
with:
version: 'v0.6.0'
- run: |
just update-test
- name: Archive production artifacts
uses: actions/upload-artifact@v3
with:
name: tests
path: |
tests
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
res*.png
diff*.png
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ Here is a template:
```
Also see `examples/` for some examples.

## Testing

This package comes with some unit tests under the `tests` directory.
To run all tests you can run the `just test` target.

You need to have ImageMagick installed on your system, which is needed for image comparison.

### Windows
If you are using the [Chocolatey](https://chocolatey.org/) package manager, you can install imagemagick using `choco install imagemagick`.
Otherwise download and install a matching package from the [ImageMagick](https://imagemagick.org/script/download.php) website.

## Requirements for First Stable Release
- [ ] Reference manual (doesn't have to inclued tutorials, just a list of currently supported components)
- [ ] Tests
Expand Down
7 changes: 3 additions & 4 deletions circuitypst.typ
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#import "components.typ"
#import "utils.typ"

#import "../typst-canvas/draw.typ": *
#import "../typst-canvas/coordinate.typ"
#import "../typst-canvas/vector.typ"
#import "@preview/cetz:0.0.1"
#import cetz.draw: fill, stroke, group, anchor, rotate, set-origin, line, content, move-to

#let canvas-fill = fill
#let canvas-stroke = stroke
Expand All @@ -17,7 +16,7 @@
#let node(component, position, label: none, name: none, anchor: none, fill: auto, stroke: auto, ..options) = {
assert(component in components.node, message: "Component '" + component + "' is unknown")
group(name: name, anchor: anchor, {
import "../typst-canvas/draw.typ": anchor
import cetz.draw: anchor

if fill != auto {
canvas-fill(fill)
Expand Down
4 changes: 3 additions & 1 deletion components.typ
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import "../typst-canvas/draw.typ": *
#import "@preview/cetz:0.0.1"

#import cetz.draw: line, circle, fill, rect

#import "parts.typ"
#import "utils.typ": anchors
Expand Down
Binary file modified examples/current-shunt.pdf
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/current-shunt.typ
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#import "../../typst-canvas/canvas.typ": canvas
#import "@preview/cetz:0.0.1"

#lorem(30)

#figure(
canvas(length: 1cm, debug: false, {
import "../../typst-canvas/draw.typ": *
import "../circuitypst.typ": node, to
cetz.canvas(length: 1cm, debug: false, {
import cetz.draw: line
import "@local/circuitypst:0.0.1": node, to

to("isourceAM", (0,0), (0,3), label: $I_0$, v: h(0.5em) + $V_0$)
to("short", (), (2,3), i: $I_0$,)
Expand Down
Binary file modified examples/exclusive-or.pdf
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/exclusive-or.typ
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#import "../../typst-canvas/canvas.typ": canvas
#import "@preview/cetz:0.0.1"

#lorem(30)

#figure(
canvas({
import "../../typst-canvas/draw.typ": *
import "../circuitypst.typ": node
cetz.canvas({
import cetz.draw: line, content
import "@local/circuitypst:0.0.1": node

node("nand gate", (0,1), name: "g1")
content("g1.out", [#h(0.5em) Q], anchor: "left")
Expand Down
Binary file modified examples/half-adder.pdf
Binary file not shown.
8 changes: 4 additions & 4 deletions examples/half-adder.typ
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#import "../../typst-canvas/canvas.typ": canvas
#import "@preview/cetz:0.0.1"

#lorem(30)

#figure(
canvas(length: 1cm, debug: false, {
import "../../typst-canvas/draw.typ": *
import "../circuitypst.typ": node
cetz.canvas(length: 1cm, debug: false, {
import cetz.draw: line, content
import "@local/circuitypst:0.0.1": node

node("and gate", (0,0), name: "g1")
content("g1.out", h(0.5em)+[Carry], anchor: "left")
Expand Down
10 changes: 10 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Local Variables:
# mode: makefile
# End:
test_dir := "./tests"

test:
./scripts/test test

update-test:
./scripts/test update
16 changes: 9 additions & 7 deletions parts.typ
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#import "../typst-canvas/draw.typ": *
#import "@preview/cetz:0.0.1"
#import cetz.draw: merge-path, arc, line, circle

#import "utils.typ": anchors
#let and-gate-body = {
merge-path(close: true, {
arc((0,0), -90deg, 90deg, radius: 0.5, name: "curve", anchor: "origin")
arc((0,0), start: -90deg, stop: 90deg, radius: 0.5, name: "curve", anchor: "origin")
line(
(0, 0.5),
(-0.5, 0.5),
Expand All @@ -27,10 +29,10 @@

#let or-gate-body = {
merge-path(close: true, {
arc((0.5, 0), -90deg, -30deg, anchor: "end", name: "bcurve")
arc((0.5,0), 30deg, 90deg, anchor: "start", name: "tcurve")
arc((0.5, 0), start: -90deg, stop: -30deg, anchor: "end", name: "bcurve")
arc((0.5,0), start: 30deg, stop: 90deg, anchor: "start", name: "tcurve")
line("tcurve.end", (-0.5, 0.5))
arc((), -30deg, 30deg, anchor: "end")
arc((), start: 30deg, stop: -30deg, anchor: "start")
})

// x coordinate of where the input legs touch the body of the gate
Expand Down Expand Up @@ -62,7 +64,7 @@
}

#let xor-bar = {
arc((-0.6, -0.5), -30deg, 30deg)
arc((-0.6, -0.5), start: -30deg, stop: 30deg)
anchors((
"ibin 1": "bin 1",
"ibin 2": "bin 2",
Expand All @@ -75,4 +77,4 @@

#let logic-gate-legs = for a in ("in 1", "in 2", "out") {
line("b" + a, a)
}
}
142 changes: 142 additions & 0 deletions scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/bin/env bash
set -eu
set -o pipefail

DIR="$(dirname "${BASH_SOURCE[0]}")"

MODE="${1:-test}";
if (( $# > 1)); then shift; fi

FLAG_INSTALL=0

while :; do
case "${1:-}" in
'--install') FLAG_INSTALL=1 ;;
*) break
esac
shift
done


TYPST_VERSION="v0.6.0"
TYPST_BASE_URL="https://github.com/typst/typst/releases/download"
TYPST_ARCHIVE="typst-x86_64-unknown-linux-musl.tar.xz"

TYPST_ROOT="$(realpath "$DIR/..")"
TEST_ROOT="$(realpath "$DIR/../tests")"

if hash magick 2>/dev/null; then
MAGICK_COMPARE="magick compare"
elif hash compare 2>/dev/null; then
MAGICK_COMPARE="compare"
else
>&2 echo "Could not find 'magick' nor 'compare' binary. Make sure you have image magick installed and in your PATH."
exit 1
fi


function install_typst()
{
if [[ "$OSTYPE" != "linux"* ]]; then
>&2 echo "Automatic installation of typst on a non linux system is currently unsupported."
exit 1
fi

#TMP="$(mktemp -d)"
TMP="${TMP:-/tmp}/typst-${TYPST_VERSION}"
if mkdir -p "$TMP" 2> /dev/null ; then
local PKG="${TMP}/typst.tar.xz"

echo "Installing typst from $TYPST_BASE_URL/$TYPST_ARCHIVE"
wget "${TYPST_BASE_URL}/${TYPST_VERSION}/${TYPST_ARCHIVE}" \
--quiet \
-O "$PKG"
mkdir -p "${TMP}/typst"
tar -xf "$PKG" -C "${TMP}/typst" --strip-components=1
rm "$PKG"
fi

PATH="${TMP}/typst/:$PATH"
export PATH
}

if [[ "$FLAG_INSTALL" == "1" ]]; then
install_typst
fi

if ! hash typst; then
>&2 echo "Could not find 'typst' binary. Run this script with the --install argument to temporarily install typst."
exit 1
fi

function img_compare()
{
$MAGICK_COMPARE -metric rmse "$1" "$2" null: 2>&1 | cut -d\ -f1
}

function update_test_ref()
{
(
cd "$1"
local NAME
NAME="$(basename "$1")"

echo "[UPDATING] ${NAME}"

typst --root "$TYPST_ROOT" compile test.typ "ref{n}.png"
)
}

function run_test()
{
(
cd "$1"
local NAME
NAME="$(basename "$1")"

echo "[TEST] ${NAME} ..."

if [[ ! -f test.typ ]]; then echo "Missing file 'test.typ'!"; exit 1; fi

typst --root "$TYPST_ROOT" compile test.typ "res{n}.png"


for res_file in res*.png; do
if [[ $res_file =~ res([0-9]+)\.png ]]; then
number="${BASH_REMATCH[1]}"
ref_file="ref${number}.png"
diff_file="diff${number}.png"

if [ -f "$ref_file" ]; then
echo "Comparing $res_file with $ref_file..."
if [[ $(img_compare $res_file $ref_file) != 0 ]] ; then
$MAGICK_COMPARE -compose src $res_file $ref_file $diff_file || true
echo "[FAIL] see $(pwd)/$diff_file for differences"
exit 1
fi
else
echo "File $ref_file not found for $res_file."
exit 1
fi
fi
done

echo "[ OK]"
)
}

echo "Typst: $(typst --version)"

find "$TEST_ROOT" -type d | \
while read -r test
do
if [[ "$test" == "$TEST_ROOT" ]]; then continue; fi

if [[ "$MODE" == "test" ]]; then
run_test "$test"
elif [[ "$MODE" == "update" ]]; then
update_test_ref "$test"
else
echo "Unknown mode '$MODE'"; exit 1
fi
done
Binary file added tests/circuits-current-shunt/ref1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tests/circuits-current-shunt/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#set page(width: auto, height: auto, margin: 0.5cm)
#import "@preview/cetz:0.0.1"

#cetz.canvas(length: 1cm, debug: false, {
import cetz.draw: line
import "../../circuitypst.typ": node, to

to("isourceAM", (0,0), (0,3), label: $I_0$, v: h(0.5em) + $V_0$)
to("short", (), (2,3), i: $I_0$,)
to("R", (), (2, 0), label: [$R_1$], poles: "*-*", i: (">_", $i_1$))
line((2,3), (4,3))
to("R", (), (4, 0), label: [$R_2$], name: "R", i: (">_", $i_2$))
line((), (0,0))
})
Binary file added tests/circuits-exclusive-or/ref1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading