1 /* === This file is part of Calamares - <https://calamares.io> ===
2  *
3  *   SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
4  *   SPDX-License-Identifier: GPL-3.0-or-later
5  *
6  *   Calamares is Free Software: see the License-Identifier above.
7  *
8  *
9  */
10 
11 #include "modulesystem/Descriptor.h"
12 #include "modulesystem/InstanceKey.h"
13 
14 #include <QtTest/QtTest>
15 
16 using Calamares::ModuleSystem::InstanceKey;
17 
18 class ModuleSystemTests : public QObject
19 {
20     Q_OBJECT
21 public:
ModuleSystemTests()22     ModuleSystemTests() {}
~ModuleSystemTests()23     ~ModuleSystemTests() override {}
24 
25 private Q_SLOTS:
26     void initTestCase();
27 
28     void testEmptyInstanceKey();
29     void testCustomInstanceKey();
30     void testFromStringInstanceKey();
31 
32     void testBadSimpleCases();
33     void testBadFromStringCases();
34 
35     void testBasicDescriptor();
36 };
37 
38 void
initTestCase()39 ModuleSystemTests::initTestCase()
40 {
41 }
42 
43 void
assert_is_invalid(const InstanceKey & k)44 assert_is_invalid( const InstanceKey& k )
45 {
46     QVERIFY( !k.isValid() );
47     QVERIFY( !k.isCustom() );
48     QVERIFY( k.module().isEmpty() );
49     QVERIFY( k.id().isEmpty() );
50     if ( k.toString().isEmpty() )
51     {
52         QVERIFY( k.toString().isEmpty() );
53     }
54     else
55     {
56         QCOMPARE( k.toString(), QString() );
57     }
58 }
59 
60 void
testEmptyInstanceKey()61 ModuleSystemTests::testEmptyInstanceKey()
62 {
63     InstanceKey k0;
64     assert_is_invalid( k0 );
65 }
66 
67 void
testCustomInstanceKey()68 ModuleSystemTests::testCustomInstanceKey()
69 {
70     InstanceKey k0( "derp", "derp" );
71     QVERIFY( k0.isValid() );
72     QVERIFY( !k0.isCustom() );
73     QCOMPARE( k0.module(), QStringLiteral( "derp" ) );
74     QCOMPARE( k0.id(), QStringLiteral( "derp" ) );
75     QCOMPARE( k0.toString(), QStringLiteral( "derp@derp" ) );
76 
77     InstanceKey k1( "derp", "horse" );
78     QVERIFY( k1.isValid() );
79     QVERIFY( k1.isCustom() );
80     QCOMPARE( k1.module(), QStringLiteral( "derp" ) );
81     QCOMPARE( k1.id(), QStringLiteral( "horse" ) );
82     QCOMPARE( k1.toString(), QStringLiteral( "derp@horse" ) );
83 
84     InstanceKey k4( "derp", QString() );
85     QVERIFY( k4.isValid() );
86     QVERIFY( !k4.isCustom() );
87     QCOMPARE( k4.module(), QStringLiteral( "derp" ) );
88     QCOMPARE( k4.id(), QStringLiteral( "derp" ) );
89     QCOMPARE( k4.toString(), QStringLiteral( "derp@derp" ) );
90 }
91 
92 void
testFromStringInstanceKey()93 ModuleSystemTests::testFromStringInstanceKey()
94 {
95     InstanceKey k0 = InstanceKey::fromString( "derp@derp" );
96     QVERIFY( k0.isValid() );
97     QVERIFY( !k0.isCustom() );
98     QCOMPARE( k0.module(), QStringLiteral( "derp" ) );
99     QCOMPARE( k0.id(), QStringLiteral( "derp" ) );
100 
101     InstanceKey k1 = InstanceKey::fromString( "derp@horse" );
102     QVERIFY( k1.isValid() );
103     QVERIFY( k1.isCustom() );
104     QCOMPARE( k1.module(), QStringLiteral( "derp" ) );
105     QCOMPARE( k1.id(), QStringLiteral( "horse" ) );
106 
107     InstanceKey k2 = InstanceKey::fromString( "derp" );
108     QVERIFY( k2.isValid() );
109     QVERIFY( !k2.isCustom() );
110     QCOMPARE( k2.module(), QStringLiteral( "derp" ) );
111     QCOMPARE( k2.id(), QStringLiteral( "derp" ) );
112 }
113 
114 /// @brief These are expected to fail since they show bugs in the code
115 void
testBadSimpleCases()116 ModuleSystemTests::testBadSimpleCases()
117 {
118     InstanceKey k4( "derp", "derp@derp" );
119     assert_is_invalid( k4 );
120 }
121 
122 void
testBadFromStringCases()123 ModuleSystemTests::testBadFromStringCases()
124 {
125     InstanceKey k0 = InstanceKey::fromString( QString() );
126     assert_is_invalid( k0 );
127 
128     k0 = InstanceKey::fromString( "derp@derp@derp" );
129     assert_is_invalid( k0 );
130 }
131 
132 void
testBasicDescriptor()133 ModuleSystemTests::testBasicDescriptor()
134 {
135     {
136         QVariantMap m;
137         auto d = Calamares::ModuleSystem::Descriptor::fromDescriptorData( m );
138 
139         QVERIFY( !d.isValid() );
140         QVERIFY( d.name().isEmpty() );
141     }
142     {
143         QVariantMap m;
144         m.insert( "name", QVariant() );
145         auto d = Calamares::ModuleSystem::Descriptor::fromDescriptorData( m );
146 
147         QVERIFY( !d.isValid() );
148         QVERIFY( d.name().isEmpty() );
149     }
150     {
151         QVariantMap m;
152         m.insert( "name", 17 );
153         auto d = Calamares::ModuleSystem::Descriptor::fromDescriptorData( m );
154 
155         QVERIFY( !d.isValid() );
156         QVERIFY( !d.name().isEmpty() );
157         QCOMPARE( d.name(), QStringLiteral( "17" ) );  // Strange but true
158     }
159     {
160         QVariantMap m;
161         m.insert( "name", "welcome" );
162         m.insert( "type", "viewmodule" );
163         m.insert( "interface", "qtplugin" );
164         auto d = Calamares::ModuleSystem::Descriptor::fromDescriptorData( m );
165 
166         // QVERIFY( !d.isValid() );
167         QCOMPARE( d.name(), QStringLiteral( "welcome" ) );
168         QCOMPARE( d.type(), Calamares::ModuleSystem::Type::View );
169         QCOMPARE( d.interface(), Calamares::ModuleSystem::Interface::QtPlugin );
170     }
171 }
172 
173 
174 QTEST_GUILESS_MAIN( ModuleSystemTests )
175 
176 #include "utils/moc-warnings.h"
177 
178 #include "Tests.moc"
179