Dino compiler: language, formatter, language server, docs, editor support
Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e2779f447
commit
eaf9567a36
117 files changed
+19970
No files matched your search
+106
@@ -0,0 +1,106 @@
|
||||
//! Tests of `dino fmt`.
|
||||
|
||||
use dino::fmt::format;
|
||||
|
||||
fn fmt(src: &str) -> String {
|
||||
format(src).unwrap_or_else(|d| panic!("{}", d.render("test.dino", src)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixes_indentation_and_spaces() {
|
||||
let messy = "func add( a:int,b :int ):int{\nreturn a+b\n}\n\n\n\nfunc main(){\nvar x=add(1,2)*3 -1\nif x>3{print( \"big\" )}else{print(-x)}\nfor i in 0..10 step 2{ print(i)}\n}\n";
|
||||
// line breaks stay where they are: a block written on one line stays on one line
|
||||
let want = "func add(a: int, b: int): int {\n return a + b\n}\n\nfunc main() {\n var x = add(1, 2) * 3 - 1\n if x > 3 { print(\"big\") } else { print(-x) }\n for i in 0..10 step 2 { print(i) }\n}\n";
|
||||
assert_eq!(fmt(messy), want);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn literals_and_types_stay_tight() {
|
||||
let src = "struct P{x,y:float}\nfunc main(){\nvar p=P{x:1.0,y:2.0}\nvar m=map[string,int]{}\nvar n={\"a\":1,\"b\":2}\nvar xs:[]int=[1,2,3]\nvar o:int?=nil\nvar s=Stack[int]{}\nprint(p,m,n,xs,o,s)\n}\n";
|
||||
let out = fmt(src);
|
||||
// struct bodies are blocks; literals have no inner spaces
|
||||
assert!(out.contains("var p = P{x: 1.0, y: 2.0}"), "{out}");
|
||||
assert!(out.contains("var m = map[string, int]{}"), "{out}");
|
||||
assert!(out.contains("var n = {\"a\": 1, \"b\": 2}"), "{out}");
|
||||
assert!(out.contains("var xs: []int = [1, 2, 3]"), "{out}");
|
||||
assert!(out.contains("var o: int? = nil"), "{out}");
|
||||
assert!(out.contains("var s = Stack[int]{}"), "{out}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn comments_and_blank_lines_survive() {
|
||||
let src = "// top\nfunc main() {\n\n\n var a = 1 // one\n var bbb = 2 // two\n\n // standalone\n print(a, bbb)\n\n}\n";
|
||||
let want = "// top\nfunc main() {\n var a = 1 // one\n var bbb = 2 // two\n\n // standalone\n print(a, bbb)\n}\n";
|
||||
assert_eq!(fmt(src), want);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closures_and_continuation_lines() {
|
||||
let src = "func main() {\nrun(func() {\nprint(1)\n})\nvar total = 1 +\n2 +\n3\nvar xs = [\n1,\n2,\n]\n}\n";
|
||||
let want = "func main() {\n run(func() {\n print(1)\n })\n var total = 1 +\n 2 +\n 3\n var xs = [\n 1,\n 2,\n ]\n}\n";
|
||||
assert_eq!(fmt(src), want);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strings_are_left_alone() {
|
||||
let src = "func main() {\nprint(\"a {x+1} b\", 'x', '\\n')\n}\n";
|
||||
let out = fmt(src);
|
||||
assert!(out.contains("\"a {x+1} b\""), "{out}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn already_formatted_code_does_not_change() {
|
||||
let src = "func main() {\n var s = \"é\"\n for i, c in s {\n match c {\n 'a' => print(i)\n _ => {\n print(-i)\n }\n }\n }\n}\n";
|
||||
assert_eq!(fmt(src), src);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn broken_code_is_refused() {
|
||||
assert!(format("func main( {").is_err());
|
||||
}
|
||||
|
||||
fn dino_files(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
|
||||
for e in std::fs::read_dir(dir).unwrap() {
|
||||
let p = e.unwrap().path();
|
||||
if p.is_dir() {
|
||||
dino_files(&p, out);
|
||||
} else if p.extension().map_or(false, |x| x == "dino") {
|
||||
out.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn examples_are_formatted_and_formatting_is_stable() {
|
||||
let mut files = Vec::new();
|
||||
dino_files(&std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("examples"), &mut files);
|
||||
assert!(files.len() > 30);
|
||||
for f in files {
|
||||
let src = std::fs::read_to_string(&f).unwrap();
|
||||
let once = fmt(&src);
|
||||
assert_eq!(once, src, "{} is not formatted (run `dino fmt examples`)", f.display());
|
||||
assert_eq!(fmt(&once), once, "formatting {} twice differs", f.display());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn doc_examples_are_stable() {
|
||||
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("docs");
|
||||
let mut n = 0;
|
||||
for e in std::fs::read_dir(dir).unwrap() {
|
||||
let p = e.unwrap().path();
|
||||
let text = std::fs::read_to_string(&p).unwrap();
|
||||
let mut rest = text.as_str();
|
||||
while let Some(start) = rest.find("```dino\n") {
|
||||
let after = &rest[start + 8..];
|
||||
let end = after.find("```").unwrap();
|
||||
let code = &after[..end];
|
||||
rest = &after[end + 3..];
|
||||
if let Ok(once) = format(code) {
|
||||
assert_eq!(once, code, "doc example in {} is not formatted:\n{code}", p.display());
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
assert!(n > 20, "only {n} doc examples were formattable");
|
||||
}
|
||||
Reference in new issue
Block a user