Added _forEach and _map, added tests for them

This commit is contained in:
2023-02-06 00:51:07 +00:00
parent c0ec5ef28e
commit c875ae7a0e
6 changed files with 358 additions and 15 deletions

View File

@@ -1,5 +1,6 @@
import compound;
//test concat
{
//test array concat
{
@@ -44,6 +45,26 @@ import compound;
}
}
//test forEach
{
var counter = 0;
fn p(k, v) {
counter++;
print string k + ": " + string v;
}
var a = ["a", "b"];
var d = ["foo": 1, "bar": 2, "bazz": 3, "fizz": 4];
a.forEach(p);
assert counter == 2, "forEach ran an unusual number of times";
counter = 0;
d.forEach(p);
assert counter == 4, "forEach ran an unusual number of times";
}
//test getKeys
{
var d = ["foo": 1, "bar": 2];
@@ -70,6 +91,28 @@ import compound;
}
//test map
{
//test map with toy functions
{
fn increment(k, v) {
return v + 1;
}
var a = [1, 2, 3];
var d = ["four": 4, "five": 5, "six": 6];
assert a.map(increment).map(increment).map(increment) == [4,5,6], "array.map() failed";
assert d.map(increment).map(increment).map(increment) == [8,9,7], "dictionary.map() failed";
}
//test map with native functions
{
//TODO: write some native functions for use with map
}
}
//test toLower
{
assert "Hello World".toLower() == "hello world", "_toLower() failed";