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 | |
Jing Wang | d0d52fe | 2020-09-28 10:33:02 +1000 | [diff] [blame] | 35 | GrammarChecker const checker = instance->CreateGrammarChecker(); |
| 36 | instance->LoadGrammarChecker(checker); |
| 37 | |
| 38 | chrome_knowledge::GrammarCheckerRequest request; |
Jing Wang | 0855364 | 2021-01-06 11:48:44 +1100 | [diff] [blame] | 39 | request.set_text("They is student."); |
Jing Wang | d0d52fe | 2020-09-28 10:33:02 +1000 | [diff] [blame] | 40 | request.set_language("en-US"); |
| 41 | |
| 42 | chrome_knowledge::GrammarCheckerResult result; |
| 43 | instance->CheckGrammar(checker, request, &result); |
| 44 | |
Jing Wang | 0855364 | 2021-01-06 11:48:44 +1100 | [diff] [blame] | 45 | ASSERT_GE(result.candidates_size(), 1); |
Jing Wang | d0d52fe | 2020-09-28 10:33:02 +1000 | [diff] [blame] | 46 | EXPECT_EQ(result.candidates(0).text(), "They are students."); |
| 47 | |
Jing Wang | 0855364 | 2021-01-06 11:48:44 +1100 | [diff] [blame] | 48 | ASSERT_EQ(result.candidates(0).fragments_size(), 1); |
| 49 | const chrome_knowledge::CorrectionFragment fragment = |
| 50 | result.candidates(0).fragments(0); |
| 51 | EXPECT_EQ(fragment.offset(), 5); |
| 52 | EXPECT_EQ(fragment.length(), 10); |
| 53 | EXPECT_EQ(fragment.replacement(), "are students"); |
| 54 | |
Jing Wang | d0d52fe | 2020-09-28 10:33:02 +1000 | [diff] [blame] | 55 | instance->DestroyGrammarChecker(checker); |
| 56 | } |
| 57 | |
| 58 | } // namespace ml |