⭐️ A friendly language for building type-safe, scalable systems!
at main 3.8 kB view raw
1use crate::assert_js; 2use crate::javascript::tests::CURRENT_PACKAGE; 3 4#[test] 5fn empty_module() { 6 // Renders an export statement to ensure it's an ESModule 7 assert_js!("", "export {}\n"); 8} 9 10#[test] 11fn unqualified_fn_call() { 12 assert_js!( 13 ("rocket_ship", r#"pub fn launch() { 1 }"#), 14 r#"import rocket_ship.{launch} 15pub fn go() { launch() } 16"#, 17 ); 18} 19 20#[test] 21fn aliased_unqualified_fn_call() { 22 assert_js!( 23 ("rocket_ship", r#"pub fn launch() { 1 }"#), 24 r#"import rocket_ship.{launch as boom_time} 25pub fn go() { boom_time() } 26"#, 27 ); 28} 29 30#[test] 31fn multiple_unqualified_fn_call() { 32 assert_js!( 33 ( 34 CURRENT_PACKAGE, 35 "rocket_ship", 36 r#" 37pub fn a() { 1 } 38pub fn b() { 2 }"# 39 ), 40 r#"import rocket_ship.{a,b as bb} 41pub fn go() { a() + bb() } 42"#, 43 ); 44} 45 46#[test] 47fn constant() { 48 assert_js!( 49 ("rocket_ship", r#"pub const x = 1"#), 50 r#" 51import rocket_ship 52pub fn go() { rocket_ship.x } 53"#, 54 ); 55} 56 57#[test] 58fn alias_aliased_constant() { 59 assert_js!( 60 ("rocket_ship", r#"pub const x = 1"#), 61 r#" 62import rocket_ship.{ x as y } 63pub const z = y 64"#, 65 ); 66} 67 68#[test] 69fn renamed_module() { 70 assert_js!( 71 ("x", r#"pub const v = 1"#), 72 r#" 73import x as y 74pub const z = y.v 75"#, 76 ); 77} 78 79#[test] 80fn nested_module_constant() { 81 assert_js!( 82 ( 83 CURRENT_PACKAGE, 84 "rocket_ship/launcher", 85 r#"pub const x = 1"# 86 ), 87 r#" 88import rocket_ship/launcher 89pub fn go() { launcher.x } 90"#, 91 ); 92} 93 94#[test] 95fn alias_constant() { 96 assert_js!( 97 ("rocket_ship", r#"pub const x = 1"#), 98 r#" 99import rocket_ship as boop 100pub fn go() { boop.x } 101"#, 102 ); 103} 104 105#[test] 106fn alias_fn_call() { 107 assert_js!( 108 ("rocket_ship", r#"pub fn go() { 1 }"#), 109 r#" 110import rocket_ship as boop 111pub fn go() { boop.go() } 112"#, 113 ); 114} 115 116#[test] 117fn nested_fn_call() { 118 assert_js!( 119 ("one/two", r#"pub fn go() { 1 }"#), 120 r#"import one/two 121pub fn go() { two.go() }"#, 122 ); 123} 124 125#[test] 126fn nested_nested_fn_call() { 127 assert_js!( 128 ("one/two/three", r#"pub fn go() { 1 }"#), 129 r#"import one/two/three 130pub fn go() { three.go() }"#, 131 ); 132} 133 134#[test] 135fn different_package_import() { 136 assert_js!( 137 ("other_package", "one", r#"pub fn go() { 1 }"#), 138 r#"import one 139pub fn go() { one.go() } 140"#, 141 ); 142} 143 144#[test] 145fn nested_same_package() { 146 assert_js!( 147 ("one/two/three", r#"pub fn go() { 1 }"#), 148 r#"import one/two/three 149pub fn go() { three.go() } 150"#, 151 ); 152} 153 154#[test] 155fn discarded_duplicate_import() { 156 assert_js!( 157 ("esa/rocket_ship", r#"pub fn go() { 1 }"#), 158 ("nasa/rocket_ship", r#"pub fn go() { 1 }"#), 159 r#" 160import esa/rocket_ship 161import nasa/rocket_ship as _nasa_rocket 162pub fn go() { rocket_ship.go() } 163"# 164 ); 165} 166 167#[test] 168fn discarded_duplicate_import_with_unqualified() { 169 assert_js!( 170 ("esa/rocket_ship", r#"pub fn go() { 1 }"#), 171 ("nasa/rocket_ship", r#"pub fn go() { 1 }"#), 172 r#" 173import esa/rocket_ship 174import nasa/rocket_ship.{go} as _nasa_rocket 175pub fn esa_go() { rocket_ship.go() } 176pub fn nasa_go() { go() } 177"# 178 ); 179} 180 181#[test] 182fn import_with_keyword() { 183 assert_js!( 184 ( 185 CURRENT_PACKAGE, 186 "rocket_ship", 187 r#" 188pub const class = 1 189pub const in = 2 190"# 191 ), 192 r#" 193import rocket_ship.{class, in as while} 194pub fn main() { 195 #(class, while) 196} 197"# 198 ); 199} 200 201// https://github.com/gleam-lang/gleam/issues/3004 202#[test] 203fn constant_module_access_with_keyword() { 204 assert_js!( 205 ("rocket_ship", r#"pub const class = 1"#), 206 r#" 207import rocket_ship 208pub const variable = rocket_ship.class 209"#, 210 ); 211}