Skip to content

Commit 580ccf9

Browse files
committed
feat: creating a new testing library, now part of the library instead of the tests part
1 parent 8ea1872 commit 580ccf9

13 files changed

+278
-379
lines changed

String.ark

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@
135135
(let _pattern_sz (len _pattern))
136136

137137
(while (!= -1 _idx) {
138+
(mut _first_segment (str:slice _out 0 _idx))
138139
(mut _next_segment (str:slice _out (+ _idx _pattern_sz) (- (len _out) (+ _idx _pattern_sz))))
139140
(set _out (+
140-
(str:slice _out 0 _idx)
141+
_first_segment
141142
_new
142143
_next_segment))
143144
(set _idx (str:find _next_segment _pattern))
144145
(if (!= -1 _idx)
145-
(set _idx (+ _idx _pattern_sz)))
146-
})
146+
(set _idx (+ _idx (len _first_segment) (len _new))))})
147147
_out }))
148148

149149
# @brief Join a list of elements with a given string delimiter

Testing.ark

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
(let _runner (fun (_name _callable) {
2+
(mut _passed 0)
3+
(mut _failed 0)
4+
(mut _failures [])
5+
6+
(let _start_time (time))
7+
# run test
8+
(_callable)
9+
(let _end_time (time))
10+
11+
# no newline, yet
12+
(puts _name)
13+
(if (> _passed 0)
14+
(puts (str:format " - {} ✅" _passed)))
15+
(if (> _failed 0)
16+
(puts (str:format ", {} ❌" _failed)))
17+
18+
(puts (str:format " in {:2.3f}ms\n" (* 1000 (- _end_time _start_time))))
19+
20+
(mut _i 0)
21+
(let _failures_count (len _failures))
22+
(while (< _i _failures_count) {
23+
(print "\t" (@ _failures _i))
24+
(set _i (+ 1 _i))})
25+
26+
[_passed _failed]}))
27+
28+
(let _report_error (fun (_lhs _rhs _lhs_repr _rhs_repr) {
29+
(set _failed (+ 1 _failed))
30+
(append! _failures (str:format "{} was not equal to {}" _lhs_repr _rhs_repr))
31+
(let _rhs_start (+ (len _lhs_repr) (len " was not equal to ")))
32+
(let _lhs_align (len _lhs_repr))
33+
(let _rhs_align (len _rhs_repr))
34+
(append! _failures (str:format (+ "{: <" (toString _rhs_start) "}{:~<" (toString _rhs_align) "} {}") "|" "\\" _lhs))
35+
(append! _failures (str:format (+ "{:~<" (toString _lhs_align) "} {}") "\\" _rhs))}))
36+
37+
(let _report_success (fun () (set _passed (+ 1 _passed))))
38+
39+
($ test:expect (_cond) {
40+
(if (!= true _cond)
41+
{
42+
(set _failed (+ 1 _failed))
43+
(append! _failures (str:format "{} was not true but {}" ($repr _cond) _cond)) }
44+
(_report_success))})
45+
46+
($ test:eq (_lhs _rhs) {
47+
(if (= _lhs _rhs)
48+
(_report_success)
49+
(_report_error _lhs _rhs ($repr _lhs) ($repr _rhs)))})
50+
51+
($ test:neq (_lhs _rhs) {
52+
(if (!= _lhs _rhs)
53+
(_report_success)
54+
(_report_error _lhs _rhs ($repr _lhs) ($repr _rhs)))})
55+
56+
($ test:suite (_name _body) {
57+
(let (symcat _name "-output") (_runner ($repr _name) (fun () {_body})))
58+
(let (symcat _name "-status") (= 0 (@ (symcat _name "-output") 1)))})

tests/events-tests.ark

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,30 @@
1-
(import tests-tools)
2-
31
(import std.Events)
2+
(import std.Testing)
43

5-
(let events-tests (fun () {
6-
(mut tests 0)
7-
(let start-time (time))
8-
9-
(let em (events:manager:make))
4+
(let em (events:manager:make))
105

6+
(test:suite events {
117
# _check_valid tests
12-
(set tests (assert-eq true (em._check_valid (fun () ())) "check_valid" tests))
13-
(set tests (assert-eq false (em._check_valid 4) "check_valid" tests))
8+
(test:expect (em._check_valid (fun () ())))
9+
(test:expect (not (em._check_valid 4)))
1410

1511
# on tests
1612
(em.on "myType" 4)
17-
(set tests (assert-eq [] em._listeners "listeners empty" tests))
13+
(test:eq [] em._listeners)
1814
(em.on "myType" (fun (_) ()))
19-
(set tests (assert-neq [] em._listeners "listeners with one element" tests))
15+
(test:neq [] em._listeners)
2016

2117
# emit tests
22-
(set tests (assert-eq false (em.emit "emitType") "emit" tests))
18+
(test:expect (not (em.emit "emitType")))
2319
(em.on "emitType" (fun (_) ()))
24-
(set tests (assert-eq true (em.emit "emitType") "emit" tests))
20+
(test:expect (em.emit "emitType"))
2521

2622
# emitWith tests
27-
(set tests (assert-eq false (em.emitWith 1 "emitWithType") "emitWith" tests))
23+
(test:expect (not (em.emitWith 1 "emitWithType")))
2824
(em.on "emitWithType" (fun (_) ()))
29-
(set tests (assert-eq true (em.emitWith 2 "emitWithType") "emitWith" tests))
25+
(test:expect (em.emitWith 2 "emitWithType"))
3026

3127
# removeListenersOfType
32-
(set tests (assert-eq false (em.removeListenersOfType "removeType") "removeListenersOfType" tests))
28+
(test:expect (not (em.removeListenersOfType "removeType")))
3329
(em.on "removeType" (fun (_) ()))
34-
(set tests (assert-eq true (em.removeListenersOfType "removeType") "removeListenersOfType" tests))
35-
36-
(recap "Events tests passed" tests (- (time) start-time))
37-
38-
tests }))
39-
40-
(let passed-events (events-tests))
30+
(test:expect (em.removeListenersOfType "removeType"))})

tests/exceptions-tests.ark

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1-
(import tests-tools)
2-
31
(import std.Exceptions)
2+
(import std.Testing)
43

5-
(let exceptions-tests (fun () {
6-
(mut tests 0)
7-
(let start-time (time))
8-
9-
(let invert (fun (x) {
10-
(if (= x 0)
11-
(throw "cannot divide by zero")
12-
(return (/ 1 x)))}))
4+
(let invert (fun (x)
5+
(if (= x 0)
6+
(throw "cannot divide by zero")
7+
(return (/ 1 x)))))
138

9+
(test:suite exceptions {
1410
(try (invert 0)
15-
(fun (inverted) (assert-val false "Exception test" tests))
16-
(fun (err) (set tests (assert-eq err "cannot divide by zero" "Exception test" tests))))
17-
18-
(try (invert 2)
19-
(fun (inverted) (set tests (assert-eq inverted 0.5 "Exception test" tests)))
20-
(fun (err) (assert-val false "Exception test" tests)))
21-
22-
(recap "Exceptions tests passed" tests (- (time) start-time))
23-
24-
tests }))
25-
26-
(let passed-exceptions (exceptions-tests))
11+
(fun (inverted) (test:expect false))
12+
(fun (err) (test:expect (= err "cannot divide by zero"))))})

tests/functional-tests.ark

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
(import tests-tools)
2-
31
(import std.Functional)
2+
(import std.Testing)
43

5-
(let functional-tests (fun () {
6-
(mut tests 0)
7-
(let start-time (time))
8-
9-
(let foo (fun (x) (+ x 3)))
10-
(let egg (fun (y) (* y 2)))
11-
(let bar (fun (x y) (- x y)))
4+
(let foo (fun (x) (+ x 3)))
5+
(let egg (fun (y) (* y 2)))
6+
(let bar (fun (x y) (- x y)))
127

13-
(set tests (assert-eq ((compose foo egg) 5) (foo (egg 5)) "compose" tests))
14-
(set tests (assert-eq ((compose egg egg) 5) (egg (egg 5)) "compose" tests))
8+
(test:suite functional {
9+
(test:eq ((compose foo egg) 5) (foo (egg 5)))
10+
(test:eq ((compose egg egg) 5) (egg (egg 5)))
1511
# no need to test left and right because they were already tested by the exceptions tests (throw, return)
16-
(set tests (assert-eq ((flip bar 5) 6) (bar 6 5) "flip" tests))
17-
(set tests (assert-eq ((flip bar 6) 5) (bar 5 6) "flip" tests))
18-
19-
(recap "Functional tests passed" tests (- (time) start-time))
20-
21-
tests }))
22-
23-
(let passed-functional (functional-tests))
12+
(test:eq ((flip bar 5) 6) (bar 6 5))
13+
(test:eq ((flip bar 6) 5) (bar 5 6))})

tests/lazy-tests.ark

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
1-
(import tests-tools)
2-
31
(import std.Lazy)
2+
(import std.Testing)
43

5-
(let lazy-tests (fun () {
6-
(mut tests 0)
7-
(let start-time (time))
8-
9-
(let calculate_the_sun_weight (fun () (begin (sys:sleep 50) 42)))
10-
(let memo (lazy:eval calculate_the_sun_weight))
4+
(let calculate_the_sun_weight (fun () (begin (sys:sleep 50) 42)))
5+
(let memo (lazy:eval calculate_the_sun_weight))
116

7+
(test:suite lazy {
128
(mut timer-0 (time))
13-
(set tests (assert-eq (memo) 42 "memoize the sun weight" tests))
14-
(set tests (assert-gt (- (time) timer-0) 0.05 "lazy:eval ran the function for the first time" tests))
9+
(test:eq 42 (memo))
10+
# test execution time, should be long because the function wasn't memoized yet
11+
(test:expect (> (- (time) timer-0) 0.050))
1512

1613
(set timer-0 (time))
17-
(set tests (assert-eq (memo) 42 "the memoized value hasn't changed" tests))
18-
(set tests (assert-lt (- (time) timer-0) 0.005 "lazy:eval returned the memoized value" tests))
19-
20-
(recap "Lazy tests passed" tests (- (time) start-time))
21-
22-
tests }))
23-
24-
(let passed-lazy (lazy-tests))
14+
(test:eq 42 (memo))
15+
(test:expect (< (- (time) timer-0) 0.050))})

tests/list-tests.ark

Lines changed: 50 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,72 @@
1-
(import tests-tools)
2-
31
(import std.List)
42
(import std.Math :even)
3+
(import std.Testing)
54

6-
(let list-tests (fun () {
7-
(mut tests 0)
8-
(let start-time (time))
9-
10-
(let a [1 2 3])
11-
(let b [4 5 6])
12-
(let zipped [[1 5] [2 6] [3 7] [4 8]])
5+
(let a [1 2 3])
6+
(let b [4 5 6])
7+
(let zipped [[1 5] [2 6] [3 7] [4 8]])
138

9+
(test:suite list {
1410
(list:forEach a (fun (e) {
1511
# just assert we have something, basically it's just a while + @
16-
(set tests (assert-neq e nil "forEach" tests))}))
17-
18-
(set tests (assert-eq (list:product b) (* 4 5 6) "product" tests))
19-
(set tests (assert-eq (list:product []) 1 "product" tests))
20-
21-
(set tests (assert-eq (list:sum b) (+ 4 5 6) "sum" tests))
22-
(set tests (assert-eq (list:sum []) 0 "sum" tests))
23-
24-
(set tests (assert-eq (list:drop a 0) [1 2 3] "drop" tests))
25-
(set tests (assert-eq (list:drop a 1) [2 3] "drop" tests))
26-
(set tests (assert-eq (list:drop a 2) [3] "drop" tests))
12+
(test:neq e nil)}))
2713

28-
(set tests (assert-eq (list:dropWhile a (fun (c) (< c 0))) [1 2 3] "dropWhile" tests))
29-
(set tests (assert-eq (list:dropWhile a (fun (c) (< c 2))) [2 3] "dropWhile" tests))
30-
(set tests (assert-eq (list:dropWhile a (fun (c) (< c 5))) [] "dropWhile" tests))
14+
(test:eq (list:product b) (* 4 5 6))
15+
(test:eq (list:product []) 1)
3116

32-
(set tests (assert-eq (list:filter a math:even) [2] "filter" tests))
33-
(set tests (assert-eq (list:filter a (fun (e) (> e 100))) [] "filter" tests))
34-
(set tests (assert-eq (list:filter [] (fun (e) (> e 100))) [] "filter" tests))
17+
(test:eq (list:sum b) (+ 4 5 6))
18+
(test:eq (list:sum []) 0)
3519

36-
(set tests (assert-eq (list:map b (fun (e) (* e e))) [16 25 36] "map" tests))
37-
(set tests (assert-eq (list:map [] (fun (e) (* e e))) [] "map" tests))
20+
(test:eq (list:drop a 0) [1 2 3])
21+
(test:eq (list:drop a 1) [2 3])
22+
(test:eq (list:drop a 2) [3])
3823

39-
(set tests (assert-eq (list:reduce a (fun (x y) (- x y))) -4 "reduce" tests))
24+
(test:eq (list:dropWhile a (fun (c) (< c 0))) [1 2 3])
25+
(test:eq (list:dropWhile a (fun (c) (< c 2))) [2 3])
26+
(test:eq (list:dropWhile a (fun (c) (< c 5))) [])
4027

41-
(set tests (assert-eq (list:flatten []) [] "flatten" tests))
42-
(set tests (assert-eq (list:flatten [[]]) [] "flatten" tests))
43-
(set tests (assert-eq (list:flatten [[1]]) [1] "flatten" tests))
44-
(set tests (assert-eq (list:flatten zipped) [1 5 2 6 3 7 4 8] "flatten" tests))
28+
(test:eq (list:filter a math:even) [2])
29+
(test:eq (list:filter a (fun (e) (> e 100))) [])
30+
(test:eq (list:filter [] (fun (e) (> e 100))) [])
4531

46-
(set tests (assert-eq (list:flatMap [] (fun (a) [a a])) [] "flatMap" tests))
47-
(set tests (assert-eq (list:flatMap a (fun (a) (* 2 a))) [2 4 6] "flatMap" tests))
48-
(set tests (assert-eq (list:flatMap a (fun (a) [a a])) [1 1 2 2 3 3] "flatMap" tests))
32+
(test:eq (list:map b (fun (e) (* e e))) [16 25 36])
33+
(test:eq (list:map [] (fun (e) (* e e))) [])
4934

50-
(set tests (assert-eq (list:take a 1) [1] "take" tests))
51-
(set tests (assert-eq (list:take a 100) a "take" tests))
35+
(test:eq (list:reduce a (fun (x y) (- x y))) -4)
5236

53-
(set tests (assert-eq (list:takeWhile a (fun (c) (< c 0))) [] "takeWhile" tests))
54-
(set tests (assert-eq (list:takeWhile a (fun (c) (< c 2))) [1] "takeWhile" tests))
55-
(set tests (assert-eq (list:takeWhile a (fun (c) (< c 3))) [1 2] "takeWhile" tests))
56-
(set tests (assert-eq (list:takeWhile a (fun (c) (< c 5))) [1 2 3] "takeWhile" tests))
37+
(test:eq (list:flatten []) [])
38+
(test:eq (list:flatten [[]]) [])
39+
(test:eq (list:flatten [[1]]) [1])
40+
(test:eq (list:flatten zipped) [1 5 2 6 3 7 4 8])
5741

58-
(set tests (assert-eq (list:unzip zipped) [[1 2 3 4] [5 6 7 8]] "unzip" tests))
59-
(set tests (assert-eq (list:unzip []) [[] []] "unzip" tests))
42+
(test:eq (list:flatMap [] (fun (a) [a a])) [])
43+
(test:eq (list:flatMap a (fun (a) (* 2 a))) [2 4 6])
44+
(test:eq (list:flatMap a (fun (a) [a a])) [1 1 2 2 3 3])
6045

61-
(set tests (assert-eq (list:zip a b) [[1 4] [2 5] [3 6]] "zip" tests))
62-
(set tests (assert-eq (list:zip [] []) [] "zip" tests))
46+
(test:eq (list:take a 1) [1])
47+
(test:eq (list:take a 100) a)
6348

64-
(set tests (assert-eq (list:foldLeft [] 0 (fun (x y) (+ x y))) 0 "foldLeft" tests))
65-
(set tests (assert-eq (list:foldLeft ["1" "2" "3"] "" (fun (x y) (+ x y))) "123" "foldLeft" tests))
66-
(set tests (assert-eq (list:foldLeft a 0 (fun (x y) (+ x y))) 6 "foldLeft" tests))
49+
(test:eq (list:takeWhile a (fun (c) (< c 0))) [])
50+
(test:eq (list:takeWhile a (fun (c) (< c 2))) [1])
51+
(test:eq (list:takeWhile a (fun (c) (< c 3))) [1 2])
52+
(test:eq (list:takeWhile a (fun (c) (< c 5))) [1 2 3])
6753

68-
(set tests (assert-eq a [1 2 3] "unmodified list" tests))
69-
(set tests (assert-eq b [4 5 6] "unmodified list" tests))
54+
(test:eq (list:unzip zipped) [[1 2 3 4] [5 6 7 8]])
55+
(test:eq (list:unzip []) [[] []])
7056

71-
(set tests (assert-eq (list:forAll a (fun (e) (< e 4))) true "list:forAll" tests))
72-
(set tests (assert-eq (list:forAll a (fun (e) (< e 2))) false "list:forAll" tests))
73-
(set tests (assert-eq (list:forAll [] (fun (e) (= e 2))) true "list:forAll" tests))
74-
(set tests (assert-eq (list:any a (fun (e) (< e 2))) true "list:any" tests))
75-
(set tests (assert-eq (list:any a (fun (e) (> e 8))) false "list:any" tests))
76-
(set tests (assert-eq (list:any [] (fun (e) (= e 8))) false "list:any" tests))
57+
(test:eq (list:zip a b) [[1 4] [2 5] [3 6]])
58+
(test:eq (list:zip [] []) [])
7759

78-
(recap "List tests passed" tests (- (time) start-time))
60+
(test:eq (list:foldLeft [] 0 (fun (x y) (+ x y))) 0)
61+
(test:eq (list:foldLeft ["1" "2" "3"] "" (fun (x y) (+ x y))) "123")
62+
(test:eq (list:foldLeft a 0 (fun (x y) (+ x y))) 6)
7963

80-
tests }))
64+
(test:eq a [1 2 3])
65+
(test:eq b [4 5 6])
8166

82-
(let passed-list (list-tests))
67+
(test:expect (list:forAll a (fun (e) (< e 4))))
68+
(test:expect (not (list:forAll a (fun (e) (< e 2)))))
69+
(test:expect (list:forAll [] (fun (e) (= e 2))))
70+
(test:expect (list:any a (fun (e) (< e 2))))
71+
(test:expect (not (list:any a (fun (e) (> e 8)))))
72+
(test:expect (not (list:any [] (fun (e) (= e 8)))))})

0 commit comments

Comments
 (0)