aboutsummaryrefslogtreecommitdiff
path: root/deps/inja/third_party/include/hayai/hayai_test_descriptor.hpp
blob: 529744e8470fe6ecc66eca7b39fcd5a091e27ea4 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#ifndef __HAYAI_TESTDESCRIPTOR
#define __HAYAI_TESTDESCRIPTOR
#include <cstring>
#include <sstream>
#include <string>
#include <vector>

#include "hayai_test.hpp"
#include "hayai_test_factory.hpp"


namespace hayai
{
    /// Parameter declaration.

    /// Describes parameter type and name.
    class TestParameterDescriptor
    {
    public:
        TestParameterDescriptor(std::string declaration,
                                std::string value)
            :   Declaration(declaration),
                Value(value)
        {

        }


        /// Declaration.
        std::string Declaration;


        /// Value.
        std::string Value;
    };


    /// Test parameters descriptor.
    class TestParametersDescriptor
    {
    private:
        /// Quoting state.
        enum QuotingState
        {
            /// Unquoted.
            Unquoted,


            /// Single quoted.
            SingleQuoted,


            /// Double quoted.
            DoubleQuoted
        };


        /// Trimmed string.

        /// @param start Start character.
        /// @param end Character one position beyond end.
        inline static std::string TrimmedString(const char* start,
                                                const char* end)
        {
            while (start < end)
            {
                if ((*start == ' ') ||
                    (*start == '\r') ||
                    (*start == '\n') ||
                    (*start == '\t'))
                    ++start;
                else
                    break;
            }

            while (end > start)
            {
                const char c = *(end - 1);

                if ((c != ' ') &&
                    (c != '\r') &&
                    (c != '\n') &&
                    (c != '\t'))
                    break;

                --end;
            }

            return std::string(start, std::string::size_type(end - start));
        }


        /// Parse comma separated parentherized value.

        /// @param separated Separated values as "(..[, ..])".
        /// @returns the individual values with white space trimmed.
        static std::vector<std::string>
            ParseCommaSeparated(const char* separated)
        {
            std::vector<std::string> result;

            if (*separated)
                ++separated;

            while ((*separated) && (*separated != ')'))
            {
                std::size_t escapeCounter = 0;
                const char* start = separated;
                QuotingState state = Unquoted;
                bool escaped = false;

                while (*separated)
                {
                    const char c = *separated++;

                    if (state == Unquoted)
                    {
                        if ((c == '"') || (c == '\''))
                        {
                            state = (c == '"' ? DoubleQuoted : SingleQuoted);
                            escaped = false;
                        }
                        else if ((c == '<') ||
                                 (c == '(') ||
                                 (c == '[') ||
                                 (c == '{'))
                            ++escapeCounter;
                        else if ((escapeCounter) &&
                                 ((c == '>') ||
                                  (c == ')') ||
                                  (c == ']') ||
                                  (c == '}')))
                            --escapeCounter;
                        else if ((!escapeCounter) &&
                                 ((c == ',') || (c == ')')))
                        {
                            result.push_back(TrimmedString(start,
                                                           separated - 1));
                            break;
                        }
                    }
                    else
                    {
                        if (escaped)
                            escaped = false;
                        else if (c == '\\')
                            escaped = true;
                        else if (c == (state == DoubleQuoted ? '"' : '\''))
                            state = Unquoted;
                    }
                }
            }

            return result;
        }


        /// Parse parameter declaration.

        /// @param raw Raw declaration.
        TestParameterDescriptor ParseDescriptor(const std::string& raw)
        {
            const char* position = raw.c_str();

            // Split the declaration into its declaration and its default
            // type.
            const char* equalPosition = NULL;
            std::size_t escapeCounter = 0;
            QuotingState state = Unquoted;
            bool escaped = false;

            while (*position)
            {
                const char c = *position++;

                if (state == Unquoted)
                {
                    if ((c == '"') || (c == '\''))
                    {
                        state = (c == '"' ? DoubleQuoted : SingleQuoted);
                        escaped = false;
                    }
                    else if ((c == '<') ||
                             (c == '(') ||
                             (c == '[') ||
                             (c == '{'))
                        ++escapeCounter;
                    else if ((escapeCounter) &&
                             ((c == '>') ||
                              (c == ')') ||
                              (c == ']') ||
                              (c == '}')))
                        --escapeCounter;
                    else if ((!escapeCounter) &&
                             (c == '='))
                    {
                        equalPosition = position;
                        break;
                    }
                }
                else
                {
                    if (escaped)
                        escaped = false;
                    else if (c == '\\')
                        escaped = true;
                    else if (c == (state == DoubleQuoted ? '"' : '\''))
                        state = Unquoted;
                }
            }

            // Construct the parameter descriptor.
            if (equalPosition)
            {
                const char* start = raw.c_str();
                const char* end = start + raw.length();

                return TestParameterDescriptor(
                    std::string(TrimmedString(start,
                                              equalPosition - 1)),
                    std::string(TrimmedString(equalPosition,
                                              end))
                );
            }
            else
                return TestParameterDescriptor(raw, std::string());
        }
    public:
        TestParametersDescriptor()
        {

        }


        TestParametersDescriptor(const char* rawDeclarations,
                                 const char* rawValues)
        {
            // Parse the declarations.
            std::vector<std::string> declarations =
                ParseCommaSeparated(rawDeclarations);

            for (std::vector<std::string>::const_iterator it =
                     declarations.begin();
                 it != declarations.end();
                 ++it)
                _parameters.push_back(ParseDescriptor(*it));

            // Parse the values.
            std::vector<std::string> values = ParseCommaSeparated(rawValues);

            std::size_t
                straightValues = (_parameters.size() > values.size() ?
                                  values.size() :
                                  _parameters.size()),
                variadicValues = 0;

            if (values.size() > _parameters.size())
            {
                if (straightValues > 0)
                    --straightValues;
                variadicValues = values.size() - _parameters.size() + 1;
            }

            for (std::size_t i = 0; i < straightValues; ++i)
                _parameters[i].Value = values[i];

            if (variadicValues)
            {
                std::stringstream variadic;

                for (std::size_t i = 0; i < variadicValues; ++i)
                {
                    if (i)
                        variadic << ", ";
                    variadic << values[straightValues + i];
                }

                _parameters[_parameters.size() - 1].Value = variadic.str();
            }
        }


        inline const std::vector<TestParameterDescriptor>& Parameters() const
        {
            return _parameters;
        }
    private:
        std::vector<TestParameterDescriptor> _parameters;
    };


    /// Test descriptor.
    class TestDescriptor
    {
    public:
        /// Initialize a new test descriptor.

        /// @param fixtureName Name of the fixture.
        /// @param testName Name of the test.
        /// @param runs Number of runs for the test.
        /// @param iterations Number of iterations per run.
        /// @param testFactory Test factory implementation for the test.
        /// @param parameters Parametrized test parameters.
        TestDescriptor(const char* fixtureName,
                       const char* testName,
                       std::size_t runs,
                       std::size_t iterations,
                       TestFactory* testFactory,
                       TestParametersDescriptor parameters,
                       bool isDisabled = false)
            :   FixtureName(fixtureName),
                TestName(testName),
                CanonicalName(std::string(fixtureName) + "." + testName),
                Runs(runs),
                Iterations(iterations),
                Factory(testFactory),
                Parameters(parameters),
                IsDisabled(isDisabled)
        {

        }


        /// Dispose of a test descriptor.
        ~TestDescriptor()
        {
            delete this->Factory;
        }


        /// Fixture name.
        std::string FixtureName;


        /// Test name.
        std::string TestName;


        /// Canonical name.

        /// As: <FixtureName>.<TestName>.
        std::string CanonicalName;


        /// Test runs.
        std::size_t Runs;


        /// Iterations per test run.
        std::size_t Iterations;


        /// Test factory.
        TestFactory* Factory;


        /// Parameters for parametrized tests
        TestParametersDescriptor Parameters;


        /// Disabled.
        bool IsDisabled;
    };
}
#endif