FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_regexConfProvider.cc
1 /*
2  SPDX-FileCopyrightText: © 2015 Siemens AG
3  Author: Maximilian Huber
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include <cppunit/TestFixture.h>
9 #include <cppunit/extensions/HelperMacros.h>
10 
11 #include "regexConfProvider.hpp"
12 #include <sstream>
13 
14 using namespace std;
15 
16 class regexConfProviderTestSuite : public CPPUNIT_NS :: TestFixture {
17  CPPUNIT_TEST_SUITE (regexConfProviderTestSuite);
18  CPPUNIT_TEST (simpleTest);
19  CPPUNIT_TEST (simpleReplacementTest);
20  CPPUNIT_TEST (multipleReplacementTest);
21  CPPUNIT_TEST (testForInfiniteRecursion);
22 
23  CPPUNIT_TEST_SUITE_END ();
24 
25 private:
36  void regexConfProviderTest (istringstream& testStream,
37  const string& testString,
38  const string& testKey)
39  {
40  string testIdentity("testIdentity");
41 
43 
44  // load parse test-stream
45  rcp.maybeLoad(testIdentity,testStream);
46 
47  // test RegexConfProvider
48  CPPUNIT_ASSERT_MESSAGE("The generated string should match the expected string",
49  0 == strcmp(testString.c_str(),
50  rcp.getRegexValue(testIdentity,testKey)));
51  }
52 
53 protected:
59  void simpleTest()
60  {
61  string testString = "Lorem Ipsum";
62  string testKey = "TEST";
63  string testLine = testKey + "=" + testString + "\n";
64  istringstream testStream(testLine);
65 
66  regexConfProviderTest(testStream,testString,testKey);
67  }
68 
75  {
76  string testString = "Lorem Ipsum";
77  string testKey = "TEST";
78  string testLine =
79  testKey + "=" + "Lorem \n" +
80  testKey + "=__" + testKey + "__Ipsum\n";
81  istringstream testStream(testLine);
82 
83  regexConfProviderTest(testStream,testString,testKey);
84  }
85 
92  {
93  string testString = "Lorem Ipsum";
94  string testKey = "TEST";
95  string testLine =
96  string("SPACE= \n") +
97  "INFIX2=su\n" +
98  "INFIX1=rem__SPACE__I\n" +
99  testKey + "=Lo__INFIX1__p__INFIX2__m\n";
100  istringstream testStream(testLine);
101 
102  regexConfProviderTest(testStream,testString,testKey);
103  }
104 
112  {
113  string testString = "Lorem Ipsum";
114  string testKey = "TEST";
115  string testLine =
116  string("LOREM=Lorem__LOREM__ \n") +
117  testKey + "=__LOREM__Ipsum\n";
118  istringstream testStream(testLine);
119 
120  string testIdentity("testIdentity");
121 
122  RegexConfProvider rcp;
123 
124  // load parse test-stream
125  rcp.maybeLoad(testIdentity,testStream);
126 
127  // evaluate and verify, that recursion does not appear
128  CPPUNIT_ASSERT_MESSAGE("This should just terminate (the return value is not specified)",
129  rcp.getRegexValue(testIdentity,testKey));
130  }
131 };
132 
133 CPPUNIT_TEST_SUITE_REGISTRATION( regexConfProviderTestSuite );
Provide regex using conf file.
void maybeLoad(const std::string &identity)
Check if identity already loaded in RegexMap, if not load them.
const char * getRegexValue(const std::string &name, const std::string &key)
Get the regex as string from the RegexMap.
void regexConfProviderTest(istringstream &testStream, const string &testString, const string &testKey)
Test RegexConfProvider.