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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#![cfg(any(
    not(any(
        feature = "parser_tests",
        feature = "analyzer_tests",
        feature = "codegen_tests",
        feature = "interpreter_tests",
        feature = "conformance_tests",
        feature = "integration_tests",
    )),
    feature = "codegen_tests",
))]

//! Comprehensive Codegen String Handling Tests
//!
//! These tests verify that the Windjammer compiler correctly handles
//! string type conversions, including:
//! - String literals to String (.to_string())
//! - String literals to &str (no conversion)
//! - String concatenation
//! - String method calls
//! - format!() macro generation

#[path = "common/test_utils.rs"]
mod test_utils;

// ============================================================================
// HELPER FUNCTIONS
// ============================================================================

// ============================================================================
// STRING LITERALS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_literal_assignment() {
    let code = r#"
pub fn greeting() -> string {
    let s = "hello".to_string()
    s
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "String literal assignment should compile. Error: {}",
        err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_literal_return() {
    let code = r#"
pub fn hello() -> string {
    "hello"
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);

    // Should convert to String for return
    assert!(success, "String return should compile. Error: {}", err);
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_mutable_string_initialization() {
    let code = r#"
pub fn build_message() -> string {
    let mut s = ""
    s += "Hello"
    s += ", "
    s += "World"
    s
}
"#;
    let (generated, success) = test_utils::compile_single_check(code);
    let err = if !success { &generated } else { "" };

    // Mutable string should be converted to String, not &str
    assert!(
        success,
        "Mutable string should compile. Generated:\n{}\nError: {}",
        generated, err
    );
}

// ============================================================================
// STRING FUNCTION PARAMETERS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_param_to_function() {
    // Test that string parameters are properly handled
    let code = r#"
pub fn get_length(s: string) -> i32 {
    s.len() as i32
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(success, "String param should compile. Error: {}", err);
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_borrowed_string_param() {
    let code = r#"
pub fn length(s: string) -> i32 {
    s.len() as i32
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "Borrowed string param should compile. Error: {}",
        err
    );
}

// ============================================================================
// STRING METHODS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_contains() {
    let code = r#"
pub fn has_hello(s: string) -> bool {
    s.contains("hello")
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);

    // contains takes &str, so literal should NOT have .to_string()
    assert!(success, "String contains should compile. Error: {}", err);
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_replace() {
    let code = r#"
pub fn sanitize(s: string) -> string {
    s.replace("bad", "good")
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);

    // replace takes Pattern which &str implements
    assert!(success, "String replace should compile. Error: {}", err);
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_split() {
    // split() returns an iterator of &str - test basic split functionality
    let code = r#"
pub fn count_words(s: string) -> i32 {
    s.split(" ").count() as i32
}
"#;
    let (generated, success) = test_utils::compile_single_check(code);
    let err = if !success { &generated } else { "" };
    assert!(
        success,
        "String split should compile. Generated:\n{}\nError: {}",
        generated, err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_trim() {
    // trim() returns &str, so we need explicit .to_string() for now
    let code = r#"
pub fn clean(s: string) -> string {
    s.trim().to_string()
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(success, "String trim should compile. Error: {}", err);
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_to_uppercase() {
    let code = r#"
pub fn shout(s: string) -> string {
    s.to_uppercase()
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "String to_uppercase should compile. Error: {}",
        err
    );
}

// ============================================================================
// STRING CONCATENATION
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_concat_literals() {
    let code = r#"
pub fn full_name() -> string {
    "John" + " " + "Doe"
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(success, "String concat should compile. Error: {}", err);
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_concat_with_variable() {
    let code = r#"
pub fn greet(name: string) -> string {
    "Hello, " + name + "!"
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "String concat with variable should compile. Error: {}",
        err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_concat_compound() {
    let code = r#"
pub fn build_list() -> string {
    let mut result = ""
    result += "item1"
    result += ", "
    result += "item2"
    result
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "Compound string concat should compile. Error: {}",
        err
    );
}

// ============================================================================
// MATCH WITH STRINGS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_match_return_string() {
    let code = r#"
pub fn describe(n: i32) -> string {
    match n {
        0 => "zero",
        1 => "one",
        _ => "many",
    }
}
"#;
    let (generated, success) = test_utils::compile_single_check(code);
    let err = if !success { &generated } else { "" };

    // All match arms should be converted to String consistently
    assert!(
        success,
        "Match with string should compile. Generated:\n{}\nError: {}",
        generated, err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_match_mixed_return() {
    let code = r#"
pub fn get_message(code: i32) -> string {
    match code {
        0 => "OK",
        1 => "ERROR".to_uppercase(),
        _ => "UNKNOWN",
    }
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "Match with mixed returns should compile. Error: {}",
        err
    );
}

// ============================================================================
// STRUCT FIELDS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_struct_with_string_field() {
    let code = r#"
@derive(Clone, Debug)
pub struct Person {
    name: string,
    age: i32,
}

impl Person {
    pub fn new(name: string, age: i32) -> Person {
        Person { name: name, age: age }
    }
    
    pub fn greet(self) -> string {
        "Hello, " + self.name
    }
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "Struct with string field should compile. Error: {}",
        err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_struct_string_field_init() {
    let code = r#"
@derive(Clone, Debug)
pub struct Config {
    name: string,
}

pub fn create_config() -> Config {
    Config { name: "default" }
}
"#;
    let (generated, success) = test_utils::compile_single_check(code);
    let err = if !success { &generated } else { "" };

    // String literal in struct init should be converted
    assert!(
        success,
        "Struct string field init should compile. Generated:\n{}\nError: {}",
        generated, err
    );
}

// ============================================================================
// VEC WITH STRINGS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_vec_push_string() {
    let code = r#"
pub fn create_list() -> Vec<string> {
    let mut list = Vec::new()
    list.push("first")
    list.push("second")
    list
}
"#;
    let (generated, success) = test_utils::compile_single_check(code);
    let err = if !success { &generated } else { "" };

    // String literals pushed to Vec<String> should be converted
    assert!(
        success,
        "Vec push string should compile. Generated:\n{}\nError: {}",
        generated, err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_vec_string_iteration() {
    let code = r#"
pub fn join_all(items: Vec<string>) -> string {
    let mut result = ""
    for item in items {
        result += item
        result += ", "
    }
    result
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "Vec string iteration should compile. Error: {}",
        err
    );
}

// ============================================================================
// HASHMAP WITH STRINGS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_hashmap_string_keys() {
    let code = r#"
use std::collections::HashMap

pub fn create_map() -> HashMap<string, i32> {
    let mut map = HashMap::new()
    map.insert("one", 1)
    map.insert("two", 2)
    map
}
"#;
    let (generated, success) = test_utils::compile_single_check(code);
    let err = if !success { &generated } else { "" };
    assert!(
        success,
        "HashMap string keys should compile. Generated:\n{}\nError: {}",
        generated, err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_hashmap_get_string() {
    // Test hashmap with string keys - basic containment check
    let code = r#"
use std::collections::HashMap

pub fn has_key(map: HashMap<string, i32>, key: string) -> bool {
    map.contains_key(key)
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "HashMap get with string should compile. Error: {}",
        err
    );
}

// ============================================================================
// INTERPOLATED STRINGS
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_interpolation() {
    let code = r#"
pub fn format_greeting(name: string) -> string {
    "Hello, ${name}!"
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);

    // Should generate format!() macro
    assert!(
        success,
        "String interpolation should compile. Error: {}",
        err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_interpolation_expression() {
    let code = r#"
pub fn format_sum(a: i32, b: i32) -> string {
    "${a} + ${b} = ${a + b}"
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "String interpolation with expr should compile. Error: {}",
        err
    );
}

// ============================================================================
// EDGE CASES
// ============================================================================

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_empty_string() {
    let code = r#"
pub fn empty() -> string {
    ""
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(success, "Empty string should compile. Error: {}", err);
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_with_escapes() {
    let code = r#"
pub fn with_escapes() -> string {
    "line1\nline2\ttab"
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(
        success,
        "String with escapes should compile. Error: {}",
        err
    );
}

#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_string_clone() {
    let code = r#"
pub fn duplicate(s: string) -> string {
    s
}
"#;
    let (success, _generated, err) = test_utils::compile_via_cli(code);
    assert!(success, "String clone should compile. Error: {}", err);
}