Skip to content

assert Module

The assert module provides fail-fast assertions for tests and runtime contracts. When an assertion fails, Harneet terminates with a runtime error and does not execute subsequent lines.

Functions

Assert(condition, message?)

Asserts that a condition is true. If the condition is false, the program will terminate with a runtime error (fail-fast).

Parameters: - condition: The condition to check. - message (optional): A message to display if the assertion fails.

Example:

Assert Condition
1
2
3
4
5
6
7
8
package main
import assert

assert.Assert(1 == 1, "1 should be equal to 1")
// ok

assert.Assert(1 == 2, "1 should not be equal to 2")
// terminates with a runtime error


AssertEq(actual, expected, message?)

Asserts that two values are equal. If they are not, the program terminates with a runtime error.

Parameters: - actual: The actual value. - expected: The expected value. - message (optional): A message to display if the assertion fails.

Example:

Assert Equal
1
2
3
4
5
6
7
8
package main
import assert

assert.AssertEq(42, 42, "42 should be equal to 42")
// ok

assert.AssertEq(42, 43, "42 should not be equal to 43")
// terminates with a runtime error


AssertNe(actual, expected, message?)

Asserts that two values are not equal. If they are, the program terminates with a runtime error.

Parameters: - actual: The actual value. - expected: The expected value. - message (optional): A message to display if the assertion fails.

Example:

Assert Not Equal
1
2
3
4
5
6
7
8
package main
import assert

assert.AssertNe(42, 43, "42 should not be equal to 43")
// ok

assert.AssertNe(42, 42, "42 should be equal to 42")
// terminates with a runtime error


AssertIsNotNone(value, message?)

Asserts that a value is not None. If it is None, the program terminates with a runtime error.

Parameters: - value: The value to check against None. - message (optional): A message to display if the assertion fails.

Example:

Assert Not None
1
2
3
4
5
6
7
8
package main
import assert

assert.AssertIsNotNone("hello")
// ok

// assert.AssertIsNotNone(None, "value must not be None")
// terminates with a runtime error


AssertLengthIsNotZero(value, message?)

Asserts that the length of a value is not zero. Supported types: string, array. Fails fast on zero length.

Parameters: - value: String or array to check. - message (optional): A message to display if the assertion fails.

Example:

Assert Length Not Zero
1
2
3
4
5
package main
import assert

assert.AssertLengthIsNotZero("abc")
assert.AssertLengthIsNotZero([1, 2])


AssertLengthIsGreaterThan(value, min, message?)

Asserts that length(value) > min. Supported types: string, array. Fails fast if not strictly greater.

Parameters: - value: String or array to check. - min (integer): Minimum length threshold (exclusive). - message (optional): A message to display if the assertion fails.

Example:

Assert Length Greater
1
2
3
4
5
package main
import assert

assert.AssertLengthIsGreaterThan("abcd", 3)
assert.AssertLengthIsGreaterThan([1, 2, 3], 2)


AssertLengthIsLessThan(value, max, message?)

Asserts that length(value) < max. Supported types: string, array. Fails fast if not strictly less.

Parameters: - value: String or array to check. - max (integer): Maximum length threshold (exclusive). - message (optional): A message to display if the assertion fails.

Example:

Assert Length Less
1
2
3
4
5
package main
import assert

assert.AssertLengthIsLessThan("ab", 3)
assert.AssertLengthIsLessThan([1], 2)


AssertIsAnInt(value, message?)

Asserts that value is an INTEGER. Fails fast on wrong type.

Parameters: - value: Value to check. - message (optional): A message to display if the assertion fails.

Example:

Assert Is Int
1
2
3
4
5
package main
import assert

assert.AssertIsAnInt(42)
// assert.AssertIsAnInt("not-int")  // terminates with a runtime error


AssertIsAString(value, message?)

Asserts that value is a STRING. Fails fast on wrong type.

Parameters: - value: Value to check. - message (optional): A message to display if the assertion fails.

Example:

Assert Is String
1
2
3
4
5
package main
import assert

assert.AssertIsAString("hello")
// assert.AssertIsAString(123)  // terminates with a runtime error


AssertIsBool(value, message?)

Asserts that value is a BOOLEAN. Fails fast on wrong type.

Parameters: - value: Value to check. - message (optional): A message to display if the assertion fails.

Example:

Assert Is Bool
1
2
3
4
5
package main
import assert

assert.AssertIsBool(true)
// assert.AssertIsBool("nope")  // terminates with a runtime error