add test
Some checks failed
Some checks failed
This commit is contained in:
parent
fff1b21e9b
commit
fdc0f499d2
@ -467,7 +467,7 @@ class E < B {} // expect runtime error
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod qi0 {
|
||||
mod inheritance_super_fun_test {
|
||||
use interpreter_starter_rust::{output::config::create_output_capurer, runner::interpret};
|
||||
|
||||
#[test]
|
||||
@ -608,3 +608,93 @@ child.method(); // expect: Base.method()
|
||||
assert_eq!(result, Ok(()));
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod inheritance_invalid_super_use {
|
||||
use interpreter_starter_rust::{output::config::create_output_capurer, runner::interpret};
|
||||
|
||||
#[test]
|
||||
fn test1() {
|
||||
let capturer = create_output_capurer();
|
||||
let input = r#"
|
||||
class Foo {
|
||||
cook() {
|
||||
// Foo is not a subclass
|
||||
super.cook(); // expect compile error
|
||||
}
|
||||
}
|
||||
"#;
|
||||
let expected_stdout = String::new();
|
||||
let expected_stderr =
|
||||
String::from("[line 5] Error: Can't use 'super' in a class with no superclass.\n");
|
||||
let result = interpret(input);
|
||||
assert_eq!(capturer.get_stdout_content(), expected_stdout);
|
||||
assert_eq!(capturer.get_stderr_content(), expected_stderr);
|
||||
assert_eq!(result, Err(65)); // Expecting a compile error (exit code 65)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test2() {
|
||||
let capturer = create_output_capurer();
|
||||
let input = r#"
|
||||
// super can't be used outside of a class
|
||||
super.notEvenInAClass(); // expect compile error
|
||||
"#;
|
||||
let expected_stdout = String::new();
|
||||
let expected_stderr =
|
||||
String::from("[line 3] Error: Can't use 'super' outside of a class.\n");
|
||||
let result = interpret(input);
|
||||
assert_eq!(capturer.get_stdout_content(), expected_stdout);
|
||||
assert_eq!(capturer.get_stderr_content(), expected_stderr);
|
||||
assert_eq!(result, Err(65)); // Expecting a compile error (exit code 65)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test3() {
|
||||
let capturer = create_output_capurer();
|
||||
let input = r#"
|
||||
class A {
|
||||
method() {}
|
||||
}
|
||||
|
||||
class B < A {
|
||||
method() {
|
||||
// super must be followed by `.`
|
||||
// and an expression
|
||||
(super).method(); // expect compile error
|
||||
}
|
||||
}
|
||||
"#;
|
||||
let expected_stdout = String::new();
|
||||
let expected_stderr = String::from(
|
||||
"[line 10] Error: Expect '.' after 'super'.\n[line 11] Error: Unexpected Token: RIGHT_BRACE\n",
|
||||
);
|
||||
let result = interpret(input);
|
||||
assert_eq!(capturer.get_stdout_content(), expected_stdout);
|
||||
assert_eq!(capturer.get_stderr_content(), expected_stderr);
|
||||
assert_eq!(result, Err(65)); // Expecting a compile error (exit code 65)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test4() {
|
||||
let capturer = create_output_capurer();
|
||||
let input = r#"
|
||||
class A {}
|
||||
|
||||
class B < A {
|
||||
method() {
|
||||
// super must be followed by `.`
|
||||
// and an expression
|
||||
super; // expect compile error
|
||||
}
|
||||
}
|
||||
"#;
|
||||
let expected_stdout = String::new();
|
||||
let expected_stderr = String::from(
|
||||
"[line 8] Error: Expect '.' after 'super'.\n[line 9] Error: Unexpected Token: RIGHT_BRACE\n",
|
||||
);
|
||||
let result = interpret(input);
|
||||
assert_eq!(capturer.get_stdout_content(), expected_stdout);
|
||||
assert_eq!(capturer.get_stderr_content(), expected_stderr);
|
||||
assert_eq!(result, Err(65)); // Expecting a compile error (exit code 65)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user