1e0857b17Sopenharmony_ci/*
2e0857b17Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3e0857b17Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0857b17Sopenharmony_ci * you may not use this file except in compliance with the License.
5e0857b17Sopenharmony_ci * You may obtain a copy of the License at
6e0857b17Sopenharmony_ci *
7e0857b17Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0857b17Sopenharmony_ci *
9e0857b17Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0857b17Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0857b17Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0857b17Sopenharmony_ci * See the License for the specific language governing permissions and
13e0857b17Sopenharmony_ci * limitations under the License.
14e0857b17Sopenharmony_ci */
15e0857b17Sopenharmony_ci
16e0857b17Sopenharmony_ci#include <gtest/gtest.h>
17e0857b17Sopenharmony_ci
18e0857b17Sopenharmony_ci#include "patterns_matcher.h"
19e0857b17Sopenharmony_ci
20e0857b17Sopenharmony_ciusing namespace testing::ext;
21e0857b17Sopenharmony_ciusing namespace OHOS::AAFwk;
22e0857b17Sopenharmony_ciusing OHOS::Parcel;
23e0857b17Sopenharmony_ci
24e0857b17Sopenharmony_cinamespace OHOS {
25e0857b17Sopenharmony_cinamespace AAFwk {
26e0857b17Sopenharmony_ciclass PatternsMatcherBaseTest : public testing::Test {
27e0857b17Sopenharmony_cipublic:
28e0857b17Sopenharmony_ci    PatternsMatcherBaseTest()
29e0857b17Sopenharmony_ci    {}
30e0857b17Sopenharmony_ci    ~PatternsMatcherBaseTest()
31e0857b17Sopenharmony_ci    {}
32e0857b17Sopenharmony_ci    static void SetUpTestCase(void);
33e0857b17Sopenharmony_ci    static void TearDownTestCase(void);
34e0857b17Sopenharmony_ci    void SetUp();
35e0857b17Sopenharmony_ci    void TearDown();
36e0857b17Sopenharmony_ci    std::shared_ptr<PatternsMatcher> PatternsMatcherIn_ = nullptr;
37e0857b17Sopenharmony_ci    std::shared_ptr<PatternsMatcher> PatternsMatcherOut_ = nullptr;
38e0857b17Sopenharmony_ci};
39e0857b17Sopenharmony_ci
40e0857b17Sopenharmony_civoid PatternsMatcherBaseTest::SetUpTestCase(void)
41e0857b17Sopenharmony_ci{}
42e0857b17Sopenharmony_ci
43e0857b17Sopenharmony_civoid PatternsMatcherBaseTest::TearDownTestCase(void)
44e0857b17Sopenharmony_ci{}
45e0857b17Sopenharmony_ci
46e0857b17Sopenharmony_civoid PatternsMatcherBaseTest::SetUp(void)
47e0857b17Sopenharmony_ci{
48e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>();
49e0857b17Sopenharmony_ci    PatternsMatcherOut_ = std::make_shared<PatternsMatcher>();
50e0857b17Sopenharmony_ci}
51e0857b17Sopenharmony_ci
52e0857b17Sopenharmony_civoid PatternsMatcherBaseTest::TearDown(void)
53e0857b17Sopenharmony_ci{}
54e0857b17Sopenharmony_ci
55e0857b17Sopenharmony_ci/**
56e0857b17Sopenharmony_ci * @tc.number: AaFwk_PatternsMatcher_Parcelable_0100
57e0857b17Sopenharmony_ci * @tc.name: Marshalling/Unmarshalling
58e0857b17Sopenharmony_ci * @tc.desc: marshalling PatternMatcher, and then check result.
59e0857b17Sopenharmony_ci */
60e0857b17Sopenharmony_ciHWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Parcelable_0100, Function | MediumTest | Level1)
61e0857b17Sopenharmony_ci{
62e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("1234", MatchType::DEFAULT);
63e0857b17Sopenharmony_ci    if (PatternsMatcherIn_ != nullptr) {
64e0857b17Sopenharmony_ci        Parcel in;
65e0857b17Sopenharmony_ci        PatternsMatcherIn_->Marshalling(in);
66e0857b17Sopenharmony_ci        std::shared_ptr<PatternsMatcher> PatternsMatcherOut_(PatternsMatcher::Unmarshalling(in));
67e0857b17Sopenharmony_ci        if (PatternsMatcherOut_ != nullptr) {
68e0857b17Sopenharmony_ci            EXPECT_EQ(PatternsMatcherIn_->GetPattern(), (PatternsMatcherOut_->GetPattern()));
69e0857b17Sopenharmony_ci            EXPECT_EQ(PatternsMatcherIn_->GetType(), (PatternsMatcherOut_->GetType()));
70e0857b17Sopenharmony_ci        }
71e0857b17Sopenharmony_ci    }
72e0857b17Sopenharmony_ci}
73e0857b17Sopenharmony_ci
74e0857b17Sopenharmony_ci/**
75e0857b17Sopenharmony_ci * @tc.number: AaFwk_PatternsMatcher_Parcelable_0200
76e0857b17Sopenharmony_ci * @tc.name: Marshalling/Unmarshalling
77e0857b17Sopenharmony_ci * @tc.desc: marshalling PatternMatcher, and then check result.
78e0857b17Sopenharmony_ci */
79e0857b17Sopenharmony_ciHWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Parcelable_0200, Function | MediumTest | Level1)
80e0857b17Sopenharmony_ci{
81e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("@#¥#3243adsafdf_中文", MatchType::PREFIX);
82e0857b17Sopenharmony_ci    if (PatternsMatcherIn_ != nullptr) {
83e0857b17Sopenharmony_ci        Parcel in;
84e0857b17Sopenharmony_ci        PatternsMatcherIn_->Marshalling(in);
85e0857b17Sopenharmony_ci        std::shared_ptr<PatternsMatcher> PatternsMatcherOut_(PatternsMatcher::Unmarshalling(in));
86e0857b17Sopenharmony_ci        if (PatternsMatcherOut_ != nullptr) {
87e0857b17Sopenharmony_ci            EXPECT_EQ(PatternsMatcherIn_->GetPattern(), (PatternsMatcherOut_->GetPattern()));
88e0857b17Sopenharmony_ci            EXPECT_EQ(PatternsMatcherIn_->GetType(), (PatternsMatcherOut_->GetType()));
89e0857b17Sopenharmony_ci        }
90e0857b17Sopenharmony_ci    }
91e0857b17Sopenharmony_ci}
92e0857b17Sopenharmony_ci
93e0857b17Sopenharmony_ci/**
94e0857b17Sopenharmony_ci * @tc.number: AaFwk_PatternsMatcher_Parcelable_0300
95e0857b17Sopenharmony_ci * @tc.name: Marshalling/Unmarshalling
96e0857b17Sopenharmony_ci * @tc.desc: marshalling PatternMatcher, and then check result.
97e0857b17Sopenharmony_ci */
98e0857b17Sopenharmony_ciHWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Parcelable_0300, Function | MediumTest | Level1)
99e0857b17Sopenharmony_ci{
100e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("", MatchType::GLOBAL);
101e0857b17Sopenharmony_ci    if (PatternsMatcherIn_ != nullptr) {
102e0857b17Sopenharmony_ci        Parcel in;
103e0857b17Sopenharmony_ci        PatternsMatcherIn_->Marshalling(in);
104e0857b17Sopenharmony_ci        std::shared_ptr<PatternsMatcher> PatternsMatcherOut_(PatternsMatcher::Unmarshalling(in));
105e0857b17Sopenharmony_ci        if (PatternsMatcherOut_ != nullptr) {
106e0857b17Sopenharmony_ci            EXPECT_EQ(PatternsMatcherIn_->GetPattern(), (PatternsMatcherOut_->GetPattern()));
107e0857b17Sopenharmony_ci            EXPECT_EQ(PatternsMatcherIn_->GetType(), (PatternsMatcherOut_->GetType()));
108e0857b17Sopenharmony_ci        }
109e0857b17Sopenharmony_ci    }
110e0857b17Sopenharmony_ci}
111e0857b17Sopenharmony_ci
112e0857b17Sopenharmony_ci/**
113e0857b17Sopenharmony_ci * @tc.number: AaFwk_PatternsMatcher_Match_0100
114e0857b17Sopenharmony_ci * @tc.name: Match
115e0857b17Sopenharmony_ci * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
116e0857b17Sopenharmony_ci */
117e0857b17Sopenharmony_ciHWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0100, Function | MediumTest | Level1)
118e0857b17Sopenharmony_ci{
119e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abcdefg", MatchType::DEFAULT);
120e0857b17Sopenharmony_ci    if (PatternsMatcherIn_ != nullptr) {
121e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcdefg"), true);
122e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("Abcdefg"), false);
123e0857b17Sopenharmony_ci    }
124e0857b17Sopenharmony_ci}
125e0857b17Sopenharmony_ci
126e0857b17Sopenharmony_ci/**
127e0857b17Sopenharmony_ci * @tc.number: AaFwk_PatternsMatcher_Match_0200
128e0857b17Sopenharmony_ci * @tc.name: Match
129e0857b17Sopenharmony_ci * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
130e0857b17Sopenharmony_ci */
131e0857b17Sopenharmony_ciHWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0200, Function | MediumTest | Level1)
132e0857b17Sopenharmony_ci{
133e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abcdefg", MatchType::PREFIX);
134e0857b17Sopenharmony_ci    if (PatternsMatcherIn_ != nullptr) {
135e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcdefgABCDEFG"), true);
136e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("AbcdefgABCDEFG"), false);
137e0857b17Sopenharmony_ci    }
138e0857b17Sopenharmony_ci}
139e0857b17Sopenharmony_ci
140e0857b17Sopenharmony_ci/**
141e0857b17Sopenharmony_ci * @tc.number: AaFwk_PatternsMatcher_Match_0300
142e0857b17Sopenharmony_ci * @tc.name: Match
143e0857b17Sopenharmony_ci * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
144e0857b17Sopenharmony_ci */
145e0857b17Sopenharmony_ciHWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0300, Function | MediumTest | Level1)
146e0857b17Sopenharmony_ci{
147e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abc*defg.", MatchType::PATTERN);
148e0857b17Sopenharmony_ci    if (PatternsMatcherIn_ != nullptr) {
149e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcdefgG"), true);
150e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcccccdefgG"), true);
151e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcdefg"), false);
152e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("ABCDEFG"), false);
153e0857b17Sopenharmony_ci    }
154e0857b17Sopenharmony_ci}
155e0857b17Sopenharmony_ci
156e0857b17Sopenharmony_ci/**
157e0857b17Sopenharmony_ci * @tc.number: AaFwk_PatternsMatcher_Match_0400
158e0857b17Sopenharmony_ci * @tc.name: Match
159e0857b17Sopenharmony_ci * @tc.desc: Match this PatternsMatcher against an Pattern's data, and then check result.
160e0857b17Sopenharmony_ci */
161e0857b17Sopenharmony_ciHWTEST_F(PatternsMatcherBaseTest, AaFwk_PatternsMatcher_Match_0400, Function | MediumTest | Level1)
162e0857b17Sopenharmony_ci{
163e0857b17Sopenharmony_ci    PatternsMatcherIn_ = std::make_shared<PatternsMatcher>("abc*ABC*123", MatchType::GLOBAL);
164e0857b17Sopenharmony_ci    if (PatternsMatcherIn_ != nullptr) {
165e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcABC123"), true);
166e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcdefgABCDEFG123"), true);
167e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("000abcdefg000ABCDEFG000123"), true);
168e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("aBc123"), false);
169e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("AbC123"), false);
170e0857b17Sopenharmony_ci        EXPECT_EQ(PatternsMatcherIn_->match("abcABC12345"), false);
171e0857b17Sopenharmony_ci    }
172e0857b17Sopenharmony_ci}
173e0857b17Sopenharmony_ci}  // namespace AAFwk
174e0857b17Sopenharmony_ci}  // namespace OHOS
175