#!/usr/bin/ruby

define r = /(....)/
define s = '1234567'

assert_eq('4567', r.match(s, 3)[0] )
assert_eq('1234', r.match(s, 0)[0] )
assert_eq('1234', r.match(s, -1)[0] )
assert_eq('1234', r.match(s, 0.7)[0] )

# No matching when pos >= length(str)
#assert_eq('1234', r.match(s, 11)[0] )
#assert_eq('1234', r.match(s, 7)[0] )
#assert_eq('1234', r.match(s, 8)[0] )

do {
    var captures = []
    var str = "foo -- bar -- baz"
    var pos = 0

    while (/(\w+)/.match(str, pos)) {|m|
        captures << "#{m}"
        pos = m.pos[1]
    }

    assert_eq(captures, ['foo', 'bar', 'baz'])
}

say "** Test passed!"
