mirror of
https://github.com/MorizzG/rlox.git
synced 2025-12-06 04:12:42 +00:00
added tests
This commit is contained in:
parent
1cca1494a4
commit
660464638f
255 changed files with 7220 additions and 3 deletions
19
interpreter/tests/lox/super/bound_method.lox
Normal file
19
interpreter/tests/lox/super/bound_method.lox
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
class A {
|
||||
method(arg) {
|
||||
print "A.method(" + arg + ")";
|
||||
}
|
||||
}
|
||||
|
||||
class B < A {
|
||||
getClosure() {
|
||||
return super.method;
|
||||
}
|
||||
|
||||
method(arg) {
|
||||
print "B.method(" + arg + ")";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var closure = B().getClosure();
|
||||
closure("arg"); // expect: A.method(arg)
|
||||
16
interpreter/tests/lox/super/call_other_method.lox
Normal file
16
interpreter/tests/lox/super/call_other_method.lox
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Base {
|
||||
foo() {
|
||||
print "Base.foo()";
|
||||
}
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
bar() {
|
||||
print "Derived.bar()";
|
||||
super.foo();
|
||||
}
|
||||
}
|
||||
|
||||
Derived().bar();
|
||||
// expect: Derived.bar()
|
||||
// expect: Base.foo()
|
||||
16
interpreter/tests/lox/super/call_same_method.lox
Normal file
16
interpreter/tests/lox/super/call_same_method.lox
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Base {
|
||||
foo() {
|
||||
print "Base.foo()";
|
||||
}
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
foo() {
|
||||
print "Derived.foo()";
|
||||
super.foo();
|
||||
}
|
||||
}
|
||||
|
||||
Derived().foo();
|
||||
// expect: Derived.foo()
|
||||
// expect: Base.foo()
|
||||
17
interpreter/tests/lox/super/closure.lox
Normal file
17
interpreter/tests/lox/super/closure.lox
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
class Base {
|
||||
toString() { return "Base"; }
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
getClosure() {
|
||||
fun closure() {
|
||||
return super.toString();
|
||||
}
|
||||
return closure;
|
||||
}
|
||||
|
||||
toString() { return "Derived"; }
|
||||
}
|
||||
|
||||
var closure = Derived().getClosure();
|
||||
print closure(); // expect: Base
|
||||
16
interpreter/tests/lox/super/constructor.lox
Normal file
16
interpreter/tests/lox/super/constructor.lox
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Base {
|
||||
init(a, b) {
|
||||
print "Base.init(" + a + ", " + b + ")";
|
||||
}
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
init() {
|
||||
print "Derived.init()";
|
||||
super.init("a", "b");
|
||||
}
|
||||
}
|
||||
|
||||
Derived();
|
||||
// expect: Derived.init()
|
||||
// expect: Base.init(a, b)
|
||||
14
interpreter/tests/lox/super/extra_arguments.lox
Normal file
14
interpreter/tests/lox/super/extra_arguments.lox
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
class Base {
|
||||
foo(a, b) {
|
||||
print "Base.foo(" + a + ", " + b + ")";
|
||||
}
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
foo() {
|
||||
print "Derived.foo()"; // expect: Derived.foo()
|
||||
super.foo("a", "b", "c", "d"); // expect runtime error: Expected 2 arguments but got 4.
|
||||
}
|
||||
}
|
||||
|
||||
Derived().foo();
|
||||
18
interpreter/tests/lox/super/indirectly_inherited.lox
Normal file
18
interpreter/tests/lox/super/indirectly_inherited.lox
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
class A {
|
||||
foo() {
|
||||
print "A.foo()";
|
||||
}
|
||||
}
|
||||
|
||||
class B < A {}
|
||||
|
||||
class C < B {
|
||||
foo() {
|
||||
print "C.foo()";
|
||||
super.foo();
|
||||
}
|
||||
}
|
||||
|
||||
C().foo();
|
||||
// expect: C.foo()
|
||||
// expect: A.foo()
|
||||
13
interpreter/tests/lox/super/missing_arguments.lox
Normal file
13
interpreter/tests/lox/super/missing_arguments.lox
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class Base {
|
||||
foo(a, b) {
|
||||
print "Base.foo(" + a + ", " + b + ")";
|
||||
}
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
foo() {
|
||||
super.foo(1); // expect runtime error: Expected 2 arguments but got 1.
|
||||
}
|
||||
}
|
||||
|
||||
Derived().foo();
|
||||
7
interpreter/tests/lox/super/no_superclass_bind.lox
Normal file
7
interpreter/tests/lox/super/no_superclass_bind.lox
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class Base {
|
||||
foo() {
|
||||
super.doesNotExist; // Error at 'super': Can't use 'super' in a class with no superclass.
|
||||
}
|
||||
}
|
||||
|
||||
Base().foo();
|
||||
7
interpreter/tests/lox/super/no_superclass_call.lox
Normal file
7
interpreter/tests/lox/super/no_superclass_call.lox
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class Base {
|
||||
foo() {
|
||||
super.doesNotExist(1); // Error at 'super': Can't use 'super' in a class with no superclass.
|
||||
}
|
||||
}
|
||||
|
||||
Base().foo();
|
||||
9
interpreter/tests/lox/super/no_superclass_method.lox
Normal file
9
interpreter/tests/lox/super/no_superclass_method.lox
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
class Base {}
|
||||
|
||||
class Derived < Base {
|
||||
foo() {
|
||||
super.doesNotExist(1); // expect runtime error: Undefined property 'doesNotExist'.
|
||||
}
|
||||
}
|
||||
|
||||
Derived().foo();
|
||||
10
interpreter/tests/lox/super/parenthesized.lox
Normal file
10
interpreter/tests/lox/super/parenthesized.lox
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
class A {
|
||||
method() {}
|
||||
}
|
||||
|
||||
class B < A {
|
||||
method() {
|
||||
// [line 8] Error at ')': Expect '.' after 'super'.
|
||||
(super).method();
|
||||
}
|
||||
}
|
||||
22
interpreter/tests/lox/super/reassign_superclass.lox
Normal file
22
interpreter/tests/lox/super/reassign_superclass.lox
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Base {
|
||||
method() {
|
||||
print "Base.method()";
|
||||
}
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
method() {
|
||||
super.method();
|
||||
}
|
||||
}
|
||||
|
||||
class OtherBase {
|
||||
method() {
|
||||
print "OtherBase.method()";
|
||||
}
|
||||
}
|
||||
|
||||
var derived = Derived();
|
||||
derived.method(); // expect: Base.method()
|
||||
Base = OtherBase;
|
||||
derived.method(); // expect: Base.method()
|
||||
2
interpreter/tests/lox/super/super_at_top_level.lox
Normal file
2
interpreter/tests/lox/super/super_at_top_level.lox
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
super.foo("bar"); // Error at 'super': Can't use 'super' outside of a class.
|
||||
super.foo; // Error at 'super': Can't use 'super' outside of a class.
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
class A {
|
||||
say() {
|
||||
print "A";
|
||||
}
|
||||
}
|
||||
|
||||
class B < A {
|
||||
getClosure() {
|
||||
fun closure() {
|
||||
super.say();
|
||||
}
|
||||
return closure;
|
||||
}
|
||||
|
||||
say() {
|
||||
print "B";
|
||||
}
|
||||
}
|
||||
|
||||
class C < B {
|
||||
say() {
|
||||
print "C";
|
||||
}
|
||||
}
|
||||
|
||||
C().getClosure()(); // expect: A
|
||||
23
interpreter/tests/lox/super/super_in_inherited_method.lox
Normal file
23
interpreter/tests/lox/super/super_in_inherited_method.lox
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
class A {
|
||||
say() {
|
||||
print "A";
|
||||
}
|
||||
}
|
||||
|
||||
class B < A {
|
||||
test() {
|
||||
super.say();
|
||||
}
|
||||
|
||||
say() {
|
||||
print "B";
|
||||
}
|
||||
}
|
||||
|
||||
class C < B {
|
||||
say() {
|
||||
print "C";
|
||||
}
|
||||
}
|
||||
|
||||
C().test(); // expect: A
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
super.bar(); // Error at 'super': Can't use 'super' outside of a class.
|
||||
fun foo() {
|
||||
}
|
||||
8
interpreter/tests/lox/super/super_without_dot.lox
Normal file
8
interpreter/tests/lox/super/super_without_dot.lox
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
class A {}
|
||||
|
||||
class B < A {
|
||||
method() {
|
||||
// [line 6] Error at ';': Expect '.' after 'super'.
|
||||
super;
|
||||
}
|
||||
}
|
||||
7
interpreter/tests/lox/super/super_without_name.lox
Normal file
7
interpreter/tests/lox/super/super_without_name.lox
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
class A {}
|
||||
|
||||
class B < A {
|
||||
method() {
|
||||
super.; // Error at ';': Expect superclass method name.
|
||||
}
|
||||
}
|
||||
16
interpreter/tests/lox/super/this_in_superclass_method.lox
Normal file
16
interpreter/tests/lox/super/this_in_superclass_method.lox
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
class Base {
|
||||
init(a) {
|
||||
this.a = a;
|
||||
}
|
||||
}
|
||||
|
||||
class Derived < Base {
|
||||
init(a, b) {
|
||||
super.init(a);
|
||||
this.b = b;
|
||||
}
|
||||
}
|
||||
|
||||
var derived = Derived("a", "b");
|
||||
print derived.a; // expect: a
|
||||
print derived.b; // expect: b
|
||||
Loading…
Add table
Add a link
Reference in a new issue