blob: 0bfabf52c6f83d1eca754f4d9486b92b4f825c4f [file] [log] [blame]
Krzysztof Grobelnyb8cc78d2021-11-29 15:54:53 +01001#include "utils/generate_id.hpp"
2
3#include <gmock/gmock.h>
4
5using namespace testing;
6
7class TestGenerateId : public Test
8{
9 public:
10 std::vector<std::string> conflicts;
11};
12
13TEST_F(TestGenerateId, returnPrefixWhenPrefixIsId)
14{
15 constexpr size_t maxSize = 32;
16 EXPECT_THAT(utils::generateId("prefix", "name", conflicts, maxSize),
17 Eq("prefix"));
18 EXPECT_THAT(utils::generateId("pre", "name123", conflicts, maxSize),
19 Eq("pre"));
20 EXPECT_THAT(utils::generateId("prefix/abc", "name", conflicts, maxSize),
21 Eq("prefix/abc"));
22 EXPECT_THAT(utils::generateId("prefix/abc", "name",
23 {"conflicts", "prefix/abc"}, maxSize),
24 Eq("prefix/abc"));
25}
26
27TEST_F(TestGenerateId, returnsNameWithPrefixWhenPrefixIsNamesapce)
28{
29 constexpr size_t maxSize = 32;
30 EXPECT_THAT(utils::generateId("prefix/", "name", conflicts, maxSize),
31 Eq("prefix/name"));
32 EXPECT_THAT(utils::generateId("pre/", "name", conflicts, maxSize),
33 Eq("pre/name"));
34}
35
36TEST_F(TestGenerateId, returnsOriginalNameWithoutInvalidCharacters)
37{
38 constexpr size_t maxSize = 32;
39 EXPECT_THAT(utils::generateId("", "n#a$m@e", conflicts, maxSize),
40 Eq("name"));
41 EXPECT_THAT(utils::generateId("/", "n!^aŹme", conflicts, maxSize),
42 Eq("/name"));
43 EXPECT_THAT(utils::generateId("p/", "n!^aŹm*(e", conflicts, maxSize),
44 Eq("p/name"));
45}
46
47TEST_F(TestGenerateId, returnsUniqueIdWhenConflictExist)
48{
49 constexpr size_t maxSize = 32;
50
51 EXPECT_THAT(utils::generateId("p/", "name",
52 {"conflicts", "p/name", "p/name1"}, maxSize),
53 Eq("p/name0"));
54 EXPECT_THAT(utils::generateId("p/", "name",
55 {"conflicts", "p/name", "p/name0", "p/name1"},
56 maxSize),
57 Eq("p/name2"));
58}
59
60TEST_F(TestGenerateId, returnsUniqueIdWithingMaxSize)
61{
62 constexpr size_t maxSize = 4;
63
64 EXPECT_THAT(utils::generateId("", "trigger", {""}, maxSize), Eq("trig"));
65 EXPECT_THAT(utils::generateId("", "trigger", {"trig"}, maxSize),
66 Eq("tri0"));
67}