Skip to content

Commit

Permalink
Merge pull request #105 from mrc-ide/test-order-precedence
Browse files Browse the repository at this point in the history
Add another test to solidify groupings precedence
  • Loading branch information
r-ash authored Sep 25, 2023
2 parents 254b829 + 60e338f commit b340174
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/testthat/test-query-search.R
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ test_that("can search for queries using boolean", {
character(0))
})


test_that("query with invalid types does no type coersion", {
root <- create_temporary_root(use_file_store = TRUE)
x <- create_random_packet(root, "x", list(a = TRUE))
Expand All @@ -984,6 +985,7 @@ test_that("query with invalid types does no type coersion", {
character(0))
})


test_that("query with mixed types returns results with valid comparison", {
root <- create_temporary_root(use_file_store = TRUE)
x <- create_random_packet(root, "x", list(a = 2))
Expand All @@ -993,3 +995,31 @@ test_that("query with mixed types returns results with valid comparison", {
orderly_search(quote(parameter:a > 1), root = root),
x)
})


test_that("&& takes precedence over ||", {
## If we have an expression like A || B && C
## R evaluates this as A || (B && C) so make sure we do this in querying too
## i.e. && has higher precedence
## You can see this difference if A is true, B is true and C is false
## A || (B && C) -> TRUE # nolint
## (A || B) && C -> FALSE # nolint

root <- create_temporary_root(use_file_store = TRUE)
x1 <- create_random_packet(root, "x", list(a = TRUE))
x2 <- create_random_packet(root, "x", list(a = FALSE))
y1 <- create_random_packet(root, "y", list(a = "TRUE"))

expect_equal(
orderly_search(quote(name == "y" || parameter:a == TRUE && name == "x"),
root = root),
c(y1, x1))
expect_equal(
orderly_search(quote((name == "y" || parameter:a == TRUE) && name == "x"),
root = root),
x1)
expect_equal(
orderly_search(quote(parameter:a == TRUE && name == "x" || name == "y"),
root = root),
c(x1, y1))
})

0 comments on commit b340174

Please sign in to comment.