common-test.c++ 13.5 KB
Newer Older
Kenton Varda's avatar
Kenton Varda committed
1 2
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
3
//
Kenton Varda's avatar
Kenton Varda committed
4 5 6 7 8 9
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
10
//
Kenton Varda's avatar
Kenton Varda committed
11 12
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
13
//
Kenton Varda's avatar
Kenton Varda committed
14 15 16 17 18 19 20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
21

22 23
#include "common.h"
#include "../string.h"
24
#include <kj/compat/gtest.h>
25 26 27 28 29 30 31

namespace kj {
namespace parse {
namespace {

typedef IteratorInput<char, const char*> Input;

32 33 34
TEST(CommonParsers, AnyParser) {
  StringPtr text = "foo";
  Input input(text.begin(), text.end());
35
  constexpr auto parser = any;
36

37
  Maybe<char> result = parser(input);
38 39 40 41 42 43 44
  KJ_IF_MAYBE(c, result) {
    EXPECT_EQ('f', *c);
  } else {
    ADD_FAILURE() << "Expected 'c', got null.";
  }
  EXPECT_FALSE(input.atEnd());

45
  result = parser(input);
46 47 48 49 50 51 52
  KJ_IF_MAYBE(c, result) {
    EXPECT_EQ('o', *c);
  } else {
    ADD_FAILURE() << "Expected 'o', got null.";
  }
  EXPECT_FALSE(input.atEnd());

53
  result = parser(input);
54 55 56 57 58 59 60
  KJ_IF_MAYBE(c, result) {
    EXPECT_EQ('o', *c);
  } else {
    ADD_FAILURE() << "Expected 'o', got null.";
  }
  EXPECT_TRUE(input.atEnd());

61
  result = parser(input);
62 63 64 65
  EXPECT_TRUE(result == nullptr);
  EXPECT_TRUE(input.atEnd());
}

66
TEST(CommonParsers, ExactElementParser) {
67 68 69
  StringPtr text = "foo";
  Input input(text.begin(), text.end());

70
  Maybe<Tuple<>> result = exactly('f')(input);
71 72 73
  EXPECT_TRUE(result != nullptr);
  EXPECT_FALSE(input.atEnd());

74
  result = exactly('o')(input);
75 76 77
  EXPECT_TRUE(result != nullptr);
  EXPECT_FALSE(input.atEnd());

78
  result = exactly('x')(input);
79 80 81
  EXPECT_TRUE(result == nullptr);
  EXPECT_FALSE(input.atEnd());

82 83
  auto parser = exactly('o');
  ParserRef<Input, Tuple<>> wrapped = ref<Input>(parser);
84 85 86 87 88
  result = wrapped(input);
  EXPECT_TRUE(result != nullptr);
  EXPECT_TRUE(input.atEnd());
}

89
TEST(CommonParsers, ExactlyConstParser) {
Kenton Varda's avatar
Kenton Varda committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  StringPtr text = "foo";
  Input input(text.begin(), text.end());

  Maybe<Tuple<>> result = exactlyConst<char, 'f'>()(input);
  EXPECT_TRUE(result != nullptr);
  EXPECT_FALSE(input.atEnd());

  result = exactlyConst<char, 'o'>()(input);
  EXPECT_TRUE(result != nullptr);
  EXPECT_FALSE(input.atEnd());

  result = exactlyConst<char, 'x'>()(input);
  EXPECT_TRUE(result == nullptr);
  EXPECT_FALSE(input.atEnd());

  auto parser = exactlyConst<char, 'o'>();
  ParserRef<Input, Tuple<>> wrapped = ref<Input>(parser);
  result = wrapped(input);
  EXPECT_TRUE(result != nullptr);
  EXPECT_TRUE(input.atEnd());
}

112
TEST(CommonParsers, ConstResultParser) {
Kenton Varda's avatar
Kenton Varda committed
113 114 115 116 117 118 119 120 121 122 123 124 125
  auto parser = constResult(exactly('o'), 123);

  StringPtr text = "o";
  Input input(text.begin(), text.end());
  Maybe<int> result = parser(input);
  KJ_IF_MAYBE(i, result) {
    EXPECT_EQ(123, *i);
  } else {
    ADD_FAILURE() << "Expected 123, got null.";
  }
  EXPECT_TRUE(input.atEnd());
}

126 127 128 129 130 131 132 133 134 135
TEST(CommonParsers, DiscardParser) {
  auto parser = discard(any);

  StringPtr text = "o";
  Input input(text.begin(), text.end());
  Maybe<Tuple<>> result = parser(input);
  EXPECT_TRUE(result != nullptr);
  EXPECT_TRUE(input.atEnd());
}

136
TEST(CommonParsers, SequenceParser) {
137 138 139 140
  StringPtr text = "foo";

  {
    Input input(text.begin(), text.end());
141
    Maybe<Tuple<>> result = sequence(exactly('f'), exactly('o'), exactly('o'))(input);
142 143 144 145 146 147
    EXPECT_TRUE(result != nullptr);
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
148
    Maybe<Tuple<>> result = sequence(exactly('f'), exactly('o'))(input);
149 150 151 152 153 154
    EXPECT_TRUE(result != nullptr);
    EXPECT_FALSE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
155
    Maybe<Tuple<>> result = sequence(exactly('x'), exactly('o'), exactly('o'))(input);
156 157 158 159 160 161
    EXPECT_TRUE(result == nullptr);
    EXPECT_FALSE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
162
    Maybe<Tuple<>> result =
163
        sequence(sequence(exactly('f'), exactly('o')), exactly('o'))(input);
164 165 166 167 168 169
    EXPECT_TRUE(result != nullptr);
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
170
    Maybe<Tuple<>> result =
171
        sequence(sequence(exactly('f')), exactly('o'), exactly('o'))(input);
172 173 174 175 176 177
    EXPECT_TRUE(result != nullptr);
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
178
    Maybe<int> result = sequence(transform(exactly('f'), [](){return 123;}),
179
                                 exactly('o'), exactly('o'))(input);
180 181 182 183 184 185 186 187 188
    KJ_IF_MAYBE(i, result) {
      EXPECT_EQ(123, *i);
    } else {
      ADD_FAILURE() << "Expected 123, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }
}

189
TEST(CommonParsers, ManyParserCountOnly) {
190 191
  StringPtr text = "foooob";

192
  auto parser = sequence(exactly('f'), many(exactly('o')));
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

  {
    Input input(text.begin(), text.begin() + 3);
    Maybe<int> result = parser(input);
    KJ_IF_MAYBE(i, result) {
      EXPECT_EQ(2, *i);
    } else {
      ADD_FAILURE() << "Expected 2, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.begin() + 5);
    Maybe<int> result = parser(input);
    KJ_IF_MAYBE(i, result) {
      EXPECT_EQ(4, *i);
    } else {
      ADD_FAILURE() << "Expected 4, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
    Maybe<int> result = parser(input);
    KJ_IF_MAYBE(i, result) {
      EXPECT_EQ(4, *i);
    } else {
      ADD_FAILURE() << "Expected 4, got null.";
    }
    EXPECT_FALSE(input.atEnd());
  }
}

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
TEST(CommonParsers, TimesParser) {
  StringPtr text = "foobar";

  auto parser = sequence(exactly('f'), times(any, 4));

  {
    Input input(text.begin(), text.begin() + 4);
    Maybe<Array<char>> result = parser(input);
    EXPECT_TRUE(result == nullptr);
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.begin() + 5);
    Maybe<Array<char>> result = parser(input);
    KJ_IF_MAYBE(s, result) {
      EXPECT_EQ("ooba", heapString(*s));
    } else {
      ADD_FAILURE() << "Expected string, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
    Maybe<Array<char>> result = parser(input);
    KJ_IF_MAYBE(s, result) {
      EXPECT_EQ("ooba", heapString(*s));
    } else {
      ADD_FAILURE() << "Expected string, got null.";
    }
    EXPECT_FALSE(input.atEnd());
  }
}

TEST(CommonParsers, TimesParserCountOnly) {
  StringPtr text = "foooob";

  auto parser = sequence(exactly('f'), times(exactly('o'), 4));

  {
    Input input(text.begin(), text.begin() + 4);
    Maybe<Tuple<>> result = parser(input);
    EXPECT_TRUE(result == nullptr);
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.begin() + 5);
    Maybe<Tuple<>> result = parser(input);
    EXPECT_TRUE(result != nullptr);
    EXPECT_TRUE(input.atEnd());
  }

  {
    Input input(text.begin(), text.end());
    Maybe<Tuple<>> result = parser(input);
    EXPECT_TRUE(result != nullptr);
    EXPECT_FALSE(input.atEnd());
  }

  text = "fooob";

  {
    Input input(text.begin(), text.end());
    Maybe<Tuple<>> result = parser(input);
    EXPECT_TRUE(result == nullptr);
    EXPECT_FALSE(input.atEnd());
  }
}

299 300 301
TEST(CommonParsers, ManyParserSubResult) {
  StringPtr text = "foooob";

302
  auto parser = many(any);
303 304 305 306 307 308 309 310 311 312 313 314 315

  {
    Input input(text.begin(), text.end());
    Maybe<Array<char>> result = parser(input);
    KJ_IF_MAYBE(chars, result) {
      EXPECT_EQ(text, heapString(*chars));
    } else {
      ADD_FAILURE() << "Expected char array, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }
}

316
TEST(CommonParsers, OptionalParser) {
317
  auto parser = sequence(
318 319 320
      transform(exactly('b'), []() -> uint { return 123; }),
      optional(transform(exactly('a'), []() -> uint { return 456; })),
      transform(exactly('r'), []() -> uint { return 789; }));
321 322 323 324 325 326

  {
    StringPtr text = "bar";
    Input input(text.begin(), text.end());
    Maybe<Tuple<uint, Maybe<uint>, uint>> result = parser(input);
    KJ_IF_MAYBE(value, result) {
Kenton Varda's avatar
Kenton Varda committed
327
      EXPECT_EQ(123u, get<0>(*value));
328
      KJ_IF_MAYBE(value2, get<1>(*value)) {
Kenton Varda's avatar
Kenton Varda committed
329
        EXPECT_EQ(456u, *value2);
330 331 332
      } else {
        ADD_FAILURE() << "Expected 456, got null.";
      }
Kenton Varda's avatar
Kenton Varda committed
333
      EXPECT_EQ(789u, get<2>(*value));
334 335 336 337 338 339 340 341 342 343 344
    } else {
      ADD_FAILURE() << "Expected result tuple, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }

  {
    StringPtr text = "br";
    Input input(text.begin(), text.end());
    Maybe<Tuple<uint, Maybe<uint>, uint>> result = parser(input);
    KJ_IF_MAYBE(value, result) {
Kenton Varda's avatar
Kenton Varda committed
345
      EXPECT_EQ(123u, get<0>(*value));
346
      EXPECT_TRUE(get<1>(*value) == nullptr);
Kenton Varda's avatar
Kenton Varda committed
347
      EXPECT_EQ(789u, get<2>(*value));
348 349 350 351 352 353 354 355 356 357 358 359 360 361
    } else {
      ADD_FAILURE() << "Expected result tuple, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }

  {
    StringPtr text = "bzr";
    Input input(text.begin(), text.end());
    Maybe<Tuple<uint, Maybe<uint>, uint>> result = parser(input);
    EXPECT_TRUE(result == nullptr);
  }
}

362
TEST(CommonParsers, OneOfParser) {
363
  auto parser = oneOf(
364
      transform(sequence(exactly('f'), exactly('o'), exactly('o')),
365
                []() -> StringPtr { return "foo"; }),
366
      transform(sequence(exactly('b'), exactly('a'), exactly('r')),
367
                []() -> StringPtr { return "bar"; }));
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393

  {
    StringPtr text = "foo";
    Input input(text.begin(), text.end());
    Maybe<StringPtr> result = parser(input);
    KJ_IF_MAYBE(s, result) {
      EXPECT_EQ("foo", *s);
    } else {
      ADD_FAILURE() << "Expected 'foo', got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }

  {
    StringPtr text = "bar";
    Input input(text.begin(), text.end());
    Maybe<StringPtr> result = parser(input);
    KJ_IF_MAYBE(s, result) {
      EXPECT_EQ("bar", *s);
    } else {
      ADD_FAILURE() << "Expected 'bar', got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }
}

394
TEST(CommonParsers, TransformParser) {
395 396
  StringPtr text = "foo";

397
  auto parser = transformWithLocation(
398
      sequence(exactly('f'), exactly('o'), exactly('o')),
399
      [](Span<const char*> location) -> int {
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
        EXPECT_EQ("foo", StringPtr(location.begin(), location.end()));
        return 123;
      });

  {
    Input input(text.begin(), text.end());
    Maybe<int> result = parser(input);
    KJ_IF_MAYBE(i, result) {
      EXPECT_EQ(123, *i);
    } else {
      ADD_FAILURE() << "Expected 123, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }
}

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
TEST(CommonParsers, TransformOrRejectParser) {
  auto parser = transformOrReject(many(any),
      [](Array<char> chars) -> Maybe<int> {
        if (heapString(chars) == "foo") {
          return 123;
        } else {
          return nullptr;
        }
      });

  {
    StringPtr text = "foo";
    Input input(text.begin(), text.end());
    Maybe<int> result = parser(input);
    KJ_IF_MAYBE(i, result) {
      EXPECT_EQ(123, *i);
    } else {
      ADD_FAILURE() << "Expected 123, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }

  {
    StringPtr text = "bar";
    Input input(text.begin(), text.end());
    Maybe<int> result = parser(input);
    EXPECT_TRUE(result == nullptr);
    EXPECT_TRUE(input.atEnd());
  }
}

447
TEST(CommonParsers, References) {
448 449 450 451 452
  struct TransformFunc {
    int value;

    TransformFunc(int value): value(value) {}

453
    int operator()() const { return value; }
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
  };

  // Don't use auto for the parsers here in order to verify that the templates are properly choosing
  // whether to use references or copies.

  Transform_<Exactly_<char>, TransformFunc> parser1 =
      transform(exactly('f'), TransformFunc(12));

  auto otherParser = exactly('o');
  Transform_<Exactly_<char>&, TransformFunc> parser2 =
      transform(otherParser, TransformFunc(34));

  auto otherParser2 = exactly('b');
  Transform_<Exactly_<char>, TransformFunc> parser3 =
      transform(mv(otherParser2), TransformFunc(56));

  StringPtr text = "foob";
  auto parser = transform(
      sequence(parser1, parser2, exactly('o'), parser3),
473
      [](int i, int j, int k) { return i + j + k; });
474 475 476 477 478 479 480 481 482 483 484 485 486

  {
    Input input(text.begin(), text.end());
    Maybe<int> result = parser(input);
    KJ_IF_MAYBE(i, result) {
      EXPECT_EQ(12 + 34 + 56, *i);
    } else {
      ADD_FAILURE() << "Expected 12 + 34 + 56, got null.";
    }
    EXPECT_TRUE(input.atEnd());
  }
}

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
TEST(CommonParsers, NotLookingAt) {
  auto parser = notLookingAt(exactly('a'));

  {
    StringPtr text = "a";
    Input input(text.begin(), text.end());
    EXPECT_TRUE(parser(input) == nullptr);
    EXPECT_FALSE(input.atEnd());
  }

  {
    StringPtr text = "b";
    Input input(text.begin(), text.end());
    EXPECT_TRUE(parser(input) != nullptr);
    EXPECT_FALSE(input.atEnd());
  }
}

TEST(CommonParsers, EndOfInput) {
  auto parser = endOfInput;

  {
    StringPtr text = "a";
    Input input(text.begin(), text.end());
    EXPECT_TRUE(parser(input) == nullptr);
    EXPECT_TRUE(parser(input) == nullptr);
    input.next();
    EXPECT_FALSE(parser(input) == nullptr);
  }
}

518 519 520
}  // namespace
}  // namespace parse
}  // namespace kj