cases:
  - note: short circuit
    query: data.x.p = 1
    modules:
      - |
        package x
        import rego.v1
        p = 1 if { true }
        else = 2 if { true }
    want_defined: true
  - note: fallthrough
    query: data.x.p = 2
    modules:
      - |
        package x
        import rego.v1
        p = 1 if { false }
        else = 2 if { true }
        else = 3 if { true }
    want_defined: true
  - note: fallthrough second
    query: data.x.p = 3
    modules:
      - |
        package x
        import rego.v1
        p = 1 if { false }
        else = 2 if { false }
        else = 3 if { true }
    want_defined: true
  - note: fallthrough multiple
    query: data.x.p = 5
    modules:
      - |
        package x
        import rego.v1
        p = 1 if { false }
        else = 2 if { false }
        else = 3 if { false }
        p = 4 if { false }
        else = 5 if { true }
    want_defined: true
  - note: short circuit (iteration)
    query: data.x.p = 1
    modules:
      - |
        package x
        import rego.v1
        p = 1 if { input.x[_] = 1 }
        else = 2 if { true }
    input: {"x": [3,2,1]}
    want_defined: true
  - note: fallthrough (iteration)
    query: data.x.p = 1
    modules:
      - |
        package x
        import rego.v1
        p = 1 if { input.x[_] = "deadbeef" }
        else = 2 if { input.x[_] = 1 }
    input: {"x": [3,2,1]}
    want_defined: false
  - note: chain
    query: data.x.p = 3
    modules:
      - |
        package x
        import rego.v1
        p = 1 if { false }
        else = q if { true }
        q = 2 if { false }
        else = r if { true }
        r = 3
    want_defined: true
  - note: chain (input)
    query: data.x.p = 2
    modules:
      - |
        package x
        import rego.v1
        p = q
        q = 1 if { input.deadbeef = 1 }
        else = 2 if { input.x = 2 }
    want_defined: true
    input: {"x": 2}
  - note: functions short circuit
    query: data.x.f(1) = 1
    modules:
      - |
        package x
        import rego.v1
        f(x) = 1 if { true }
        else = 2 if { true }
    want_defined: true
  - note: functions fallthrough
    query: data.x.f(1) = 3
    modules:
      - |
        package x
        import rego.v1
        f(x) = 1 if { false }
        else = 2 if { false }
        else = 3 if { true }
    want_defined: true
  - note: short-circuit after failing
    query: data.x.p = 1
    modules:
      - |
        package x
        import rego.v1

        # data.y[_] causes the planner to scan the virtual _and_ base document trees.
        # The virtual document scan will succeed (because of the second module)
        # but the base document scan will fail (because no base documents are loaded).
        #
        # This test ensures that the else keyword short-circuits as expected and does
        # not evaluate the second block even though a failure has occurred.
        p = x if { x = data.y[_] }
        else = 2 if { true }
      - |
        package y
        q = 1
    want_defined: true