dol 0.8.1

DOL (Design Ontology Language) - A declarative specification language for ontology-first development
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! dol-test - Generate Rust tests from DOL test specifications
//!
//! Transforms `.dol.test` files into compilable Rust test modules,
//! enabling test-driven ontology development.
//!
//! # Usage
//!
//! ```bash
//! # Generate tests from a single file
//! dol-test examples/traits/container.lifecycle.dol.test
//!
//! # Generate tests to specific output directory
//! dol-test --output tests/generated/ examples/
//!
//! # Generate stubs only (for manual implementation)
//! dol-test --stubs examples/traits/container.lifecycle.dol.test
//!
//! # Watch mode for development
//! dol-test --watch examples/
//! ```

use clap::{Parser, Subcommand};
use colored::Colorize;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

/// Generate Rust tests from DOL test specifications
#[derive(Parser, Debug)]
#[command(name = "dol-test")]
#[command(author, version, about, long_about = None)]
struct Args {
    #[command(subcommand)]
    command: Option<Commands>,

    /// DOL test files or directories to process
    paths: Vec<PathBuf>,

    /// Output directory for generated tests
    #[arg(short, long, default_value = "tests/generated")]
    output: PathBuf,

    /// Generate stub tests only (unimplemented!() bodies)
    #[arg(short, long)]
    stubs: bool,

    /// Overwrite existing generated files
    #[arg(short, long)]
    force: bool,

    /// Quiet mode
    #[arg(short, long)]
    quiet: bool,
}

#[derive(Subcommand, Debug)]
enum Commands {
    /// Generate tests from DOL test files
    Generate {
        /// Files or directories to process
        paths: Vec<PathBuf>,

        /// Output directory
        #[arg(short, long, default_value = "tests/generated")]
        output: PathBuf,

        /// Generate stubs only
        #[arg(short, long)]
        stubs: bool,

        /// Overwrite existing generated files
        #[arg(short, long)]
        force: bool,
    },

    /// Validate DOL test files without generating
    Validate {
        /// Files or directories to validate
        paths: Vec<PathBuf>,
    },

    /// Show what would be generated (dry run)
    Plan {
        /// Files or directories to plan
        paths: Vec<PathBuf>,
    },
}

fn main() -> ExitCode {
    let args = Args::parse();

    match args.command {
        Some(Commands::Generate {
            paths,
            output,
            stubs,
            force,
        }) => generate_tests(&paths, &output, stubs, force, args.quiet),
        Some(Commands::Validate { paths }) => validate_test_files(&paths, args.quiet),
        Some(Commands::Plan { paths }) => plan_generation(&paths),
        None => generate_tests(
            &args.paths,
            &args.output,
            args.stubs,
            args.force,
            args.quiet,
        ),
    }
}

/// Parsed DOL test file structure
#[derive(Debug)]
struct DolTestFile {
    /// The trait/gene being tested
    subject: String,
    /// Individual test cases
    tests: Vec<TestCase>,
}

#[derive(Debug)]
struct TestCase {
    /// Test name
    name: String,
    /// Given preconditions
    given: Vec<String>,
    /// When actions
    when: Vec<String>,
    /// Then assertions
    then: Vec<String>,
    /// Whether this test should always pass
    always: bool,
}

fn generate_tests(
    paths: &[PathBuf],
    output: &PathBuf,
    stubs: bool,
    force: bool,
    quiet: bool,
) -> ExitCode {
    let files = collect_test_files(paths);

    if files.is_empty() {
        if !quiet {
            eprintln!("{}: No .dol.test files found", "warning".yellow());
        }
        return ExitCode::SUCCESS;
    }

    // Ensure output directory exists
    if let Err(e) = std::fs::create_dir_all(output) {
        eprintln!(
            "{}: Failed to create output directory: {}",
            "error".red(),
            e
        );
        return ExitCode::FAILURE;
    }

    let mut generated = 0;
    let mut failed = 0;

    for path in &files {
        match process_test_file(path, output, stubs, force) {
            Ok(output_path) => {
                generated += 1;
                if !quiet {
                    println!(
                        "{} {} → {}",
                        "✓".green(),
                        path.display(),
                        output_path.display()
                    );
                }
            }
            Err(e) => {
                failed += 1;
                eprintln!("{} {}: {}", "✗".red(), path.display(), e);
            }
        }
    }

    if !quiet {
        println!();
        println!("{}", "Summary".bold());
        println!("  Generated: {}", generated.to_string().green());
        if failed > 0 {
            println!("  Failed:    {}", failed.to_string().red());
        }
    }

    if failed > 0 {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    }
}

fn validate_test_files(paths: &[PathBuf], quiet: bool) -> ExitCode {
    let files = collect_test_files(paths);
    let mut valid = 0;
    let mut invalid = 0;

    for path in &files {
        match parse_test_file(path) {
            Ok(test_file) => {
                valid += 1;
                if !quiet {
                    println!(
                        "{} {} ({} tests)",
                        "✓".green(),
                        path.display(),
                        test_file.tests.len()
                    );
                }
            }
            Err(e) => {
                invalid += 1;
                eprintln!("{} {}: {}", "✗".red(), path.display(), e);
            }
        }
    }

    if !quiet {
        println!();
        println!("Valid: {}, Invalid: {}", valid, invalid);
    }

    if invalid > 0 {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    }
}

fn plan_generation(paths: &[PathBuf]) -> ExitCode {
    let files = collect_test_files(paths);

    println!("{}", "Generation Plan".bold());
    println!();

    for path in &files {
        match parse_test_file(path) {
            Ok(test_file) => {
                let output_name = path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .unwrap_or("unknown")
                    .replace('.', "_");

                println!("{}:", path.display().to_string().cyan());
                println!("  Output: tests/generated/{}.rs", output_name);
                println!("  Tests:  {}", test_file.tests.len());
                for test in &test_file.tests {
                    println!("    - {}", test.name);
                }
                println!();
            }
            Err(e) => {
                eprintln!("{}: {} - {}", "skip".yellow(), path.display(), e);
            }
        }
    }

    ExitCode::SUCCESS
}

fn collect_test_files(paths: &[PathBuf]) -> Vec<PathBuf> {
    let mut files = Vec::new();

    for path in paths {
        if path.is_file() {
            if path.to_string_lossy().ends_with(".dol.test") {
                files.push(path.clone());
            }
        } else if path.is_dir() {
            collect_test_files_recursive(path, &mut files);
        }
    }

    files.sort();
    files
}

fn collect_test_files_recursive(dir: &PathBuf, files: &mut Vec<PathBuf>) {
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                collect_test_files_recursive(&path, files);
            } else if path.to_string_lossy().ends_with(".dol.test") {
                files.push(path);
            }
        }
    }
}

fn parse_test_file(path: &Path) -> Result<DolTestFile, String> {
    let source =
        std::fs::read_to_string(path).map_err(|e| format!("Failed to read file: {}", e))?;

    parse_test_source(&source)
}

fn parse_test_source(source: &str) -> Result<DolTestFile, String> {
    let mut subject = String::new();
    let mut tests = Vec::new();
    let mut current_test: Option<TestCase> = None;
    let mut current_section = "";

    for line in source.lines() {
        let line = line.trim();

        // Skip empty lines and comments
        if line.is_empty() || line.starts_with("//") {
            continue;
        }

        // Inline test declaration (quoted test name) - check FIRST before subject header
        if line.starts_with("test \"") || line.starts_with("test '") {
            let name = line
                .trim_start_matches("test ")
                .trim_end_matches('{')
                .trim()
                .trim_matches(|c| c == '"' || c == '\'')
                .to_string();

            if let Some(test) = current_test.take() {
                tests.push(test);
            }
            current_test = Some(TestCase {
                name,
                given: Vec::new(),
                when: Vec::new(),
                then: Vec::new(),
                always: false,
            });
            continue;
        }

        // Parse test file header: "test <subject> {" (non-quoted subject)
        if line.starts_with("test ") && line.ends_with('{') {
            subject = line
                .strip_prefix("test ")
                .unwrap()
                .strip_suffix('{')
                .unwrap()
                .trim()
                .to_string();
            continue;
        }

        // Parse sections
        if line == "given {" || line == "given:" {
            current_section = "given";
            continue;
        }
        if line == "when {" || line == "when:" {
            current_section = "when";
            continue;
        }
        if line == "then {" || line == "then:" {
            current_section = "then";
            continue;
        }
        if line == "always" || line == "always:" {
            if let Some(ref mut test) = current_test {
                test.always = true;
            }
            continue;
        }

        // End of block
        if line == "}" {
            current_section = "";
            continue;
        }

        // Add line to current section
        if let Some(ref mut test) = current_test {
            let content = line.to_string();
            match current_section {
                "given" => test.given.push(content),
                "when" => test.when.push(content),
                "then" => test.then.push(content),
                _ => {}
            }
        }
    }

    // Don't forget the last test
    if let Some(test) = current_test {
        tests.push(test);
    }

    if subject.is_empty() {
        return Err("No test subject found".to_string());
    }

    Ok(DolTestFile { subject, tests })
}

fn process_test_file(
    path: &Path,
    output_dir: &Path,
    stubs: bool,
    force: bool,
) -> Result<PathBuf, String> {
    let test_file = parse_test_file(path)?;

    let output_name = path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown")
        .replace(['.', '-'], "_");

    let output_path = output_dir.join(format!("{}.rs", output_name));

    // Check if file exists and force not set
    if output_path.exists() && !force {
        return Err(format!(
            "Output file exists (use --force to overwrite): {}",
            output_path.display()
        ));
    }

    let code = generate_rust_tests(&test_file, stubs);

    std::fs::write(&output_path, code).map_err(|e| format!("Failed to write output: {}", e))?;

    Ok(output_path)
}

fn generate_rust_tests(test_file: &DolTestFile, stubs: bool) -> String {
    let mut code = String::new();

    // Header
    code.push_str(&format!(
        r#"//! Generated tests for {}
//!
//! This file was automatically generated by dol-test.
//! Do not edit manually - changes will be overwritten.

#![allow(unused_imports)]

use metadol::{{parse_file, validate}};

"#,
        test_file.subject
    ));

    // Generate each test
    for test in &test_file.tests {
        let fn_name = test
            .name
            .to_lowercase()
            .replace([' ', '-'], "_")
            .chars()
            .filter(|c| c.is_alphanumeric() || *c == '_')
            .collect::<String>();

        code.push_str(&format!("#[test]\nfn test_{}() {{\n", fn_name));

        if stubs {
            code.push_str("    // TODO: Implement this test\n");
            code.push_str(&format!("    // Test: {}\n", test.name));
            if !test.given.is_empty() {
                code.push_str("    // Given:\n");
                for g in &test.given {
                    code.push_str(&format!("    //   - {}\n", g));
                }
            }
            if !test.when.is_empty() {
                code.push_str("    // When:\n");
                for w in &test.when {
                    code.push_str(&format!("    //   - {}\n", w));
                }
            }
            if !test.then.is_empty() {
                code.push_str("    // Then:\n");
                for t in &test.then {
                    code.push_str(&format!("    //   - {}\n", t));
                }
            }
            code.push_str("    unimplemented!(\"Test not yet implemented\")\n");
        } else {
            // Generate actual test code
            code.push_str("    // Arrange\n");
            for given in &test.given {
                code.push_str(&format!("    // Given: {}\n", given));
            }
            code.push('\n');

            code.push_str("    // Act\n");
            for when_clause in &test.when {
                code.push_str(&format!("    // When: {}\n", when_clause));
            }
            code.push('\n');

            code.push_str("    // Assert\n");
            for then_clause in &test.then {
                code.push_str(&format!(
                    "    // Then: {}\n    assert!(true, \"TODO: Implement assertion for: {}\");\n",
                    then_clause, then_clause
                ));
            }

            if test.always {
                code.push_str("\n    // This test should always pass\n");
            }
        }

        code.push_str("}\n\n");
    }

    code
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_simple_test_file() {
        let source = r#"
test container.lifecycle {
    test "container can be created" {
        given:
            container does not exist
        when:
            create container
        then:
            container exists
            container is created
    }
}
"#;
        let result = parse_test_source(source);
        assert!(result.is_ok());
        let test_file = result.unwrap();
        assert_eq!(test_file.subject, "container.lifecycle");
        assert_eq!(test_file.tests.len(), 1);
    }

    #[test]
    fn test_generate_stubs() {
        let test_file = DolTestFile {
            subject: "test.subject".to_string(),
            tests: vec![TestCase {
                name: "example test".to_string(),
                given: vec!["precondition".to_string()],
                when: vec!["action".to_string()],
                then: vec!["assertion".to_string()],
                always: false,
            }],
        };

        let code = generate_rust_tests(&test_file, true);
        assert!(code.contains("unimplemented!"));
        assert!(code.contains("test_example_test"));
    }
}