File: live_config.cpp

package info (click to toggle)
mir 2.25.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,104 kB
  • sloc: cpp: 192,777; xml: 13,784; ansic: 8,207; python: 1,304; sh: 794; makefile: 258
file content (145 lines) | stat: -rw-r--r-- 5,416 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <miral/live_config.h>

#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace testing;
namespace mlc = miral::live_config;

TEST(LiveConfig, key_cannot_be_empty)
{
    EXPECT_THAT([] { mlc::Key{}; },
        ThrowsMessage<std::invalid_argument>("Invalid (empty) live config key"));
}

TEST(LiveConfig, key_cannot_contain_empty_element)
{
    EXPECT_THAT([] { mlc::Key{""}; },
        ThrowsMessage<std::invalid_argument>(HasSubstr("Invalid config key element:")));

    EXPECT_THAT(([] { mlc::Key{"", "bar", "baz"}; }),
        ThrowsMessage<std::invalid_argument>(HasSubstr("Invalid config key element:")));

    EXPECT_THAT(([] { mlc::Key{"foo", "", "baz"}; }),
        ThrowsMessage<std::invalid_argument>(HasSubstr("Invalid config key element:")));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar", ""}; }),
        ThrowsMessage<std::invalid_argument>(HasSubstr("Invalid config key element:")));
}

TEST(LiveConfig, key_cannot_contain_an_element_starting_with_a_digit)
{
    EXPECT_THAT([] { mlc::Key{"1"}; },
        ThrowsMessage<std::invalid_argument>("Invalid config key element: 1"));

    EXPECT_THAT(([] { mlc::Key{"0oo", "bar", "baz"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: 0oo"));

    EXPECT_THAT(([] { mlc::Key{"foo", "2ar", "baz"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: 2ar"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar", "9az"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: 9az"));
}

TEST(LiveConfig, key_cannot_contain_an_element_starting_with_underscore)
{
    EXPECT_THAT([] { mlc::Key{"_"}; },
        ThrowsMessage<std::invalid_argument>("Invalid config key element: _"));

    EXPECT_THAT(([] { mlc::Key{"_oo", "bar", "baz"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: _oo"));

    EXPECT_THAT(([] { mlc::Key{"foo", "_ar", "baz"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: _ar"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar", "_az"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: _az"));
}

TEST(LiveConfig, key_cannot_contain_an_element_containing_uppercase)
{
    EXPECT_THAT([] { mlc::Key{"Foo"}; },
        ThrowsMessage<std::invalid_argument>("Invalid config key element: Foo"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bAr", "baz"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: bAr"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar", "baZ"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: baZ"));
}

TEST(LiveConfig, key_cannot_contain_an_element_containing_whitespaces_or_punctuation)
{
    EXPECT_THAT([] { mlc::Key{"foo bar baz"}; },
        ThrowsMessage<std::invalid_argument>("Invalid config key element: foo bar baz"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar\n", "baz"}; }),
        ThrowsMessage<std::invalid_argument>(HasSubstr("Invalid config key element:")));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar", "baz%"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: baz%"));

    EXPECT_THAT([] { mlc::Key{"foo.bar.baz"}; },
        ThrowsMessage<std::invalid_argument>("Invalid config key element: foo.bar.baz"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar", "baz!"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: baz!"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar/", "baz"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: bar/"));

    EXPECT_THAT(([] { mlc::Key{"foo", "bar", "baz*"}; }),
        ThrowsMessage<std::invalid_argument>("Invalid config key element: baz*"));
}

TEST(LiveConfig, key_can_contain_elements_with_digit_and_underscores)
{
    mlc::Key{"foo_bar_baz"};

    mlc::Key{"foo1", "bar2", "baz3"};

    mlc::Key{"foo_122", "bar_", "baz"};

    mlc::Key{"foo", "bar", "baz9"};
}

TEST(LiveConfig, key_has_expected_representations)
{
    mlc::Key const foo_bar_baz{"foo_bar_baz"};
    EXPECT_THAT(foo_bar_baz.to_string(), Eq("foo_bar_baz"));
    foo_bar_baz.with_key([&](std::vector<std::string> const& key)
    {
        EXPECT_THAT(key, ElementsAre("foo_bar_baz"));
    });

    mlc::Key const foo1_bar2_baz3{"foo1", "bar2", "baz3"};
    EXPECT_THAT(foo1_bar2_baz3.to_string(), Eq("foo1_bar2_baz3"));
    foo1_bar2_baz3.with_key([&](std::vector<std::string> const& key)
    {
        EXPECT_THAT(key, ElementsAre("foo1", "bar2", "baz3"));
    });

    mlc::Key const foo_122_bar__baz{"foo_122", "bar_", "baz"};
    EXPECT_THAT(foo_122_bar__baz.to_string(), Eq("foo_122_bar__baz"));
    foo_122_bar__baz.with_key([&](std::vector<std::string> const& key)
    {
        EXPECT_THAT(key, ElementsAre("foo_122", "bar_", "baz"));
    });
}