Skip to content

Commit

Permalink
[add] tests (Mc-Zen#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-Zen authored Nov 24, 2024
1 parent fa70b28 commit 445cabf
Show file tree
Hide file tree
Showing 26 changed files with 280 additions and 31 deletions.
36 changes: 30 additions & 6 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
name: Tests
on: push
name: tests
on:
push:
branches: [ main ]
paths:
- src/**
- tests/**
pull_request:
branches: [ main ]

jobs:
build_typst_documents:
tests:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Typst
uses: lvignoli/typst-action@main

- name: Probe runner package cache
uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: cargo
version: 1.0

- name: Install typst-test from github
uses: baptiste0928/[email protected]
with:
source_file: tests/test_tidy.typ
crate: typst-test
git: https://github.com/tingerrr/typst-test.git
tag: ci-semi-stable

- name: Setup typst
uses: yusancky/setup-typst@v2
with:
version: 'v0.12.0'

- name: Run test suite
run: typst-test run
7 changes: 4 additions & 3 deletions src/helping.typ
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#import "show-module.typ": show-module

#let help-box(content) = {
set text(size: .9em)
block(
above: 1em,
inset: 1em,
stroke: rgb("#AAA"),
fill: rgb("#F5F5F544"),
text(size: 2em, [? #smallcaps("help")#h(1fr)?]) + content
fill: rgb("#F5F5F544"), {
text(size: 1.8em, [? #smallcaps("help")#h(1fr)?])
text(.9em, content)
}
)
}

Expand Down
Binary file added tests/helping/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 25 additions & 12 deletions tests/test_helping.typ → tests/helping/test.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import "/src/tidy.typ"

#set page(width: 10cm, height: auto, margin: 2pt)

#let heymath = ```
#import "vector.typ": *
Expand All @@ -20,15 +21,20 @@
#let matrix = ```
#import "solve.typ"
/// - n (int): dimension
#let id(n)
///
#let id(
/// dimension -> int
n
)
```.text
#let solve = ```
///
#let solve(n)
```.text
#let error = highlight
#let help(..args) = {
let namespace = (
Expand All @@ -39,17 +45,16 @@
tidy.generate-help(
namespace: namespace,
package-name: "heymath",
onerror: msg => block(msg)
onerror: msg => error(msg)
)(..args)
}
#help("pi-squared")
#let assert-not-failed(help-result) = {
assert.eq(
help-result.child.body.children.at(1)
.child.children.first().fill,
rgb("#d8dbed44")
let body = help-result.body.children.at(1).child
assert(
body.func() != highlight,
)
}
Expand All @@ -60,35 +65,43 @@
#assert-not-failed(help("vec-subtract"))
#assert-not-failed(help("matrix.id"))
#assert-not-failed(help("matrix.solve.solve"))
// #assert-not-failed(help("matrix.id(n)"))
#assert-not-failed(help("matrix.id(n)"))
#help("matrix.id(n)")
// Invalid definition
#assert.eq(
help("ma"),
tidy.helping.help-box(block("The package `heymath` contains no (documented) definition `ma`"))
tidy.helping.help-box(
error("The package `heymath` contains no (documented) definition `ma`")
)
)
// Invalid submodule
#assert.eq(
help("matrixs.id"),
tidy.helping.help-box(block("The package `heymath` contains no module `matrixs`"))
tidy.helping.help-box(
error("The package `heymath` contains no module `matrixs`")
)
)
// Invalid submodule
#assert.eq(
help("matrix.id(aaaa)"),
tidy.helping.help-box(block("The function `matrix.id` has no parameter `aaaa`"))
tidy.helping.help-box(
error("The function `matrix.id` has no parameter `aaaa`")
)
)
// Invalid submodule
#assert.eq(
help("cos(aaaa)"),
tidy.helping.help-box(block("The function `cos` has no parameter `aaaa`"))
tidy.helping.help-box(
error("The function `cos` has no parameter `aaaa`")
)
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file added tests/new-parser/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions tests/parser/test.typ → tests/new-parser/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#include "argument-description.typ"
#include "argument-type.typ"
#include "currying.typ"

#set page(width: auto, height: auto, margin: 2pt)
File renamed without changes.
211 changes: 211 additions & 0 deletions tests/old-parser/parse-module.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#import "/src/tidy.typ": *
#import "/src/old-parser.typ": *
#import "/src/utilities.typ": *

#let eval-string(string) = eval-docstring(string, (scope: (:)))

#let parse-module = parse-module.with(old-syntax: true)

#{
let code = ```
/// - alpha (str):
/// - beta (length):
// / - ..children (any):
#let z(alpha, beta: 2pt, ..children) = {}
```
let k = parse-module(code.text)
}

// Test reference-matcher
#{
let matches = " @@func".matches(reference-matcher)
assert.eq(matches.len(), 1)
assert.eq(matches.at(0).captures, ("func",))

let matches = " @@func()".matches(reference-matcher)
assert.eq(matches.len(), 1)
assert.eq(matches.at(0).captures, ("func()",))

let matches = " ()@@@@my-func12-bliblablub @@ @@a".matches(reference-matcher)
assert.eq(matches.len(), 2)
assert.eq(matches.at(0).captures, ("my-func12-bliblablub",))
assert.eq(matches.at(1).captures, ("a",))
}


// Test argument-documentation-matcher
#{
let matches = " \t\n\t /// - my-arg1 (string, content): desc".matches(argument-documentation-matcher)
assert.eq(matches.len(), 1)
assert.eq(matches.at(0).captures, ("my-arg1","string, content", "desc"))

// multiline argument description
let matches = "/// - arg (type): desc\n\tasd\n-3$234$".matches(argument-documentation-matcher)
assert.eq(matches.len(), 1)
assert.eq(matches.at(0).captures, ("arg", "type", "desc"))
}


// Basic tests
#{
let a = ```
/// Func
#let a-3_56C() = {}
```.text
let result = parse-module(a)
assert.eq(result.functions.len(), 1)
assert.eq(result.functions.at(0).name, "a-3_56C")
assert.eq(eval-string(result.functions.at(0).description), [Func])
assert.eq(result.functions.at(0).return-types, none)
}


#{
let a = ```
#{
/// Func
///
let a() = {}
}
```.text
let result = parse-module(a)
assert.eq(result.functions.len(), 1)
assert.eq(result.functions.at(0).name, "a")
assert.eq(eval-string(result.functions.at(0).description), [Func])
assert.eq(result.functions.at(0).return-types, none)
}



// Parameters and defaults
#{
let a = ```
/// Func
#let a(p1, p2: 2, p3: (), p4: ("entries": ())) = {}
```.text
let result = parse-module(a)
assert.eq(result.functions.len(), 1)
let f0 = result.functions.at(0)

assert.eq(f0.name, "a")
assert.eq(eval-string(f0.description), [Func])
assert.eq(f0.args.len(), 4)
assert.eq(f0.args.p1, (:))
assert.eq(f0.args.p2, (default: "2"))
assert.eq(f0.args.p3, (default: "()"))
assert.eq(f0.args.p4, (default: "(\"entries\": ())"))
assert.eq(f0.return-types, none)
}




// Parameter and return types
#{
let a = ```
/// Func
/// - p1 (string): a param $a$
/// - p2 (boolean, function): a param $b$
/// Oh yes
/// - p3 (string):
/// -> content, integer
#let a(p1, p2: 2, p3: (), p4: ("entries": ())) = {}
```.text
let result = parse-module(a)
assert.eq(result.functions.len(), 1)
let f0 = result.functions.at(0)

assert.eq(f0.name, "a")
assert.eq(eval-string(f0.description), [Func])
assert.eq(f0.args.len(), 4)
assert.eq(f0.args.p1.types, ("string",))
assert.eq(eval-string(f0.args.p1.description), [a param $a$])
assert.eq(f0.args.p2.default, "2")
assert.eq(eval-string(f0.args.p2.description), [a param $b$ Oh yes])
assert.eq(f0.args.p2.types, ("boolean", "function"))
assert.eq(f0.return-types, ("content", "integer"))
}





// // Ignore args that are not in the argument list
// #{
// let a = ```
// /// Func
// /// - bar (content): asd
// #let a(bar) = {}
// ```.text
// let result = parse-module(a)
// assert.eq(result.functions.len(), 1)
// assert.eq(result.functions.at(0).args.len(), 1)
// }
// Ignore interrupted docstring
#{
let a = ```
/// Func
// a
#let a()
```.text
let result = parse-module(a)
assert.eq(result.functions.len(), 0)
}


// Ensure that wide unicode characters don't disturb line calculation for error messages
#{
let code = ```
// ⇒⇐ßáà€

/// foo
/// >>> 2 == 2
#let f()
```

let result = show-module(parse-module(code.text))
}

// Curried functions
#{
let code = ```
/// - foo (content): Something.
/// - bar (boolean): A boolean.
/// -> content
#let myfunc(foo, bar: false) = strong(foo)

/// My curried function.
/// -> content
#let curried = myfunc.with(bar: true, 2)
```

let result = parse-module(code.text)
let f1 = result.functions.at(0)
let f2 = result.functions.at(1)
assert.eq(f1.name, "myfunc")
assert.eq(f2.parent.name, "myfunc")
assert.eq(f2.parent.pos, ("2",))
assert.eq(f2.parent.named, (bar: "true"))
// assert.eq(f2.args.len(), 1)
// assert.eq(f2.args.bar, (
// default: "true",
// description: "A boolean.",
// types: ("boolean",),
// ))
}

// module description
#{
let code = ```
// License

/// This is a module

a
```

let result = parse-module(code.text)
assert.eq(result.description, "This is a module")
}
Binary file added tests/old-parser/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions tests/old-parser/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#set page(width: auto, height: auto, margin: 2pt)

#include "parse-argument-list.typ"
#include "parse-module.typ"
Binary file removed tests/parser/ref/1.png
Binary file not shown.
Binary file modified tests/show-example/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/show-example/ref/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/show-example/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#pagebreak()

#show-example(`Fit that code in a tiny space`)
#almost-default-show-example(`Fit that code in a tiny space`)

#pagebreak()

Expand Down
Loading

0 comments on commit 445cabf

Please sign in to comment.