| 1 | /* |
| 2 | * chewing-editor: Chewing userphrase editor |
| 3 | * Copyright (C) 2014 Chewing Development Team |
| 4 | |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include "gtest/gtest.h" |
| 21 | |
| 22 | #include <QDebug> |
| 23 | #include <QFile> |
| 24 | #include <QJsonDocument> |
| 25 | #include <QJsonObject> |
| 26 | #include <QJsonValue> |
| 27 | #include <QTemporaryDir> |
| 28 | |
| 29 | #include "ChewingExporter.h" |
| 30 | |
| 31 | class TestChewingExporter : public ::testing::Test { |
| 32 | protected: |
| 33 | TestChewingExporter(); |
| 34 | virtual ~TestChewingExporter() = default; |
| 35 | virtual void SetUp() {} |
| 36 | virtual void TearDown() {} |
| 37 | |
| 38 | QTemporaryDir dir_; |
| 39 | QString path_; |
| 40 | }; |
| 41 | |
| 42 | TestChewingExporter::TestChewingExporter() |
| 43 | :dir_() |
| 44 | ,path_() |
| 45 | { |
| 46 | path_ = QString("%1/chewing.json").arg(dir_.path()); |
| 47 | //dir_.setAutoRemove(false); |
| 48 | } |
| 49 | |
| 50 | TEST_F(TestChewingExporter, WriteNoUserphrase) |
| 51 | { |
| 52 | ASSERT_TRUE(dir_.isValid()); |
| 53 | |
| 54 | ChewingExporter exporter(path_); |
| 55 | ASSERT_TRUE(exporter.save()); |
| 56 | |
| 57 | QFile file{QString{path_}}; |
| 58 | ASSERT_TRUE(file.open(QIODevice::ReadOnly | QIODevice::Text)); |
| 59 | |
| 60 | auto root = QJsonDocument::fromJson(file.readAll()).object(); |
| 61 | |
| 62 | auto userphrase = root.value("userphrase"); |
| 63 | ASSERT_TRUE(userphrase.isArray()); |
| 64 | |
| 65 | auto array = userphrase.toArray(); |
| 66 | ASSERT_EQ(0, array.size()); |
| 67 | } |
| 68 | |
| 69 | TEST_F(TestChewingExporter, WriteOneUserphrase) |
| 70 | { |
| 71 | ASSERT_TRUE(dir_.isValid()); |
| 72 | |
| 73 | ChewingExporter exporter(path_.toStdString().c_str()); |
| 74 | exporter.addUserphrase( |
| 75 | QString("\xE6\xB8\xAC\xE8\xA9\xA6" /* 測試 */), |
| 76 | QString("\xE3\x84\x98\xE3\x84\x9C\xCB\x8B \xE3\x84\x95\xCB\x8B" /* ㄘㄜˋ ㄕˋ */) |
| 77 | ); |
| 78 | |
| 79 | ASSERT_TRUE(exporter.save()); |
| 80 | |
| 81 | QFile file{path_}; |
| 82 | ASSERT_TRUE(file.open(QIODevice::ReadOnly | QIODevice::Text)); |
| 83 | |
| 84 | auto root = QJsonDocument::fromJson(file.readAll()).object(); |
| 85 | |
| 86 | auto userphrase = root.value("userphrase"); |
| 87 | ASSERT_TRUE(userphrase.isArray()); |
| 88 | |
| 89 | auto array = userphrase.toArray(); |
| 90 | ASSERT_EQ(1, array.size()); |
| 91 | ASSERT_TRUE(array.at(0).isObject()); |
| 92 | |
| 93 | auto obj = array.at(0).toObject(); |
| 94 | EXPECT_EQ( |
| 95 | QString("\xE6\xB8\xAC\xE8\xA9\xA6" /* 測試 */), |
| 96 | obj["phrase"].toString()); |
| 97 | EXPECT_EQ( |
| 98 | QString("\xE3\x84\x98\xE3\x84\x9C\xCB\x8B \xE3\x84\x95\xCB\x8B" /* ㄘㄜˋ ㄕˋ */), |
| 99 | obj["bopomofo"].toString()); |
| 100 | } |
| 101 | |
| 102 | TEST_F(TestChewingExporter, PathError) |
| 103 | { |
| 104 | ASSERT_TRUE(dir_.isValid()); |
| 105 | |
| 106 | ChewingExporter exporter{TESTDATA "/NoSuchPath/chewing.json"}; |
| 107 | ASSERT_FALSE(exporter.save()); |
| 108 | } |