1 module toml_foolery.encode.util;
2 
3 import std.array : Appender;
4 
5 
6 version(unittest)
7 {
8     import exceeds_expectations;
9 
10     package(toml_foolery) void expectToEqualNoBlanks(
11         string actual,
12         string expected,
13         string file = __FILE__,
14         size_t line = __LINE__
15     )
16     {
17         immutable string actualClean = actual.clean();
18         immutable string expectedClean = expected.clean();
19         expect(actual, file, line).toSatisfy(actual => compareStringsNoBlanks(actual, expected));
20     }
21 
22     private string clean(string s)
23     {
24         import std..string : strip;
25         import std.regex : ctRegex, replaceAll;
26 
27         enum auto cleaner1 = ctRegex!(`(\r\n|\n)[\s\t]+`, "g");
28         enum auto cleaner2 = ctRegex!(`\n\n+|\r\n(\r\n)+`, "g");
29 
30         return s.replaceAll(cleaner1, "\n").replaceAll(cleaner2, "\n").strip();
31     }
32 
33     /// Compares two strings without caring about newlines.
34     private bool compareStringsNoBlanks(string a, string b)
35     {
36         return a.clean() == b.clean();
37     }
38 
39     @("Test compareStringsNoBlanks")
40     unittest
41     {
42     string a = `a
43 
44     b
45 
46 c
47   d
48  e
49 
50 f`;
51 
52     string b = `
53 
54      a
55   b
56 
57 c
58 
59 d
60       e
61 f
62 
63 
64 `;
65         assert(compareStringsNoBlanks(a, b),
66             "The following two strings differ in more ways than just line breaks " ~
67             "and leading/trailing whitespace: \n\nExpected:\n\n" ~
68             a ~ "\n\n" ~
69             b
70         );
71     }
72 }