name: "Status tests"
cases:
  - name: "Basic status"
    stdin: |
      cat /non/existent >/dev/null
      echo "[1] Status: $?; pipe status: ${PIPESTATUS[@]}"

  - name: "Parse error status"
    ignore_stderr: true
    stdin: |
      # Generate parse error
      for f done
      echo "[2] Status: $?; pipe status: ${PIPESTATUS[@]}"

  - name: "Pipeline status"
    stdin: |
      /non/existent/program 2>/dev/null | cat
      echo "Status: $?; pipe status: ${PIPESTATUS[@]}"

  - name: "Command substitution status"
    stdin: |
      x=$(echo hi | wc -l)
      echo "[1] Status: $?; pipe status: ${PIPESTATUS[@]}"

      x=$(cat /non/existent 2>/dev/null)
      echo "[2] Status: $?; pipe status: ${PIPESTATUS[@]}"

  - name: "Subshell status"
    stdin: |
      (echo hi | wc -l)
      echo "[1] Status: $?; pipe status: ${PIPESTATUS[@]}"

      (cat /non/existent 2>/dev/null)
      echo "[2] Status: $?; pipe status: ${PIPESTATUS[@]}"

  - name: "Stored status"
    stdin: |
      false
      status=$?
      echo "Stored status from false: $status"

      true
      status=$?
      echo "Stored status from true: $status"

  - name: "Status from assignment"
    stdin: |
      false
      false_status=$?
      assign_status=$?

      echo "Status after assignment: $assign_status"

  - name: "Status within assignments"
    stdin: |
      f() {
        return $1
      }

      x=$(f 42) y=0 z=$? w=$?
      echo "x: $x; y: $y; z: $z; w: $w"

  - name: "Status in for loop"
    stdin: |
      function ret() {
        return $1
      }

      for f in a b; do
        echo "Loop entry: $?"
        ret 1
        echo "Loop 1: $?"
        ret 2
        echo "Loop 2: $?"
      done

      echo "After loop: $?"

  - name: "Status in arithmetic for loop"
    stdin: |
      function ret() {
        return $1
      }

      for ((i = 0; i < 2; i++)); do
        echo "Loop entry: $?"
        ret 1
        echo "Loop 1: $?"
        ret 2
        echo "Loop 2: $?"
      done

      echo "After loop: $?"
