61890b15cb
* wip * move print and JSON functions to modules * builtin functions are not replacable now * builtin functions are added for default nil symbol table * importables: builtin modules and source modules * refactoring runtime tests * fix tests * update documentation * cleanup * clean up cli * fix REPL prints
25 lines
1.3 KiB
Go
25 lines
1.3 KiB
Go
package runtime_test
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestForIn(t *testing.T) {
|
|
// array
|
|
expect(t, `out = 0; for x in [1, 2, 3] { out += x }`, nil, 6) // value
|
|
expect(t, `out = 0; for i, x in [1, 2, 3] { out += i + x }`, nil, 9) // index, value
|
|
expect(t, `out = 0; func() { for i, x in [1, 2, 3] { out += i + x } }()`, nil, 9) // index, value
|
|
expect(t, `out = 0; for i, _ in [1, 2, 3] { out += i }`, nil, 3) // index, _
|
|
expect(t, `out = 0; func() { for i, _ in [1, 2, 3] { out += i } }()`, nil, 3) // index, _
|
|
|
|
// map
|
|
expect(t, `out = 0; for v in {a:2,b:3,c:4} { out += v }`, nil, 9) // value
|
|
expect(t, `out = ""; for k, v in {a:2,b:3,c:4} { out = k; if v==3 { break } }`, nil, "b") // key, value
|
|
expect(t, `out = ""; for k, _ in {a:2} { out += k }`, nil, "a") // key, _
|
|
expect(t, `out = 0; for _, v in {a:2,b:3,c:4} { out += v }`, nil, 9) // _, value
|
|
expect(t, `out = ""; func() { for k, v in {a:2,b:3,c:4} { out = k; if v==3 { break } } }()`, nil, "b") // key, value
|
|
|
|
// string
|
|
expect(t, `out = ""; for c in "abcde" { out += c }`, nil, "abcde")
|
|
expect(t, `out = ""; for i, c in "abcde" { if i == 2 { continue }; out += c }`, nil, "abde")
|
|
}
|