File indexing completed on 2024-04-21 05:44:40
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Andreas Cord-Landwehr <cordlandwehr@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "test_copyrightconvert.h" 0008 #include "../licenseregistry.h" 0009 #include "../directoryparser.h" 0010 #include <QTest> 0011 #include <QDebug> 0012 #include <QDir> 0013 #include <QDirIterator> 0014 0015 struct CopyrightExample { 0016 QString text; 0017 QString years; 0018 QString name; 0019 QString contact; 0020 }; 0021 0022 void TestCopyrightConvert::detectCopyright() 0023 { 0024 DirectoryParser parser; 0025 QRegularExpression exp = parser.copyrightRegExp(); 0026 0027 std::vector<CopyrightExample> testdata = { 0028 { "SPDX-FileCopyrightText: 2018 John Doe <mail@example.com>" , "2018", "John Doe", "<mail@example.com>" }, 0029 { "SPDX-FileCopyrightText: 2018 Aurélien Utfät <mail@example.com>" , "2018", "Aurélien Utfät", "<mail@example.com>" }, 0030 { "Copyright: 2018 John Doe <mail@example.com>" , "2018", "John Doe", "<mail@example.com>" }, 0031 { "copyright: 2018 John Doe <mail@example.com>" , "2018", "John Doe", "<mail@example.com>" }, 0032 { "Copyright: 2018, John Doe, <mail@example.com>" , "2018", "John Doe", "<mail@example.com>" }, 0033 { "Copyright: 2018 John M. C. Doe <mail@example.com>" , "2018", "John M. C. Doe", "<mail@example.com>" }, 0034 { "Copyright : 2018 John M. C. Doe <mail@example.com>" , "2018", "John M. C. Doe", "<mail@example.com>" }, 0035 { "Copyright (C) 2020 Andreas Cord-Landwehr <mail@example.com>", "2020", "Andreas Cord-Landwehr", "<mail@example.com>" }, 0036 { "Copyright 2018-2019 John Doe <mail@example.com>" , "2018-2019", "John Doe", "<mail@example.com>" }, 0037 { "Copyright © 2018-2019 John Doe" , "2018-2019", "John Doe", "" }, 0038 { "© 2018-2019 John Doe" , "2018-2019", "John Doe", "" }, 0039 { "(C) 2018-2019 John Doe" , "2018-2019", "John Doe", "" }, 0040 { "Copyright (C) 2015-2019 Jane Doe <mail@example.com>" , "2015-2019", "Jane Doe", "<mail@example.com>" }, 0041 { "Copyright 2018-2019 Singlename <mail@example.com>" , "2018-2019", "Singlename", "<mail@example.com>" }, 0042 { "Copyright 2018-2019 Foo of Baa <mail@example.com>" , "2018-2019", "Foo of Baa", "<mail@example.com>" }, 0043 { "Copyright 2018, 2019-2020 Foo of Baa <mail@example.com>" , "2018, 2019-2020", "Foo of Baa", "<mail@example.com>" }, 0044 { "Copyright 2006,2011 John M. C. Doe <mail@example.com>" , "2006, 2011", "John M. C. Doe", "<mail@example.com>" }, 0045 { "Copyright (C) 2010 - 2016 by Jane Doe <my.mail(at)example.com>" , "2010-2016", "Jane Doe", "<my.mail(at)example.com>" }, 0046 { "Copyright (C) 2020, Méven Doe <mail@example.com>", "2020", "Méven Doe", "<mail@example.com>" }, 0047 { "Copyright (c) 2006,2007 Laurent Doe, <mail@example.com>", "2006, 2007", "Laurent Doe", "<mail@example.com>" }, 0048 { "Copyright (C) 1997 to 2002 Jane Doe <mail@example.com>", "1997-2002", "Jane Doe", "<mail@example.com>" }, 0049 { "Copyright 2011 A.B. Foo Software Ltda.", "2011", "A.B. Foo Software Ltda.", "" }, 0050 // KAppTemplate template variables 0051 { "Copyright (C) %{CURRENT_YEAR} by %{AUTHOR} <%{EMAIL}>" , "%{CURRENT_YEAR}", "%{AUTHOR}", "<%{EMAIL}>" }, 0052 }; 0053 0054 for (const auto &example : testdata) { 0055 qDebug() << "Run:" << example.text; 0056 QRegularExpressionMatch match = exp.match(example.text); 0057 QVERIFY(match.hasMatch()); 0058 0059 QString years = match.captured("years"); 0060 years = parser.cleanupSpaceInCopyrightYearList(years); 0061 QString name = match.captured("name"); 0062 QString contact = match.captured("contact"); 0063 QCOMPARE(years, example.years); 0064 QCOMPARE(name, example.name); 0065 QCOMPARE(contact, example.contact); 0066 } 0067 } 0068 0069 void TestCopyrightConvert::convertSingleCopyrightStatement() 0070 { 0071 DirectoryParser parser; 0072 QRegularExpression exp = parser.copyrightRegExp(); 0073 0074 std::vector<std::pair<QString, QString>> testdata = { 0075 {"Copyright 2018 John Doe <mail@example.com>", "SPDX-FileCopyrightText: 2018 John Doe <mail@example.com>"}, 0076 }; 0077 0078 for (const auto &example : testdata) { 0079 QRegularExpressionMatch match = exp.match(example.first); 0080 QVERIFY(match.hasMatch()); 0081 0082 QString years = match.captured("years"); 0083 QString name = match.captured("name"); 0084 QString contact = match.captured("contact"); 0085 0086 QString unifiedCopyright = QString("SPDX-FileCopyrightText: %1 %2 %3").arg(years).arg(name).arg(contact); 0087 QCOMPARE(unifiedCopyright, example.second); 0088 } 0089 } 0090 0091 void TestCopyrightConvert::convertFullHeader() 0092 { 0093 const QString originalHeader = 0094 "/*\n" 0095 " * Copyright 2018 John Doe <mail@example.com>\n" 0096 " * Copyright 2018-2019 Jane Doe <another@example.com>\n" 0097 " * Copyright (C) 2013 by Volker Paul\n" 0098 " * Copyright : (C) 2013 by Volker Paul\n" 0099 " * Copyright © 2013 by Volker Paul\n" 0100 " * Copyright (c) 2001 the KMime authors.\n" 0101 " */\n"; 0102 0103 const QString targetHeader = 0104 "/*\n" 0105 " * SPDX-FileCopyrightText: 2018 John Doe <mail@example.com>\n" 0106 " * SPDX-FileCopyrightText: 2018-2019 Jane Doe <another@example.com>\n" 0107 " * SPDX-FileCopyrightText: 2013 Volker Paul\n" 0108 " * SPDX-FileCopyrightText: 2013 Volker Paul\n" 0109 " * SPDX-FileCopyrightText: 2013 Volker Paul\n" 0110 " * SPDX-FileCopyrightText: 2001 the KMime authors.\n" 0111 " */\n"; 0112 0113 DirectoryParser parser; 0114 QString header = parser.unifyCopyrightStatements(originalHeader); 0115 QCOMPARE(header, targetHeader); 0116 } 0117 0118 void TestCopyrightConvert::skipSourceCodeStrings() 0119 { 0120 const QString originalHeader = 0121 "i18n(\"Copyright 2020 KDE Developers\")"; 0122 0123 DirectoryParser parser; 0124 QString header = parser.unifyCopyrightStatements(originalHeader); 0125 QCOMPARE(header, originalHeader); 0126 } 0127 0128 void TestCopyrightConvert::prettyPrintCopyrightComment() 0129 { 0130 const QString originalHeader = 0131 "/**********************************************************************\n" 0132 " * SPDX-FileCopyrightText: 2018 John Doe <mail@example.com> *\n" 0133 " * SPDX-FileCopyrightText: 2018-2019 Jane Doe <another@example.com> *\n" 0134 " * SPDX-FileCopyrightText: 2001 the KMime authors.\n" 0135 " **********************************************************************/\n"; 0136 0137 const QString targetHeader = 0138 "/*\n" 0139 " SPDX-FileCopyrightText: 2018 John Doe <mail@example.com>\n" 0140 " SPDX-FileCopyrightText: 2018-2019 Jane Doe <another@example.com>\n" 0141 " SPDX-FileCopyrightText: 2001 the KMime authors.\n" 0142 "*/\n"; 0143 DirectoryParser parser; 0144 QString header = parser.unifyCopyrightCommentHeader(originalHeader); 0145 QCOMPARE(header, targetHeader); 0146 } 0147 0148 void TestCopyrightConvert::doNotConvertCopyrightKeyworsInCode() 0149 { 0150 const QString originalSnippet = 0151 "i18n(\"Copyright © 2019 John Doe\"))\n"; 0152 0153 DirectoryParser parser; 0154 QString convertedSnippet = parser.unifyCopyrightStatements(originalSnippet); 0155 QCOMPARE(convertedSnippet, originalSnippet); 0156 } 0157 0158 QTEST_GUILESS_MAIN(TestCopyrightConvert);