mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 12:22:42 +00:00
added tests
This commit is contained in:
parent
1cca1494a4
commit
660464638f
255 changed files with 7220 additions and 3 deletions
11
interpreter/tests/lox/constructor/arguments.lox
Normal file
11
interpreter/tests/lox/constructor/arguments.lox
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class Foo {
|
||||
init(a, b) {
|
||||
print "init"; // expect: init
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo(1, 2);
|
||||
print foo.a; // expect: 1
|
||||
print foo.b; // expect: 2
|
||||
11
interpreter/tests/lox/constructor/call_init_early_return.lox
Normal file
11
interpreter/tests/lox/constructor/call_init_early_return.lox
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
class Foo {
|
||||
init() {
|
||||
print "init";
|
||||
return;
|
||||
print "nope";
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo(); // expect: init
|
||||
print foo.init(); // expect: init
|
||||
// expect: Foo instance
|
||||
15
interpreter/tests/lox/constructor/call_init_explicitly.lox
Normal file
15
interpreter/tests/lox/constructor/call_init_explicitly.lox
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class Foo {
|
||||
init(arg) {
|
||||
print "Foo.init(" + arg + ")";
|
||||
this.field = "init";
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo("one"); // expect: Foo.init(one)
|
||||
foo.field = "field";
|
||||
|
||||
var foo2 = foo.init("two"); // expect: Foo.init(two)
|
||||
print foo2; // expect: Foo instance
|
||||
|
||||
// Make sure init() doesn't create a fresh instance.
|
||||
print foo.field; // expect: init
|
||||
4
interpreter/tests/lox/constructor/default.lox
Normal file
4
interpreter/tests/lox/constructor/default.lox
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
class Foo {}
|
||||
|
||||
var foo = Foo();
|
||||
print foo; // expect: Foo instance
|
||||
3
interpreter/tests/lox/constructor/default_arguments.lox
Normal file
3
interpreter/tests/lox/constructor/default_arguments.lox
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class Foo {}
|
||||
|
||||
var foo = Foo(1, 2, 3); // expect runtime error: Expected 0 arguments but got 3.
|
||||
10
interpreter/tests/lox/constructor/early_return.lox
Normal file
10
interpreter/tests/lox/constructor/early_return.lox
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
class Foo {
|
||||
init() {
|
||||
print "init";
|
||||
return;
|
||||
print "nope";
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo(); // expect: init
|
||||
print foo; // expect: Foo instance
|
||||
8
interpreter/tests/lox/constructor/extra_arguments.lox
Normal file
8
interpreter/tests/lox/constructor/extra_arguments.lox
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class Foo {
|
||||
init(a, b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
|
||||
var foo = Foo(1, 2, 3, 4); // expect runtime error: Expected 2 arguments but got 4.
|
||||
12
interpreter/tests/lox/constructor/init_not_method.lox
Normal file
12
interpreter/tests/lox/constructor/init_not_method.lox
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
class Foo {
|
||||
init(arg) {
|
||||
print "Foo.init(" + arg + ")";
|
||||
this.field = "init";
|
||||
}
|
||||
}
|
||||
|
||||
fun init() {
|
||||
print "not initializer";
|
||||
}
|
||||
|
||||
init(); // expect: not initializer
|
||||
5
interpreter/tests/lox/constructor/missing_arguments.lox
Normal file
5
interpreter/tests/lox/constructor/missing_arguments.lox
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Foo {
|
||||
init(a, b) {}
|
||||
}
|
||||
|
||||
var foo = Foo(1); // expect runtime error: Expected 2 arguments but got 1.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
class Foo {
|
||||
init() {
|
||||
fun init() {
|
||||
return "bar";
|
||||
}
|
||||
print init(); // expect: bar
|
||||
}
|
||||
}
|
||||
|
||||
print Foo(); // expect: Foo instance
|
||||
5
interpreter/tests/lox/constructor/return_value.lox
Normal file
5
interpreter/tests/lox/constructor/return_value.lox
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class Foo {
|
||||
init() {
|
||||
return "result"; // Error at 'return': Can't return a value from an initializer.
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue