Skip to content

Commit

Permalink
Merge pull request #43 from moonbitlang/fix_moon_new
Browse files Browse the repository at this point in the history
fix: moon new should generate hello_test.mbt
  • Loading branch information
lijunchen authored Jul 23, 2024
2 parents 2c8c62f + ff75ecb commit eaf4847
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 36 deletions.
2 changes: 0 additions & 2 deletions crates/moon/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ pub fn replace_dir(s: &str, dir: &impl AsRef<std::path::Path>) -> String {
.to_str()
.unwrap()
.to_string();
println!("path_str1: {:?}", path_str1);
let s = s.replace("\\\\", "\\");
println!("s: {:?}", s);
let s = s.replace(&path_str1, "$ROOT");
let s = s.replace(
dunce::canonicalize(moonutil::moon_dir::home())
Expand Down
32 changes: 18 additions & 14 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ fn test_moon_new_new() {
check(
&get_stdout_with_args(&hello3, ["test", "-v"]),
expect![[r#"
test moonbitlang/hello/lib/hello_wbtest.mbt::hello ok
test moonbitlang/hello/lib/hello_test.mbt::hello ok
Total tests: 1, passed: 1, failed: 0.
"#]],
);
Expand Down Expand Up @@ -603,7 +603,7 @@ fn test_moon_new_new() {
check(
&get_stdout_with_args(&hello4, ["test", "-v"]),
expect![[r#"
test moonbitlang/hello/lib/hello_wbtest.mbt::hello ok
test moonbitlang/hello/lib/hello_test.mbt::hello ok
Total tests: 1, passed: 1, failed: 0.
"#]],
);
Expand Down Expand Up @@ -670,7 +670,7 @@ fn test_moon_new_snapshot() {
.assert()
.success();
check(
&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap(),
&replace_crlf_to_lf(&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap()),
expect![[r#"
pub fn hello() -> String {
"Hello, world!"
Expand All @@ -697,19 +697,21 @@ fn test_moon_new_snapshot() {
.assert()
.success();
check(
&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap(),
&replace_crlf_to_lf(&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap()),
expect![[r#"
pub fn hello() -> String {
"Hello, world!"
}
"#]],
);
check(
&std::fs::read_to_string(hello.join("lib").join("hello_wbtest.mbt")).unwrap(),
&replace_crlf_to_lf(
&std::fs::read_to_string(hello.join("lib").join("hello_test.mbt")).unwrap(),
),
expect![[r#"
test "hello" {
if hello() != "Hello, world!" {
raise "hello() != \"Hello, world!\""
if @lib.hello() != "Hello, world!" {
raise "@lib.hello() != \"Hello, world!\""
}
}
"#]],
Expand All @@ -719,7 +721,7 @@ fn test_moon_new_snapshot() {
expect!["{}"],
);
check(
&std::fs::read_to_string(hello.join("main").join("main.mbt")).unwrap(),
&replace_crlf_to_lf(&std::fs::read_to_string(hello.join("main").join("main.mbt")).unwrap()),
expect![[r#"
fn main {
println(@lib.hello())
Expand Down Expand Up @@ -771,7 +773,7 @@ fn test_moon_new_snapshot_lib() {
.assert()
.success();
check(
&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap(),
&replace_crlf_to_lf(&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap()),
expect![[r#"
pub fn hello() -> String {
"Hello, world!"
Expand Down Expand Up @@ -799,19 +801,21 @@ fn test_moon_new_snapshot_lib() {
.assert()
.success();
check(
&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap(),
&replace_crlf_to_lf(&std::fs::read_to_string(hello.join("lib").join("hello.mbt")).unwrap()),
expect![[r#"
pub fn hello() -> String {
"Hello, world!"
}
"#]],
);
check(
&std::fs::read_to_string(hello.join("lib").join("hello_wbtest.mbt")).unwrap(),
&replace_crlf_to_lf(
&std::fs::read_to_string(hello.join("lib").join("hello_test.mbt")).unwrap(),
),
expect![[r#"
test "hello" {
if hello() != "Hello, world!" {
raise "hello() != \"Hello, world!\""
if @lib.hello() != "Hello, world!" {
raise "@lib.hello() != \"Hello, world!\""
}
}
"#]],
Expand Down Expand Up @@ -844,7 +848,7 @@ fn test_moon_new_snapshot_lib() {
}"#]],
);
check(
&std::fs::read_to_string(hello.join("top.mbt")).unwrap(),
&replace_crlf_to_lf(&std::fs::read_to_string(hello.join("top.mbt")).unwrap()),
expect![[r#"
pub fn greeting() -> Unit {
println(@lib.hello())
Expand Down
39 changes: 19 additions & 20 deletions crates/moonbuild/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ pub fn moon_new_exec(
// main/main.mbt
{
let main_moon = main_dir.join("main.mbt");
let content = r#"fn main {
println(@lib.hello())
}
"#;
let content = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../moonbuild/template/moon_new_template/main.mbt"
));

let mut file = std::fs::File::create(main_moon).unwrap();
file.write_all(content.as_bytes()).unwrap();
}
Expand Down Expand Up @@ -93,10 +94,10 @@ pub fn moon_new_lib(
// top.mbt
{
let top_mbt = target_dir.join("top.mbt");
let content = r#"pub fn greeting() -> Unit {
println(@lib.hello())
}
"#;
let content = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../moonbuild/template/moon_new_template/top.mbt"
));
let mut file = std::fs::File::create(top_mbt).unwrap();
file.write_all(content.as_bytes()).unwrap();
}
Expand Down Expand Up @@ -171,22 +172,20 @@ fn common(target_dir: &Path, cake_full_name: &str, license: Option<&str>) -> any
// lib/hello.mbt
{
let hello_mbt = lib_dir.join("hello.mbt");
let content = r#"pub fn hello() -> String {
"Hello, world!"
}
"#;
let content = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../moonbuild/template/moon_new_template/hello.mbt"
));
let mut file = std::fs::File::create(hello_mbt).unwrap();
file.write_all(content.as_bytes()).unwrap();
}
// lib/hello_wbtest.mbt
// lib/hello_test.mbt
{
let hello_mbt = lib_dir.join("hello_wbtest.mbt");
let content = r#"test "hello" {
if hello() != "Hello, world!" {
raise "hello() != \"Hello, world!\""
}
}
"#;
let hello_mbt = lib_dir.join("hello_test.mbt");
let content = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../moonbuild/template/moon_new_template/hello_test.mbt"
));

let mut file = std::fs::File::create(hello_mbt).unwrap();
file.write_all(content.as_bytes()).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions crates/moonbuild/template/moon_new_template/hello.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() -> String {
"Hello, world!"
}
5 changes: 5 additions & 0 deletions crates/moonbuild/template/moon_new_template/hello_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test "hello" {
if @lib.hello() != "Hello, world!" {
raise "@lib.hello() != \"Hello, world!\""
}
}
3 changes: 3 additions & 0 deletions crates/moonbuild/template/moon_new_template/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main {
println(@lib.hello())
}
3 changes: 3 additions & 0 deletions crates/moonbuild/template/moon_new_template/top.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn greeting() -> Unit {
println(@lib.hello())
}

0 comments on commit eaf4847

Please sign in to comment.