PINE LIBRARY

assert

Diupdate
Library "assert"
Production ready assertions and auto-reporting for unit testing pine scripts.

This library was born from the need to maintain production level stability and catch regressions / bugs early and fast. I hope this help you trust your pine scripts too. More libraries and tools on their way... please follow for more.

Please see the script for helpers to copy into your own scripts as well as examples at the bottom of the library unit testing itself.

Quick Reference

```
case = assert.init()

new_case(case, 'Asserts for floats and ints')
assert.equal(a, b, case, 'a == b')
assert.not_equal(a, b, case, 'a != b')
assert.nan(a, case, 'a == na')
assert.not_nan(a, case, 'a != na')
assert.is_in(a, b, case, 'a in b[]')
assert.is_not_in(a, b, case, 'a not in b[]')
assert.array_equal(a, b, case, 'a[] == b[]')

new_case(case, 'Asserts for ints only')
assert.int_in(a, b, case, 'a in b[]')
assert.int_not_in(a, b, case, 'a not in b[]')
assert.int_array_equal(a, b, case, 'a[] == b[]')

new_case(case, 'Asserts for bools only')
assert.is_true(a, case, 'a == true')
assert.is_false(a, case, 'a == false')
assert.bool_equal(a, b, case, 'a == b')
assert.bool_not_equal(a, b, case, 'a != b')
assert.bool_nan(a, case, 'a == na')
assert.bool_not_nan(a, case, 'a != na')
assert.bool_array_equal(a, b, case, 'a[] == b[]')

new_case(case, 'Asserts for strings only')
assert.str_equal(a, b, case, 'a == b')
assert.str_not_equal(a, b, case, 'a != b')
assert.str_nan(a, case, 'a == na')
assert.str_not_nan(a, case, 'a != na')
assert.str_in(a, b, case, 'a in b[]')
assert.str_not_in(a, b, case, 'a not in b[]')
assert.str_array_equal(a, b, case, 'a[] == b[]')

assert.report(case)
```

Detailed Interface

once() Restrict execution to only happen once. Usage: if assert.once()\n happens_once()
  Returns: bool, true on first execution within scope, false subsequently

init() Initialises the asserts array
  Returns: string[], tuple based array containing all unit test results and current case details (__ASSERTS)

equal(a, b, case, name) Numeric assert equal. Usage: assert.equal(1, 1, case, 'one == one')
  Parameters:
    a: float, numeric value "a" to compare equal to "b"
    b: float, numeric value "b" to compare equal to "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

not_equal(a, b, case, name) Numeric assert not equal. Usage: assert.not_equal(1, 2, case, 'one != two')
  Parameters:
    a: float, numeric value "a" to compare not equal "b"
    b: float, numeric value "b" to compare not equal "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

nan(a, case, name) Numeric assert is NaN. Usage: assert.nan(float(na), case, 'number is NaN')
  Parameters:
    a: float, numeric value "a" to check is NaN
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

not_nan(a, case, name) Numeric assert is not NaN. Usage: assert.not_nan(1, case, 'number is not NaN')
  Parameters:
    a: float, numeric value "a" to check is not NaN
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

is_in(a, b, case, name) Numeric assert value in float array. Usage: assert.is_in(1, array.from(1.0), case, '1 is in [1.0]')
  Parameters:
    a: float, numeric value "a" to check is in array "b"
    b: float[], array "b" to check contains "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

is_not_in(a, b, case, name) Numeric assert value not in float array. Usage: assert.is_not_in(2, array.from(1.0), case, '2 is not in [1.0]')
  Parameters:
    a: float, numeric value "a" to check is not in array "b"
    b: float[], array "b" to check does not contain "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

array_equal(a, b, case, name) Float assert arrays are equal. Usage: assert.array_equal(array.from(1.0), array.from(1.0), case, '[1.0] == [1.0]')
  Parameters:
    a: float[], array "a" to check is identical to array "b"
    b: float[], array "b" to check is identical to array "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

int_in(a, b, case, name) Integer assert value in integer array. Usage: assert.int_in(1, array.from(1), case, '1 is in [1]')
  Parameters:
    a: int, value "a" to check is in array "b"
    b: int[], array "b" to check contains "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

int_not_in(a, b, case, name) Integer assert value not in integer array. Usage: assert.int_not_in(2, array.from(1), case, '2 is not in [1]')
  Parameters:
    a: int, value "a" to check is not in array "b"
    b: int[], array "b" to check does not contain "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

int_array_equal(a, b, case, name) Integer assert arrays are equal. Usage: assert.int_array_equal(array.from(1), array.from(1), case, '[1] == [1]')
  Parameters:
    a: int[], array "a" to check is identical to array "b"
    b: int[], array "b" to check is identical to array "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

is_true(a, case, name) Boolean assert is true. Usage: assert.is_true(true, case, 'is true')
  Parameters:
    a: bool, value "a" to check is true
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

is_false(a, case, name) Boolean assert is false. Usage: assert.is_false(false, case, 'is false')
  Parameters:
    a: bool, value "a" to check is false
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

bool_equal(a, b, case, name) Boolean assert equal. Usage: assert.bool_equal(true, true, case, 'true == true')
  Parameters:
    a: bool, value "a" to compare equal to "b"
    b: bool, value "b" to compare equal to "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

bool_not_equal(a, b, case, name) Boolean assert not equal. Usage: assert.bool_not_equal(true, false, case, 'true != false')
  Parameters:
    a: bool, value "a" to compare not equal "b"
    b: bool, value "b" to compare not equal "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

bool_nan(a, case, name) Boolean assert is NaN. Usage: assert.bool_nan(bool(na), case, 'bool is NaN')
  Parameters:
    a: bool, value "a" to check is NaN
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

bool_not_nan(a, case, name) Boolean assert is not NaN. Usage: assert.bool_not_nan(true, case, 'bool is not NaN')
  Parameters:
    a: bool, value "a" to check is not NaN
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

bool_array_equal(a, b, case, name) Boolean assert arrays are equal. Usage: assert.bool_array_equal(array.from(true), array.from(true), case, '[true] == [true]')
  Parameters:
    a: bool[], array "a" to check is identical to array "b"
    b: bool[], array "b" to check is identical to array "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

str_equal(a, b, case, name) String assert equal. Usage: assert.str_equal('hi', 'hi', case, '"hi" == "hi"')
  Parameters:
    a: string, value "a" to compare equal to "b"
    b: string, value "b" to compare equal to "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

str_not_equal(a, b, case, name) String assert not equal. Usage: assert.str_not_equal('hi', 'bye', case, '"hi" != "bye"')
  Parameters:
    a: string, value "a" to compare not equal "b"
    b: string, value "b" to compare not equal "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

str_nan(a, case, name) String assert is NaN. Usage: assert.str_nan(string(na), case, 'string is NaN')
  Parameters:
    a: string, value "a" to check is NaN
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

str_not_nan(a, case, name) String assert is not NaN. Usage: assert.str_not_nan('hi', case', 'string is not NaN')
  Parameters:
    a: string, value "a" to check is not NaN
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

str_in(a, b, case, name) String assert value in string array. Usage: assert.str_in('hi', array.from('hi'), case, '"hi" in ["hi"]')
  Parameters:
    a: string, value "a" to check is in array "b"
    b: string[], array "b" to check contains "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

str_not_in(a, b, case, name) String assert value not in string array. Usage: assert.str_in('hi', array.from('bye'), case, '"hi" in ["bye"]')
  Parameters:
    a: string, value "a" to check is not in array "b"
    b: string[], array "b" to check does not contain "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

str_array_equal(a, b, case, name) String assert arrays are equal. Usage: assert.str_array_equal(array.from('hi'), array.from('hi'), case, '["hi"] == ["hi"]')
  Parameters:
    a: string[], array "a" to check is identical to array "b"
    b: string[], array "b" to check is identical to array "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

new_case(case, name) Assign a new test case name, for the next set of unit tests. Usage: assert.new_case(case, 'My tests')
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the case name for the next suite of tests

clear(case) Clear all stored unit tests from all cases. Usage: assert.clear(case)
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)

revert(case) Revert the previous unit test. Usage: [string msg, bool result] = assert.revert(case)
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
  Returns: [msg string, result bool], tuple containing the msg and result of the reverted test

passed(case, revert) Check if the last unit test has passed. Usage: bool success = assert.passed(case)
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    revert: bool, optionally revert the test
  Returns: bool, true only if the test passed

failed(case, revert) Check if the last unit test has failed. Usage: bool failure = assert.failed(case)
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    revert: bool, optionally revert the test
  Returns: bool, true only if the test failed

report(case, verbose) Report the outcome of unit tests that fail. Usage: bool passed = assert.report(case)
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    verbose: bool, optionally display full report that includes the outcome of all tests
  Returns: bool, true only if all tests passed

unittest_assert(case) Assert module unit tests, for inclusion in parent script test suite. Usage: assert.unittest_assert(__ASSERTS)
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)

unittest(verbose) Run the assert module unit tests as a stand alone. Usage: assert.unittest()
  Parameters:
    verbose: bool, optionally toggle report to display the outcome of all unit tests
Catatan Rilis
v2 - Fixed bug with assert_array_equal and tidied up quick reference
Catatan Rilis
v3 - Added color assertions. Added report position option. Improved is_bool messages. Revised string formatting.

Added:
color_equal(a, b, case, name) Color assert equal. Usage: assert.color_equal(#000000, #000000, case, 'black == black')
  Parameters:
    a: color, value "a" to compare equal to "b"
    b: color, value "b" to compare equal to "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

color_not_equal(a, b, case, name) Color assert not equal. Usage: assert.str_not_equal(#000000, #FFFFFF, case, 'black != white')
  Parameters:
    a: color, value "a" to compare not equal "b"
    b: color, value "b" to compare not equal "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

Updated:
report(case, verbose, position) Report the outcome of unit tests that fail. Usage: bool passed = assert.report(case)
  Parameters:
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    verbose: bool, toggle displaying the full report that includes the outcome of all tests
    position: string, the position for the report (global position vars)
  Returns: bool, true only if all tests passed
Catatan Rilis
v4 - Added greater than and lesser than assertions.

Added:
greater(a, b, case, name) Numeric assert greater than. Usage: assert.greater(2, 1, case, 'two > one')
  Parameters:
    a: float, numeric value "a" to compare greater than "b"
    b: float, numeric value "b" to compare lesser than "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

lesser(a, b, case, name) Numeric assert lesser than. Usage: assert.not_equal(1, 2, case, 'one < two')
  Parameters:
    a: float, numeric value "a" to compare lesser than "b"
    b: float, numeric value "b" to compare greater than "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

greater_or_equal(a, b, case, name) Numeric assert greater than or equal to. Usage: assert.greater(2, 1, case, 'two >= one')
  Parameters:
    a: float, numeric value "a" to compare greater or equal to "b"
    b: float, numeric value "b" to compare lesser than or equal to "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise

lesser_or_equal(a, b, case, name) Numeric assert lesser than or equal to. Usage: assert.not_equal(1, 2, case, 'one <= two')
  Parameters:
    a: float, numeric value "a" to compare lesser than or equal to "b"
    b: float, numeric value "b" to compare greater than or equal to "a"
    case: string[], the current test case and array of previous unit tests (__ASSERTS)
    name: string, the current unit test name, if undefined the test index of the current case is used
  Returns: bool, true if the assertion passes, false otherwise
Catatan Rilis
v5 - Added only. Fixed strange output when using slices with array_tostring.

Added:
only(repeat) Restrict execution to happen a set number of times. Usage: if assert.only(5)\n happens_five_times()
  Parameters:
    repeat: int, the number of times to return true
  Returns: bool, true for the set number of times within scope, false subsequently
debugging

Perpustakaan pine

Dengan semangat TradingView yang sesungguhnya, penulis telah menerbitkan Kode Pine ini sebagai pustaka sumber terbuka sehingga programmer Pine lain dari komunitas kami dapat menggunakannya kembali. Hormat untuk penulis! Anda dapat menggunakan pustaka ini secara pribadi atau dalam publikasi sumber terbuka lainnya, namun penggunaan kembali kode ini dalam publikasi diatur oleh Tata Tertib.

Pernyataan Penyangkalan