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

Simplify test macros #4371

Merged
merged 1 commit into from
Mar 19, 2025
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
9 changes: 3 additions & 6 deletions compiler-core/src/javascript/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,18 @@ mod use_;
pub static CURRENT_PACKAGE: &str = "thepackage";

#[macro_export]
macro_rules! assert_js_with_multiple_imports {
($(($name:literal, $module_src:literal)),+; $src:literal) => {
macro_rules! assert_js {
($(($name:literal, $module_src:literal)),+, $src:literal $(,)?) => {
let compiled =
$crate::javascript::tests::compile_js($src, vec![$((CURRENT_PACKAGE, $name, $module_src)),*]).expect("compilation failed");
$crate::javascript::tests::compile_js($src, vec![$(($crate::javascript::tests::CURRENT_PACKAGE, $name, $module_src)),*]).expect("compilation failed");
let mut output = String::from("----- SOURCE CODE\n");
for (name, src) in [$(($name, $module_src)),*] {
output.push_str(&format!("-- {name}.gleam\n{src}\n\n"));
}
output.push_str(&format!("-- main.gleam\n{}\n\n----- COMPILED JAVASCRIPT\n{compiled}", $src));
insta::assert_snapshot!(insta::internals::AutoName, output, $src);
};
}

#[macro_export]
macro_rules! assert_js {
(($dep_package:expr, $dep_name:expr, $dep_src:expr), $src:expr $(,)?) => {{
let compiled =
$crate::javascript::tests::compile_js($src, vec![($dep_package, $dep_name, $dep_src)])
Expand Down
110 changes: 25 additions & 85 deletions compiler-core/src/javascript/tests/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const that = ThatOneIsAMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchMuchLongerVar
#[test]
fn zero_arity_imported() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two }"#),
("other", r#"pub type One { Two }"#),
r#"import other
pub fn main() {
other.Two
Expand All @@ -58,7 +58,7 @@ pub fn main() {
#[test]
fn zero_arity_imported_unqualified() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two }"#),
("other", r#"pub type One { Two }"#),
r#"import other.{Two}
pub fn main() {
Two
Expand All @@ -80,7 +80,7 @@ pub fn main() {
#[test]
fn zero_arity_imported_unqualified_aliased() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two }"#),
("other", r#"pub type One { Two }"#),
r#"import other.{Two as Three}
pub fn main() {
Three
Expand All @@ -102,7 +102,7 @@ pub fn main() {
#[test]
fn const_zero_arity_imported() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two }"#),
("other", r#"pub type One { Two }"#),
r#"import other
const x = other.Two
"#,
Expand All @@ -112,7 +112,7 @@ const x = other.Two
#[test]
fn const_zero_arity_imported_unqualified() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two }"#),
("other", r#"pub type One { Two }"#),
r#"import other.{Two}
const a = Two
"#,
Expand Down Expand Up @@ -328,7 +328,7 @@ fn go(x) {
#[test]
fn imported_no_label() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two(Int) }"#),
("other", r#"pub type One { Two(Int) }"#),
r#"import other
pub fn main() {
other.Two(1)
Expand All @@ -339,11 +339,7 @@ pub fn main() {
#[test]
fn imported_ignoring_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other
pub fn main() {
other.Two(1)
Expand All @@ -354,11 +350,7 @@ pub fn main() {
#[test]
fn imported_using_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other
pub fn main() {
other.Two(field: 1)
Expand All @@ -369,11 +361,7 @@ pub fn main() {
#[test]
fn imported_multiple_fields() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(a: Int, b: Int, c: Int) }"#
),
("other", r#"pub type One { Two(a: Int, b: Int, c: Int) }"#),
r#"import other
pub fn main() {
other.Two(b: 2, c: 3, a: 1)
Expand All @@ -384,7 +372,7 @@ pub fn main() {
#[test]
fn unqualified_imported_no_label() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two(Int) }"#),
("other", r#"pub type One { Two(Int) }"#),
r#"import other.{Two}
pub fn main() {
Two(1)
Expand All @@ -406,11 +394,7 @@ pub fn main() {
#[test]
fn unqualified_imported_ignoring_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other.{Two}
pub fn main() {
Two(1)
Expand All @@ -421,11 +405,7 @@ pub fn main() {
#[test]
fn unqualified_imported_using_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other.{Two}
pub fn main() {
Two(field: 1)
Expand All @@ -436,11 +416,7 @@ pub fn main() {
#[test]
fn unqualified_imported_multiple_fields() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(a: Int, b: Int, c: Int) }"#
),
("other", r#"pub type One { Two(a: Int, b: Int, c: Int) }"#),
r#"import other.{Two}
pub fn main() {
Two(b: 2, c: 3, a: 1)
Expand All @@ -451,11 +427,7 @@ pub fn main() {
#[test]
fn constructor_as_value() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(a: Int, b: Int, c: Int) }"#
),
("other", r#"pub type One { Two(a: Int, b: Int, c: Int) }"#),
r#"import other
pub fn main() {
other.Two
Expand All @@ -466,11 +438,7 @@ pub fn main() {
#[test]
fn unqualified_constructor_as_value() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(a: Int, b: Int, c: Int) }"#
),
("other", r#"pub type One { Two(a: Int, b: Int, c: Int) }"#),
r#"import other.{Two}
pub fn main() {
Two
Expand All @@ -481,7 +449,7 @@ pub fn main() {
#[test]
fn const_imported_no_label() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two(Int) }"#),
("other", r#"pub type One { Two(Int) }"#),
r#"import other
pub const main = other.Two(1)
"#,
Expand All @@ -491,11 +459,7 @@ pub const main = other.Two(1)
#[test]
fn const_imported_ignoring_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other
pub const main = other.Two(1)
"#,
Expand All @@ -505,11 +469,7 @@ pub const main = other.Two(1)
#[test]
fn const_imported_using_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other
pub const main = other.Two(field: 1)
"#,
Expand All @@ -519,11 +479,7 @@ pub const main = other.Two(field: 1)
#[test]
fn const_imported_multiple_fields() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(a: Int, b: Int, c: Int) }"#
),
("other", r#"pub type One { Two(a: Int, b: Int, c: Int) }"#),
r#"import other
pub const main = other.Two(b: 2, c: 3, a: 1)
"#,
Expand All @@ -533,7 +489,7 @@ pub const main = other.Two(b: 2, c: 3, a: 1)
#[test]
fn const_unqualified_imported_no_label() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { Two(Int) }"#),
("other", r#"pub type One { Two(Int) }"#),
r#"import other.{Two}
pub const main = Two(1)
"#,
Expand All @@ -543,11 +499,7 @@ pub const main = Two(1)
#[test]
fn const_unqualified_imported_ignoring_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other.{Two}
pub const main = Two(1)
"#,
Expand All @@ -557,11 +509,7 @@ pub const main = Two(1)
#[test]
fn const_unqualified_imported_using_label() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(field: Int) }"#
),
("other", r#"pub type One { Two(field: Int) }"#),
r#"import other.{Two}
pub const main = Two(field: 1)
"#,
Expand All @@ -571,11 +519,7 @@ pub const main = Two(field: 1)
#[test]
fn const_unqualified_imported_multiple_fields() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(a: Int, b: Int, c: Int) }"#
),
("other", r#"pub type One { Two(a: Int, b: Int, c: Int) }"#),
r#"import other.{Two}
pub const main = Two(b: 2, c: 3, a: 1)
"#,
Expand All @@ -585,11 +529,7 @@ pub const main = Two(b: 2, c: 3, a: 1)
#[test]
fn imported_pattern() {
assert_js!(
(
CURRENT_PACKAGE,
"other",
r#"pub type One { Two(a: Int, b: Int, c: Int) }"#
),
("other", r#"pub type One { Two(a: Int, b: Int, c: Int) }"#),
r#"import other.{Two}

pub fn main(x) {
Expand All @@ -616,7 +556,7 @@ fn keyword_label_name() {
#[test]
fn qualified() {
assert_js!(
(CURRENT_PACKAGE, "other", r#"pub type One { One }"#),
("other", r#"pub type One { One }"#),
r#"import other

pub fn main() {
Expand Down
8 changes: 3 additions & 5 deletions compiler-core/src/javascript/tests/functions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{assert_js, assert_ts_def};

use super::CURRENT_PACKAGE;

#[test]
fn exported_functions() {
assert_js!(
Expand Down Expand Up @@ -212,7 +210,7 @@ fn reserved_word_fn() {
#[test]
fn reserved_word_imported() {
assert_js!(
(CURRENT_PACKAGE, "for", "pub fn class() { 1 }"),
("for", "pub fn class() { 1 }"),
r#"import for.{class}

pub fn export() {
Expand All @@ -225,7 +223,7 @@ pub fn export() {
#[test]
fn reserved_word_imported_alias() {
assert_js!(
(CURRENT_PACKAGE, "for", "pub fn class() { 1 }"),
("for", "pub fn class() { 1 }"),
r#"import for.{class as while} as function

pub fn export() {
Expand Down Expand Up @@ -370,7 +368,7 @@ pub fn version(n) {
#[test]
fn pipe_shadow_import() {
assert_js!(
(CURRENT_PACKAGE, "wibble", "pub fn println(x: String) { }"),
("wibble", "pub fn println(x: String) { }"),
r#"
import wibble.{println}
pub fn main() {
Expand Down
Loading
Loading