Skip to content

Commit 1a1d126

Browse files
committed
✨ Add assurances
1 parent e91a423 commit 1a1d126

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

spec/vunit_test.v

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import vunit { assert_bool, assert_false, assert_nil, assert_true, expect }
1+
import vunit { ensure, expect }
22

3-
fn test_assert_nil() {
4-
assert_nil(unsafe { nil })
3+
fn test_nil_unit() {
4+
ensure(unsafe { nil }).is_nil()
5+
expect(unsafe { nil }).to_be_nil()
56
}
67

7-
fn test_assert_bool() {
8-
assert_bool(true)
9-
assert_bool(false)
10-
assert_true(true)
11-
assert_false(false)
12-
}
13-
14-
fn test_expect_bool() {
8+
fn test_bool_unit() {
9+
ensure(true).is_bool().is_true()
10+
ensure(false).is_bool().is_false()
1511
expect(true).to_be_bool().to_be_true()
1612
expect(false).to_be_bool().to_be_false()
1713
}

vunit/assertions.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn assert_type[T](assertion bool, val T, typ string) {
1414
assert_that(assertion, typ, typeof(val).name)
1515
}
1616

17-
pub fn assert_nil(val voidptr) {
17+
pub fn assert_nil[T](val T) {
1818
assert_type(filter(val).is_nil(), val, 'nil')
1919
}
2020

vunit/assurances.v

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module vunit
2+
3+
import vest { filter }
4+
5+
struct Assurance[T] {
6+
val T
7+
}
8+
9+
pub fn ensure[T](val T) Assurance[T] {
10+
return Assurance[T]{val}
11+
}
12+
13+
pub fn (this Assurance[T]) is_nil() Assurance[T] {
14+
if !filter(this.val).is_nil() {
15+
panic('${this.val} is not nil')
16+
}
17+
return this
18+
}
19+
20+
pub fn (this Assurance[T]) is_bool() Assurance[T] {
21+
if !filter(this.val).is_bool() {
22+
panic('${this.val} is not bool')
23+
}
24+
return this
25+
}
26+
27+
pub fn (this Assurance[bool]) is_true() Assurance[bool] {
28+
if !filter(this.val).is_true() {
29+
panic('${this.val} is not true')
30+
}
31+
return this
32+
}
33+
34+
pub fn (this Assurance[bool]) is_false() Assurance[bool] {
35+
if !filter(this.val).is_false() {
36+
panic('${this.val} is not false')
37+
}
38+
return this
39+
}

vunit/expectations.v

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module vunit
22

3-
@[params]
43
struct Expectation[T] {
54
val T
65
}
@@ -9,7 +8,7 @@ pub fn expect[T](val T) Expectation[T] {
98
return Expectation[T]{val}
109
}
1110

12-
pub fn (this Expectation[voidptr]) to_be_nil() Expectation[voidptr] {
11+
pub fn (this Expectation[T]) to_be_nil() Expectation[T] {
1312
assert_nil(this.val)
1413
return this
1514
}

0 commit comments

Comments
 (0)