#!/usr/bin/env tclkit
# %renumber<^\s*test >%

source [file join [file dir [info script]] initests.tcl]

test 0 {} {
    package require vlerq
} $version

set v [vlerq def {A B C} {1 2 3 11 22 33 111 222 333}]

test 1 {} {
    set i 0
    vlerq loop $v c { incr i }
    set i
} 3

test 2 {} {
    set r {}
    vlerq loop $v c { lappend r $c(#) }
    set r
} {0 1 2}
    
test 3 {} {
    set r {}
    vlerq loop $v c { lappend r $c(B) $c(A) }
    set r
} {2 1 22 11 222 111}

set w [vlerq def A {1 2 3 4 5 6 7 8 9}]

test 4 {} {
    vlerq loop $w c -index 1
} {0 1 2 3 4 5 6 7 8}

test 5 {} {
    vlerq loop $w c -index { $c(A) % 2 }
} {0 2 4 6 8}

test 6 {} {
    vlerq loop $w c -index { $c(A) > 3 && $c(A) < 6 }
} {3 4}

test 7 {} {
    vlerq loop $w c -index { $c(A) == $c(#) + 1 }
} {0 1 2 3 4 5 6 7 8}

test 8 {} {
    vlerq get [vlerq loop $w c -where 1]
} {1 2 3 4 5 6 7 8 9}
    
test 9 {} {
    vlerq get [vlerq loop $w c -where { $c(A) % 2 }]
} {1 3 5 7 9}
    
test 10 {} {
    vlerq get [vlerq loop $w c -where { $c(A) > 3 && $c(A) < 6 }]
} {4 5}
    
test 11 {} {
    vlerq get [vlerq loop $w c -where { $c(A) == $c(#) + 1 }]
} {1 2 3 4 5 6 7 8 9}

test 12 {loop var must end up in current namespace} {
    namespace eval t1 { vlerq loop $w c1 -index { $c1(A) % 2 } }
    list [info exists c1] [array get t1::c1]
} {0 {A 9}}
namespace forget t1

unset c
test 13 {loop with parray} -body {
    vlerq loop [vlerq def {A B C} {1 2 3 11 22 33 111 222 333}] c {parray c}
} -output {}

test 14 {loop on single column} {
    set r {}
    vlerq loop [vlerq def A {1 2 3}] c { lappend r $c(A) }
    set r
} {1 2 3}
unset c

test 15 {try to loop on non-array} {
    set c 1
    catch { vlerq loop [vlerq def A {1 2 3}] c { lappend r $c(A) } } r
    unset c
    set r
} {can't trace "c(#)": variable isn't array}

test 16 {} {
    set r {}
    vlerq loop [vlerq def A {1 2 3}] {
      lappend r x$(A)
      if {$(A) >= 2} break
      lappend r y$(A)
    }
    set r
} {x1 y1 x2}

test 17 {} {
    set r {}
    vlerq loop [vlerq def A {1 2 3}] {
      lappend r x$(A)
      if {$(A) >= 2} continue
      lappend r y$(A)
    }
    set r
} {x1 y1 x2 x3}

test 18 {} {
    set r {}
    set c [catch {
      vlerq loop [vlerq def A {1 2 3}] {
        lappend r x$(A)
        if {$(A) >= 2} { error booh! }
        lappend r y$(A)
      }
    } e]
    lappend r $c $e
} {x1 y1 x2 1 booh!}

unset -nocomplain r c e v w i

::tcltest::cleanupTests
