Skip to content

Instantly share code, notes, and snippets.

@kmcquade
Last active September 27, 2019 19:45
Show Gist options
  • Save kmcquade/7754cdc9c2604e5ff891cfca8d1e6ec6 to your computer and use it in GitHub Desktop.
Save kmcquade/7754cdc9c2604e5ff891cfca8d1e6ec6 to your computer and use it in GitHub Desktop.
Rego negation question
package common
# ---------------------------------------------------------------------------------------------------------------------
# Not Working
# ---------------------------------------------------------------------------------------------------------------------
list_should_not_contain(attributes_list, undesired_item) {
list_item = attributes_list[_]
list_item != undesired_item
}
# ---------------------------------------------------------------------------------------------------------------------
# Working
# ---------------------------------------------------------------------------------------------------------------------
### These ones are working ###
# Determines if a dict/hash contains a key
list_should_contain(attributes_list, desired_item) {
list_item = attributes_list[_]
list_item == desired_item
}
package commmon
# ---------------------------------------------------------------------------------------------------------------------
# Not working
# ---------------------------------------------------------------------------------------------------------------------
# list_should_not_contain
test_list_should_not_contain {
attributes_list = [
"marshall",
"mathers",
"haley",
"kim"
]
undesired_item := "jayz"
list_should_not_contain(attributes_list, undesired_item)
}
test_list_should_not_contain_fail {
attributes_list = [
"marshall",
"mathers",
"haley",
"kim"
]
undesired_item := "marshall"
not list_should_not_contain(attributes_list, undesired_item)
}
# ---------------------------------------------------------------------------------------------------------------------
# Working
# ---------------------------------------------------------------------------------------------------------------------
# list_should_contain
test_list_should_contain {
attributes_list = [
"marshall",
"mathers",
"haley",
"kim"
]
desired_item := "marshall"
list_should_contain(attributes_list, desired_item)
}
test_list_should_contain_fail {
attributes_list = [
"marshall",
"mathers",
"haley",
"kim"
]
desired_item := "jayz"
not list_should_contain(attributes_list, desired_item)
}

Hi,

I think I'm having an issue with double negation in Rego. Can I get some help on this?

Output of opa test -v . --explain fails

FAILURES
--------------------------------------------------------------------------------
data.common.test_list_should_not_contain_fail: FAIL (369.077µs)

  Enter data.common.test_list_should_not_contain_fail = _
  | Enter data.common.test_list_should_not_contain_fail
  | | Enter data.common.list_should_not_contain(attributes_list, __local13__)
  | | | Enter data.common.list_should_not_contain
  | | | | Fail neq(list_item, undesired_item)
  | | Fail not data.common.list_should_not_contain(attributes_list, __local13__)
  | Fail data.common.test_list_should_not_contain_fail = _

SUMMARY
--------------------------------------------------------------------------------
data.common.test_should_not_equal: PASS (638.863µs)
data.common.test_should_not_equal_fail: PASS (314.325µs)
data.common.test_should_equal: PASS (283.209µs)
data.common.test_should_equal_fail: PASS (286.037µs)
data.common.test_should_have_property: PASS (292.273µs)
data.common.test_should_have_property_fail: PASS (359.747µs)
data.common.test_list_should_contain: PASS (327.73µs)
data.common.test_list_should_contain_fail: PASS (331.304µs)
data.common.test_list_should_not_contain: PASS (383.242µs)
data.common.test_list_should_not_contain_fail: FAIL (369.077µs)
--------------------------------------------------------------------------------
PASS: 9/10
FAIL: 1/10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment