syster-base 0.1.10-alpha

Core library for SysML v2 and KerML parsing, AST, and semantic analysis
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
#![allow(clippy::unwrap_used)]
#![allow(clippy::panic)]

use crate::syntax::kerml::parser::{load_and_parse, parse_content, parse_with_result};
use std::path::{Path, PathBuf};

// ============================================================================
// Tests for load_and_parse function (Issue #356)
// ============================================================================

#[test]
fn test_load_and_parse_valid_kerml_file() {
    // Create a temporary valid .kerml file
    let test_dir = std::env::temp_dir().join("kerml_parser_tests");
    std::fs::create_dir_all(&test_dir).unwrap();

    let test_file = test_dir.join("valid.kerml");
    std::fs::write(
        &test_file,
        "package TestPackage {\n    class TestClass;\n}\n",
    )
    .unwrap();

    let result = load_and_parse(&test_file);
    assert!(
        result.is_ok(),
        "Expected successful parsing of valid .kerml file"
    );

    let kerml_file = result.unwrap();
    assert_eq!(
        kerml_file.elements.len(),
        1,
        "Expected one top-level element"
    );
}

#[test]
fn test_load_and_parse_valid_sysml_file() {
    // Create a temporary valid .sysml file
    let test_dir = std::env::temp_dir().join("kerml_parser_tests");
    std::fs::create_dir_all(&test_dir).unwrap();

    let test_file = test_dir.join("valid.sysml");
    std::fs::write(
        &test_file,
        "package TestPackage {\n    part def TestPart;\n}\n",
    )
    .unwrap();

    let result = load_and_parse(&test_file);
    assert!(
        result.is_ok(),
        "Expected successful parsing of valid .sysml file"
    );

    let kerml_file = result.unwrap();
    assert_eq!(
        kerml_file.elements.len(),
        1,
        "Expected one top-level element"
    );
}

#[test]
fn test_load_and_parse_invalid_extension() {
    let test_dir = std::env::temp_dir().join("kerml_parser_tests");
    std::fs::create_dir_all(&test_dir).unwrap();

    let test_file = test_dir.join("invalid.txt");
    std::fs::write(
        &test_file,
        "package TestPackage {\n    class TestClass;\n}\n",
    )
    .unwrap();

    let result = load_and_parse(&test_file);
    assert!(result.is_err(), "Expected error for invalid file extension");

    let error_msg = result.unwrap_err();
    assert!(
        error_msg.contains("Unsupported file extension"),
        "Error message should mention unsupported extension: {error_msg}"
    );
}

#[test]
fn test_load_and_parse_nonexistent_file() {
    let test_file = PathBuf::from("/tmp/nonexistent_file_12345.kerml");

    let result = load_and_parse(&test_file);
    assert!(result.is_err(), "Expected error for non-existent file");

    let error_msg = result.unwrap_err();
    assert!(
        error_msg.contains("Failed to read"),
        "Error message should mention failed read: {error_msg}"
    );
}

#[test]
fn test_load_and_parse_invalid_syntax() {
    let test_dir = std::env::temp_dir().join("kerml_parser_tests");
    std::fs::create_dir_all(&test_dir).unwrap();

    let test_file = test_dir.join("invalid_syntax.kerml");
    // Missing semicolon after class declaration
    std::fs::write(
        &test_file,
        "package TestPackage {\n    class TestClass\n}\n",
    )
    .unwrap();

    let result = load_and_parse(&test_file);
    assert!(result.is_err(), "Expected error for invalid syntax");

    let error_msg = result.unwrap_err();
    assert!(
        error_msg.contains("Parse error"),
        "Error message should mention parse error: {error_msg}"
    );
}

#[test]
fn test_load_and_parse_empty_file() {
    let test_dir = std::env::temp_dir().join("kerml_parser_tests");
    std::fs::create_dir_all(&test_dir).unwrap();

    let test_file = test_dir.join("empty.kerml");
    std::fs::write(&test_file, "").unwrap();

    let result = load_and_parse(&test_file);
    assert!(
        result.is_ok(),
        "Expected successful parsing of empty .kerml file"
    );

    let kerml_file = result.unwrap();
    assert_eq!(
        kerml_file.elements.len(),
        0,
        "Expected no top-level elements in empty file"
    );
}

// ============================================================================
// Tests for parse_content function (Issue #352)
// ============================================================================

#[test]
fn test_parse_content_valid_kerml() {
    let content = "package TestPackage {\n    class TestClass;\n}\n";
    let path = Path::new("test.kerml");

    let result = parse_content(content, path);
    assert!(
        result.is_ok(),
        "Expected successful parsing of valid KerML content"
    );

    let kerml_file = result.unwrap();
    assert_eq!(
        kerml_file.elements.len(),
        1,
        "Expected one top-level element"
    );
}

#[test]
fn test_parse_content_empty_string() {
    let content = "";
    let path = Path::new("empty.kerml");

    let result = parse_content(content, path);
    assert!(
        result.is_ok(),
        "Expected successful parsing of empty content"
    );

    let kerml_file = result.unwrap();
    assert_eq!(
        kerml_file.elements.len(),
        0,
        "Expected no elements in empty content"
    );
}

#[test]
fn test_parse_content_syntax_error() {
    let content = "package TestPackage {\n    class TestClass\n}\n"; // Missing semicolon
    let path = Path::new("test.kerml");

    let result = parse_content(content, path);
    assert!(result.is_err(), "Expected error for invalid syntax");

    let error_msg = result.unwrap_err();
    assert!(
        error_msg.contains("Parse error"),
        "Error message should mention parse error: {error_msg}"
    );
}

#[test]
fn test_parse_content_error_includes_path() {
    let content = "invalid syntax here!@#$";
    let path = Path::new("/some/test/path.kerml");

    let result = parse_content(content, path);
    assert!(result.is_err(), "Expected error for invalid content");

    let error_msg = result.unwrap_err();
    assert!(
        error_msg.contains("path.kerml"),
        "Error message should include the file path: {error_msg}"
    );
}

#[test]
fn test_parse_content_multiple_elements() {
    let content = "class FirstClass;\nclass SecondClass;";
    let path = Path::new("test.kerml");

    let result = parse_content(content, path);
    assert!(
        result.is_ok(),
        "Expected successful parsing of multiple elements"
    );

    let kerml_file = result.unwrap();
    assert_eq!(
        kerml_file.elements.len(),
        2,
        "Expected two top-level elements"
    );
}

#[test]
fn test_parse_content_with_package() {
    // Test package instead of namespace, which is more commonly used
    let content = "package MyPackage;";
    let path = Path::new("test.kerml");

    let result = parse_content(content, path);
    assert!(
        result.is_ok(),
        "Expected successful parsing with package declaration"
    );

    let kerml_file = result.unwrap();
    // An empty package becomes a namespace declaration
    assert!(
        kerml_file.namespace.is_some(),
        "Expected namespace to be present"
    );
    assert_eq!(
        kerml_file.namespace.unwrap().name,
        "MyPackage",
        "Expected correct namespace name"
    );
}

// ============================================================================
// Tests for parse_content internal behavior (Issue #353)
// Testing the closure/internal logic through the public API
// ============================================================================

#[test]
fn test_parse_content_pest_parser_integration() {
    // Test that parse_content properly uses pest parser and constructs AST
    let content = "package TestPkg {\n    class MyClass {\n        feature myFeature;\n    }\n}";
    let path = Path::new("test.kerml");

    let result = parse_content(content, path);
    assert!(
        result.is_ok(),
        "Expected successful parsing and AST construction"
    );

    let kerml_file = result.unwrap();
    assert_eq!(kerml_file.elements.len(), 1, "Expected one package");
}

#[test]
fn test_parse_content_pest_error_handling() {
    // Test error handling when pest parser fails
    let content = "this is completely invalid @#$%^&*";
    let path = Path::new("test.kerml");

    let result = parse_content(content, path);
    assert!(result.is_err(), "Expected parse error from pest parser");

    let error_msg = result.unwrap_err();
    assert!(
        error_msg.contains("Parse error in test.kerml"),
        "Expected formatted parse error message: {error_msg}"
    );
}

#[test]
fn test_parse_content_ast_construction() {
    // Test that AST is properly constructed from pest pairs
    let content = "class SimpleClass;";
    let path = Path::new("test.kerml");

    let result = parse_content(content, path);
    assert!(result.is_ok(), "Expected successful AST construction");

    let kerml_file = result.unwrap();
    assert_eq!(kerml_file.elements.len(), 1, "Expected one element in AST");
}

#[test]
fn test_parse_content_complex_structure() {
    // Test parsing of more complex structures to ensure closure handles them
    let content = r#"
        package ComplexPackage {
            class BaseClass;
            class DerivedClass specializes BaseClass {
                feature attr1;
                feature attr2;
            }
        }
    "#;
    let path = Path::new("complex.kerml");

    let result = parse_content(content, path);
    assert!(
        result.is_ok(),
        "Expected successful parsing of complex structure"
    );

    let kerml_file = result.unwrap();
    assert_eq!(kerml_file.elements.len(), 1, "Expected one package element");
}

// ============================================================================
// Tests for parse_with_result function (additional coverage)
// ============================================================================

#[test]
fn test_parse_with_result_success() {
    let content = "class TestClass;";
    let path = Path::new("test.kerml");

    let result = parse_with_result(content, path);
    assert!(result.is_ok(), "Expected successful parse result");
    assert!(result.content.is_some(), "Expected content to be present");
    assert!(result.errors.is_empty(), "Expected no errors");
}

#[test]
fn test_parse_with_result_invalid_extension() {
    let content = "class TestClass;";
    let path = Path::new("test.txt");

    let result = parse_with_result(content, path);
    assert!(result.has_errors(), "Expected parse result to have errors");
    assert!(result.content.is_none(), "Expected no content");
    assert_eq!(result.errors.len(), 1, "Expected one error");
}

#[test]
fn test_parse_with_result_syntax_error() {
    let content = "class TestClass"; // Missing semicolon
    let path = Path::new("test.kerml");

    let result = parse_with_result(content, path);
    assert!(result.has_errors(), "Expected parse result to have errors");
    assert!(result.content.is_none(), "Expected no content");

    let error = &result.errors[0];
    assert_eq!(
        error.kind,
        crate::core::ParseErrorKind::SyntaxError,
        "Expected syntax error kind"
    );
}

#[test]
fn test_parse_with_result_error_position() {
    let content = "class TestClass"; // Missing semicolon at end of line
    let path = Path::new("test.kerml");

    let result = parse_with_result(content, path);
    assert!(result.has_errors(), "Expected parse result to have errors");

    let error = &result.errors[0];
    // Position should be present and valid (no need to check >= 0 for usize)
    assert!(
        error.position.line < 1000,
        "Expected reasonable line number"
    );
    assert!(
        error.position.column < 1000,
        "Expected reasonable column number"
    );
}

#[test]
fn test_parse_with_result_empty_content() {
    let content = "";
    let path = Path::new("empty.kerml");

    let result = parse_with_result(content, path);
    assert!(result.is_ok(), "Expected successful parse of empty content");
    assert!(result.content.is_some(), "Expected content to be present");

    let kerml_file = result.content.unwrap();
    assert_eq!(kerml_file.elements.len(), 0, "Expected no elements");
}