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

WIP: Adding some Tests #15

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Run tests


From inside the package run
```bash
typst watch tests/test_all.typ --root ..
```
53 changes: 53 additions & 0 deletions tests/test_all.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#import "../bytefield.typ": *



#let run_tests_global_config = true
#let run_bitfield_tests = true
#let run_bitheader_tests = true
#let run_annotation_tests = true




#if (run_tests_global_config) [
= Test global settings with `bf-config`

#show: bf-config.with(
row_height: 3em, // default is 2.5em
header_font_size: 0.8em, // default is 9pt
)

#locate(loc => [
row height: #_get_row_height(loc) \
header font size: #_get_header_font_size(loc)
])
]




#if (run_bitfield_tests) [
#import "test_bitfields.typ": *
= Test bitfields

#test_bitfield_all_colored
#test_bitfield_all
]

#if (run_bitheader_tests) [
#import "test_bitheader.typ": *
= Test bitheader
#test_bitheader_auto
#test_bitheader_bounds
#test_bitheader_custom_array

]


#if (run_annotation_tests) [
#import "test_annotations.typ": *
= Test annotations
#test_annotation_simple
#test_annotation_skipping
]
35 changes: 35 additions & 0 deletions tests/test_annotations.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#import "test_setup.typ": *

// Basic annotation test
#let test_annotation_simple = test(title: "simple", ```typst
#bytefield(
note(left)[0x00],
group(right,2)[group],
bytes(4)[some thing],

note(left)[0x04],
bytes(4)[some other thing],
)
```)

// skipping notes and mapping to row test
#let test_annotation_skipping = test(title: "skipping",```typst
#bytefield(
note(left, level: 1)[note 1],
note(left, level: 0)[note 0],
bytes(3,
fill: red.lighten(30%)
)[Test],
bytes(3)[Break],
bits(16,
fill: green.lighten(30%)
)[Fill],
bytes(12)[Addr],
note(left)[note 2 [l]],
note(right)[note 2 [r]],

padding(
fill: purple.lighten(40%)
)[Padding],
)
```)
34 changes: 34 additions & 0 deletions tests/test_bitfields.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#import "test_setup.typ": *

#let test_bitfield_all_colored = test(title: "colored version", ```typst
#bytefield(
bitheader: 8,
bytes(3,
fill: red.lighten(60%)
)[Test],
bytes(3, fill: yellow.lighten(60%))[Break],
bits(16,
fill: green.lighten(60%)
)[Fill],
bytes(8, fill: blue.lighten(60%))[Multirow],
bytes(2, fill: red.lighten(60%))[2 Bytes],
bytes(12, fill: orange.lighten(60%))[Addr],
padding(
fill: purple.lighten(60%)
)[Padding],
)
```)


#let test_bitfield_all = test(title: "black/white version",```typst
#bytefield(
bitheader: 8,
bytes(3)[Test],
bytes(3)[Break],
bits(16)[Fill],
bytes(8)[Multirow],
bytes(2)[2 Bytes],
bytes(12)[Addr],
padding[Padding],
)
```)
34 changes: 34 additions & 0 deletions tests/test_bitheader.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#import "test_setup.typ": *

#let default_fields = (byte[First], bytes(2)[Second], byte[Third],
bytes(2)[Fourth],bytes(2)[Fifth])

#let test_bitheader_auto = test(title: "auto",```typ
#bytefield(
bitheader: auto,
byte[First], bytes(2)[Second], byte[Third],
bytes(2)[Fourth],bytes(2)[Fifth],
)
```)


#let test_bitheader_bounds = test(title: "bounds",```typ
#bytefield(
bitheader: "bounds",
byte[First], bytes(2)[Second], byte[Third],
bytes(2)[Fourth],bytes(2)[Fifth],
)
```)

#let test_bitheader_custom_array = test(title: "custom array",```typ
#bytefield(
bitheader: (1,2,5,15,29,30,31),
byte[First], bytes(2)[Second], byte[Third],
bytes(2)[Fourth],bytes(2)[Fifth],
)
```)





31 changes: 31 additions & 0 deletions tests/test_setup.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#import "../bytefield.typ": *
#import "@preview/codelst:2.0.0": sourcecode


#let eval_bytefield(source) = eval(
source.text, mode:"markup",
scope: (
"bytefield" : bytefield,
"byte" : byte,
"bytes" : bytes,
"bit" : bit,
"bits" : bits,
"padding" : padding,
"flagtext" : flagtext,
"note" : note,
"group" : group,
)
)

#let test(title: "Test", columns:(1fr,1fr),source) = {
block[
== Test: #title
#grid(
columns:columns,
gutter: 10pt,
box(inset: (y: 1em),sourcecode(source)),
align(horizon,eval_bytefield(source))
)
#eval_bytefield(source)
]
}