summaryrefslogtreecommitdiff
path: root/src/qdoc/catch_generators/tests/generators/catch_path_generator.cpp
blob: deb33421b891136f1fa25a44665891bfdf4ec2e2 (plain)
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <catch_generators/namespaces.h>
#include <catch_generators/generators/qchar_generator.h>
#include <catch_generators/generators/qstring_generator.h>
#include <catch_generators/generators/path_generator.h>
#include <catch_generators/generators/combinators/cycle_generator.h>
#include <catch_generators/utilities/statistics/percentages.h>
#include <catch_generators/utilities/statistics/distribution.h>
#include <catch_generators/utilities/semantics/copy_value.h>

#include <catch_conversions/qt_catch_conversions.h>

#include <catch/catch.hpp>

#include <QString>
#include <QStringList>
#include <QRegularExpression>

using namespace QDOC_CATCH_GENERATORS_ROOT_NAMESPACE;
using namespace QDOC_CATCH_GENERATORS_UTILITIES_ABSOLUTE_NAMESPACE;

using namespace Qt::StringLiterals;

TEST_CASE("A path generated with a multi_device_path_probability of 1.0 always contains a device component.", "[Path][Content][SpecialCase]") {
    QString device_component_value{"C:"};
    auto path_generator = path(
        Catch::Generators::value(copy_value(device_component_value)),
        empty_string(),
        empty_string(),
        empty_string(),
        empty_string(),
        PathGeneratorConfiguration{}.set_multi_device_path_probability(1.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE(generated_path.contains(device_component_value));
}

TEST_CASE("A path generated with a multi_device_path_probability of 0.0 never contains a device component.", "[Path][Content][SpecialCase]") {
    QString device_component_value{"C:"};
    auto path_generator = path(
        Catch::Generators::value(copy_value(device_component_value)),
        empty_string(),
        empty_string(),
        empty_string(),
        empty_string(),
        PathGeneratorConfiguration{}.set_multi_device_path_probability(0.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE(!generated_path.contains(device_component_value));
}

TEST_CASE("A path generated with an absolute_path_probability of 1.0 always contains a root component.", "[Path][Content][SpecialCase]") {
    QString root_component_value{"\\"};
    auto path_generator = path(
        empty_string(),
        Catch::Generators::value(copy_value(root_component_value)),
        empty_string(),
        empty_string(),
        empty_string(),
        PathGeneratorConfiguration{}.set_absolute_path_probability(1.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE(generated_path.contains(root_component_value));
}

TEST_CASE("A path generated with an absolute_path_probability of 0.0 never contains a root component.", "[Path][Content][SpecialCase]") {
    QString root_component_value{"\\"};
    auto path_generator = path(
        empty_string(),
        Catch::Generators::value(copy_value(root_component_value)),
        empty_string(),
        empty_string(),
        empty_string(),
        PathGeneratorConfiguration{}.set_absolute_path_probability(0.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE(!generated_path.contains(root_component_value));
}

TEST_CASE("A path generated with a directory_path_probability of 1.0 always ends with a root, directory or directory followed by separator component.", "[Path][Content][SpecialCase]") {
    QString root_component_value{"root"};
    QString directory_component_value{"dir"};
    QString separator_component_value{"sep"};

    auto path_generator = path(
        cycle(Catch::Generators::value(QString("device"))),
        cycle(Catch::Generators::value(copy_value(root_component_value))),
        cycle(Catch::Generators::value(copy_value(directory_component_value))),
        cycle(Catch::Generators::value(QString("filename"))),
        cycle(Catch::Generators::value(copy_value(separator_component_value))),
        PathGeneratorConfiguration{}.set_directory_path_probability(1.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE((
        generated_path.endsWith(root_component_value) ||
        generated_path.endsWith(directory_component_value) ||
        generated_path.endsWith(directory_component_value + separator_component_value)
    ));
}

TEST_CASE("A path generated with a directory_path_probability of 0.0 always ends with a filename component.", "[Path][Content][SpecialCase]") {
    QString filename_component_value{"file"};

    auto path_generator = path(
        cycle(Catch::Generators::value(QString("device"))),
        cycle(Catch::Generators::value(QString("root"))),
        cycle(Catch::Generators::value(QString("dir"))),
        cycle(Catch::Generators::value(copy_value(filename_component_value))),
        cycle(Catch::Generators::value(QString("sep"))),
        PathGeneratorConfiguration{}.set_directory_path_probability(0.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE(generated_path.endsWith(filename_component_value));
}

TEST_CASE("A directory path generated with a has_trailing_separator_probability of 1.0 always ends with a separator component.", "[Path][Content][SpecialCase]") {
    QString separator_component_value{"sep"};

    auto path_generator = path(
        cycle(Catch::Generators::value(QString("device"))),
        cycle(Catch::Generators::value(QString("root"))),
        cycle(Catch::Generators::value(QString("directory"))),
        cycle(Catch::Generators::value(QString("filename"))),
        cycle(Catch::Generators::value(copy_value(separator_component_value))),
        PathGeneratorConfiguration{}.set_directory_path_probability(1.0).set_has_trailing_separator_probability(1.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE(generated_path.endsWith(separator_component_value));
}

TEST_CASE("A directory path generated with a has_trailing_separator_probability of 0.0 never ends with a separator component.", "[Path][Content][SpecialCase]") {
    QString separator_component_value{"sep"};

    auto path_generator = path(
        cycle(Catch::Generators::value(QString("device"))),
        cycle(Catch::Generators::value(QString("root"))),
        cycle(Catch::Generators::value(QString("directory"))),
        cycle(Catch::Generators::value(QString("filename"))),
        cycle(Catch::Generators::value(copy_value(separator_component_value))),
        PathGeneratorConfiguration{}.set_directory_path_probability(1.0).set_has_trailing_separator_probability(0.0)
    );

    auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

    REQUIRE(!generated_path.endsWith(separator_component_value));
}

SCENARIO("Binding a path to a component range", "[Path][Bounds]") {
    GIVEN("A minimum amount of components") {
        auto minimum_components_amount = GENERATE(take(100, random(std::size_t{1}, std::size_t{100})));

        AND_GIVEN("A maximum amount of components that is greater or equal than the minimum amount of components") {
            auto maximum_components_amount = GENERATE_COPY(take(100, random(minimum_components_amount, std::size_t{100})));

            WHEN("A path is generated from those bounds") {
                QString countable_component_value{"a"};

                QString generated_path = GENERATE_COPY(
                    take(1,
                         path(
                            empty_string(),
                            empty_string(),
                            cycle(Catch::Generators::value(copy_value(countable_component_value))),
                            cycle(Catch::Generators::value(copy_value(countable_component_value))),
                            empty_string(),
                            PathGeneratorConfiguration{}.set_minimum_components_amount(minimum_components_amount).set_maximum_components_amount(maximum_components_amount)
                         )
                    )
                );

                THEN("The amount of non device, non root, non separator components in the generated path is in the range [minimum_components_amount, maximum_components_amount]") {
                    std::size_t components_amount{static_cast<std::size_t>(generated_path.count(countable_component_value))};

                    REQUIRE(components_amount >= minimum_components_amount);
                    REQUIRE(components_amount <= maximum_components_amount);
                }
            }
        }
    }
}

TEST_CASE(
    "When the maximum amount of components and the minimum amount of components are equal, all generated paths have the same amount of non device, non root, non separator components",
    "[Path][Bounds][SpecialCase]")
{
    auto components_amount = GENERATE(take(10, random(std::size_t{1}, std::size_t{100})));

    QString countable_component_value{"a"};
    QString generated_path = GENERATE_COPY(
        take(10,
            path(
                empty_string(),
                empty_string(),
                cycle(Catch::Generators::value(copy_value(countable_component_value))),
                cycle(Catch::Generators::value(copy_value(countable_component_value))),
                empty_string(),
                PathGeneratorConfiguration{}.set_minimum_components_amount(components_amount).set_maximum_components_amount(components_amount)
            )
        )
    );

    REQUIRE(static_cast<std::size_t>(generated_path.count(countable_component_value)) == components_amount);
}

SCENARIO("The format of a path", "[Path][Contents]") {
    GIVEN("A series of components generators") {
        // TODO: Could probably move this to the global scope to
        // lighen the tests.
        QString device_component_value{"device"};
        QString root_component_value{"root"};
        QString directory_component_value{"dir"};
        QString filename_component_value{"file"};
        QString separator_component_value{"sep"};

        auto device_component_generator = cycle(Catch::Generators::value(copy_value(device_component_value)));
        auto root_component_generator = cycle(Catch::Generators::value(copy_value(root_component_value)));
        auto directory_component_generator = cycle(Catch::Generators::value(copy_value(directory_component_value)));
        auto filename_component_generator = cycle(Catch::Generators::value(copy_value(filename_component_value)));
        auto separator_component_generator = cycle(Catch::Generators::value(copy_value(separator_component_value)));

        AND_GIVEN("A generator of paths using those components generator") {
            // TODO: We should actually randomize the configuration by
            // making a simple generator for it.
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("At most one device component is in the generated path") {
                    REQUIRE(generated_path.count(device_component_value) <= 1);
                }

                THEN("At most one root component is in the generated path") {
                    REQUIRE(generated_path.count(root_component_value) <= 1);
                }

                THEN("At most one filename component is in the generated path") {
                    REQUIRE(generated_path.count(filename_component_value) <= 1);
                }

                THEN("At least one non device, non root, non separator component is in the generated path") {
                    REQUIRE((generated_path.contains(directory_component_value) || generated_path.contains(filename_component_value)));
                }

                THEN("There is a separator component between any two successive directory components") {
                    // REMARK: To test this condition, which is not
                    // easy to test directly, as, if the generator is
                    // working as it should, the concept of successive
                    // directories stops existing.
                    // To test it, then, we split the condition into
                    // two parts, that are easier to test, that
                    // achieve the same effect.
                    // First, if all directories have a separator
                    // component between them, it is impossible to
                    // have a directory component that is directly
                    // followed by another directory component.
                    // Second, when this holds, any two directory
                    // components must have one or more non-directory
                    // components between them.
                    // For those directories that have exactly one
                    // component between them, it must be a separator.
                    // This is equivalent to the original condition as
                    // long as it is not allowed for anything else to
                    // be between two directory components that have
                    // exactly one component between them.
                    // This is true at the time of writing of this
                    // test, such that this will work correctly, but
                    // if this changes the test is invalidated.
                    // If a test for the original condition is found
                    // that is not contrived (as it is possible to
                    // test the original condition but it is a bit
                    // more complex than we would like the test to
                    // be), it should replace this current
                    // implementation to improve the resiliency of the
                    // test.
                    REQUIRE_FALSE(generated_path.contains(directory_component_value + directory_component_value));

                    auto successive_directories_re{
                    QRegularExpression(u"%1(%2)%3"_s.arg(directory_component_value)
                                                     .arg(QStringList{device_component_value, root_component_value, filename_component_value, separator_component_value}.join("|"))
                                                     .arg(directory_component_value)
                    )};

                    auto successive_directories_match(successive_directories_re.match(generated_path));
                    while (successive_directories_match.hasMatch()) {
                        auto in_between_component{successive_directories_match.captured(1)};

                        // TODO: Having this in a loop makes it so
                        // the amount of assertions will vary slightly
                        // per-run.
                        // It would be better to avoid this, even if
                        // it should not really be a problem
                        // generally.
                        // Try to find a better way to express this
                        // condition that does not require a loop.
                        // This could be as easy as just collection
                        // the results and then using a std::all_of.
                        REQUIRE(in_between_component == separator_component_value);

                        successive_directories_match = successive_directories_re.match(generated_path, successive_directories_match.capturedEnd(1));
                    }
                }


                THEN("There is a separator component between each successive directory and filename components") {
                    REQUIRE_FALSE(generated_path.contains(directory_component_value + filename_component_value));

                    auto successive_directory_filename_re{
                    QRegularExpression(u"%1(%2)%3"_s.arg(directory_component_value)
                                                     .arg(QStringList{device_component_value, root_component_value, filename_component_value, separator_component_value}.join("|"))
                                                     .arg(filename_component_value)
                    )};

                    auto successive_directory_filename_match(successive_directory_filename_re.match(generated_path));
                    while (successive_directory_filename_match.hasMatch()) {
                        auto in_between_component{successive_directory_filename_match.captured(1)};

                        REQUIRE(in_between_component == separator_component_value);

                        successive_directory_filename_match = successive_directory_filename_re.match(generated_path, successive_directory_filename_match.capturedEnd(1));
                    }
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that generates Multi-Device paths") {
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_multi_device_path_probability(1.0)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("Exactly one device component is in the generated path") {
                    REQUIRE(generated_path.count(device_component_value) == 1);

                    AND_THEN("The device component is the first component in the generated path") {
                        REQUIRE(generated_path.startsWith(device_component_value));
                    }
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that generates Absolute paths") {
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_absolute_path_probability(1.0)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("Exactly one root component is in the generated path") {
                    REQUIRE(generated_path.count(root_component_value) == 1);
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that generates Absolute paths that are not Multi-Device") {
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_multi_device_path_probability(0.0).set_absolute_path_probability(1.0)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("The root component is the first component in the generated path") {
                    REQUIRE(generated_path.startsWith(root_component_value));
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that generates Multi-Device, Absolute paths") {
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_multi_device_path_probability(1.0).set_absolute_path_probability(1.0)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("The root component succeeds the device component in the generated path") {
                    REQUIRE(generated_path.contains(device_component_value + root_component_value));
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that generates paths that are To a Directory and do not Have a Trailing Separator") {
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_directory_path_probability(1.0).set_has_trailing_separator_probability(0.0)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("The last component of in the path is a directory component") {
                    REQUIRE(generated_path.endsWith(directory_component_value));
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that generates paths that are To a Directory and Have a Trailing Separator") {
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_directory_path_probability(1.0).set_has_trailing_separator_probability(1.0)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("The last component in the path is a separator component that is preceded by a directory component") {
                    REQUIRE(generated_path.endsWith(directory_component_value + separator_component_value));
                }
            }
        }


        AND_GIVEN("A generator of paths using those components generator that generates paths that are To a File") {
            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_directory_path_probability(0.0)
            );

            WHEN("A path is generated from that generator") {
                auto generated_path = GENERATE_REF(take(10, std::move(path_generator)));

                THEN("Exactly one filename component is in the path") {
                    REQUIRE(generated_path.contains(filename_component_value));

                    AND_THEN("The filename component is the last component in the path") {
                        REQUIRE(generated_path.endsWith(filename_component_value));
                    }
                }
            }
        }
    }
}

// REMARK: [mayfail][distribution]
SCENARIO("Observing the distribution of paths based on their configuration", "[Path][Statistics][!mayfail]") {
    GIVEN("A series of components generators") {
        QString device_component_value{"device"};
        QString root_component_value{"root"};
        QString directory_component_value{"dir"};
        QString filename_component_value{"file"};
        QString separator_component_value{"sep"};

        auto device_component_generator = cycle(Catch::Generators::value(copy_value(device_component_value)));
        auto root_component_generator = cycle(Catch::Generators::value(copy_value(root_component_value)));
        auto directory_component_generator = cycle(Catch::Generators::value(copy_value(directory_component_value)));
        auto filename_component_generator = cycle(Catch::Generators::value(copy_value(filename_component_value)));
        auto separator_component_generator = cycle(Catch::Generators::value(copy_value(separator_component_value)));

        AND_GIVEN("A generator of paths using those components generator that produces paths that are Multi-Device with a probability of n") {
            double multi_device_path_probability = GENERATE(take(10, random(0.0, 1.0)));

            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_multi_device_path_probability(multi_device_path_probability)
            );

            WHEN("A certain amount of paths are generated from that generator") {
                auto paths = GENERATE_REF(take(1, chunk(10000, std::move(path_generator))));

                THEN("The amount of paths that are Multi-Device approximately respects the given probability and the amount of paths that are not approximately respects a probability of 1 - n") {
                    auto maybe_distribution_error{respects_distribution(
                        std::move(paths),
                        [&device_component_value](const QString& path){ return (path.startsWith(device_component_value)) ? "Multi-Device" : "Non Multi-Device"; },
                        [multi_device_path_probability](const QString& key){ return probability_to_percentage((key == "Multi-Device") ? multi_device_path_probability : 1 - multi_device_path_probability); }
                    )};

                    REQUIRE_FALSE(maybe_distribution_error);
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that produces paths that are Absolute with a probability of n") {
            double absolute_path_probability = GENERATE(take(10, random(0.0, 1.0)));

            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_absolute_path_probability(absolute_path_probability)
            );

            WHEN("A certain amount of paths are generated from that generator") {
                auto paths = GENERATE_REF(take(1, chunk(10000, std::move(path_generator))));

                THEN("The amount of paths that are Absolute approximately respects the given probability and the amount of paths that are Relative approximately respects a probability of 1 - n") {
                    auto maybe_distribution_error{respects_distribution(
                        std::move(paths),
                        [&root_component_value](const QString& path){ return (path.contains(root_component_value)) ? "Absolute" : "Relative"; },
                        [absolute_path_probability](const QString& key){ return probability_to_percentage((key == "Absolute") ? absolute_path_probability : 1 - absolute_path_probability); }
                    )};

                    REQUIRE_FALSE(maybe_distribution_error);
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that produces paths that are To a Directory with a probability of n") {
            double directory_path_probability = GENERATE(take(10, random(0.0, 1.0)));

            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_directory_path_probability(directory_path_probability)
            );

            WHEN("A certain amount of paths are generated from that generator") {
                auto paths = GENERATE_REF(take(1, chunk(10000, std::move(path_generator))));

                THEN("The amount of paths that are To a Directory approximately respects the given probability and the amount of paths that are To a File approximately respects a probability of 1 - n") {
                    auto maybe_distribution_error{respects_distribution(
                        std::move(paths),
                        [&filename_component_value](const QString& path){ return (path.contains(filename_component_value)) ? "To a File" : "To a Directory"; },
                        [directory_path_probability](const QString& key){ return probability_to_percentage((key == "To a Directory") ? directory_path_probability : 1 - directory_path_probability); }
                    )};

                    REQUIRE_FALSE(maybe_distribution_error);
                }
            }
        }

        AND_GIVEN("A generator of paths using those components generator that produces paths that are To a Directory with a probability of n to Have a Trailing Separator") {
            double has_trailing_separator_probability = GENERATE(take(10, random(0.0, 1.0)));

            auto path_generator = path(
                std::move(device_component_generator),
                std::move(root_component_generator),
                std::move(directory_component_generator),
                std::move(filename_component_generator),
                std::move(separator_component_generator),
                PathGeneratorConfiguration{}.set_directory_path_probability(1.0).set_has_trailing_separator_probability(has_trailing_separator_probability)
            );

            WHEN("A certain amount of paths are generated from that generator") {
                auto paths = GENERATE_REF(take(1, chunk(10000, std::move(path_generator))));

                THEN("The amount of paths that are Have a Trailing Separator approximately respects the given probability and the amount of paths that do not Have a Trailing Separator approximately respects a probability of 1 - n") {
                    auto maybe_distribution_error{respects_distribution(
                        std::move(paths),
                        [&separator_component_value](const QString& path){ return (path.endsWith(separator_component_value)) ? "Have a Trailing Separator" : "Doesn't Have a Trailing Separator"; },
                        [has_trailing_separator_probability](const QString& key){ return probability_to_percentage((key == "Have a Trailing Separator") ? has_trailing_separator_probability : 1 - has_trailing_separator_probability); }
                    )};

                    REQUIRE_FALSE(maybe_distribution_error);
                }
            }
        }
    }
}

TEST_CASE("The first component of the passed in device components generator is not lost", "[Path][GeneratorFirstElement][SpecialCase]") {
    QString device_component_generator_first_value{"device"};

    auto generated_path = GENERATE_COPY(take(1,
        path(
            values({device_component_generator_first_value, QString{""}}),
            empty_string(),
            empty_string(),
            empty_string(),
            empty_string(),
            PathGeneratorConfiguration{}
                .set_multi_device_path_probability(1.0)
                .set_minimum_components_amount(1)
                .set_maximum_components_amount(1)
        )
    ));

    REQUIRE(generated_path.contains(device_component_generator_first_value));
}

TEST_CASE("The first component of the passed in root components generator is not lost", "[Path][GeneratorFirstElement][SpecialCase]") {
    QString root_component_generator_first_value{"root"};

    auto generated_path = GENERATE_COPY(take(1,
        path(
            empty_string(),
            values({root_component_generator_first_value, QString{""}}),
            empty_string(),
            empty_string(),
            empty_string(),
            PathGeneratorConfiguration{}
                .set_absolute_path_probability(1.0)
                .set_minimum_components_amount(1)
                .set_maximum_components_amount(1)
        )
    ));

    REQUIRE(generated_path.contains(root_component_generator_first_value));
}

TEST_CASE("The first component of the passed in directory components generator is not lost", "[Path][GeneratorFirstElement][SpecialCase]") {
    QString directory_component_generator_first_value{"dir"};

    auto generated_path = GENERATE_COPY(take(1,
        path(
            empty_string(),
            empty_string(),
            values({directory_component_generator_first_value, QString{""}}),
            empty_string(),
            empty_string(),
            PathGeneratorConfiguration{}
                .set_directory_path_probability(1.0)
                .set_minimum_components_amount(1)
                .set_maximum_components_amount(1)
        )
    ));

    REQUIRE(generated_path.contains(directory_component_generator_first_value));
}

TEST_CASE("The first component of the passed in filename components generator is not lost", "[Path][GeneratorFirstElement][SpecialCase]") {
    QString filename_component_generator_first_value{"dir"};

    auto generated_path = GENERATE_COPY(take(1,
        path(
            empty_string(),
            empty_string(),
            empty_string(),
            values({filename_component_generator_first_value, QString{""}}),
            empty_string(),
            PathGeneratorConfiguration{}
                .set_directory_path_probability(0.0)
                .set_minimum_components_amount(1)
                .set_maximum_components_amount(1)
        )
    ));

    REQUIRE(generated_path.contains(filename_component_generator_first_value));
}

TEST_CASE("The first component of the passed in separator components generator is not lost", "[Path][GeneratorFirstElement][SpecialCase]") {
    QString separator_component_generator_first_value{"sep"};

    auto generated_path = GENERATE_COPY(take(1,
        path(
            empty_string(),
            empty_string(),
            empty_string(),
            empty_string(),
            values({separator_component_generator_first_value, QString{""}}),
            PathGeneratorConfiguration{}
                .set_directory_path_probability(0.0)
                .set_minimum_components_amount(2)
                .set_maximum_components_amount(2)
        )
    ));

    REQUIRE(generated_path.contains(separator_component_generator_first_value));
}

SCENARIO("Generating paths that are suitable to be used on POSIX systems", "[Path][POSIX][Content]") {
    GIVEN("A generator that generates Strings representing paths on a POSIX system that are portable") {
        auto path_generator = relaxed_portable_posix_path();

        WHEN("A path is generated from it") {
            auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

            THEN("The path is composed only by one or more characters in the class [-_./a-zA-Z0-9]") {
                REQUIRE(QRegularExpression{R"(\A[-_.\/a-zA-Z0-9]+\z)"}.match(generated_path).hasMatch());
            }
        }
    }
}

SCENARIO("Generating paths that are suitable to be used on Windows", "[Path][Windows][Content]") {
    GIVEN("A generator that generates Strings representing paths on a Windows system") {
        auto path_generator = traditional_dos_path();

        WHEN("A path is generated from it") {
            auto generated_path = GENERATE_REF(take(100, std::move(path_generator)));

            CAPTURE(generated_path);

            THEN("The path starts with an uppercase letter followed by a colon, a backward or forward slash or a character in the class [-_.a-zA-Z0-9]") {
                QRegularExpression beginning_re{"([A-Z]:|\\|\\/|[-_.a-zA-Z0-9])"};

                auto beginning_match{beginning_re.match(generated_path)};

                REQUIRE(beginning_match.hasMatch());

                generated_path.remove(0, beginning_match.capturedEnd());

                AND_THEN("The rest of the path is composed by zero or more characters in the class [-_./\\a-zA-Z0-9]") {
                    REQUIRE(QRegularExpression{R"(\A[-_.\/\\a-zA-Z0-9]*\z)"}.match(generated_path).hasMatch());
                }
            }
        }
    }
}