1
0

Fix u tests and examples

This commit is contained in:
Jeff 2023-08-22 22:20:42 -04:00
parent 5cd1629738
commit e7e8c4f9e5
16 changed files with 84 additions and 102 deletions

View File

@ -1,9 +0,0 @@
loop('
speed = cpu_speed();
message = "Current CPU clock: " + string(speed);
output(message);
wait 300;
');

4
examples/fetch.ds Normal file
View File

@ -0,0 +1,4 @@
raw_data = download("https://api.sampleapis.com/futurama/cast");
data = from_json(raw_data);
assert_equal("Billy West", data.0.name);

View File

@ -1,4 +0,0 @@
raw_data = download "https://api.sampleapis.com/futurama/cast";
data = raw_data:from_json();
first = data:get 0;
first.name

View File

@ -1 +0,0 @@
output "Hello world!"

View File

@ -1,7 +0,0 @@
x = 1;
y = 2;
z = 3;
a = "foo";
b = "bar";
c = "foobar";
d = ("abc", "123", "xyz");

View File

@ -1,14 +0,0 @@
create_user = '
names = ("bob", "bill", "mary", "susan");
(random_integer(), names:random(), random_integer(0, 100))
';
create_table(
("id", "name", "age"),
(
repeat (
create_user,
10
)
)
)

View File

@ -1,21 +0,0 @@
enable_copr_repositories = ("varlad/helix");
enable_rpm_repositories (
"https://mirrors.rpmfusion.org/free/fedora/rpmfusion-free-release-38.noarch.rpm",
"https://mirrors.rpmfusion.org/free/fedora/rpmfusion-nonfree-release-38.noarch.rpm"
);
install_package (
"bat",
"egl-wayland",
"fd-find",
"fish",
"fzf",
"lsd",
"kitty",
"helix",
"libwayland-cursor",
"libwayland-egl",
"wl-clipboard"
);
sh "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh";
sh "cargo install starship zoxide";

View File

@ -1,3 +0,0 @@
output "foo...";
wait 1;
output "bar!";

View File

@ -29,8 +29,21 @@ pub fn eval(string: &str) -> Result<Value> {
/// ``` /// ```
/// ///
/// *See the [crate doc](index.html) for more examples and explanations of the expression format.* /// *See the [crate doc](index.html) for more examples and explanations of the expression format.*
pub fn eval_with_context(string: &str, context: &mut VariableMap) -> Result<Value> { pub fn eval_with_context(input: &str, context: &mut VariableMap) -> Result<Value> {
let split = string.split_once("::"); let without_comments = input
.lines()
.map(|line| {
let split = line.split_once('#');
if let Some((code, _comment)) = split {
code
} else {
line
}
})
.collect::<String>();
let split = without_comments.split_once("->");
if let Some((left, right)) = split { if let Some((left, right)) = split {
let left_result = tree::tokens_to_operator_tree(token::tokenize(left)?)? let left_result = tree::tokens_to_operator_tree(token::tokenize(left)?)?
@ -42,6 +55,7 @@ pub fn eval_with_context(string: &str, context: &mut VariableMap) -> Result<Valu
Ok(right_result) Ok(right_result)
} else { } else {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context) tree::tokens_to_operator_tree(token::tokenize(&without_comments)?)?
.eval_with_context_mut(context)
} }
} }

View File

@ -41,7 +41,7 @@ impl VariableMap {
for (key, value) in &self.variables { for (key, value) in &self.variables {
if identifier == key { if identifier == key {
if let Ok(function) = value.as_function() { if let Ok(function) = value.as_function() {
let mut context = VariableMap::new(); let mut context = self.clone();
context.set_value("input", argument.clone())?; context.set_value("input", argument.clone())?;

View File

@ -8,3 +8,31 @@ fn collections() {
eval(&file_contents).unwrap(); eval(&file_contents).unwrap();
} }
#[test]
fn list() {
let file_contents = read_to_string("tests/list.ds").unwrap();
eval(&file_contents).unwrap();
}
#[test]
fn table() {
let file_contents = read_to_string("tests/table.ds").unwrap();
eval(&file_contents).unwrap();
}
#[test]
fn variables() {
let file_contents = read_to_string("tests/variables.ds").unwrap();
eval(&file_contents).unwrap();
}
#[test]
fn scope() {
let file_contents = read_to_string("tests/scope.ds").unwrap();
eval(&file_contents).unwrap();
}

12
tests/list.ds Normal file
View File

@ -0,0 +1,12 @@
# Lists are created by grouping items in partheses and separating them with
# commas.
numbers = (1, 2, 3);
# To access the values in a list, use an integer as an index.
x = numbers.0;
y = numbers.1;
z = numbers.2;
assert_equal(x + y, z);

View File

@ -1,39 +0,0 @@
use whale_lib::*;
#[test]
fn assert() {
eval("assert(true)").unwrap();
eval("assert(false)").unwrap_err();
}
#[test]
fn assert_equal() {
eval("assert_equal(true, true)").unwrap();
eval("assert_equal(true, false)").unwrap_err();
}
#[test]
fn r#if() {
eval("if(true, assert(true))").unwrap();
let value = eval("if(true, 1)").unwrap();
assert_eq!(Value::Integer(1), value);
let value = eval("if(false, 1)").unwrap();
assert_eq!(Value::Empty, value);
}
#[test]
fn r#if_else() {
let value = eval("if_else(true, 1, 2)").unwrap();
assert_eq!(Value::Integer(1), value);
let value = eval("if_else(false, 1, 2)").unwrap();
assert_eq!(Value::Integer(2), value);
let value = eval("if_else(true, '1', '2')").unwrap();
assert_eq!(Value::Integer(1), value);
let value = eval("if_else(false, '1', '2')").unwrap();
assert_eq!(Value::Integer(2), value);
}

9
tests/scope.ds Normal file
View File

@ -0,0 +1,9 @@
foo = "bar";
func = 'foo';
assert_equal("bar", func());
foo = "xyz";
assert_equal("xyz", func());

13
tests/variables.ds Normal file
View File

@ -0,0 +1,13 @@
# Dust is data-oriented, so variables are declared with minimal syntax. A
# single character, the assignment operator (`=`), sets a variable.
x = 1;
y = "hello dust!";
z = 42.0;
list = (3, 2, x);
big_list = (x, y, z, list);
map.x = "foobar";
function = '
message = "I am a function!";
output message;
';