File indexing completed on 2023-12-03 05:54:54
0001 /* This file is part of Kairo Timer 0002 0003 SPDX-FileCopyrightText: 2016 (c) Kevin Ottens <ervin@kde.org> 0004 0005 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0006 0007 */ 0008 0009 #include <QtTest> 0010 0011 #include <QBuffer> 0012 #include <QTextStream> 0013 0014 #include <memory> 0015 0016 #include "circuitreader.h" 0017 0018 static_assert(!std::is_copy_assignable<CircuitReader>::value, "Should not be copy assignable"); 0019 static_assert(!std::is_copy_constructible<CircuitReader>::value, "Should not be copy constructible"); 0020 static_assert(std::is_move_assignable<CircuitReader>::value, "Should be move assignable"); 0021 static_assert(std::is_move_constructible<CircuitReader>::value, "Should be move constructible"); 0022 0023 class CircuitReaderTest : public QObject 0024 { 0025 Q_OBJECT 0026 private: 0027 std::unique_ptr<QIODevice> convertToDevice(const QString &fileContent) 0028 { 0029 auto buffer = new QBuffer; 0030 Q_ASSERT(buffer->open(QIODevice::WriteOnly)); 0031 auto stream = std::make_unique<QTextStream>(buffer); 0032 *stream << fileContent; 0033 buffer->close(); 0034 return std::unique_ptr<QIODevice>{buffer}; 0035 } 0036 0037 private slots: 0038 void shouldHaveDefaultState() 0039 { 0040 // GIVEN 0041 auto reader = CircuitReader{}; 0042 0043 // THEN 0044 QCOMPARE(reader.device(), static_cast<QIODevice*>(nullptr)); 0045 QVERIFY(reader.readMetaData().isEmpty()); 0046 QVERIFY(reader.readCircuit().isEmpty()); 0047 0048 // WHEN 0049 auto buffer = std::make_unique<QBuffer>(); 0050 reader = CircuitReader{buffer.get()}; 0051 0052 // THEN 0053 QCOMPARE(reader.device(), buffer.get()); 0054 QVERIFY(reader.readMetaData().isEmpty()); 0055 QVERIFY(reader.readCircuit().isEmpty()); 0056 } 0057 0058 void shouldDecodeDeviceContent_data() 0059 { 0060 QTest::addColumn<QString>("content"); 0061 QTest::addColumn<CircuitReader::MetaData>("expectedMetaData"); 0062 QTest::addColumn<CircuitModel>("expectedCircuit"); 0063 0064 0065 QStringList content; 0066 QTest::newRow("empty") << content.join('\n') 0067 << CircuitReader::MetaData{} 0068 << CircuitModel{}; 0069 0070 0071 0072 content.clear(); 0073 content << "---" 0074 << "author: John Doe" 0075 << "revision: 42" 0076 << "---"; 0077 QTest::newRow("header only") << content.join('\n') 0078 << CircuitReader::MetaData{ 0079 {"author", "John Doe"}, 0080 {"revision", 42} 0081 } 0082 << CircuitModel{}; 0083 0084 content.clear(); 0085 content << "---" 0086 << "author: John Doe" 0087 << "" 0088 << "revision: 42"; 0089 QTest::newRow("header only no end mark") << content.join('\n') 0090 << CircuitReader::MetaData{} 0091 << CircuitModel{}; 0092 content.clear(); 0093 content << "author: John Doe" 0094 << "" 0095 << "revision: 42" 0096 << "---"; 0097 QTest::newRow("header only no begin mark") << content.join('\n') 0098 << CircuitReader::MetaData{} 0099 << CircuitModel{}; 0100 0101 content.clear(); 0102 content << "---" 0103 << "author: John Doe" 0104 << "revision: 42" 0105 << "name: HIIT" 0106 << "---"; 0107 QTest::newRow("header only with name") << content.join('\n') 0108 << CircuitReader::MetaData{ 0109 {"author", "John Doe"}, 0110 {"name", "HIIT"}, 0111 {"revision", 42} 0112 } 0113 << CircuitModel{"HIIT", {}}; 0114 0115 content.clear(); 0116 content << "[" 0117 << " {" 0118 << " \"text\": \"foo\"," 0119 << " \"duration\": 4000" 0120 << " }," 0121 << " {" 0122 << " \"text\": \"bar\"" 0123 << " }" 0124 << "]"; 0125 QTest::newRow("json only") << content.join('\n') 0126 << CircuitReader::MetaData{} 0127 << CircuitModel{}; 0128 content.clear(); 0129 content << "---" 0130 << "---" 0131 << "[" 0132 << " {" 0133 << " \"text\": \"bar\"" 0134 << " }," 0135 << " {" 0136 << " \"text\": \"foo\"," 0137 << " \"duration\": 4000" 0138 << " }" 0139 << "]"; 0140 QTest::newRow("empty header") << content.join('\n') 0141 << CircuitReader::MetaData{} 0142 << CircuitModel{ 0143 {}, 0144 { 0145 {"bar"}, 0146 {"foo", 4000}, 0147 } 0148 }; 0149 0150 content.clear(); 0151 content << "---" 0152 << "author: John Doe" 0153 << "revision: 42" 0154 << "name: HIIT" 0155 << "---" 0156 << "[" 0157 << " {" 0158 << " \"text\": \"foo\"," 0159 << " \"duration\": 4000" 0160 << " }," 0161 << " {" 0162 << " \"text\": \"bar\"" 0163 << " }" 0164 << "]"; 0165 QTest::newRow("complete") << content.join('\n') 0166 << CircuitReader::MetaData{ 0167 {"author", "John Doe"}, 0168 {"name", "HIIT"}, 0169 {"revision", 42} 0170 } 0171 << CircuitModel{ 0172 "HIIT", 0173 { 0174 {"foo", 4000}, 0175 {"bar"} 0176 } 0177 }; 0178 0179 0180 content.clear(); 0181 content << "---" 0182 << "author: John Doe" 0183 << "revision: 42" 0184 << "name: HIIT" 0185 << "---" 0186 << "[" 0187 << " {" 0188 << " \"text\": \"foo\"," 0189 << " \"duration\": 4000" 0190 << " }," 0191 << " {" 0192 << " \"text\": \"One Loop\"," 0193 << " \"count\": 3," 0194 << " \"content\": [" 0195 << " {" 0196 << " \"text\": \"bar\"" 0197 << " }," 0198 << " {" 0199 << " \"text\": \"baz\"," 0200 << " \"duration\": 500" 0201 << " }" 0202 << " ]" 0203 << " }," 0204 << " {" 0205 << " \"text\": \"bleh\"" 0206 << " }," 0207 << " {" 0208 << " \"count\": 4," 0209 << " \"removeLast\": true," 0210 << " \"content\": [" 0211 << " {" 0212 << " \"text\": \"baz\"" 0213 << " }," 0214 << " {" 0215 << " \"text\": \"bar\"," 0216 << " \"duration\": 400" 0217 << " }" 0218 << " ]" 0219 << " }" 0220 << "]"; 0221 QTest::newRow("complete with loops") << content.join('\n') 0222 << CircuitReader::MetaData{ 0223 {"author", "John Doe"}, 0224 {"name", "HIIT"}, 0225 {"revision", 42} 0226 } 0227 << CircuitModel{ 0228 "HIIT", 0229 { 0230 {"foo", 4000}, 0231 {"One Loop, bar (1/3)"}, 0232 {"One Loop, baz (1/3)", 500}, 0233 {"One Loop, bar (2/3)"}, 0234 {"One Loop, baz (2/3)", 500}, 0235 {"One Loop, bar (3/3)"}, 0236 {"One Loop, baz (3/3)", 500}, 0237 {"bleh"}, 0238 {"baz (1/4)"}, 0239 {"bar (1/4)", 400}, 0240 {"baz (2/4)"}, 0241 {"bar (2/4)", 400}, 0242 {"baz (3/4)"}, 0243 {"bar (3/4)", 400}, 0244 {"baz (4/4)"} 0245 } 0246 }; 0247 } 0248 0249 void shouldDecodeDeviceContent() 0250 { 0251 // GIVEN 0252 QFETCH(QString, content); 0253 QFETCH(CircuitReader::MetaData, expectedMetaData); 0254 QFETCH(CircuitModel, expectedCircuit); 0255 0256 auto device = convertToDevice(content); 0257 auto reader = CircuitReader{device.get()}; 0258 0259 // WHEN 0260 auto metaData = reader.readMetaData(); 0261 0262 // THEN 0263 QCOMPARE(reader.device(), device.get()); 0264 QCOMPARE(metaData, expectedMetaData); 0265 if (!content.isEmpty()) { 0266 QVERIFY(device->pos() != 0); 0267 } 0268 0269 // WHEN 0270 auto circuit = reader.readCircuit(); 0271 0272 // THEN 0273 QCOMPARE(circuit, expectedCircuit); 0274 QVERIFY(device->atEnd()); 0275 0276 // Now try circuit first, then metadata 0277 0278 // GIVEN 0279 device = convertToDevice(content); 0280 reader = CircuitReader{device.get()}; 0281 0282 // WHEN 0283 circuit = reader.readCircuit(); 0284 metaData = reader.readMetaData(); 0285 0286 // THEN 0287 QCOMPARE(reader.device(), device.get()); 0288 QCOMPARE(metaData, expectedMetaData); 0289 QCOMPARE(circuit, expectedCircuit); 0290 QVERIFY(device->atEnd()); 0291 } 0292 }; 0293 0294 QTEST_APPLESS_MAIN(CircuitReaderTest) 0295 0296 #include "circuitreadertest.moc"