File indexing completed on 2024-05-12 05:53:55

0001 /*
0002  * Copyright (c) 2018 Sune Vuorela <sune@vuorela.dk>
0003  *
0004  * Permission is hereby granted, free of charge, to any person
0005  * obtaining a copy of this software and associated documentation
0006  * files (the "Software"), to deal in the Software without
0007  * restriction, including without limitation the rights to use,
0008  * copy, modify, merge, publish, distribute, sublicense, and/or sell
0009  * copies of the Software, and to permit persons to whom the
0010  * Software is furnished to do so, subject to the following
0011  * conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be
0014  * included in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0018  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0020  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0021  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0022  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0023  * OTHER DEALINGS IN THE SOFTWARE.
0024  */
0025 
0026 #include "recipeparser.h"
0027 #include <QTest>
0028 #include <QBuffer>
0029 #include <memory>
0030 
0031 class RecipeParserTest : public QObject
0032 {
0033     Q_OBJECT
0034 private Q_SLOTS:
0035     void testParseRecipe();
0036     void testParseRecipe_data();
0037 };
0038 
0039 Q_DECLARE_METATYPE(RecipeParser::ParsedRecipe)
0040 
0041 void RecipeParserTest::testParseRecipe()
0042 {
0043     QFETCH(QByteArray, input);
0044     QFETCH(bool, inputIsFile);
0045     QFETCH(RecipeParser::ParsedRecipe, result);
0046 
0047     std::unique_ptr<QIODevice> inputDevice;
0048     if (inputIsFile) {
0049         QString filename = QFINDTESTDATA("data/"+QString::fromUtf8(input));
0050         inputDevice.reset(new QFile(filename));
0051         inputDevice->open(QIODevice::ReadOnly);
0052     } else {
0053         inputDevice.reset(new QBuffer(&input));
0054         inputDevice->open(QIODevice::ReadOnly);
0055     }
0056 
0057     auto parsedResult = RecipeParser::parseRecipe(inputDevice.get());
0058 
0059     QCOMPARE(parsedResult.title, result.title);
0060     QCOMPARE(parsedResult.tags.size(), result.tags.size());
0061     for(int i = 0 ; i < parsedResult.tags.size() ; i++) {
0062         QCOMPARE(parsedResult.tags.at(i), result.tags.at(i));
0063     }
0064     QCOMPARE(parsedResult.ingredients.size(), result.ingredients.size());
0065     for(int i = 0 ; i < parsedResult.ingredients.size() ; i++) {
0066         QCOMPARE(parsedResult.ingredients.at(i).amount, result.ingredients.at(i).amount);
0067         QCOMPARE(parsedResult.ingredients.at(i).unit, result.ingredients.at(i).unit);
0068         QCOMPARE(parsedResult.ingredients.at(i).ingredient, result.ingredients.at(i).ingredient);
0069     }
0070     QCOMPARE(parsedResult.otherMeta.size(), result.otherMeta.size());
0071     QCOMPARE(parsedResult.otherMeta, result.otherMeta);
0072 }
0073 
0074 
0075 void RecipeParserTest::testParseRecipe_data()
0076 {
0077     QTest::addColumn<QByteArray>("input");
0078     QTest::addColumn<bool>("inputIsFile");
0079     QTest::addColumn<RecipeParser::ParsedRecipe>("result");
0080 
0081     QTest::newRow("empty") << QByteArray() << false << RecipeParser::ParsedRecipe{};
0082     QTest::newRow("justtitle") << QByteArray("# This is title\n") << false << RecipeParser::ParsedRecipe{"This is title",{},{},{}};
0083     QTest::newRow("justags") << QByteArray("# Title\n### Ingredienst\n### Directio \n### Metadata\ntags: horse, cow, sheep") << false << RecipeParser::ParsedRecipe{"Title",{},{"horse","cow","sheep"},{}};
0084     QTest::newRow("emptytags") << QByteArray("# Title\n### Ingredienst\n### Directio \n### Metadata\ntags: ") << false << RecipeParser::ParsedRecipe{"Title",{},{}, {}};
0085     QTest::newRow("twoauthors") << QByteArray("# Title\n### Ingredienst\n### Directio \n### Metadata\nauthor: horse\nauthor:cow") << false << RecipeParser::ParsedRecipe{"Title",{},{},{{"author",{"horse","cow"}}}};
0086     QTest::newRow("emptyauthors") << QByteArray("# Title\n### Ingredienst\n### Directio \n### Metadata\nauthor: \nauthor:") << false << RecipeParser::ParsedRecipe{"Title",{},{},{}};
0087     QTest::newRow("sometext") << QByteArray("# Title\n### Ingredienst\n\n\n * 1 kg mel\n * 3 l mælk\nunparsablestuff\n### Directio \n do stuff") << false << RecipeParser::ParsedRecipe{"Title",{{"1","kg","mel"},{"3","l","mælk"}},{},{}};
0088     QTest::newRow("couscousfile") << QByteArray("couscous.recipe.md") << true << RecipeParser::ParsedRecipe{"CousCous",{{"3","dl","kraftig boullion"},{"1.5","dl","cous cous"},{"1","spsk","vineddike"}, {"1","spsk","olivenolie"}},{},{{"author",{"Sune Vuorela <sune@vuorela.dk>"}}}};
0089     QTest::newRow("two ingredients sections") << QByteArray("twoingredients.recipe.md") << true << RecipeParser::ParsedRecipe{"CousCous",{{"3","dl","kraftig boullion"},{"1.5","dl","cous cous"},{"1","spsk","vineddike"}, {"1","spsk","olivenolie"}},{},{{"author",{"Sune Vuorela <sune@vuorela.dk>"}}}};
0090 
0091 }
0092 
0093 QTEST_APPLESS_MAIN(RecipeParserTest)
0094 
0095 #include "recipeparsertest.moc"