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
#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "integration_tests",
))]
// TDD Tests for Nested Module System - The Windjammer Way!
//
// Bug discovered: Windjammer doesn't support nested module structures
// like game engines need (math/vec2.wj, rendering/color.wj, etc.)
//
// The Windjammer Philosophy:
// - Compiler does the work, not the developer
// - Auto-discover modules from directory structure
// - Respect explicit pub mod / pub use declarations
// - Generate correct lib.rs for libraries
//
// This is NOT just copying Rust - it's the Windjammer way!
use std::fs;
use tempfile::TempDir;
/// Helper: Create a test directory structure
fn create_test_project(files: &[(&str, &str)]) -> TempDir {
let temp_dir = TempDir::new().unwrap();
for (path, content) in files {
let full_path = temp_dir.path().join(path);
// Create parent directories
if let Some(parent) = full_path.parent() {
fs::create_dir_all(parent).unwrap();
}
fs::write(&full_path, content).unwrap();
}
temp_dir
}
#[cfg(test)]
mod module_discovery_tests {
use super::*;
#[test]
fn test_discover_flat_modules() {
// Flat structure: src/vec2.wj, src/vec3.wj
let _temp_dir = create_test_project(&[
("vec2.wj", "pub struct Vec2 { pub x: f64, pub y: f64 }"),
(
"vec3.wj",
"pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64 }",
),
]);
// TODO: Implement discover_modules function
// let modules = windjammer::module_system::discover_modules(temp_dir.path()).unwrap();
// assert_eq!(modules.len(), 2);
// assert!(modules.contains_key("vec2"));
// assert!(modules.contains_key("vec3"));
}
#[test]
fn test_discover_nested_modules() {
// Nested structure:
// src/
// mod.wj
// math/
// mod.wj
// vec2.wj
// vec3.wj
// rendering/
// color.wj
let _temp_dir = create_test_project(&[
("mod.wj", "pub mod math\npub mod rendering"),
("math/mod.wj", "pub mod vec2\npub mod vec3"),
("math/vec2.wj", "pub struct Vec2 { pub x: f64, pub y: f64 }"),
(
"math/vec3.wj",
"pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64 }",
),
(
"rendering/color.wj",
"pub struct Color { pub r: u8, pub g: u8, pub b: u8 }",
),
]);
// TODO: Implement discover_nested_modules function
// let module_tree = windjammer::module_system::discover_nested_modules(temp_dir.path()).unwrap();
// Should discover:
// - math (directory module)
// - vec2 (file submodule)
// - vec3 (file submodule)
// - rendering (directory module)
// - color (file submodule - no mod.wj, auto-discovered!)
// assert_eq!(module_tree.root_modules.len(), 2);
// assert!(module_tree.has_module(&["math"]));
// assert!(module_tree.has_module(&["math", "vec2"]));
// assert!(module_tree.has_module(&["math", "vec3"]));
// assert!(module_tree.has_module(&["rendering", "color"]));
}
#[test]
fn test_auto_discover_without_mod_wj() {
// Windjammer Way: Auto-discover modules even without mod.wj!
// src/
// math/ (no mod.wj!)
// vec2.wj
// vec3.wj
let _temp_dir = create_test_project(&[
("math/vec2.wj", "pub struct Vec2 { pub x: f64, pub y: f64 }"),
(
"math/vec3.wj",
"pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64 }",
),
]);
// TODO: Should auto-discover math/ as a module!
// let module_tree = windjammer::module_system::discover_nested_modules(temp_dir.path()).unwrap();
// assert!(module_tree.has_module(&["math"]));
// assert!(module_tree.has_module(&["math", "vec2"]));
// assert!(module_tree.has_module(&["math", "vec3"]));
}
}
#[cfg(test)]
mod lib_rs_generation_tests {
use super::*;
#[test]
fn test_generate_lib_rs_flat() {
// Flat structure should generate simple lib.rs
let _temp_dir = create_test_project(&[
("vec2.wj", "pub struct Vec2 { pub x: f64, pub y: f64 }"),
(
"vec3.wj",
"pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64 }",
),
]);
// TODO: Implement generate_lib_rs function
// let lib_rs = windjammer::module_system::generate_lib_rs(temp_dir.path()).unwrap();
// Should generate:
// pub mod vec2;
// pub mod vec3;
// pub use vec2::*;
// pub use vec3::*;
// assert!(lib_rs.contains("pub mod vec2;"));
// assert!(lib_rs.contains("pub mod vec3;"));
// assert!(lib_rs.contains("pub use vec2::*;") || lib_rs.contains("pub use vec2::Vec2;"));
}
#[test]
fn test_generate_lib_rs_nested() {
// Nested structure should generate hierarchical lib.rs
let _temp_dir = create_test_project(&[
("mod.wj", "pub mod math\npub mod rendering"),
("math/mod.wj", "pub mod vec2\npub mod vec3"),
("math/vec2.wj", "pub struct Vec2 { pub x: f64, pub y: f64 }"),
(
"math/vec3.wj",
"pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64 }",
),
(
"rendering/color.wj",
"pub struct Color { pub r: u8, pub g: u8, pub b: u8 }",
),
]);
// TODO: Should generate lib.rs that declares top-level modules
// let lib_rs = windjammer::module_system::generate_lib_rs(temp_dir.path()).unwrap();
// Should generate (simplified):
// pub mod math;
// pub mod rendering;
// assert!(lib_rs.contains("pub mod math;"));
// assert!(lib_rs.contains("pub mod rendering;"));
// math/mod.rs should be generated separately with:
// pub mod vec2;
// pub mod vec3;
}
#[test]
fn test_preserve_pub_use_from_mod_wj() {
// THE CRITICAL TEST - Respect explicit declarations!
let _temp_dir = create_test_project(&[
("mod.wj", "pub mod math\npub use math::Vec2\npub use math::Vec3"),
("math/mod.wj", "pub struct Vec2 { pub x: f64, pub y: f64 }\npub struct Vec3 { pub x: f64, pub y: f64, pub z: f64 }"),
]);
// TODO: Should preserve pub use declarations from mod.wj
// let lib_rs = windjammer::module_system::generate_lib_rs(temp_dir.path()).unwrap();
// Should generate:
// pub mod math;
// pub use math::Vec2;
// pub use math::Vec3;
// NOT: pub use math::*; (too broad!)
// assert!(lib_rs.contains("pub mod math;"));
// assert!(lib_rs.contains("pub use math::Vec2;"));
// assert!(lib_rs.contains("pub use math::Vec3;"));
// assert!(!lib_rs.contains("pub use math::*;"), "Should NOT use wildcard re-exports when explicit pub use exists");
}
}
#[cfg(test)]
mod integration_tests {
use super::*;
#[test]
fn test_compile_game_engine_structure() {
// Realistic game engine structure like windjammer-game!
let _temp_dir = create_test_project(&[
(
"mod.wj",
r#"
pub mod math
pub mod rendering
pub mod physics
pub use math::Vec2
pub use math::Vec3
pub use rendering::Color
pub use physics::RigidBody2D
"#,
),
("math/mod.wj", "pub mod vec2\npub mod vec3"),
("math/vec2.wj", "pub struct Vec2 { pub x: f64, pub y: f64 }"),
(
"math/vec3.wj",
"pub struct Vec3 { pub x: f64, pub y: f64, pub z: f64 }",
),
(
"rendering/color.wj",
"pub struct Color { pub r: u8, pub g: u8, pub b: u8 }",
),
(
"physics/rigidbody2d.wj",
"pub struct RigidBody2D { pub mass: f64 }",
),
]);
// TODO: Full compilation pipeline
// let output = windjammer::compile_project(temp_dir.path(), target_dir).unwrap();
// Should generate:
// - lib.rs (top-level with pub mod + explicit pub use)
// - math/mod.rs (pub mod vec2; pub mod vec3;)
// - math/vec2.rs
// - math/vec3.rs
// - rendering/color.rs
// - physics/rigidbody2d.rs
// And lib.rs should have:
// pub mod math;
// pub mod rendering;
// pub mod physics;
// pub use math::Vec2;
// pub use math::Vec3;
// pub use rendering::Color;
// pub use physics::RigidBody2D;
}
#[test]
fn test_windjammer_vs_rust_comparison() {
// This test documents the Windjammer philosophy!
// RUST WAY (manual declarations everywhere):
// src/lib.rs:
// pub mod math;
// src/math/mod.rs:
// pub mod vec2;
// pub mod vec3;
// src/math/vec2.rs:
// pub struct Vec2 { ... }
// WINDJAMMER WAY (auto-discover + smart defaults):
// src/mod.wj:
// pub mod math // optional! auto-discovered if directory exists
// pub use math::Vec2 // explicit re-export (matters!)
//
// Compiler generates everything else automatically!
// The difference: Windjammer infers structure, Rust forces declaration
// This is consistent with Windjammer's philosophy of inferring what doesn't matter
}
}
#[cfg(test)]
mod regression_tests {
use super::*;
#[test]
fn test_flat_structure_still_works() {
// Ensure we don't break existing flat projects!
let _temp_dir = create_test_project(&[
("main.wj", "fn main() { println(\"Hello\") }"),
("utils.wj", "pub fn helper() -> i32 { 42 }"),
]);
// TODO: Should still generate mod.rs for flat projects
// (This is the existing behavior - don't break it!)
}
}
#[cfg(test)]
mod file_placement_tests {
#[test]
fn test_preserve_directory_structure_in_output() {
// When compiling nested modules, output .rs files should maintain
// the same directory structure as source .wj files
//
// Input: src/math/vec2.wj
// Output: build/math/vec2.rs (NOT build/vec2.rs!)
// This test validates that get_relative_output_path() works correctly
// TODO: Implement get_relative_output_path(source_root, input_file, output_root)
}
#[test]
fn test_output_path_calculation() {
// source_root = "src/"
// input_file = "src/math/vec2.wj"
// output_root = "build/"
// Expected: "build/math/vec2.rs"
// source_root = "src/"
// input_file = "src/rendering/color.wj"
// output_root = "build/"
// Expected: "build/rendering/color.rs"
}
}