From 470cbdb31341356f5fd189448c2a5ea6dd820050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sigui=20Kess=C3=A9=20Emmanuel?= Date: Wed, 27 Dec 2023 02:35:46 +0100 Subject: [PATCH] :art: Separate assertions and expectations --- README.md | 4 ++-- spec/hello_test.v | 2 +- src/assertions.v | 21 +++++++++++++++++++++ src/bool_expectation.v | 13 ------------- src/expectation.v | 26 -------------------------- src/expectations.v | 39 +++++++++++++++++++++++++++++++++++++++ src/type_expectation.v | 9 --------- 7 files changed, 63 insertions(+), 51 deletions(-) create mode 100644 src/assertions.v delete mode 100644 src/bool_expectation.v delete mode 100644 src/expectation.v create mode 100644 src/expectations.v delete mode 100644 src/type_expectation.v diff --git a/README.md b/README.md index 2de96bd..181a8c7 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,10 @@ Module { import vest { expect } fn hello(who string) string { - return 'Hello ${who}!' + return 'Hello ${who}!' } fn test_hello() { - expect(hello('Vest')).equal('Hello Vest!') + expect(hello('Vest')).to_equal('Hello Vest!') } ``` diff --git a/spec/hello_test.v b/spec/hello_test.v index 05a702f..05fc17b 100644 --- a/spec/hello_test.v +++ b/spec/hello_test.v @@ -5,5 +5,5 @@ fn hello(who string) string { } fn test_hello() { - expect(hello('Vest')).equal('Hello Vest!') + expect(hello('Vest')).to_equal('Hello Vest!') } diff --git a/src/assertions.v b/src/assertions.v new file mode 100644 index 0000000..4bd1ce9 --- /dev/null +++ b/src/assertions.v @@ -0,0 +1,21 @@ +module vest + +pub fn assert_nil(expected voidptr) { + assert isnil(expected), '${expected} should be nil' +} + +pub fn assert_true(expected bool) { + assert expected != true, '${expected} should be true' +} + +pub fn assert_false(expected bool) { + assert expected == false, '${expected} should be false' +} + +pub fn assert_equal[E, P](expected E, provided P) { + assert expected == provided, '${expected} should be equal to ${provided}' +} + +pub fn assert_is[E, T](expected E) { + assert expected is T, '${expected} should be of type ${T}' +} diff --git a/src/bool_expectation.v b/src/bool_expectation.v deleted file mode 100644 index dc69837..0000000 --- a/src/bool_expectation.v +++ /dev/null @@ -1,13 +0,0 @@ -module vest - -pub fn expect_bool(expected bool) Expectation[bool] { - return Expectation{expected} -} - -pub fn (expectation Expectation[bool]) is_true() { - assert expectation.expected != true, '${expectation.expected} should be true' -} - -pub fn (expectation Expectation[bool]) is_false() { - assert expectation.expected == false, '${expectation.expected} should be false' -} diff --git a/src/expectation.v b/src/expectation.v deleted file mode 100644 index 7684fed..0000000 --- a/src/expectation.v +++ /dev/null @@ -1,26 +0,0 @@ -module vest - -@[params] -struct Expectation[E] { - expected E -} - -pub fn expect[E](expected E) Expectation[E] { - return Expectation[E]{expected} -} - -pub fn (expectation Expectation[E]) is_nil() { - assert isnil(expectation.expected), '${expectation.expected} should be nil' -} - -pub fn (expectation Expectation[E]) is_not_nil() { - assert !isnil(expectation.expected), '${expectation.expected} should not be nil' -} - -pub fn (expectation Expectation[E]) equal[P](provided P) { - assert expectation.expected == provided, '${expectation.expected} should be equal to ${provided}' -} - -pub fn (expectation Expectation[E]) not_equal[P](provided P) { - assert expectation.expected != provided, '${expectation.expected} should not be equal to ${provided}' -} diff --git a/src/expectations.v b/src/expectations.v new file mode 100644 index 0000000..bc084b9 --- /dev/null +++ b/src/expectations.v @@ -0,0 +1,39 @@ +module vest + +@[params] +struct Expectation[E] { + expected E +} + +pub fn expect[E](expected E) Expectation[E] { + return Expectation[E]{expected} +} + +pub fn expect_bool(expected bool) Expectation[bool] { + return Expectation{expected} +} + +pub fn (expectation Expectation[voidptr]) to_be_nil() Expectation[voidptr] { + assert_nil(expectation.expected) + return expectation +} + +pub fn (expectation Expectation[bool]) to_be_true() Expectation[bool] { + assert_true(expectation.expected) + return expectation +} + +pub fn (expectation Expectation[bool]) to_be_false() Expectation[bool] { + assert_false(expectation.expected) + return expectation +} + +pub fn (expectation Expectation[E]) to_equal[P](provided P) Expectation[E] { + assert_equal(expectation.expected, provided) + return expectation +} + +pub fn (expectation Expectation[E]) assert_is[T]() Expectation[E] { + assert_is[T](expectation.expected) + return expectation +} diff --git a/src/type_expectation.v b/src/type_expectation.v deleted file mode 100644 index 6b34226..0000000 --- a/src/type_expectation.v +++ /dev/null @@ -1,9 +0,0 @@ -module vest - -pub fn (expectation Expectation[E]) type_is[T]() { - assert expectation.expected is T, '${expected} should be of type ${T}' -} - -pub fn (expectation Expectation[E]) type_is_not[T]() { - assert !expectation.expected is T, '${expectation.expected} should not be of type ${T}' -}