File indexing completed on 2025-02-16 04:45:49
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "grantleeprinttest.h" 0008 #include "grantlee/grantleeprint.h" 0009 #include <QTest> 0010 0011 GrantleePrintTest::GrantleePrintTest(QObject *parent) 0012 : QObject(parent) 0013 { 0014 } 0015 0016 GrantleePrintTest::~GrantleePrintTest() = default; 0017 0018 void GrantleePrintTest::shouldReturnEmptyStringWhenNotContentAndNoContacts() 0019 { 0020 KAddressBookGrantlee::GrantleePrint grantleePrint; 0021 KContacts::Addressee::List lst; 0022 QVERIFY(grantleePrint.contactsToHtml(lst).isEmpty()); 0023 } 0024 0025 void GrantleePrintTest::shouldReturnEmptyStringWhenAddContentWithoutContacts() 0026 { 0027 KAddressBookGrantlee::GrantleePrint grantleePrint; 0028 grantleePrint.setTemplateContent(QStringLiteral("foo")); 0029 KContacts::Addressee::List lst; 0030 QVERIFY(grantleePrint.contactsToHtml(lst).isEmpty()); 0031 } 0032 0033 void GrantleePrintTest::shouldReturnStringWhenAddContentAndContacts() 0034 { 0035 KAddressBookGrantlee::GrantleePrint grantleePrint; 0036 grantleePrint.setTemplateContent(QStringLiteral("foo")); 0037 KContacts::Addressee::List lst; 0038 KContacts::Addressee address; 0039 address.setName(QStringLiteral("foo1")); 0040 KContacts::Email email(QStringLiteral("foo@kde.org")); 0041 email.setPreferred(true); 0042 address.addEmail(email); 0043 lst << address; 0044 0045 QCOMPARE(grantleePrint.contactsToHtml(lst), QStringLiteral("foo")); 0046 } 0047 0048 void GrantleePrintTest::shouldReturnEmails() 0049 { 0050 KAddressBookGrantlee::GrantleePrint grantleePrint; 0051 KContacts::Addressee::List lst; 0052 KContacts::Addressee address; 0053 address.setName(QStringLiteral("foo1")); 0054 KContacts::Email email(QStringLiteral("foo@kde.org")); 0055 email.setPreferred(true); 0056 address.addEmail(email); 0057 lst << address; 0058 grantleePrint.setTemplateContent( 0059 QStringLiteral("{% if contacts %}{% for contact in contacts %}{% if contact.name %}{{ contact.name }}{% endif %}{% endfor %}{% endif %}")); 0060 0061 QCOMPARE(grantleePrint.contactsToHtml(lst), QStringLiteral("foo1")); 0062 } 0063 0064 void GrantleePrintTest::shouldDisplayContactInfo_data() 0065 { 0066 QTest::addColumn<QString>("variable"); 0067 QTest::addColumn<QString>("result"); 0068 QTest::newRow("name") << QStringLiteral("name") << QStringLiteral("foo1"); 0069 QTest::newRow("organization") << QStringLiteral("organization") << QStringLiteral("kde"); 0070 QTest::newRow("note") << QStringLiteral("note") << QStringLiteral("foo-note"); 0071 QTest::newRow("title") << QStringLiteral("title") << QStringLiteral("foo-title"); 0072 QTest::newRow("nickName") << QStringLiteral("nickName") << QStringLiteral("foo-nickname"); 0073 QTest::newRow("familyName") << QStringLiteral("familyName") << QStringLiteral("foo-familyname"); 0074 QTest::newRow("role") << QStringLiteral("role") << QStringLiteral("foo-role"); 0075 QTest::newRow("suffix") << QStringLiteral("suffix") << QStringLiteral("foo-suffix"); 0076 QTest::newRow("prefix") << QStringLiteral("prefix") << QStringLiteral("foo-prefix"); 0077 QTest::newRow("department") << QStringLiteral("department") << QStringLiteral("foo-department"); 0078 QTest::newRow("office") << QStringLiteral("office") << QStringLiteral("foo-office"); 0079 QTest::newRow("profession") << QStringLiteral("profession") << QStringLiteral("foo-profession"); 0080 QTest::newRow("manager") << QStringLiteral("managersName") << QStringLiteral("foo-managersname"); 0081 QTest::newRow("assistant") << QStringLiteral("assistantsName") << QStringLiteral("foo-assistantsname"); 0082 QTest::newRow("spouse") << QStringLiteral("spousesName") << QStringLiteral("foo-spousesname"); 0083 QTest::newRow("givenname") << QStringLiteral("givenName") << QStringLiteral("foo-givenname"); 0084 QTest::newRow("additionalname") << QStringLiteral("additionalName") << QStringLiteral("foo-additionalname"); 0085 } 0086 0087 void GrantleePrintTest::shouldDisplayContactInfo() 0088 { 0089 QFETCH(QString, variable); 0090 QFETCH(QString, result); 0091 0092 KAddressBookGrantlee::GrantleePrint grantleePrint; 0093 KContacts::Addressee::List lst; 0094 KContacts::Addressee address; 0095 address.setGivenName(QStringLiteral("foo-givenname")); 0096 address.setAdditionalName(QStringLiteral("foo-additionalname")); 0097 address.setName(QStringLiteral("foo1")); 0098 KContacts::Email email(QStringLiteral("foo@kde.org")); 0099 email.setPreferred(true); 0100 address.addEmail(email); 0101 address.setOrganization(QStringLiteral("kde")); 0102 address.insertLang(KContacts::Lang(QStringLiteral("fr"))); 0103 address.setNote(QStringLiteral("foo-note")); 0104 address.setTitle(QStringLiteral("foo-title")); 0105 address.setNickName(QStringLiteral("foo-nickname")); 0106 address.setFamilyName(QStringLiteral("foo-familyname")); 0107 address.setRole(QStringLiteral("foo-role")); 0108 address.setSuffix(QStringLiteral("foo-suffix")); 0109 address.setPrefix(QStringLiteral("foo-prefix")); 0110 address.setDepartment(QStringLiteral("foo-department")); 0111 address.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office"), QStringLiteral("foo-office")); 0112 address.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Profession"), QStringLiteral("foo-profession")); 0113 address.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-Office"), QStringLiteral("foo-office")); 0114 address.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-ManagersName"), QStringLiteral("foo-managersname")); 0115 address.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-AssistantsName"), QStringLiteral("foo-assistantsname")); 0116 address.insertCustom(QStringLiteral("KADDRESSBOOK"), QStringLiteral("X-SpousesName"), QStringLiteral("foo-spousesname")); 0117 0118 lst << address; 0119 grantleePrint.setTemplateContent( 0120 QStringLiteral("{% if contacts %}{% for contact in contacts %}{% if contact.%1 %}{{ contact.%1 }}{% endif %}{% endfor %}{% endif %}").arg(variable)); 0121 0122 QCOMPARE(grantleePrint.contactsToHtml(lst), result); 0123 } 0124 0125 QTEST_MAIN(GrantleePrintTest) 0126 0127 #include "moc_grantleeprinttest.cpp"