relateby-pattern 0.4.2

Core pattern data structures
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
//! Integration tests for Pattern Default trait with iterators and standard library
//!
//! These tests verify that the default pattern integrates naturally with
//! Rust's iterator methods (fold, reduce) and standard library functions.
//!
//! # Test Organization
//!
//! - **Fold Tests**: Using Pattern::default() as initial value
//! - **Empty Collection Tests**: Handling empty iterators
//! - **Standard Library Tests**: mem::take, unwrap_or_default
//! - **Existing Operation Tests**: Integration with map, values, etc.

use pattern_core::{Combinable, Pattern};

// ============================================================================
// T035: Fold with Default Initial Value
// ============================================================================

#[test]
fn test_fold_multiple_patterns() {
    let patterns = vec![
        Pattern::point("hello".to_string()),
        Pattern::point(" ".to_string()),
        Pattern::point("world".to_string()),
    ];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result.value(), "hello world");
    assert_eq!(result.length(), 0); // All atomic
}

#[test]
fn test_fold_patterns_with_elements() {
    let patterns = vec![
        Pattern::pattern("a".to_string(), vec![Pattern::point("1".to_string())]),
        Pattern::pattern("b".to_string(), vec![Pattern::point("2".to_string())]),
        Pattern::pattern("c".to_string(), vec![Pattern::point("3".to_string())]),
    ];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result.value(), "abc");
    assert_eq!(result.length(), 3); // Three children: 1, 2, 3
}

#[test]
fn test_fold_mixed_structures() {
    let patterns = vec![
        Pattern::point("atomic".to_string()),
        Pattern::pattern(
            "compound".to_string(),
            vec![
                Pattern::point("x".to_string()),
                Pattern::point("y".to_string()),
            ],
        ),
        Pattern::point("atomic2".to_string()),
    ];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result.value(), "atomiccompoundatomic2");
    assert_eq!(result.length(), 2); // Two children: x, y
}

#[test]
fn test_fold_large_collection() {
    // Test folding many patterns
    let patterns: Vec<_> = (0..100).map(|i| Pattern::point(i.to_string())).collect();

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    // Verify all numbers are concatenated
    assert!(result.value().starts_with("01234"));
    assert!(result.value().ends_with("99"));
    assert_eq!(result.length(), 0); // All atomic
}

// ============================================================================
// T036: Fold with Empty Collection Returns Default
// ============================================================================

#[test]
fn test_fold_empty_collection() {
    let patterns: Vec<Pattern<String>> = vec![];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    // Folding empty collection returns the initial value (default)
    assert_eq!(result, Pattern::default());
    assert_eq!(result.value(), "");
    assert_eq!(result.length(), 0);
}

#[test]
fn test_fold_empty_vec_pattern() {
    let patterns: Vec<Pattern<Vec<i32>>> = vec![];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result, Pattern::default());
    let expected: Vec<i32> = vec![];
    assert_eq!(result.value(), &expected);
}

#[test]
fn test_fold_empty_unit_pattern() {
    let patterns: Vec<Pattern<()>> = vec![];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result, Pattern::default());
    assert_eq!(result.value(), &());
}

// ============================================================================
// T037: Fold with Single Pattern Returns That Pattern
// ============================================================================

#[test]
fn test_fold_single_pattern() {
    let pattern = Pattern::point("only".to_string());
    let patterns = vec![pattern.clone()];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    // Folding single pattern with default returns that pattern (identity)
    assert_eq!(result, pattern);
    assert_eq!(result.value(), "only");
}

#[test]
fn test_fold_single_compound_pattern() {
    let pattern = Pattern::pattern(
        "root".to_string(),
        vec![
            Pattern::point("a".to_string()),
            Pattern::point("b".to_string()),
        ],
    );
    let patterns = vec![pattern.clone()];

    let result = patterns
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result, pattern);
    assert_eq!(result.length(), 2);
}

// ============================================================================
// T038: Reduce with unwrap_or_default Pattern
// ============================================================================

#[test]
fn test_reduce_with_unwrap_or_default() {
    let patterns = vec![
        Pattern::point("a".to_string()),
        Pattern::point("b".to_string()),
        Pattern::point("c".to_string()),
    ];

    let result = patterns
        .into_iter()
        .reduce(|acc, p| acc.combine(p))
        .unwrap_or_default();

    assert_eq!(result.value(), "abc");
}

#[test]
fn test_reduce_empty_with_unwrap_or_default() {
    let patterns: Vec<Pattern<String>> = vec![];

    let result = patterns
        .into_iter()
        .reduce(|acc, p| acc.combine(p))
        .unwrap_or_default();

    // Empty reduce with unwrap_or_default gives default pattern
    assert_eq!(result, Pattern::default());
    assert_eq!(result.value(), "");
}

#[test]
fn test_reduce_single_with_unwrap_or_default() {
    let pattern = Pattern::point("single".to_string());
    let patterns = vec![pattern.clone()];

    let result = patterns
        .into_iter()
        .reduce(|acc, p| acc.combine(p))
        .unwrap_or_default();

    // Single element reduce returns that element
    assert_eq!(result, pattern);
}

// ============================================================================
// T039: mem::take with Pattern Uses Default
// ============================================================================

#[test]
fn test_mem_take_uses_default() {
    use std::mem;

    let mut pattern = Pattern::point("original".to_string());

    // mem::take replaces with default and returns original
    let taken = mem::take(&mut pattern);

    assert_eq!(taken.value(), "original");
    assert_eq!(pattern, Pattern::default());
    assert_eq!(pattern.value(), "");
}

#[test]
fn test_mem_take_compound_pattern() {
    use std::mem;

    let mut pattern = Pattern::pattern(
        "parent".to_string(),
        vec![Pattern::point("child".to_string())],
    );

    let taken = mem::take(&mut pattern);

    assert_eq!(taken.value(), "parent");
    assert_eq!(taken.length(), 1);
    assert_eq!(pattern, Pattern::default());
    assert_eq!(pattern.length(), 0);
}

// ============================================================================
// T040: Incremental Accumulation Starting from Default
// ============================================================================

#[test]
fn test_incremental_accumulation() {
    let mut accumulator = Pattern::<String>::default();

    // Start from default and incrementally add patterns
    let items = vec!["first", "second", "third"];

    for item in items {
        let p = Pattern::point(item.to_string());
        accumulator = accumulator.combine(p);
    }

    assert_eq!(accumulator.value(), "firstsecondthird");
}

#[test]
fn test_incremental_with_compound_patterns() {
    let mut accumulator = Pattern::<String>::default();

    // Incrementally build complex structure
    for i in 0..5 {
        let p = Pattern::pattern(
            format!("val{}", i),
            vec![Pattern::point(format!("elem{}", i))],
        );
        accumulator = accumulator.combine(p);
    }

    assert_eq!(accumulator.value(), "val0val1val2val3val4");
    assert_eq!(accumulator.length(), 5); // Five children
}

// ============================================================================
// T041: map() Over Default Pattern Preserves Identity
// ============================================================================

#[test]
fn test_map_over_default() {
    let empty = Pattern::<String>::default();

    // Mapping over default pattern preserves identity
    let mapped = empty.map(|s| s.to_uppercase());

    assert_eq!(mapped, Pattern::default());
    assert_eq!(mapped.value(), "");
}

#[test]
fn test_map_over_default_then_combine() {
    let empty = Pattern::<String>::default();
    let p = Pattern::point("test".to_string());

    // Mapping over default doesn't break identity
    let mapped_empty = empty.map(|s| s.to_uppercase());
    let result = mapped_empty.combine(p.clone());

    assert_eq!(result, p);
}

// ============================================================================
// T042: values() on Default Pattern Returns Single Default Value
// ============================================================================

#[test]
fn test_values_on_default() {
    let empty: Pattern<String> = Pattern::default();

    // Default pattern has single value (the default value)
    let values = empty.values();

    assert_eq!(values.len(), 1);
    assert_eq!(values[0], "");
}

#[test]
fn test_values_on_default_vec() {
    let empty: Pattern<Vec<i32>> = Pattern::default();

    let values = empty.values();

    assert_eq!(values.len(), 1);
    let expected = Vec::<i32>::new();
    assert_eq!(values[0], &expected);
}

// ============================================================================
// Additional Integration Tests
// ============================================================================

#[test]
fn test_filter_then_fold() {
    let patterns = vec![
        Pattern::point("keep".to_string()),
        Pattern::point("".to_string()), // Empty string
        Pattern::point("this".to_string()),
        Pattern::point("".to_string()),
        Pattern::point("text".to_string()),
    ];

    // Filter out empty values, then fold
    let result = patterns
        .into_iter()
        .filter(|p| !p.value().is_empty())
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result.value(), "keepthistext");
}

#[test]
fn test_chain_iterators_with_fold() {
    let batch1 = vec![
        Pattern::point("a".to_string()),
        Pattern::point("b".to_string()),
    ];
    let batch2 = vec![
        Pattern::point("c".to_string()),
        Pattern::point("d".to_string()),
    ];

    // Chain two iterators and fold
    let result = batch1
        .into_iter()
        .chain(batch2.into_iter())
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result.value(), "abcd");
}

#[test]
fn test_collect_then_fold() {
    // Generate patterns, collect them, then fold
    let result = (0..10)
        .map(|i| Pattern::point(i.to_string()))
        .collect::<Vec<_>>()
        .into_iter()
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    let expected: String = (0..10).map(|i| i.to_string()).collect();
    assert_eq!(result.value(), &expected);
}

#[test]
fn test_default_with_iterator_combinators() {
    let patterns = vec![
        Pattern::point("1".to_string()),
        Pattern::point("2".to_string()),
        Pattern::point("3".to_string()),
    ];

    // Use various iterator combinators with default
    let result = patterns
        .into_iter()
        .map(|p| p) // Identity map
        .filter(|p| !p.value().is_empty())
        .fold(Pattern::default(), |acc, p| acc.combine(p));

    assert_eq!(result.value(), "123");
}