summaryrefslogtreecommitdiff
path: root/vendor/bandit/bandit/assertion_frameworks/snowhouse/example/string_line_tests.cpp
blob: 8cf90cfb37a3fc03901849f7e3055b805ddf8c9c (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
#include <snowhouse/snowhouse.h>
using namespace snowhouse;
#include "tests.h"

void StringLineTests()
{
  std::cout << "================================================" << std::endl;
  std::cout << "   StringLineTests" << std::endl;
  std::cout << "================================================" << std::endl;

  std::cout << "CanAssertThatAtLeastOneLineInAStreamMatches" << std::endl;
  {
    Assert::That("First line\n", Has().AtLeast(1).EqualTo("First line"));
  }

  std::cout << "CanDetectWhenAssertionFails" << std::endl;
  {
    AssertTestFails(Assert::That("First line\n", Has().AtLeast(1).EqualTo("Second line")), "Expected: at least 1 equal to Second line");
  }

  std::cout << "CanHandleLineMissingNewline" << std::endl;
  {
    Assert::That("First line", Has().AtLeast(1).EqualTo("First line"));
  }

  std::cout << "CanHandleSeveralLines" << std::endl;
  {
    std::string lines = "First line\nSecond line";
    Assert::That(lines, Has().Exactly(2).EndingWith("line"));
  }

  std::cout << "CanHandleWindowsLineEndings" << std::endl;
  {
    std::string lines = "First line\r\nSecond line\r\nThird line";
    Assert::That(lines, Has().Exactly(3).EndingWith("line"));
  }

  std::cout << "CanMatchBeginningOfLinesWithWindowsLineEndings" << std::endl;
  {
    std::string lines = "First line\nSecond line\r\nThird line";
    Assert::That(lines, Has().Exactly(1).StartingWith("Second"));
  }

  std::cout << "CanHandleEmptyLinesWhenUsingWindowsLineEndings" << std::endl;
  {
    std::string lines = "\r\nSecond line\r\n\r\n";
    Assert::That(lines, Has().Exactly(2).OfLength(0));
  }

  std::cout << "CanHandleLastLineMissingNewlineForWindowsLineEndings" << std::endl;
  {
    std::string lines = "First line\r\nSecond line";
    Assert::That(lines, Has().Exactly(2).EndingWith("line"));
  }

  std::cout << "CanHandleAllEmptyLines" << std::endl;
  {
    Assert::That("\n\n\n\n\n\n", Has().Exactly(6).OfLength(0));
  }

  std::cout << "CanHandleAllEmptyLinesWithWindowsLineEndings" << std::endl;
  {
    Assert::That("\r\n\r\n\r\n", Has().Exactly(3).OfLength(0));
  }


  std::cout << "================================================" << std::endl;
  std::cout << "   StringLineParserTests" << std::endl;
  std::cout << "================================================" << std::endl;


  std::cout << "CanParseEmptyString" << std::endl;
  {
    std::vector<std::string> res;

    StringLineParser::Parse("", res);

    Assert::That(res, HasLength(0));
  }

  std::cout << "CanParseSingleLine" << std::endl;
  {
    std::vector<std::string> res;

    StringLineParser::Parse("Simple line", res);

    Assert::That(res, HasLength(1));
    Assert::That(res, Has().Exactly(1).EqualTo("Simple line"));
  }

  std::cout << "CanParseTwoLines" << std::endl;
  {
    std::vector<std::string> res;

    StringLineParser::Parse("One line\nTwo lines", res);

    Assert::That(res, HasLength(2));
    Assert::That(res, Has().Exactly(1).EqualTo("One line"));
    Assert::That(res, Has().Exactly(1).EqualTo("Two lines"));
  }

  std::cout << "CanParseThreeLines" << std::endl;
  {
    std::vector<std::string> res;

    StringLineParser::Parse("One line\nTwo lines\nThree lines", res);

    Assert::That(res, HasLength(3));
    Assert::That(res, Has().Exactly(1).EqualTo("One line"));
    Assert::That(res, Has().Exactly(1).EqualTo("Two lines"));
    Assert::That(res, Has().Exactly(1).EqualTo("Three lines"));
  }

  std::cout << "CanHandleStringEndingWithNewline" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("One line\n", res);
    Assert::That(res, HasLength(1));
    Assert::That(res, Has().Exactly(1).EqualTo("One line"));
  }

  std::cout << "CanHandleSingleLineWithWindowsLineEnding" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("One line\r\n", res);
    Assert::That(res, HasLength(1));
    Assert::That(res, Has().Exactly(1).EqualTo("One line"));
  }

  std::cout << "CanHandleTwoLinesWithWindowsLineEndings" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("One line\r\nTwo lines", res);
    Assert::That(res, HasLength(2));
    Assert::That(res, Has().Exactly(1).EqualTo("One line"));
    Assert::That(res, Has().Exactly(1).EqualTo("Two lines"));
  }

  std::cout << "CanHandleEmptyLineWithNewline" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("\n", res);
    Assert::That(res, Is().OfLength(1).And().Exactly(1).OfLength(0));
  }

  std::cout << "CanHandleTwoEmptyLines" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("\n\n", res);
    Assert::That(res, HasLength(2));
    Assert::That(res, Has().Exactly(2).OfLength(0));
  }

  std::cout << "CanHandleTwoEmptyLinesWithWindowsLineEndings" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("\r\n\r\n", res);
    Assert::That(res, HasLength(2));
    Assert::That(res, Has().Exactly(2).OfLength(0));
  }

  std::cout << "CanHandleCarriageReturnOnly" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("One line\rTwo lines", res);
    Assert::That(res, HasLength(2));
    Assert::That(res, Has().Exactly(1).EqualTo("One line"));
    Assert::That(res, Has().Exactly(1).EqualTo("Two lines"));
  }

  std::cout << "CanHandleCarriageReturnOnlyAtEndOfString" << std::endl;
  {
    std::vector<std::string> res;
    StringLineParser::Parse("One line\r\nTwo lines\r", res);
    Assert::That(res, HasLength(2));
    Assert::That(res, Has().Exactly(1).EqualTo("One line"));
    Assert::That(res, Has().Exactly(1).EqualTo("Two lines"));
  }
}