Write and pass tests

This commit is contained in:
Jeff 2023-08-24 08:05:51 -04:00
parent fabcbdd216
commit 8d04e0bb04
2 changed files with 110 additions and 18 deletions

View File

@ -18,27 +18,22 @@ impl Tool for Transform {
} }
fn run(&self, argument: &Value) -> Result<Value> { fn run(&self, argument: &Value) -> Result<Value> {
let argument = self.check_type(argument)?; let argument = self.check_type(argument)?.as_list()?;
let list = argument[0].as_list()?;
let function = argument[1].as_function()?;
let mut mapped_list = Vec::with_capacity(list.len());
if let Value::List(list) = argument { for value in list {
let list = list[0].as_list()?; let mut context = VariableMap::new();
let function = list[1].as_function()?;
let mut mapped_list = Vec::with_capacity(list.len());
for value in list { context.set_value("input", value.clone())?;
let mut context = VariableMap::new();
context.set_value("input", value.clone())?; let mapped_value = function.run_with_context(&mut context)?;
let mapped_value = function.run_with_context(&mut context)?; mapped_list.push(mapped_value);
mapped_list.push(mapped_value);
}
return Ok(Value::List(mapped_list));
} }
self.fail(argument) return Ok(Value::List(mapped_list));
} }
} }
@ -84,7 +79,12 @@ impl Tool for Count {
identifier: "count", identifier: "count",
description: "Return the number of items in a collection.", description: "Return the number of items in a collection.",
group: "collections", group: "collections",
inputs: vec![ValueType::Any], inputs: vec![
ValueType::String,
ValueType::List,
ValueType::Map,
ValueType::Table,
],
} }
} }

View File

@ -1,10 +1,11 @@
# transform # transform
list = (1, 2, 3); list = (1, 2, 3);
test = transform(list, 'input + 1'); test = transform(list, 'input + 1');
assert_equal((2, 3, 4), test); assert_equal((2, 3, 4), test);
# string # string
test = string(42); test = string(42);
assert_equal("42", test); assert_equal("42", test);
@ -14,9 +15,100 @@ assert_equal("42.42", test);
test = string(false); test = string(false);
assert_equal("false", test); assert_equal("false", test);
# create_table
table = create_table(
("text", "num"),
(
("foo", 1),
("bar", 2)
)
);
# rows
test = rows(table);
assert_equal(
(
("foo", 1),
("bar", 2)
),
test
);
# insert
test = insert(
table,
(
("foo", 1),
("bar", 2)
)
);
assert_equal(
create_table(
("text", "num"),
(
("foo", 1),
("bar", 2),
("foo", 1),
("bar", 2)
)
),
test
);
# select
table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
("b", 2, true),
("a", 3, true)
)
);
test_table = create_table(
("text", "bool"),
(
("a", true),
("b", true),
("a", true)
)
);
assert_equal(select(table, ("text", "bool")), test_table);
test_table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
("a", 3, true)
)
);
assert_equal(where(table, 'text == "a"'), test_table);
# count # count
table = create_table(
("text", "number", "bool"),
(
("a", 1, true),
("b", 2, true),
("a", 3, true)
)
);
test = count(table);
assert_equal(3, test);
test = count("123"); test = count("123");
assert_equal(3, test); assert_equal(3, test);
# create_table test = count(1, 2, 3);
assert_equal(3, test);
map.x.z.y = 1;
test = count(map);
assert_equal(1, test);