10 #include <cppunit/TestFixture.h>
11 #include <cppunit/extensions/HelperMacros.h>
12 #include <boost/regex.hpp>
22 class regexTest :
public CPPUNIT_NS :: TestFixture {
24 CPPUNIT_TEST (regTest);
25 CPPUNIT_TEST (badNameTest);
26 CPPUNIT_TEST (regTestSpecialEnd);
28 CPPUNIT_TEST_SUITE_END ();
43 const std::string gplLicense =
"GPL-2.0";
44 const std::string lgplLicense =
"LGPL-2.1+";
46 std::string content =
"SPDX-License-Identifier: " + gplLicense +
" AND "
52 std::string::const_iterator begin = content.begin();
53 std::string::const_iterator end = content.end();
54 boost::match_results<std::string::const_iterator> what;
57 boost::regex_search(begin, end, what, listRegex);
58 licenseList = what[1].str();
61 CPPUNIT_ASSERT_EQUAL(gplLicense +
" AND " + lgplLicense, licenseList);
64 begin = licenseList.begin();
65 end = licenseList.end();
66 list<string> licensesFound;
71 if (boost::regex_search(begin, end, res, nameRegex))
73 licensesFound.push_back(res[1].str());
74 begin = res[0].second;
82 size_t expectedNos = 2;
83 size_t actualNos = licensesFound.size();
85 CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
88 std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
89 != licensesFound.end());
91 std::find(licensesFound.begin(), licensesFound.end(), lgplLicense)
92 != licensesFound.end());
107 const std::string gplLicense =
"GPL-2.0";
108 const std::string badLicense =
"AB";
110 std::string content =
"SPDX-License-Identifier: " + gplLicense +
" AND "
116 std::string::const_iterator begin = content.begin();
117 std::string::const_iterator end = content.end();
118 boost::match_results<std::string::const_iterator> what;
121 boost::regex_search(begin, end, what, listRegex);
122 licenseList = what[1].str();
125 CPPUNIT_ASSERT_EQUAL(gplLicense, licenseList);
128 begin = licenseList.begin();
129 end = licenseList.end();
130 list<string> licensesFound;
135 if (boost::regex_search(begin, end, res, nameRegex))
137 licensesFound.push_back(res[1].str());
138 begin = res[0].second;
146 size_t expectedNos = 1;
147 size_t actualNos = licensesFound.size();
149 CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
152 std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
153 != licensesFound.end());
156 std::find(licensesFound.begin(), licensesFound.end(), badLicense)
157 == licensesFound.end());
172 const std::string gplLicense =
"GPL-2.0-only";
173 const std::string lgplLicense =
"LGPL-2.1-or-later";
174 const std::string mitLicense =
"MIT";
175 const std::string mplLicense =
"MPL-1.1+";
177 std::string content =
"SPDX-License-Identifier: (" + gplLicense +
" AND "
178 + lgplLicense +
") OR " + mplLicense +
" AND " + mitLicense +
".";
183 std::string::const_iterator begin = content.begin();
184 std::string::const_iterator end = content.end();
185 boost::match_results<std::string::const_iterator> what;
188 boost::regex_search(begin, end, what, listRegex);
189 licenseList = what[1].str();
192 CPPUNIT_ASSERT_EQUAL(
"(" + gplLicense +
" AND " + lgplLicense +
") OR " +
193 mplLicense +
" AND " + mitLicense +
".", licenseList);
196 begin = licenseList.begin();
197 end = licenseList.end();
198 list<string> licensesFound;
203 if (boost::regex_search(begin, end, res, nameRegex))
205 licensesFound.push_back(res[1].str());
206 begin = res[0].second;
214 size_t expectedNos = 4;
215 size_t actualNos = licensesFound.size();
217 CPPUNIT_ASSERT_EQUAL(expectedNos, actualNos);
220 std::find(licensesFound.begin(), licensesFound.end(), gplLicense)
221 != licensesFound.end());
223 std::find(licensesFound.begin(), licensesFound.end(), lgplLicense)
224 != licensesFound.end());
226 std::find(licensesFound.begin(), licensesFound.end(), mitLicense)
227 != licensesFound.end());
229 std::find(licensesFound.begin(), licensesFound.end(), mplLicense)
230 != licensesFound.end());
235 CPPUNIT_TEST_SUITE_REGISTRATION(
regexTest );
Test fixture to test regex accuracy.
void regTestSpecialEnd(void)
Test regex on a special test string.
void regTest(void)
Test regex on a test string.
void badNameTest(void)
Test regex on a string with bad identifier.
#define SPDX_LICENSE_NAMES
Regex to filter license names from list of license list.
#define SPDX_LICENSE_LIST
Regex to filter the list of licenses.