Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests for lsp_completion, lsp_check #176

Merged
merged 5 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion quartz-vscode/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ connection.onCompletion(async (params) => {
const file = params.textDocument.uri.replace("file://", "");
const isDotCompletion = params.context?.triggerCharacter === ".";

const command = `quartz completion ${file} --project ${path.join(
const command = `quartz completion --project ${path.join(
file,
"..",
".."
Expand Down
52 changes: 51 additions & 1 deletion quartz/main.qz
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,57 @@ module CliCommand {
}
}

@[test]
fun test_parse() {
let command or err = CliCommand::parse(make[vec[string]](
"completion",
"--project",
"/tmp/",
"--line",
"0",
"--column",
"2",
"/tmp/foobar",
));
assert(err == nil);
assert_eq(
command!,
CliCommand {
completion: struct {
input: "/tmp/foobar"?,
line: 0,
column: 2,
stdin: false,
dot: false,
},
},
);

let command or err = CliCommand::parse(make[vec[string]](
"completion",
"--project",
"/tmp/",
"--line",
"0",
"--column",
"2",
"--stdin",
));
assert(err == nil);
assert_eq(
command!,
CliCommand {
completion: struct {
input: nil,
line: 0,
column: 2,
stdin: true,
dot: false,
},
},
);
}

fun start(): nil or error {
let compiler_version = "3.1.1";

Expand Down Expand Up @@ -598,4 +649,3 @@ fun main() {
panic("{}", err!.message);
}
}

133 changes: 132 additions & 1 deletion tests/compile_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use walkdir::WalkDir;

#[derive(Clone, Debug)]
struct RunCommandOutput {
status: std::process::ExitStatus,
success: bool,
stdout: String,
stderr: String,
Expand Down Expand Up @@ -36,6 +37,7 @@ fn run_command(
.unwrap();

Some(RunCommandOutput {
status: output.status,
success: output.status.success(),
stdout: String::from_utf8(output.stdout).unwrap(),
stderr: String::from_utf8(output.stderr).unwrap(),
Expand All @@ -44,7 +46,7 @@ fn run_command(

fn quartz_compile(input_path: &Path, output_path: &Path) -> RunCommandOutput {
run_command(
&format!("./target/release/quartz",),
"./target/release/quartz",
&[
"compile",
"--validate-address",
Expand Down Expand Up @@ -80,6 +82,42 @@ fn quartz_run_wat(input_path: &Path) -> RunCommandOutput {
.unwrap()
}

fn quartz_check(input_path: &Path) -> RunCommandOutput {
run_command(
"./target/release/quartz",
&["check", input_path.to_str().unwrap()],
&[],
vec![
("WAT_FILE", "./build/quartz-current.wat"),
("MODE", "run-wat"),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
)
.unwrap()
}

fn quartz_completion(input_path: &Path, arg: &str) -> RunCommandOutput {
let mut args = vec!["completion"];
args.extend(arg.split(" "));
args.push(input_path.to_str().unwrap());

run_command(
"./target/release/quartz",
&args,
&[],
vec![
("WAT_FILE", "./build/quartz-current.wat"),
("MODE", "run-wat"),
]
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
)
.unwrap()
}

#[derive(Clone, Debug)]
struct QuartzRunOutput {
compile: RunCommandOutput,
Expand Down Expand Up @@ -166,3 +204,96 @@ fn test_run() -> Result<()> {

Ok(())
}

#[test]
fn test_check() -> Result<()> {
use rayon::prelude::*;

WalkDir::new("./tests/lsp_check")
.into_iter()
.filter_map(Result::ok)
.collect::<Vec<_>>()
.into_par_iter()
.map(|entry| -> Result<()> {
let path = entry.path();
let ext = path.extension().and_then(|s| s.to_str());
if ext == Some("qz") {
let stdout_path = path.with_extension("stdout");
let stderr_path = path.with_extension("stderr");
let output = quartz_check(path);

assert!(output.status.success(), "{}", path.display());

if stdout_path.exists() {
let expected = fs::read_to_string(stdout_path)?;
assert_eq!(expected, output.stdout, "{}", path.display(),);
}
if stderr_path.exists() {
let expected_fragment = fs::read_to_string(stderr_path)?;

assert!(
output.stderr.contains(&expected_fragment),
"{}\n\n[expected]\n{}\n[actual]\n{}\n",
path.display(),
expected_fragment,
output.stderr,
);
}
}

Ok(())
})
.collect::<Result<_>>()?;

Ok(())
}

#[test]
fn test_completion() -> Result<()> {
use rayon::prelude::*;

WalkDir::new("./tests/lsp_completion")
.into_iter()
.filter_map(Result::ok)
.collect::<Vec<_>>()
.into_par_iter()
.map(|entry| -> Result<()> {
let path = entry.path();
let ext = path.extension().and_then(|s| s.to_str());
if ext == Some("qz") {
let stdout_path = path.with_extension("stdout");
let stderr_path = path.with_extension("stderr");
let arg_path = path.with_extension("arg");
let output = quartz_completion(path, &fs::read_to_string(arg_path)?);

assert!(
output.status.success(),
"{}\n[stderr] {}\n[stdout] {}",
path.display(),
output.stderr,
output.stdout,
);

if stdout_path.exists() {
let expected = fs::read_to_string(stdout_path)?;
assert_eq!(expected, output.stdout, "{}", path.display(),);
}
if stderr_path.exists() {
let expected_fragment = fs::read_to_string(stderr_path)?;

assert!(
output.stderr.contains(&expected_fragment),
"{}\n\n[expected]\n{}\n[actual]\n{}\n",
path.display(),
expected_fragment,
output.stderr,
);
}
}

Ok(())
})
.collect::<Result<_>>()?;

Ok(())
}
3 changes: 3 additions & 0 deletions tests/lsp_check/test1.qz
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main(): i32 {
return "foobar";
}
1 change: 1 addition & 0 deletions tests/lsp_check/test1.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"end":[1,19],"message":"unification failed: string != i32","start":[1,11]}]
1 change: 1 addition & 0 deletions tests/lsp_completion/test1.arg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--line 10 --column 7 --dot
11 changes: 11 additions & 0 deletions tests/lsp_completion/test1.qz
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct T {
a: i32,
b: i32,
c: i32,
d: string,
}

fun main() {
let t = T { a: 1, b: 2, c: 3, d: "hello" };
t.
}
1 change: 1 addition & 0 deletions tests/lsp_completion/test1.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"items":[{"kind":"field","detail":"i32","label":"c"},{"kind":"field","detail":"string","label":"d"},{"kind":"field","detail":"i32","label":"a"},{"kind":"field","detail":"i32","label":"b"}]}
1 change: 1 addition & 0 deletions tests/lsp_completion/test_broken1.arg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--line 0 --column 0
1 change: 1 addition & 0 deletions tests/lsp_completion/test_broken1.qz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
st