Jing Wang | d0d52fe | 2020-09-28 10:33:02 +1000 | [diff] [blame^] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <gtest/gtest.h> |
| 6 | |
| 7 | #include "ml/grammar_library.h" |
| 8 | #include "ml/util.h" |
| 9 | |
| 10 | namespace ml { |
| 11 | |
| 12 | TEST(GrammarLibraryTest, CanLoadLibrary) { |
| 13 | auto* const instance = ml::GrammarLibrary::GetInstance(); |
| 14 | if (IsAsan()) { |
| 15 | EXPECT_FALSE(ml::GrammarLibrary::IsGrammarLibrarySupported()); |
| 16 | EXPECT_EQ(instance->GetStatus(), ml::GrammarLibrary::Status::kNotSupported); |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | if (ml::GrammarLibrary::IsGrammarLibrarySupported()) { |
| 21 | EXPECT_EQ(instance->GetStatus(), ml::GrammarLibrary::Status::kOk); |
| 22 | } else { |
| 23 | EXPECT_EQ(instance->GetStatus(), ml::GrammarLibrary::Status::kNotSupported); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | TEST(GrammarLibraryTest, ExampleRequest) { |
| 28 | auto* const instance = ml::GrammarLibrary::GetInstance(); |
| 29 | // Nothing to test on an unsupported platform. |
| 30 | if (instance->GetStatus() == ml::GrammarLibrary::Status::kNotSupported) { |
| 31 | return; |
| 32 | } |
| 33 | ASSERT_EQ(instance->GetStatus(), GrammarLibrary::Status::kOk); |
| 34 | |
| 35 | instance->InitEnvironment(); |
| 36 | |
| 37 | GrammarChecker const checker = instance->CreateGrammarChecker(); |
| 38 | instance->LoadGrammarChecker(checker); |
| 39 | |
| 40 | chrome_knowledge::GrammarCheckerRequest request; |
| 41 | request.set_text("They are student."); |
| 42 | request.set_language("en-US"); |
| 43 | |
| 44 | chrome_knowledge::GrammarCheckerResult result; |
| 45 | instance->CheckGrammar(checker, request, &result); |
| 46 | |
| 47 | EXPECT_EQ(result.candidates(0).text(), "They are students."); |
| 48 | |
| 49 | instance->DestroyGrammarChecker(checker); |
| 50 | } |
| 51 | |
| 52 | } // namespace ml |