Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Postgres Backup S3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Circles
Postgres Backup S3
Commits
54341518
Commit
54341518
authored
1 year ago
by
Elliott Shugerman
Browse files
Options
Downloads
Patches
Plain Diff
goin big with da test lib
parent
b5704e78
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/lib.janet
+116
-31
116 additions, 31 deletions
test/lib.janet
with
116 additions
and
31 deletions
test/lib.janet
+
116
−
31
View file @
54341518
(
import
spork/path
)
(
import
spork/generators
)
(
def
divider-heavy
"================================================================================"
)
(
def
divider-light
"--------------------------------------------------------------------------------"
)
(
var
tests
:private
@[])
(
defn
it
"Defines a test"
[
description
thunk
]
(
array/push
tests
{
:description
description
:thunk
thunk
}))
# no reason to use a dynamic binding here since it's global anyway (?)
(
def
toplevel
:private
@[])
(
defn
group
[
description
thunk
]
(
with-dyns
[
:children
@[]
:before
nil
:before-each
nil
:after
nil
:after-each
nil
]
(
thunk
)
(
array/push
toplevel
{
:type
'
group
:description
description
:children
(
dyn
:children
)
:before
(
dyn
:before
)
:before-each
(
dyn
:before-each
)
:after
(
dyn
:after
)
:after-each
(
dyn
:after-each
)})))
(
defn
before
[
thunk
]
(
setdyn
:before
thunk
))
(
defn
before-each
[
thunk
]
(
setdyn
:before-each
thunk
))
(
defn
after
[
thunk
]
(
setdyn
:after
thunk
))
(
defn
after-each
[
thunk
]
(
setdyn
:after-each
thunk
))
(
defn
test
[
description
thunk
]
(
array/push
(
dyn
:children
)
{
:type
'
test
:description
description
:thunk
thunk
}))
(
defn
execute-toplevel
[]
(
map
(
fn
[
group
]
# TODO: catch errors in hooks?
(
when-let
[
before
(
group
:before
)]
(
before
))
(
def
child-results
(
map
(
fn
[
child
]
(
when-let
[
before-each
(
group
:before-each
)]
(
before-each
))
(
match
child
{
:type
'
test
:thunk
thunk
:description
desc
}
(
try
(
do
((
test
:thunk
))
(
printf
"* %s ✅"
(
test
:description
))
{
:type
'
test
:test
test
:passed
true
})
([
err
]
(
printf
"* %s ❌"
(
test
:description
))
{
:type
'
test
:test
test
:passed
false
:error
err
}))
# TODO: :type 'group
)
(
when-let
[
after-each
(
group
:after-each
)]
(
after-each
)))
(
group
:children
)))
(
when-let
[
after
(
group
:after
)]
(
after
))
{
:type
'
group
:group
group
:children
child-results
})
toplevel
))
(
defn
run-tests
[]
(
def
results
(
execute-toplevel
))
(
print
divider-heavy
)
(
report
results
)
(
let
# TODO: do better
[
is-repl
(
and
(
=
"janet"
(
path/basename
(
dyn
*executable*
)))
(
all
(
fn
[
arg
]
(
not=
".janet"
(
path/ext
arg
)))
(
dyn
*args*
)))
some-failed
(
some
(
fn
[
res
]
(
not
(
res
:passed
)))
results
)]
(
when
(
and
some-failed
(
not
is-repl
))
(
os/exit
1
))))
(
defn-
report
"Default reporter"
[
results
]
(
print
"FAILURES:"
)
(
loop
[
result
:in
results
]
(
unless
(
result
:passed
)
(
print
divider-light
)
(
let
[{
:test
{
:description
test-desc
}
:error
err
}
result
]
(
printf
"* %s"
test-desc
)
(
printf
" %s"
err
))))
# TODO: recursion
(
def
failure-results
(
reduce
(
fn
[
acc
group-result
]
(
if-let
[
failed-children
(
filter
(
fn
[
res
]
(
not
(
res
:passed
)))
(
group-result
:children
))
failed-group-result
(
merge
group-result
{
:children
failed-children
})]
(
array/push
acc
failed-group-result
)
acc
))
(
array/new
)
results
))
(
defn
get-spaces
[
n
]
(
string
(
splice
(
g/to-array
(
g/take
10
(
g/cycle
[
" "
])))))
(
->>
[
" "
]
(
generators/cycle
)
(
generators/take
10
)
(
generators/to-array
)
(
splice
)
(
string
)))
(
defn
print-failure-results
[
result
depth
]
(
def
indent
(
get-spaces
(
*
2
depth
)))
(
match
result
{
:type
'
group
:group
{
:description
desc
}
:children
child-results
}
(
do
(
print
indent
desc
)
(
loop
[
child-result
:in
child-results
]
(
print-failure-results
child-result
(
+
1
depth
))))
{
:type
'
test
:test
{
:description
desc
}
:error
err
}
(
do
(
print
indent
desc
)
(
print
err
)
(
print
))))
(
loop
[
result
:in
failure-results
]
(
print-failure-result
result
0
))
(
print
divider-heavy
)
(
print
"SUMMARY:"
)
...
...
@@ -27,27 +133,6 @@
(
printf
"Failed: %i"
num-failed
))
(
print
divider-heavy
))
# TODO: do better
(
defn-
is-repl?
[]
(
and
(
=
"janet"
(
path/basename
(
dyn
*executable*
)))
(
all
(
fn
[
arg
]
(
not=
".janet"
(
path/ext
arg
)))
(
dyn
*args*
))))
(
defn
run-tests
"Runs all test cases"
[]
(
def
results
@[])
(
loop
[
test
:in
tests
]
(
array/push
results
(
try
(
do
(
apply
(
test
:thunk
))
(
printf
"* %s ✅"
(
test
:description
))
{
:test
test
:passed
true
})
([
err
]
(
printf
"* %s ❌"
(
test
:description
))
{
:test
test
:passed
false
:error
err
}))))
(
print
divider-heavy
)
(
report
results
)
(
when
(
and
(
not
(
is-repl?
))
(
some
(
fn
[
res
]
(
not
(
res
:passed
)))
results
))
(
os/exit
1
)))
(
defn
reset
"Clear defined tests"
[]
(
set
tests
@[]))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment