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