name: "Compound commands: arithmetic for"
cases:
  - name: "Single-line arithmetic for loop"
    stdin: |
      for ((i = 0; i < 5; i++)); do echo $i; done
      echo "Result: $?"

  - name: "Arithmetic for loop without separator"
    stdin: |
      for ((i = 0; i < 5; i++)) do echo $i; done
      echo "Result: $?"

  - name: "Arithmetic for loop with empty condition"
    stdin: |
      for (( ; ; )); do
        echo "In loop; status: $?"
        break
      done
    echo "Result: $?"

  - name: "Arithmetic for loop with ;;"
    known_failure: true
    stdin: |
      for ((;;)); do
        echo "In loop; status: $?"
        break
      done
    echo "Result: $?"

  - name: "Break in arithmetic for loop"
    stdin: |
      for ((i = 0; i < 5; i++)); do
        echo $i
        break
      done
      echo "Result: $?"

  - name: "Continue in arithmetic for loop"
    stdin: |
      for ((i = 0; i < 5; i++)); do
        continue
        echo "Should not print"
      done
      echo "Result: $?"

  - name: "Arithmetic for with break in condition inside outer loop"
    stdin: |
      for j in 1 2 3; do
        echo "Outer iteration $j"
        for ((i = 0; break; i++)); do
          echo "Inner body"
        done
        echo "After inner for"
      done
      echo "After outer loop"

  - name: "Arithmetic for with continue in condition inside outer loop"
    stdin: |
      for j in 1 2 3; do
        echo "Outer iteration $j"
        for ((i = 0; continue; i++)); do
          echo "Inner body"
        done
        echo "After inner for"
      done
      echo "After outer loop"

  - name: "Arithmetic for with return in body"
    stdin: |
      f() {
        for ((i = 0; i < 5; i++)); do
          echo "Iteration $i"
          if [ $i -eq 2 ]; then
            return 42
          fi
        done
        echo "After for"
      }
      f
      echo "Exit code: $?"

  - name: "Arithmetic for with exit in body"
    stdin: |
      for ((i = 0; i < 5; i++)); do
        echo "Iteration $i"
        if [ $i -eq 2 ]; then
          exit 42
        fi
      done
      echo "After for"

  - name: "Arithmetic for with alternate syntax"
    stdin: |
      for ((i = 0; i < 5; i++)) {
        echo "Iteration $i"
      }

  - name: "Arithmetic for with alternate syntax (one line)"
    stdin: |
      for ((i = 0; i < 5; i++)) { echo "Iteration $i"; }

  - name: "Arithmetic for loop with nameref"
    known_failure: true
    stdin: |
      target=0
      declare -n ref=target
      for ((ref=1; ref<=3; ref++)); do
        echo "ref=$ref target=$target"
      done
      echo "final target=$target"
