1 module unit_threaded.ut.behave;
2 
3 import unit_threaded.behave;
4 import unit_threaded.runner.io;
5 
6 unittest {
7     import std.algorithm: map;
8     import std.exception: enforce;
9     import std.string: splitLines;
10     import unit_threaded.runner.testcase: TestCase;
11     import unit_threaded.should;
12 
13     class TestOutput: Output {
14         string output;
15         override void send(in string output) {
16             this.output ~= output;
17         }
18         override void flush(bool failed) {}
19     }
20 
21     class BehaveTest: TestCase {
22         override void test() {
23             given("the given step");
24             when("the when step");
25             then("the then step");
26             enforce!UnitTestError(false);
27         }
28         override string getPath() @safe pure nothrow const {
29             return "BehaveTest";
30         }
31     }
32 
33     auto test = new BehaveTest;
34     auto writer = new TestOutput;
35     test.setOutput(writer);
36 
37     test();
38     if (_useEscCodes) {
39         writer.output.splitLines.map!escapeAnsi.shouldEqual([
40             "BehaveTest:",
41             "",
42             "(tab)(intense)Given(normal) the given step      # behave.d:23(clearLine)",
43             "(green)(tab)(intense)Given(normal) the given step(default)      # behave.d:23",
44             "(tab)(intense)When(normal) the when step        # behave.d:24(clearLine)",
45             "(green)(tab)(intense)When(normal) the when step(default)        # behave.d:24",
46             "(tab)(intense)Then(normal) the then step        # behave.d:25(clearLine)",
47             "    tests/unit_threaded/ut/behave.d:26 - Enforcement failed",
48             "",
49             "(red)(tab)(intense)Then(normal) the then step(default)        # behave.d:25",
50             "",
51         ]);
52     } else {
53         writer.output.splitLines.map!escapeAnsi.shouldEqual([
54             "BehaveTest:",
55             "",
56             "(tab)Given the given step      # behave.d:23",
57             "(tab)When the when step        # behave.d:24",
58             "    tests/unit_threaded/ut/behave.d:26 - Enforcement failed",
59             "",
60             "(tab)Then the then step        # behave.d:25",
61             "",
62         ]);
63     }
64 }
65 
66 private string escapeAnsi(string line) {
67     import std.string: replace;
68 
69     return line
70         .replace("\033[1m", "(intense)")
71         .replace("\033[31m", "(red)")
72         .replace("\033[32m", "(green)")
73         .replace("\033[39m", "(default)")
74         .replace("\033[22m", "(normal)")
75         .replace("\033[2K", "(clearLine)")
76         .replace("\t", "(tab)")
77         .replace("\033", "\\033");
78 }