~aleteoryx/muditaos

ref: a0677af340a1fec8e8652b64fb97ca7f0902de12 muditaos/module-utils/phonenumber/tests/unittest_numbermatcher.cpp -rw-r--r-- 4.6 KiB
a0677af3 — Marek Niepieklo [CP-615] Update existing backup/restore implementation in OS 4 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>

#include "country.hpp"
#include "PhoneNumber.hpp"
#include "NumberHolderMatcher.hpp"

#include <initializer_list>
#include <string>
#include <vector>

using namespace utils;

class DummyHolder
{
    PhoneNumber number;

  public:
    DummyHolder(const PhoneNumber &number) : number(number)
    {}
    const PhoneNumber &getNumber() const
    {
        return number;
    }
};

const static std::string NumberAPlNational = "600123456";
const static std::string NumberAPlE164     = "+48600123456";
const static PhoneNumber NumberAPlValid(NumberAPlE164, country::Id::UNKNOWN);
const static PhoneNumber NumberAPlInvalid(NumberAPlNational, country::Id::UNKNOWN);
const static PhoneNumber NumberAPlValidNational(NumberAPlNational, country::Id::POLAND);

const static std::string NumberBPlNational = "500123456";
const static std::string NumberBPlE164     = "+48500123456";
const static PhoneNumber NumberBPlValid(NumberBPlE164, country::Id::UNKNOWN);
const static PhoneNumber NumberBPlInvalid(NumberBPlNational, country::Id::UNKNOWN);
const static PhoneNumber NumberBPlValidNational(NumberBPlNational, country::Id::POLAND);

const static std::string NumberCPlNational = "500555555";
const static std::string NumberCPlE164     = "+48500555555";
const static PhoneNumber NumberCPlValid(NumberCPlE164, country::Id::UNKNOWN);
const static PhoneNumber NumberCPlInvalid(NumberCPlNational, country::Id::UNKNOWN);
const static PhoneNumber NumberCPlValidNational(NumberCPlNational, country::Id::POLAND);

const static PhoneNumber invalidNumber("456", country::Id::UNKNOWN);

std::initializer_list<PhoneNumber> all_test_numbers = {NumberAPlValid,
                                                       NumberAPlInvalid,
                                                       NumberAPlValidNational,
                                                       NumberBPlValid,
                                                       NumberBPlInvalid,
                                                       NumberBPlValidNational,
                                                       invalidNumber};

auto make_test_vector(std::initializer_list<PhoneNumber> lnumber)
{
    std::vector<DummyHolder> v;
    for (auto &number : lnumber) {
        v.push_back(DummyHolder(number));
    }

    return v;
}

TEST_CASE("Number matcher - basics")
{
    std::vector<DummyHolder> numberHolders = {DummyHolder(PhoneNumber("500123456")),
                                              DummyHolder(PhoneNumber("600123456"))};

    NumberHolderMatcher<std::vector, DummyHolder> matcher(std::cbegin(numberHolders), std::cend(numberHolders));
    auto match = matcher.bestMatch(PhoneNumber("600123456"));
    REQUIRE(match != std::cend(numberHolders));
    REQUIRE(match->getNumber().get() == "600123456");

    // match again to see if search is started at the beginning again
    auto newmatch = matcher.bestMatch(PhoneNumber("600123456"));
    REQUIRE(newmatch != std::cend(numberHolders));
    REQUIRE(newmatch->getNumber().get() == "600123456");
}

TEST_CASE("Number matcher - match incoming (full list)")
{
    auto dummyHolders = make_test_vector(all_test_numbers);
    auto matcher = NumberHolderMatcher<std::vector, DummyHolder>(std::cbegin(dummyHolders), std::cend(dummyHolders));

    SECTION("Incoming e164")
    {
        auto match = matcher.bestMatch(PhoneNumber("+48600123456"));
        REQUIRE(match != matcher.END);
        REQUIRE(match->getNumber().get() == NumberAPlE164);
    }

    SECTION("Incoming national")
    {
        auto match = matcher.bestMatch(PhoneNumber("600123456", country::Id::UNKNOWN));
        REQUIRE(match != matcher.END);
        REQUIRE(match->getNumber().get() == NumberAPlNational);
    }

    SECTION("Incoming valid national")
    {
        auto match = matcher.bestMatch(PhoneNumber("600123456", country::Id::POLAND));
        REQUIRE(match != matcher.END);
        REQUIRE(match->getNumber().get() == NumberAPlE164);
    }
}

TEST_CASE("Number matcher - match incoming (loose)")
{
    auto dummyHolders = make_test_vector({NumberAPlInvalid, NumberBPlInvalid, NumberCPlInvalid});
    auto matcher = NumberHolderMatcher<std::vector, DummyHolder>(std::cbegin(dummyHolders), std::cend(dummyHolders));

    auto match = matcher.bestMatch(PhoneNumber("+48500123456"));
    REQUIRE(match == matcher.END);

    match = matcher.bestMatch(PhoneNumber("+48500123456"), PhoneNumber::Match::POSSIBLE);
    REQUIRE(match != matcher.END);
    REQUIRE(match->getNumber().get() == NumberBPlNational);
}