windjammer 0.48.0

A simple language inspired by Go, Ruby, and Elixir that transpiles to Rust - 80% of Rust's power with 20% of the complexity
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#![cfg(any(
    not(any(
        feature = "parser_tests",
        feature = "analyzer_tests",
        feature = "codegen_tests",
        feature = "interpreter_tests",
        feature = "conformance_tests",
        feature = "integration_tests",
    )),
    feature = "analyzer_tests",
))]

//! Integration tests for multi-target code generation
//!
//! Tests that verify Windjammer can correctly compile to Rust, JavaScript, and WebAssembly
//! with consistent behavior across all targets.

use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// Helper function to compile Windjammer code to a specific target
fn compile_to_target(source: &str, target: &str) -> Result<TempDir, String> {
    let temp_dir = TempDir::new().map_err(|e| format!("Failed to create temp dir: {}", e))?;
    let source_file = temp_dir.path().join("test.wj");

    fs::write(&source_file, source).map_err(|e| format!("Failed to write source file: {}", e))?;

    let output = Command::new(env!("CARGO_BIN_EXE_wj"))
        .arg("build")
        .arg("--no-cargo")
        .arg("--target")
        .arg(target)
        .arg(&source_file)
        .arg("--output")
        .arg(temp_dir.path().join("build"))
        .output()
        .map_err(|e| format!("Failed to run wj: {}", e))?;

    if !output.status.success() {
        return Err(format!(
            "Compilation failed:\n{}",
            String::from_utf8_lossy(&output.stderr)
        ));
    }

    Ok(temp_dir)
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_simple_function_rust() {
    let source = r#"
fn add(a: int, b: int) -> int {
    a + b
}

fn main() {
    let result = add(2, 3)
    println!("{}", result)
}
"#;

    let temp_dir = compile_to_target(source, "rust").expect("Rust compilation failed");

    // Verify generated files exist
    let build_dir = temp_dir.path().join("build");
    assert!(
        build_dir.join("Cargo.toml").exists(),
        "Cargo.toml should exist"
    );

    // Check for generated Rust files (typically test.rs for single file compilation)
    let rust_files: Vec<_> = fs::read_dir(&build_dir)
        .unwrap()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().and_then(|s| s.to_str()) == Some("rs"))
        .collect();

    assert!(!rust_files.is_empty(), "Should have at least one .rs file");
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_simple_function_javascript() {
    let source = r#"
fn add(a: int, b: int) -> int {
    a + b
}

fn main() {
    let result = add(2, 3)
    println!("{}", result)
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    // Verify generated files exist
    let build_dir = temp_dir.path().join("build");
    assert!(
        build_dir.join("output.js").exists(),
        "output.js should exist"
    );
    assert!(
        build_dir.join("output.d.ts").exists(),
        "output.d.ts should exist"
    );
    assert!(
        build_dir.join("package.json").exists(),
        "package.json should exist"
    );

    // Verify JavaScript output is valid
    let js_content =
        fs::read_to_string(build_dir.join("output.js")).expect("Failed to read output.js");
    assert!(
        js_content.contains("export function add"),
        "Should have add function"
    );
    assert!(
        js_content.contains("export function main"),
        "Should have main function"
    );
    assert!(
        js_content.contains("Windjammer JavaScript transpiler"),
        "Should have header comment"
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_simple_function_wasm() {
    let source = r#"
fn add(a: int, b: int) -> int {
    a + b
}

fn main() {
    let result = add(2, 3)
    println!("{}", result)
}
"#;

    let temp_dir = compile_to_target(source, "wasm").expect("WASM compilation failed");

    // Verify generated files exist
    let build_dir = temp_dir.path().join("build");
    assert!(
        build_dir.join("Cargo.toml").exists(),
        "Cargo.toml should exist"
    );
}

#[test]
fn test_typescript_definitions_quality() {
    let source = r#"
fn greet(name: string) -> string {
    "Hello, ${name}!"
}

fn calculate(x: int, y: int) -> int {
    x * y + 10
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    let build_dir = temp_dir.path().join("build");
    let ts_content =
        fs::read_to_string(build_dir.join("output.d.ts")).expect("Failed to read output.d.ts");

    // Verify TypeScript definitions are generated correctly
    assert!(
        ts_content.contains("export declare function greet"),
        "Should declare greet function"
    );
    assert!(
        ts_content.contains("(name: string): string")
            || ts_content.contains("(name: &str): String"),
        "Should have correct type signature, got:\n{}",
        ts_content
    );
    assert!(
        ts_content.contains("export declare function calculate"),
        "Should declare calculate function"
    );
    assert!(
        ts_content.contains("(x: number, y: number): number"),
        "Should have correct numeric types"
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_javascript_async_detection() {
    let source = r#"
@async
fn fetch_data(url: string) -> string {
    "data"
}

@async
fn main() {
    let data = fetch_data("http://example.com").await
    println!("{}", data)
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    let build_dir = temp_dir.path().join("build");
    let js_content =
        fs::read_to_string(build_dir.join("output.js")).expect("Failed to read output.js");

    // Verify async functions are detected
    assert!(
        js_content.contains("async function fetch_data"),
        "Should have async fetch_data"
    );
    assert!(
        js_content.contains("async function main"),
        "Should have async main"
    );
    assert!(js_content.contains("await"), "Should have await");
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_javascript_struct_generation() {
    let source = r#"
struct Point {
    x: int,
    y: int,
}

fn main() {
    let p = Point { x: 10, y: 20 }
    println!("{}, {}", p.x, p.y)
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    let build_dir = temp_dir.path().join("build");
    let js_content =
        fs::read_to_string(build_dir.join("output.js")).expect("Failed to read output.js");

    // Verify struct is generated as a class
    assert!(
        js_content.contains("export class Point"),
        "Should have Point class"
    );
    assert!(
        js_content.contains("constructor"),
        "Should have constructor"
    );
    assert!(js_content.contains("this.x"), "Should initialize x");
    assert!(js_content.contains("this.y"), "Should initialize y");
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_javascript_enum_generation() {
    let source = r#"
enum Color {
    Red,
    Green,
    Blue,
}

fn main() {
    let c = Color::Red
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    let build_dir = temp_dir.path().join("build");
    let js_content =
        fs::read_to_string(build_dir.join("output.js")).expect("Failed to read output.js");

    // Verify enum is generated as frozen object with string values
    assert!(
        js_content.contains("export const Color"),
        "Should have Color enum"
    );
    assert!(js_content.contains("Object.freeze"), "Should be frozen");
    assert!(
        js_content.contains("Color.Red"),
        "Should have string-based enum values"
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_package_json_generation() {
    let source = r#"
fn hello() {
    println!("Hello, World!")
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    let build_dir = temp_dir.path().join("build");
    let package_json =
        fs::read_to_string(build_dir.join("package.json")).expect("Failed to read package.json");

    // Verify package.json is valid JSON
    let json: serde_json::Value =
        serde_json::from_str(&package_json).expect("package.json should be valid JSON");

    assert!(json["name"].is_string(), "Should have name field");
    assert!(json["version"].is_string(), "Should have version field");
    assert!(json["type"] == "module", "Should be ES module");
    assert!(json["engines"].is_object(), "Should have engines field");
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_javascript_control_flow() {
    let source = r#"
fn test_if(x: int) -> int {
    if x > 0 {
        1
    } else {
        -1
    }
}

fn test_loop() {
    let mut i = 0
    while i < 10 {
        i = i + 1
    }
}

fn test_for() {
    for i in 0..5 {
        println!("{}", i)
    }
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    let build_dir = temp_dir.path().join("build");
    let js_content =
        fs::read_to_string(build_dir.join("output.js")).expect("Failed to read output.js");

    // Verify control flow structures are correctly generated
    assert!(js_content.contains("if ("), "Should have if statement");
    assert!(js_content.contains("} else {"), "Should have else block");
    assert!(js_content.contains("while ("), "Should have while loop");
    assert!(js_content.contains("for (const"), "Should have for loop");
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_javascript_jsdoc_comments() {
    let source = r#"
fn multiply(a: int, b: int) -> int {
    a * b
}
"#;

    let temp_dir = compile_to_target(source, "javascript").expect("JavaScript compilation failed");

    let build_dir = temp_dir.path().join("build");
    let js_content =
        fs::read_to_string(build_dir.join("output.js")).expect("Failed to read output.js");

    // Verify JSDoc comments are generated
    assert!(
        js_content.contains("/**"),
        "Should have JSDoc comment start"
    );
    assert!(js_content.contains("@param"), "Should have @param tags");
    assert!(js_content.contains("@returns"), "Should have @returns tag");
    assert!(js_content.contains("*/"), "Should have JSDoc comment end");
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_rust_output_still_works() {
    // Ensure Rust output hasn't regressed
    let source = r#"
fn fibonacci(n: int) -> int {
    if n <= 1 {
        n
    } else {
        fibonacci(n - 1) + fibonacci(n - 2)
    }
}

fn main() {
    let result = fibonacci(10)
    println!("{}", result)
}
"#;

    let temp_dir = compile_to_target(source, "rust").expect("Rust compilation failed");

    let build_dir = temp_dir.path().join("build");
    let cargo_toml =
        fs::read_to_string(build_dir.join("Cargo.toml")).expect("Failed to read Cargo.toml");

    assert!(
        cargo_toml.contains("[package]"),
        "Should have package section"
    );
    assert!(cargo_toml.contains("name"), "Should have name field");
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_all_targets_compile_same_source() {
    // Verify the same source can compile to all three targets without errors
    let source = r#"
fn add(a: int, b: int) -> int {
    a + b
}

fn main() {
    let x = add(2, 3)
    println!("{}", x)
}
"#;

    // Compile to all targets
    compile_to_target(source, "rust").expect("Rust compilation should succeed");
    compile_to_target(source, "javascript").expect("JavaScript compilation should succeed");
    compile_to_target(source, "wasm").expect("WASM compilation should succeed");
}