Skip to content

Commit

Permalink
fix: Function tests
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Dec 6, 2024
1 parent 61736a2 commit abe97ff
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 382 deletions.
8 changes: 1 addition & 7 deletions lykiadb-server/src/engine/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ impl Output {
}
assert_eq!(self.out, rv)
}

// TODO(vck): Remove this
pub fn expect_str(&mut self, rv: Vec<String>) {
assert_eq!(self.out.iter().map(|x| x.to_string()).collect::<Vec<String>>(), rv)
}
Expand Down Expand Up @@ -726,12 +726,6 @@ pub mod test_helpers {
out.write().unwrap().expect(output);
}

pub fn assert_out_str(code: &str, output: Vec<String>) {
let (out, mut runtime) = get_runtime();
runtime.interpret(code).unwrap();
out.write().unwrap().expect_str(output);
}

pub fn assert_err(code: &str, error: &str) {
let (_, mut runtime) = get_runtime();
let result = runtime.interpret(code);
Expand Down
51 changes: 51 additions & 0 deletions lykiadb-server/tests/interpreter/functions/anonymous
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#[name=anonymous_fn_0, run=interpreter]>

var $pr = function a() {
test_utils::out("hello");
};

$pr();
a();

---

hello
hello


#[name=anonymous_fn_1_iife, run=interpreter]>

(function a() {
test_utils::out("hello");
})();

a();

---

hello
hello


#[name=anonymous_fn_2, run=interpreter]>

var $pr = function() {
test_utils::out("hello");
};

$pr();

---

hello


#[name=anonymous_fn_3_iife, run=interpreter]>

(function() {
test_utils::out("hello");
})();

---

hello
47 changes: 47 additions & 0 deletions lykiadb-server/tests/interpreter/functions/hof
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#[name=higher_order_0, run=interpreter]>

function f($x, $q) {
$x($q);
};

function g($q) {
test_utils::out($q);
};

for (var $i = 0; $i < 10; $i = $i + 1) {
f(g, $i);
}

---

0
1
2
3
4
5
6
7
8
9


#[name=higher_order_1, run=interpreter]>

function makeCounter() {
var $i = 0;
function count() {
$i = $i + 1;
test_utils::out($i);
};

return count;
};
var $count = makeCounter();
$count();
$count();

---

1
2
140 changes: 140 additions & 0 deletions lykiadb-server/tests/interpreter/functions/resolving_read
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#[name=resolving_read_0, run=interpreter]>

var $a = "global";
{
function showA() {
test_utils::out($a);
};

showA();
var $a = "block";
showA();
}

---

global
global


#[name=resolving_read_1, run=interpreter]>

var $a = "global";
{
function showA() {
test_utils::out($a);
};

showA();
var $a = "block";
showA();

function showB() {
test_utils::out($a);
};

showB();
}

---

global
global
block

#[name=resolving_read_2, run=interpreter]>

{
var $a = "global";
{
function showA() {
test_utils::out($a);
};

showA();
var $a = "block";
showA();
}
}

---

global
global


#[name=resolve_object, run=interpreter]>

var $text = 'outer $text';

var $a = {
myFun: function() {
function pre_define() {
test_utils::out($text);
};
pre_define();

var $text = 'inner $text';

function post_define() {
test_utils::out($text);
};
post_define();
}
};

$a.myFun();

---

outer $text
inner $text


#[name=resolve_deeper_object, run=interpreter]>

var $text = 'outer $text';

var $a = {
b: {
c0: {
myFun: function() {
function pre_define() {
test_utils::out($text);
};
pre_define();

var $text = 'c0 inner $text';

function post_define() {
test_utils::out($text);
};
post_define();
}
},
c1: {
myFun: function() {
function pre_define() {
test_utils::out($text);
};
pre_define();

var $text = 'c1 inner $text';

function post_define() {
test_utils::out($text);
};
post_define();
}
}
}
};
$a.b.c0.myFun();
$a.b.c1.myFun();

---

outer $text
c0 inner $text
outer $text
c1 inner $text
61 changes: 61 additions & 0 deletions lykiadb-server/tests/interpreter/functions/resolving_write
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#[name=resolving_write_0, run=interpreter]>

var $a = "global";
{
function showA() {
test_utils::out($a);
};

var $a = "block";

function showB() {
test_utils::out($a);
};


showA();
showB();

$a = "test";

showA();
showB();
}

---

global
block
global
test


#[name=resolving_write_1, run=interpreter]>

var $a = "global";
{
var $showA = function() {
test_utils::out($a);
};

var $a = "block";

var $showB = function() {
test_utils::out($a);
};

$showA();
$showB();

$a = "test";

$showA();
$showB();
}

---

global
block
global
test
31 changes: 31 additions & 0 deletions lykiadb-server/tests/interpreter/programs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[name=multiple_programs, run=interpreter]>

function resolvedFirst() {
var $a = "global";
{
function showA() {
test_utils::out($a);
};

showA();
var $a = "block";
showA();
}
};
resolvedFirst();

---

global
global

---

resolvedFirst();

---

global
global
global
global
Loading

0 comments on commit abe97ff

Please sign in to comment.