rudof_generate 0.3.4

RDF data shapes implementation in Rust
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
#![allow(clippy::regex_creation_in_loops)]

#[cfg(not(target_family = "wasm"))]
use regex::Regex;
#[cfg(not(target_family = "wasm"))]
use rudof_generate::field_generators::pattern::PatternGenerator;
#[cfg(not(target_family = "wasm"))]
use rudof_generate::field_generators::{FieldGenerator, GenerationContext};
#[cfg(not(target_family = "wasm"))]
use serde_json::json;
#[cfg(not(target_family = "wasm"))]
use std::collections::HashSet;

/// Test pattern generator with various constraint combinations (adjusted for current implementation)
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_with_length_constraints() {
    let generator = PatternGenerator;

    // Test with a specific pattern that has predictable length (license plate format)
    let mut context = GenerationContext::new(
        "http://example.org/constrainedString".to_string(),
        "http://www.w3.org/2001/XMLSchema#string".to_string(),
        "test_subject".to_string(),
    );
    // Use a pattern with fixed length to test pattern compliance
    context
        .parameters
        .insert("pattern".to_string(), json!("[A-Z]{3}\\d{3}"));

    for _ in 0..10 {
        let result = generator.generate(&context).unwrap();
        println!("Generated constrained string: {result}");

        // Validate pattern compliance (license plate format)
        let pattern_regex = Regex::new(r"^[A-Z]{3}\d{3}$").unwrap();
        assert!(
            pattern_regex.is_match(&result),
            "Should match license plate pattern: {result}"
        );

        // Should be exactly 6 characters for this pattern
        assert_eq!(result.len(), 6, "Should be exactly 6 characters: {result}");

        // Validate structure
        let letters = &result[0..3];
        assert!(
            letters.chars().all(|c| c.is_ascii_uppercase()),
            "First 3 should be uppercase: {letters}"
        );

        let digits = &result[3..6];
        assert!(
            digits.chars().all(|c| c.is_ascii_digit()),
            "Last 3 should be digits: {digits}"
        );
    }
}

/// Test pattern generator with basic patterns (enumeration not supported in current implementation)
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_with_enumeration() {
    let generator = PatternGenerator;

    // Test with numeric pattern since enumeration is not currently supported
    let mut context = GenerationContext::new(
        "http://example.org/numericCode".to_string(),
        "http://www.w3.org/2001/XMLSchema#string".to_string(),
        "test_subject".to_string(),
    );
    context.parameters.insert("pattern".to_string(), json!("\\d{3}"));

    let mut generated_values = HashSet::new();
    for _ in 0..10 {
        let result = generator.generate(&context).unwrap();
        generated_values.insert(result.clone());
        println!("Generated numeric code: {result}");

        // Should be exactly 3 digits
        assert_eq!(result.len(), 3, "Should be 3 digits: {result}");
        assert!(
            result.chars().all(|c| c.is_ascii_digit()),
            "Should be all digits: {result}"
        );

        // Should be a valid number (pattern \d{3} allows 000-999, including leading zeros)
        let num: u32 = result.parse().unwrap();
        assert!(num <= 999, "Should be a 3-digit pattern (000-999): {num}");
    }

    // Should generate variety from enumeration
    assert!(generated_values.len() > 1, "Should generate variety from enumeration");
    println!("Generated enumerated values: {generated_values:?}");
}

/// Test pattern generator with different datatype contexts
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_with_different_datatypes() {
    let generator = PatternGenerator;

    // Test with xsd:token datatype (normalized string)
    let mut token_context = GenerationContext::new(
        "http://example.org/token".to_string(),
        "http://www.w3.org/2001/XMLSchema#token".to_string(),
        "test_subject".to_string(),
    );
    token_context
        .parameters
        .insert("pattern".to_string(), json!("[A-Za-z0-9_-]+"));

    let token_result = generator.generate(&token_context).unwrap();
    println!("Generated token: {token_result}");

    let token_regex = Regex::new(r"^[A-Za-z0-9_-]+$").unwrap();
    assert!(
        token_regex.is_match(&token_result),
        "Token should match pattern: {token_result}"
    );

    // Test with custom datatype (should still work)
    let mut custom_context = GenerationContext::new(
        "http://example.org/custom".to_string(),
        "http://example.org/CustomType".to_string(),
        "test_subject".to_string(),
    );
    custom_context
        .parameters
        .insert("pattern".to_string(), json!("CUSTOM-\\d{4}"));

    let custom_result = generator.generate(&custom_context).unwrap();
    println!("Generated custom type: {custom_result}");

    let custom_regex = Regex::new(r"^CUSTOM-\d{4}$").unwrap();
    assert!(
        custom_regex.is_match(&custom_result),
        "Custom should match pattern: {custom_result}"
    );
}

/// Test pattern generator with SHACL-like patterns (configuration-based)
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_shacl_constraint_integration() {
    // Test SHACL-like patterns directly since schema parsing may not extract patterns correctly
    let generator = PatternGenerator;

    // Test SHACL-inspired patterns
    let shacl_patterns = vec![
        ("productCode", "PROD-\\d{4}-[A-Z]{2}"),
        ("serialNumber", "[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}"),
        ("category", "[A-Z]{3,8}"),
    ];

    let mut generated_results = Vec::new();
    for (prop_name, pattern) in shacl_patterns {
        let mut context = GenerationContext::new(
            format!("http://example.org/{prop_name}"),
            "http://www.w3.org/2001/XMLSchema#string".to_string(),
            "test_subject".to_string(),
        );
        context.parameters.insert("pattern".to_string(), json!(pattern));

        let result = generator.generate(&context).unwrap();
        println!("Generated SHACL {prop_name} pattern: {result}");
        generated_results.push((prop_name, result));
    }

    // Validate we generated all SHACL pattern types
    assert_eq!(generated_results.len(), 3, "Should generate all 3 SHACL pattern types");

    // Validate specific SHACL pattern compliance
    for (prop_name, result) in &generated_results {
        match *prop_name {
            "productCode" => {
                assert!(
                    result.starts_with("PROD-"),
                    "Product code should start with PROD-: {result}"
                );
                assert!(result.len() >= 10, "Product code should have proper length: {result}");
            },
            "serialNumber" => {
                assert!(result.contains("-"), "Serial number should contain hyphens: {result}");
                let parts: Vec<&str> = result.split('-').collect();
                assert!(parts.len() >= 3, "Serial number should have multiple parts: {result}");
            },
            "category" => {
                assert!(
                    result
                        .chars()
                        .all(|c| c.is_ascii_uppercase() || c.is_ascii_alphabetic()),
                    "Category should be letters: {result}"
                );
                assert!(
                    result.len() >= 3 && result.len() <= 8,
                    "Category should be 3-8 chars: {result}"
                );
            },
            _ => {},
        }
    }
}

/// Test pattern generator with multiple patterns (simplified without ShEx schema)
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_shex_cardinality_integration() {
    // Test multiple pattern generation without ShEx schema (since regex patterns in ShEx don't parse)
    let generator = PatternGenerator;

    // Test phone pattern variations
    let phone_patterns = vec![
        ("primaryPhone", "\\d{3}-\\d{3}-\\d{4}"),
        ("alternatePhone", "\\d{3}-\\d{3}-\\d{4}"),
        ("emergencyContacts", "\\d{3}-\\d{3}-\\d{4}"),
    ];

    // Test tag patterns
    let tag_patterns = vec![
        ("shortTag", "[A-Z]{2}"),
        ("mediumTag", "[A-Z]{3}"),
        ("longTag", "[A-Z]{5}"),
    ];

    let mut all_results = Vec::new();

    // Generate multiple phone numbers
    for (name, pattern) in phone_patterns {
        let mut context = GenerationContext::new(
            format!("http://example.org/{name}"),
            "http://www.w3.org/2001/XMLSchema#string".to_string(),
            "test_subject".to_string(),
        );
        context.parameters.insert("pattern".to_string(), json!(pattern));

        let result = generator.generate(&context).unwrap();
        all_results.push((name, result));
        println!("Generated {}: {}", name, all_results.last().unwrap().1);
    }

    // Generate multiple tags
    for (name, pattern) in tag_patterns {
        let mut context = GenerationContext::new(
            format!("http://example.org/{name}"),
            "http://www.w3.org/2001/XMLSchema#string".to_string(),
            "test_subject".to_string(),
        );
        context.parameters.insert("pattern".to_string(), json!(pattern));

        let result = generator.generate(&context).unwrap();
        all_results.push((name, result));
        println!("Generated {}: {}", name, all_results.last().unwrap().1);
    }

    // Validate we generated all types
    assert_eq!(all_results.len(), 6, "Should generate all 6 pattern variations");

    // Validate phone number formats
    for (name, result) in &all_results {
        if name.contains("Phone") || name.contains("Contacts") {
            assert!(result.contains("-"), "Phone numbers should contain hyphens: {result}");
            assert_eq!(result.len(), 12, "Phone numbers should be 12 chars: {result}");
        } else if name.contains("Tag") {
            assert!(
                result.chars().all(|c| c.is_ascii_uppercase()),
                "Tags should be uppercase: {result}"
            );
            assert!(
                result.len() >= 2 && result.len() <= 5,
                "Tags should be 2-5 chars: {result}"
            );
        }
    }
}

/// Test pattern generator robustness with malformed patterns
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_generator_robustness() {
    let generator = PatternGenerator;

    // Test with various malformed or problematic patterns
    let test_cases = vec![
        ("", "empty pattern"),
        ("(", "unmatched parenthesis"),
        ("[", "unmatched bracket"),
        ("\\", "trailing backslash"),
        ("*", "invalid quantifier"),
        ("+", "invalid quantifier at start"),
        ("?", "invalid quantifier at start"),
        ("{5}", "invalid quantifier at start"),
        ("a{", "incomplete quantifier"),
        ("a{5", "incomplete quantifier"),
        ("a{5,", "incomplete quantifier"),
        ("[a-", "incomplete range"),
        ("(?:", "incomplete group"),
        ("\\x", "invalid escape"),
        ("\\u", "incomplete unicode"),
    ];

    for (pattern, description) in test_cases {
        println!("Testing {description}: '{pattern}'");

        let mut context = GenerationContext::new(
            "http://example.org/malformed".to_string(),
            "http://www.w3.org/2001/XMLSchema#string".to_string(),
            "test_subject".to_string(),
        );
        context.parameters.insert("pattern".to_string(), json!(pattern));

        let result = generator.generate(&context);

        // Should handle gracefully (either generate fallback or reasonable error)
        match result {
            Ok(value) => {
                assert!(
                    !value.is_empty(),
                    "Should generate non-empty fallback for {description}: '{pattern}'"
                );
                println!("  -> Generated fallback: '{value}'");
            },
            Err(e) => {
                println!("  -> Error (acceptable): {e}");
                // Errors are acceptable for malformed patterns
            },
        }
    }
}

/// Test pattern generator with complex nested structures (configuration-based)
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_generator_complex_integration() {
    // Test complex pattern generation without schema (since ShEx parsing with patterns fails)
    let generator = PatternGenerator;

    // Test various complex patterns directly
    let complex_patterns = vec![
        ("organizationId", "ORG-\\d{6}-[A-Z]{2}"),
        ("employeeId", "EMP-\\d{5}-[A-Z]{1}"),
        ("departmentCode", "DEPT-[A-Z]{3,4}"),
        ("email", "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"),
        ("phone", "\\d{3}-\\d{3}-\\d{4}"),
    ];

    let mut generated_results = Vec::new();
    for (prop_name, pattern) in complex_patterns {
        let mut context = GenerationContext::new(
            format!("http://example.org/{prop_name}"),
            "http://www.w3.org/2001/XMLSchema#string".to_string(),
            "test_subject".to_string(),
        );
        context.parameters.insert("pattern".to_string(), json!(pattern));

        let result = generator.generate(&context).unwrap();
        generated_results.push((prop_name, result));
        println!(
            "Generated {} pattern: {}",
            prop_name,
            generated_results.last().unwrap().1
        );
    }

    // Verify we generated all pattern types
    assert_eq!(
        generated_results.len(),
        5,
        "Should generate all 5 complex pattern types"
    );

    // Validate specific pattern matches
    for (prop_name, result) in &generated_results {
        match *prop_name {
            "organizationId" => {
                assert!(
                    result.starts_with("ORG-"),
                    "Organization ID should start with ORG-: {result}"
                );
                assert!(
                    result.len() >= 11,
                    "Organization ID should have proper length: {result}"
                );
            },
            "employeeId" => {
                assert!(
                    result.starts_with("EMP-"),
                    "Employee ID should start with EMP-: {result}"
                );
                assert!(result.len() >= 9, "Employee ID should have proper length: {result}");
            },
            "email" => {
                assert!(result.contains("@"), "Email should contain @: {result}");
                assert!(result.contains("."), "Email should contain .: {result}");
            },
            "phone" => {
                assert!(result.contains("-"), "Phone should contain -: {result}");
                assert_eq!(result.len(), 12, "Phone should be exactly 12 chars: {result}");
            },
            _ => {},
        }
    }
}

/// Test pattern generator consistency across multiple invocations
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_generator_consistency() {
    let generator = PatternGenerator;

    // Test that the same context produces varied but valid results
    let mut context = GenerationContext::new(
        "http://example.org/consistent".to_string(),
        "http://www.w3.org/2001/XMLSchema#string".to_string(),
        "test_subject".to_string(),
    );
    context
        .parameters
        .insert("pattern".to_string(), json!("[A-Z]{2}\\d{4}"));

    let mut results = Vec::new();
    for _ in 0..100 {
        let result = generator.generate(&context).unwrap();
        results.push(result);
    }

    // All should match the pattern
    let pattern_regex = Regex::new(r"^[A-Z]{2}\d{4}$").unwrap();
    for result in &results {
        assert!(
            pattern_regex.is_match(result),
            "All results should match pattern: {result}"
        );
    }

    // Should have reasonable variety
    let unique_results: HashSet<_> = results.iter().collect();
    let variety_ratio = unique_results.len() as f64 / results.len() as f64;

    println!(
        "Generated {} unique results out of {} total (variety: {:.2}%)",
        unique_results.len(),
        results.len(),
        variety_ratio * 100.0
    );

    // Should have decent variety (at least 50% unique for this simple pattern)
    assert!(
        variety_ratio > 0.5,
        "Should generate reasonable variety: {:.2}%",
        variety_ratio * 100.0
    );
}

/// Test pattern generator memory usage and cleanup
#[cfg(not(target_family = "wasm"))]
#[tokio::test]
async fn test_pattern_generator_memory_efficiency() {
    let generator = PatternGenerator;

    // Create many contexts and generate values to test memory efficiency
    let patterns = [
        "\\d{3}-\\d{3}-\\d{4}",
        "[A-Z]{2,4}\\d{3,5}",
        "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}",
        "https?://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}",
        "#[0-9A-Fa-f]{6}",
    ];

    for (i, pattern) in patterns.iter().enumerate() {
        for j in 0..100 {
            let mut context = GenerationContext::new(
                format!("http://example.org/prop{i}-{j}"),
                "http://www.w3.org/2001/XMLSchema#string".to_string(),
                format!("subject_{j}"),
            );
            context.parameters.insert("pattern".to_string(), json!(pattern));

            let result = generator.generate(&context);
            assert!(
                result.is_ok(),
                "Should generate successfully for pattern {i}: {pattern}"
            );

            let value = result.unwrap();
            assert!(!value.is_empty(), "Should generate non-empty value");

            // Don't store results to test that generator doesn't leak memory
        }
    }

    println!(
        "Successfully generated {} values across {} patterns",
        100 * patterns.len(),
        patterns.len()
    );
}