diff --git a/src/engine/BUILD.bazel b/src/engine/BUILD.bazel index 45fae985c..e6008fbc5 100644 --- a/src/engine/BUILD.bazel +++ b/src/engine/BUILD.bazel @@ -134,6 +134,24 @@ mozc_cc_library( ], ) +mozc_cc_test( + name = "modules_test", + srcs = ["modules_test.cc"], + deps = [ + ":modules", + "//data_manager/testing:mock_data_manager", + "//dictionary:dictionary_interface", + "//dictionary:dictionary_mock", + "//dictionary:pos_matcher", + "//dictionary:suppression_dictionary", + "//dictionary:user_dictionary_stub", + "//prediction:rescorer_interface", + "//prediction:rescorer_mock", + "//testing:gunit_main", + "@com_google_absl//absl/status", + ], +) + mozc_cc_library( name = "engine", srcs = [ diff --git a/src/engine/modules_test.cc b/src/engine/modules_test.cc new file mode 100644 index 000000000..f0929229b --- /dev/null +++ b/src/engine/modules_test.cc @@ -0,0 +1,131 @@ +// Copyright 2010-2021, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "engine/modules.h" + +#include +#include + +#include "absl/status/status.h" +#include "data_manager/testing/mock_data_manager.h" +#include "dictionary/dictionary_interface.h" +#include "dictionary/dictionary_mock.h" +#include "dictionary/pos_matcher.h" +#include "dictionary/suppression_dictionary.h" +#include "dictionary/user_dictionary_stub.h" +#include "prediction/rescorer_interface.h" +#include "prediction/rescorer_mock.h" +#include "testing/gmock.h" +#include "testing/gunit.h" + +namespace mozc { +namespace engine { + +TEST(ModulesTest, Init) { + Modules modules; + + EXPECT_EQ(modules.GetPosMatcher(), nullptr); + EXPECT_EQ(modules.GetSuppressionDictionary(), nullptr); + EXPECT_EQ(modules.GetMutableSuppressionDictionary(), nullptr); + EXPECT_EQ(modules.GetSegmenter(), nullptr); + EXPECT_EQ(modules.GetUserDictionary(), nullptr); + EXPECT_EQ(modules.GetPosGroup(), nullptr); + EXPECT_EQ(modules.GetRescorer(), nullptr); + + testing::MockDataManager data_manager; + absl::Status status = modules.Init(&data_manager); + ASSERT_OK(status); + + EXPECT_NE(modules.GetPosMatcher(), nullptr); + EXPECT_NE(modules.GetSuppressionDictionary(), nullptr); + EXPECT_NE(modules.GetMutableSuppressionDictionary(), nullptr); + EXPECT_NE(modules.GetSegmenter(), nullptr); + EXPECT_NE(modules.GetUserDictionary(), nullptr); + EXPECT_NE(modules.GetPosGroup(), nullptr); + // Rescorer is not initialized by MockDataManager. + EXPECT_EQ(modules.GetRescorer(), nullptr); +} + +TEST(ModulesTest, Preset) { + Modules modules; + + // PosMatcher + auto pos_matcher = std::make_unique(); + const dictionary::PosMatcher *pos_matcher_ptr = pos_matcher.get(); + EXPECT_NE(pos_matcher_ptr, nullptr); + modules.PresetPosMatcher(std::move(pos_matcher)); + + // SuppressionDictionary + auto suppression_dictionary = + std::make_unique(); + const dictionary::SuppressionDictionary *suppression_dictionary_ptr = + suppression_dictionary.get(); + EXPECT_NE(suppression_dictionary_ptr, nullptr); + modules.PresetSuppressionDictionary(std::move(suppression_dictionary)); + + // UserDictionary + auto user_dictionary = std::make_unique(); + const dictionary::UserDictionaryStub *user_dictionary_ptr = + user_dictionary.get(); + EXPECT_NE(user_dictionary_ptr, nullptr); + modules.PresetUserDictionary(std::move(user_dictionary)); + + // SuffixDictionary + auto suffix_dictionary = std::make_unique(); + const dictionary::DictionaryInterface *suffix_dictionary_ptr = + suffix_dictionary.get(); + EXPECT_NE(suffix_dictionary_ptr, nullptr); + modules.PresetSuffixDictionary(std::move(suffix_dictionary)); + + // Dictionary + auto dictionary = std::make_unique(); + const dictionary::DictionaryInterface *dictionary_ptr = dictionary.get(); + EXPECT_NE(dictionary_ptr, nullptr); + modules.PresetDictionary(std::move(dictionary)); + + // Rescorer + auto rescorer = std::make_unique(); + const prediction::RescorerInterface *rescorer_ptr = rescorer.get(); + EXPECT_NE(rescorer_ptr, nullptr); + modules.PresetRescorer(std::move(rescorer)); + + testing::MockDataManager data_manager; + absl::Status status = modules.Init(&data_manager); + ASSERT_OK(status); + + EXPECT_EQ(modules.GetPosMatcher(), pos_matcher_ptr); + EXPECT_EQ(modules.GetSuppressionDictionary(), suppression_dictionary_ptr); + EXPECT_EQ(modules.GetUserDictionary(), user_dictionary_ptr); + EXPECT_EQ(modules.GetSuffixDictionary(), suffix_dictionary_ptr); + EXPECT_EQ(modules.GetDictionary(), dictionary_ptr); + EXPECT_EQ(modules.GetRescorer(), rescorer_ptr); +} + +} // namespace engine +} // namespace mozc