File: DefTokenisers.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (131 lines) | stat: -rw-r--r-- 3,839 bytes parent folder | download | duplicates (3)
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
#include "gtest/gtest.h"

#include "parser/DefTokeniser.h"

namespace test
{

inline std::map<std::string, std::string> parseStringPairs(parser::DefTokeniser& tokeniser)
{
    std::map<std::string, std::string> pairs;

    while (tokeniser.hasMoreTokens())
    {
        auto key = tokeniser.nextToken();
        auto value = tokeniser.nextToken();

        pairs.emplace(std::move(key), std::move(value));
    }

    return pairs;
}

TEST(DefTokeniser, ParseEmptyString)
{
    std::string testString = "";
    parser::BasicDefTokeniser<std::string> tokeniser(testString);

    // Tokeniser should be exhausted right after construction
    EXPECT_FALSE(tokeniser.hasMoreTokens());
}

TEST(DefTokeniser, ParseJustWhitespace)
{
    std::string testString = " \t \r\n\t";
    parser::BasicDefTokeniser<std::string> tokeniser(testString);

    // Tokeniser should be exhausted right after construction
    EXPECT_FALSE(tokeniser.hasMoreTokens());
}

TEST(DefTokeniser, SinglePair)
{
    std::string testString = R"("inherit"					"atdm:mover_handle_base")";
    parser::BasicDefTokeniser<std::string> tokeniser(testString);

    auto keyValuePairs = parseStringPairs(tokeniser);
    EXPECT_EQ(keyValuePairs.size(), 1) << "Expected 1 key value pair after parsing";

    EXPECT_EQ(keyValuePairs["inherit"], "atdm:mover_handle_base");
}

TEST(DefTokeniser, SingleEmptyQuote)
{
    std::string testString = R"("")";
    parser::BasicDefTokeniser<std::string> tokeniser(testString);

    // We should receive a single empty string
    EXPECT_TRUE(tokeniser.hasMoreTokens());
    EXPECT_EQ(tokeniser.nextToken(), "");
    EXPECT_FALSE(tokeniser.hasMoreTokens());

    testString = R"(		"" )";
    parser::BasicDefTokeniser<std::string> tokeniser2(testString);

    // We should receive a single empty string
    EXPECT_TRUE(tokeniser2.hasMoreTokens());
    EXPECT_EQ(tokeniser2.nextToken(), "");
    EXPECT_FALSE(tokeniser2.hasMoreTokens());
}

TEST(DefTokeniser, StringContinuation)
{
    std::string testString = R"( "inherit"	"atdm:" \
    "mover_handle_base")";

    parser::BasicDefTokeniser<std::string> tokeniser(testString);

    auto keyValuePairs = parseStringPairs(tokeniser);

    EXPECT_EQ(keyValuePairs.size(), 1) << "Expected 1 key value pair after parsing";
    EXPECT_EQ(keyValuePairs["inherit"], "atdm:mover_handle_base");
}

TEST(DefTokeniser, StringContinuationWithWhitespace)
{
    std::string testString = R"( "inherit"	"atdm:" \ 	 
    "mover_handle_base")";

    parser::BasicDefTokeniser<std::string> tokeniser(testString);

    auto keyValuePairs = parseStringPairs(tokeniser);

    EXPECT_EQ(keyValuePairs.size(), 1) << "Expected 1 key value pair after parsing";
    EXPECT_EQ(keyValuePairs["inherit"], "atdm:mover_handle_base");
}

TEST(DefTokeniser, EmptyQuotesAtEndOfBlock)
{
    std::string testString = R"(
	"inherit"					"atdm:mover_handle_base"

	"spawnclass"				"CFrobLockHandle"

	"editor_DisplayFolder"		"Movers"
	"editor_usage"				"Attach to a lock by binding it."

	"editor_snd snd_tap_locked"		"Called when the handle starts to move and its door is locked."
	"editor_snd snd_tap_default"	"Called when the handle starts to move and its door is unlocked."

    "noclipmodel"               "1"

    "mins"                      "-1 -1 -3"
    "maxs"                      "1 1 3"
    "frobbox_size"              "1 1 1"

	"snd_tap_default"			""
	"snd_tap_locked"			""
)";

    parser::BasicDefTokeniser<std::string> tokeniser(testString);

    auto keyValuePairs = parseStringPairs(tokeniser);

    EXPECT_EQ(keyValuePairs.size(), 12) << "Expected 12 key value pairs after parsing";

    EXPECT_EQ(keyValuePairs["snd_tap_locked"], "");
    EXPECT_EQ(keyValuePairs["editor_snd snd_tap_locked"], "Called when the handle starts to move and its door is locked.");
    EXPECT_EQ(keyValuePairs["mins"], "-1 -1 -3");
}

}