Add bit operators, defer, list methods, rand, assert and rex test
- Bit operators & | ^ ~ << >> and their op= forms (Go precedence),
plus hex, binary and octal literals (0xff, 0b1010, 0o755)
- defer stmt / defer { ... }: runs when the block ends, last first,
also on return, ?, break and continue
- List methods insert, remove, clear, contains, index_of, reverse,
sort and sort_by (stable merge sort)
- rand module: int(a, b), float(), seed(n)
- assert(cond[, msg]) and `rex test`, which runs each test_ function
in its own process
- rex.toml marks a project's top folder: imports count from there
- Docs, formatter, LSP completions and editor grammars updated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
910bde692a
commit
300d767124
28 files changed
+1373
-65
No files matched your search
@@ -0,0 +1,48 @@
|
||||
//! Tests of `rex test`.
|
||||
|
||||
use std::process::Command;
|
||||
|
||||
#[test]
|
||||
fn runs_each_test_on_its_own() {
|
||||
let dir = std::env::temp_dir().join(format!("rex-test-cmd-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(dir.join("sub")).unwrap();
|
||||
std::fs::write(
|
||||
dir.join("calc.rex"),
|
||||
"func add(a, b: int): int {\n return a + b\n}\n\nfunc test_add() {\n assert(add(2, 2) == 4)\n}\n\nfunc test_wrong() {\n print(\"shown when it fails\")\n assert(add(2, 2) == 5, \"2 + 2 is {add(2, 2)}\")\n}\n\nfunc test_error(): error {\n return error(\"nope\")\n}\n\nfunc test_panic() {\n var xs: []int\n xs.pop()\n}\n\nfunc test_after_panic() {}\n\nfunc main() {\n print(\"main is not run\")\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
// a file without tests is skipped; one without `main` is fine
|
||||
std::fs::write(dir.join("sub/plain.rex"), "func main() {}\n").unwrap();
|
||||
std::fs::write(dir.join("sub/lib.rex"), "func test_ok(): error {\n return nil\n}\n").unwrap();
|
||||
let o = Command::new(env!("CARGO_BIN_EXE_rex")).arg("test").current_dir(&dir).output().unwrap();
|
||||
let out = String::from_utf8_lossy(&o.stdout).into_owned();
|
||||
assert_eq!(o.status.code(), Some(1), "{out}");
|
||||
for want in [
|
||||
"ok calc.rex: test_add",
|
||||
"FAIL calc.rex: test_wrong",
|
||||
" shown when it fails",
|
||||
" assertion failed: 2 + 2 is 4 (line 11)",
|
||||
"FAIL calc.rex: test_error",
|
||||
" error: nope",
|
||||
"FAIL calc.rex: test_panic",
|
||||
" panic: pop from an empty list",
|
||||
"ok calc.rex: test_after_panic",
|
||||
"ok sub/lib.rex: test_ok",
|
||||
"3 passed, 3 failed",
|
||||
] {
|
||||
assert!(out.contains(want), "missing {want:?} in:\n{out}");
|
||||
}
|
||||
assert!(!out.contains("main is not run"), "{out}");
|
||||
|
||||
// only one file, all passing
|
||||
let o = Command::new(env!("CARGO_BIN_EXE_rex")).args(["test", "sub/lib.rex"]).current_dir(&dir).output().unwrap();
|
||||
assert_eq!(o.status.code(), Some(0), "{}", String::from_utf8_lossy(&o.stdout));
|
||||
|
||||
// a test with parameters is an error
|
||||
std::fs::write(dir.join("sub/lib.rex"), "func test_bad(n: int) {}\n").unwrap();
|
||||
let o = Command::new(env!("CARGO_BIN_EXE_rex")).args(["test", "sub/lib.rex"]).current_dir(&dir).output().unwrap();
|
||||
assert_eq!(o.status.code(), Some(1));
|
||||
assert!(String::from_utf8_lossy(&o.stderr).contains("a test takes nothing"));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user