|
| 1 | +# https://github.com/anderseknert/rego-test-assertions/blob/main/test/assert/assert.rego |
| 2 | + |
| 3 | +# METADATA |
| 4 | +# description: Utility functions for working with Rego unit tests |
| 5 | +# authors: |
| 6 | +# - Anders Eknert <[email protected]> |
| 7 | +# |
| 8 | +package test.assert |
| 9 | + |
| 10 | +import rego.v1 |
| 11 | + |
| 12 | +# METADATA |
| 13 | +# description: Assert expected is equal to result |
| 14 | +equals(expected, result) if { |
| 15 | + expected == result |
| 16 | +} else := false if { |
| 17 | + print("expected equals:", _quote_str(expected), "got:", result) |
| 18 | +} |
| 19 | + |
| 20 | +# METADATA |
| 21 | +# description: Assert expected is not equal to result |
| 22 | +not_equals(expected, result) if { |
| 23 | + expected != result |
| 24 | +} else := false if { |
| 25 | + print("expected not equals:", _quote_str(expected), "got:", result) |
| 26 | +} |
| 27 | + |
| 28 | +# METADATA |
| 29 | +# description: Assert all items in coll are equal to value |
| 30 | +all_equals(coll, value) if { |
| 31 | + every item in coll { |
| 32 | + item == value |
| 33 | + } |
| 34 | +} else := false if { |
| 35 | + exceptions := [item | some item in coll; item != value] |
| 36 | + print("expected all items to have value", _append_comma(value), "failed for", exceptions) |
| 37 | +} |
| 38 | + |
| 39 | +# METADATA |
| 40 | +# description: Assert no items in coll are equal to value |
| 41 | +none_equals(coll, value) if { |
| 42 | + every item in coll { |
| 43 | + item != value |
| 44 | + } |
| 45 | +} else := false if { |
| 46 | + exceptions := [item | some item in coll; item == value] |
| 47 | + print("expected no items to have value", _append_comma(value), "failed for", exceptions) |
| 48 | +} |
| 49 | + |
| 50 | +# METADATA |
| 51 | +# description: Assert item is in coll |
| 52 | +has(item, coll) if { |
| 53 | + item in coll |
| 54 | +} else := false if { |
| 55 | + print("expected", type_name(item), _quote_str(item), "in", type_name(coll), "got:", coll) |
| 56 | +} |
| 57 | + |
| 58 | +# METADATA |
| 59 | +# description: Assert item is not in coll |
| 60 | +not_has(item, coll) if { |
| 61 | + not item in coll |
| 62 | +} else := false if { |
| 63 | + print("expected", type_name(item), _quote_str(item), "not in", type_name(coll), "got:", coll) |
| 64 | +} |
| 65 | + |
| 66 | +# METADATA |
| 67 | +# description: Assert key is in obj |
| 68 | +has_key(key, obj) if { |
| 69 | + some keys, _ in obj |
| 70 | + key == keys |
| 71 | +} else := false if { |
| 72 | + print("expected", type_name(key), _quote_str(key), "in", type_name(obj), "got:", obj) |
| 73 | +} |
| 74 | + |
| 75 | +# METADATA |
| 76 | +# description: Assert key is not in obj |
| 77 | +not_has_key(key, obj) := false if { |
| 78 | + some keys, _ in obj |
| 79 | + key == keys |
| 80 | +} else if { |
| 81 | + print("expected", type_name(key), _quote_str(key), "not in", type_name(obj), "got:", obj) |
| 82 | +} |
| 83 | + |
| 84 | +# METADATA |
| 85 | +# description: Assert value is in obj |
| 86 | +has_value(value, obj) if { |
| 87 | + some _, values in obj |
| 88 | + value == values |
| 89 | +} else := false if { |
| 90 | + print("expected", type_name(value), _quote_str(value), "in", type_name(obj), "got:", obj) |
| 91 | +} |
| 92 | + |
| 93 | +# METADATA |
| 94 | +# description: Assert value is not in obj |
| 95 | +not_has_value(value, obj) := false if { |
| 96 | + some _, values in obj |
| 97 | + value == values |
| 98 | +} else if { |
| 99 | + print("expected", type_name(value), _quote_str(value), "not in", type_name(obj), "got:", obj) |
| 100 | +} |
| 101 | + |
| 102 | +# METADATA |
| 103 | +# description: Assert provided collection is empty |
| 104 | +empty(coll) if { |
| 105 | + count(coll) == 0 |
| 106 | +} else := false if { |
| 107 | + print("expected empty", type_name(coll), "got:", coll) |
| 108 | +} |
| 109 | + |
| 110 | +# METADATA |
| 111 | +# description: Assert provided collection is not empty |
| 112 | +not_empty(coll) if { |
| 113 | + count(coll) != 0 |
| 114 | +} else := false if { |
| 115 | + print("expected not empty", type_name(coll)) |
| 116 | +} |
| 117 | + |
| 118 | +# METADATA |
| 119 | +# description: Assert string starts with search |
| 120 | +starts_with(str, search) if { |
| 121 | + startswith(str, search) |
| 122 | +} else := false if { |
| 123 | + print("expected", _quote_str(str), "to start with", _quote_str(search)) |
| 124 | +} |
| 125 | + |
| 126 | +# METADATA |
| 127 | +# description: Assert string ends with search |
| 128 | +ends_with(str, search) if { |
| 129 | + endswith(str, search) |
| 130 | +} else := false if { |
| 131 | + print("expected", _quote_str(str), "to end with", _quote_str(search)) |
| 132 | +} |
| 133 | + |
| 134 | +# METADATA |
| 135 | +# description: Assert all strings in coll starts with search |
| 136 | +all_starts_with(coll, search) if { |
| 137 | + every str in coll { |
| 138 | + startswith(str, search) |
| 139 | + } |
| 140 | +} else := false if { |
| 141 | + exceptions := [str | some str in coll; not startswith(str, search)] |
| 142 | + print("expected all strings to start with", _append_comma(search), "failed for", exceptions) |
| 143 | +} |
| 144 | + |
| 145 | +# METADATA |
| 146 | +# description: Assert all strings in coll ends with search |
| 147 | +all_ends_with(coll, search) if { |
| 148 | + every str in coll { |
| 149 | + endswith(str, search) |
| 150 | + } |
| 151 | +} else := false if { |
| 152 | + exceptions := [str | some str in coll; not endswith(str, search)] |
| 153 | + print("expected all strings to end with", _append_comma(search), "failed for", exceptions) |
| 154 | +} |
| 155 | + |
| 156 | +# METADATA |
| 157 | +# description: Assert no strings in coll starts with search |
| 158 | +none_starts_with(coll, search) if { |
| 159 | + every str in coll { |
| 160 | + not startswith(str, search) |
| 161 | + } |
| 162 | +} else := false if { |
| 163 | + exceptions := [str | some str in coll; startswith(str, search)] |
| 164 | + print("expected no strings to start with", _append_comma(search), "failed for", exceptions) |
| 165 | +} |
| 166 | + |
| 167 | +# METADATA |
| 168 | +# description: Assert no strings in coll ends with search |
| 169 | +none_ends_with(coll, search) if { |
| 170 | + every str in coll { |
| 171 | + not endswith(str, search) |
| 172 | + } |
| 173 | +} else := false if { |
| 174 | + exceptions := [str | some str in coll; endswith(str, search)] |
| 175 | + print("expected no strings to end with", _append_comma(search), "failed for", exceptions) |
| 176 | +} |
| 177 | + |
| 178 | +# METADATA |
| 179 | +# description: Fail with provided message |
| 180 | +fail(msg) := [][0] if print(msg) |
| 181 | + |
| 182 | +_quote_str(x) := concat("", [`"`, x, `"`]) if is_string(x) |
| 183 | + |
| 184 | +_quote_str(x) := x if not is_string(x) |
| 185 | + |
| 186 | +_append_comma(str) := sprintf("%v,", [_quote_str(str)]) |
0 commit comments