Debian Codegraph @

Testing/kwsBadCharactersTest.cxx

raw
1 /*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19 #include "kwsParser.h"
20
21 int kwsBadCharactersTest(int, char* [] )
22 {
23 constexpr char nonUTF8[] = {static_cast<char>(0xE4),
24 static_cast<char>(0xFC),
25 static_cast<char>(0xF6),
26 static_cast<char>(0xDF),
27 0};
28
29 // 1) String with bad characters only in the code.
30 std::string buffer = "void()\n{\ntest";
31 buffer += nonUTF8;
32 buffer += ";\n}\n";
33
34 // Test for bad characters (including within comments).
35 kws::Parser parser;
36 parser.SetBuffer(buffer);
37 parser.Check("BadCharacters","true");
38
39 std::cout << "Test with bad characters only in code: ";
40 kws::Parser::ErrorVectorType errors = parser.GetErrors();
41 if (!errors.empty()) {
42 for (auto &error : errors) {
43 std::cout << error.description << std::endl;
44 }
45 std::cout << "[PASSED]" << std::endl;
46 } else {
47 std::cout << "[FAILED]" << std::endl;
48 return EXIT_FAILURE;
49 }
50
51 // 2) String with bad characters only in the comments.
52 buffer = "void()\n{\n //test";
53 buffer += nonUTF8;
54 buffer += "\n}\n";
55
56 parser.SetBuffer(buffer);
57
58 // 2a) Test for bad characters (including within comments).
59 parser.ClearErrors();
60 parser.Check("BadCharacters","true");
61
62 std::cout << "Test with bad characters in comments, include comments: ";
63 errors = parser.GetErrors();
64 if (!errors.empty()) {
65 for (auto &error : errors) {
66 std::cout << error.description << std::endl;
67 }
68 std::cout << "[PASSED]" << std::endl;
69 } else {
70 std::cout << "[FAILED]" << std::endl;
71 return EXIT_FAILURE;
72 }
73
74 // 2b) Test for bad characters (excluding within comments).
75 parser.ClearErrors();
76 parser.Check("BadCharacters","false");
77
78 std::cout << "Test with bad characters in comments, exclude comments: ";
79 errors = parser.GetErrors();
80 if (errors.empty()) {
81 std::cout << "[PASSED]" << std::endl;
82 } else {
83 for (auto &error : errors) {
84 std::cout << error.description << std::endl;
85 }
86 std::cout << "[FAILED]" << std::endl;
87 return EXIT_FAILURE;
88 }
89
90 std::cout << "[DONE]" << std::endl;
91
92 return 0;
93 }