common-test.c++ 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

24 25
#include "common.h"
#include "../string.h"
26 27 28 29 30 31 32 33
#include <gtest/gtest.h>

namespace kj {
namespace parse {
namespace {

typedef IteratorInput<char, const char*> Input;

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

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

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

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

63
  result = parser(input);
64 65 66 67
  EXPECT_TRUE(result == nullptr);
  EXPECT_TRUE(input.atEnd());
}

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

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

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

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

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

91
TEST(CommonParsers, ExactlyConstParser) {
Kenton Varda's avatar
Kenton Varda committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  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());
}

114
TEST(CommonParsers, ConstResultParser) {
Kenton Varda's avatar
Kenton Varda committed
115 116 117 118 119 120 121 122 123 124 125 126 127
  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());
}

128 129 130 131 132 133 134 135 136 137
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());
}

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

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

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

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

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

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

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

191
TEST(CommonParsers, ManyParserCountOnly) {
192 193
  StringPtr text = "foooob";

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

  {
    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());
  }
}

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
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());
  }
}

301 302 303
TEST(CommonParsers, ManyParserSubResult) {
  StringPtr text = "foooob";

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

  {
    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());
  }
}

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

  {
    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
329
      EXPECT_EQ(123u, get<0>(*value));
330
      KJ_IF_MAYBE(value2, get<1>(*value)) {
Kenton Varda's avatar
Kenton Varda committed
331
        EXPECT_EQ(456u, *value2);
332 333 334
      } else {
        ADD_FAILURE() << "Expected 456, got null.";
      }
Kenton Varda's avatar
Kenton Varda committed
335
      EXPECT_EQ(789u, get<2>(*value));
336 337 338 339 340 341 342 343 344 345 346
    } 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
347
      EXPECT_EQ(123u, get<0>(*value));
348
      EXPECT_TRUE(get<1>(*value) == nullptr);
Kenton Varda's avatar
Kenton Varda committed
349
      EXPECT_EQ(789u, get<2>(*value));
350 351 352 353 354 355 356 357 358 359 360 361 362 363
    } 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);
  }
}

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

  {
    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());
  }
}

396
TEST(CommonParsers, TransformParser) {
397 398
  StringPtr text = "foo";

399
  auto parser = transformWithLocation(
400
      sequence(exactly('f'), exactly('o'), exactly('o')),
401
      [](Span<const char*> location) -> int {
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
        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());
  }
}

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 447 448
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());
  }
}

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

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

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

  // 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),
475
      [](int i, int j, int k) { return i + j + k; });
476 477 478 479 480 481 482 483 484 485 486 487 488

  {
    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());
  }
}

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 518 519
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);
  }
}

520 521 522
}  // namespace
}  // namespace parse
}  // namespace kj