Skip to content

Commit

Permalink
Fix #204
Browse files Browse the repository at this point in the history
Replace all duplicated spaces (included tabs) into single space.
Move `testBopomofo` method from UserphraseModel into BopomofoUtil for testing.
  • Loading branch information
david50407 committed Apr 10, 2017
1 parent 1679266 commit 7e9950b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 14 deletions.
15 changes: 3 additions & 12 deletions src/model/UserphraseModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,21 @@ void UserphraseModel::exportUserphrase(std::shared_ptr<UserphraseExporter> expor
emit exportCompleted(result, exporter.get()->getPath(), exported);
}

QString UserphraseModel::checkBopomofo(const QString &bopomofo) const
{
QString replaceBopomofo = bopomofo;
replaceBopomofo.replace(QString::fromUtf8(""),QString::fromUtf8(""));
replaceBopomofo.replace(QString::fromUtf8(""),QString::fromUtf8(""));

return replaceBopomofo;
}

void UserphraseModel::add(const QString &phrase, const QString &bopomofo)
{
QString replaceBopomofo = checkBopomofo(bopomofo);
QString normalizedBopomofo = BopomofoUtil::normalize(bopomofo);
auto ret = chewing_userphrase_add(
ctx_.get(),
phrase.toUtf8().constData(),
replaceBopomofo.toUtf8().constData());
normalizedBopomofo.toUtf8().constData());

addresult_ = ret;

if (ret > 0) {
emit beginResetModel();
userphrase_.insert(Userphrase{
phrase,
bopomofo
normalizedBopomofo
});
emit endResetModel();
emit addNewPhraseCompleted(userphrase_[userphrase_.size()-1]);
Expand Down
6 changes: 4 additions & 2 deletions src/model/UserphraseModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@

#include <QAbstractListModel>
#include <QString>
#include <QRegExp>

#include <chewing.h>

#include "Userphrase.h"
#include "UserphraseExporter.h"
#include "UserphraseImporter.h"
#include "UserphraseSet.h"
#include "BopomofoUtil.hpp"

#include "gtest/gtest_prod.h"

class UserphraseModel final: public QAbstractListModel {
Q_OBJECT
Expand Down Expand Up @@ -71,8 +75,6 @@ public slots:
void undo();

private:
QString checkBopomofo(const QString &bopomofo) const;

std::unique_ptr<ChewingContext, void (*)(ChewingContext*)> ctx_;
UserphraseSet userphrase_;
std::vector<Userphrase> removerecord_;
Expand Down
39 changes: 39 additions & 0 deletions src/util/BopomofoUtil.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* chewing-editor: Chewing userphrase editor
* Copyright (C) 2014 Chewing Development Team
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <QString>
#include <QRegExp>

namespace BopomofoUtil {
static QString normalize(const QString &bopomofo)
{
QString normalized = bopomofo;

// Issue #106: replace ambiguity chars
normalized.replace(QString::fromUtf8(""), QString::fromUtf8(""));
normalized.replace(QString::fromUtf8(""), QString::fromUtf8(""));

// Issue #204: reduce spaces into one space
normalized.replace(QRegExp("\\s+"), " ");

return normalized;
}
};
38 changes: 38 additions & 0 deletions test/testBopomofoUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* chewing-editor: Chewing userphrase editor
* Copyright (C) 2014 Chewing Development Team
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "gtest/gtest.h"

#include "BopomofoUtil.hpp"

#if (defined _MSC_VER) && (_MSC_VER >= 1600)
# pragma execution_character_set("utf-8")
#endif

class TestBopomofoUtil : public ::testing::Test {
protected:
TestBopomofoUtil() = default;
virtual ~TestBopomofoUtil() = default;
};

TEST_F(TestBopomofoUtil, ReduceDuplicatedSpacesInsideBopomofo)
{
ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ")));
ASSERT_EQ(0, QString::compare("ㄘㄜˋ ㄕˋ", BopomofoUtil::normalize("ㄘㄜˋ ㄕˋ")));
}

0 comments on commit 7e9950b

Please sign in to comment.