From 145d4b8176e4c5330354c62a315ceb026c3f14e9 Mon Sep 17 00:00:00 2001 From: arapower Date: Wed, 29 May 2024 08:30:29 +0900 Subject: [PATCH] Add test code: For dir_exist, file_exist, and lines functions --- src/tests/stdlib.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/src/tests/stdlib.rs b/src/tests/stdlib.rs index d8f01249..98864331 100644 --- a/src/tests/stdlib.rs +++ b/src/tests/stdlib.rs @@ -309,3 +309,62 @@ fn includes() { "; test_amber!(code, "Found") } + +#[test] +fn dir_exist() { + let temp_dir = tempdir().expect("Failed to create temporary directory"); + + let code = format!( + " + import * from \"std\" + main {{ + if dir_exist(\"{tmpdir}\") {{ + echo \"Found\" + }} else {{ + echo \"Not Found\" + }} + }} + ", + tmpdir = temp_dir.path().to_str().unwrap() + ); + test_amber!(code, "Found") +} + +#[test] +fn file_exist() { + let temp_dir = tempdir().expect("Failed to create temporary directory"); + let file_path = temp_dir.path().join("test_file.txt"); + + let _file = fs::File::create(&file_path).expect("Failed to create temporary file"); + + let code = format!( + " + import * from \"std\" + main {{ + if file_exist(\"{file_path}\") {{ + echo \"Found\" + }} else {{ + echo \"Not Found\" + }} + }} + ", + file_path = file_path.to_str().unwrap() + ); + test_amber!(code, "Found"); + + fs::remove_file(&file_path).expect("Failed to delete temporary file"); +} + +#[test] +fn lines() { + let code = " + import { lines } from \"std\" + main { + loop line in lines(\"hello\\nworld\") { + echo \"line: \" + line + } + } + "; + test_amber!(code, "line: hello\nline: world") +} +