stringify-test.c++ 16.1 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
// 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.

// 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.

#include "message.h"
Kenton Varda's avatar
Kenton Varda committed
48
#include "dynamic.h"
49
#include "pretty-print.h"
Kenton Varda's avatar
Kenton Varda committed
50
#include <kj/debug.h>
51 52 53
#include <gtest/gtest.h>
#include "test-util.h"

Kenton Varda's avatar
Kenton Varda committed
54 55 56 57 58 59
namespace kj {
  inline std::ostream& operator<<(std::ostream& os, const kj::String& s) {
    return os.write(s.begin(), s.size());
  }
}

60
namespace capnp {
61
namespace _ {  // private
62 63
namespace {

64
TEST(Stringify, KjStringification) {
65 66 67
  MallocMessageBuilder builder;
  auto root = builder.initRoot<TestAllTypes>();

Kenton Varda's avatar
Kenton Varda committed
68
  EXPECT_EQ("()", kj::str(root));
69 70 71

  initTestMessage(root);

Kenton Varda's avatar
Kenton Varda committed
72
  EXPECT_EQ("("
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
      "boolField = true, "
      "int8Field = -123, "
      "int16Field = -12345, "
      "int32Field = -12345678, "
      "int64Field = -123456789012345, "
      "uInt8Field = 234, "
      "uInt16Field = 45678, "
      "uInt32Field = 3456789012, "
      "uInt64Field = 12345678901234567890, "
      "float32Field = 1234.5, "
      "float64Field = -1.23e47, "
      "textField = \"foo\", "
      "dataField = \"bar\", "
      "structField = ("
          "boolField = true, "
          "int8Field = -12, "
          "int16Field = 3456, "
          "int32Field = -78901234, "
          "int64Field = 56789012345678, "
          "uInt8Field = 90, "
          "uInt16Field = 1234, "
          "uInt32Field = 56789012, "
          "uInt64Field = 345678901234567890, "
          "float32Field = -1.25e-10, "
          "float64Field = 345, "
          "textField = \"baz\", "
          "dataField = \"qux\", "
          "structField = ("
              "textField = \"nested\", "
              "structField = (textField = \"really nested\")), "
          "enumField = baz, "
          "voidList = [void, void, void], "
          "boolList = [false, true, false, true, true], "
          "int8List = [12, -34, -128, 127], "
          "int16List = [1234, -5678, -32768, 32767], "
          "int32List = [12345678, -90123456, -2147483648, 2147483647], "
          "int64List = [123456789012345, -678901234567890, "
                       "-9223372036854775808, 9223372036854775807], "
          "uInt8List = [12, 34, 0, 255], "
          "uInt16List = [1234, 5678, 0, 65535], "
          "uInt32List = [12345678, 90123456, 0, 4294967295], "
          "uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615], "
          "float32List = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37], "
          "float64List = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306], "
          "textList = [\"quux\", \"corge\", \"grault\"], "
          "dataList = [\"garply\", \"waldo\", \"fred\"], "
          "structList = ["
              "(textField = \"x structlist 1\"), "
              "(textField = \"x structlist 2\"), "
              "(textField = \"x structlist 3\")], "
          "enumList = [qux, bar, grault]), "
      "enumField = corge, "
      "voidList = [void, void, void, void, void, void], "
      "boolList = [true, false, false, true], "
      "int8List = [111, -111], "
      "int16List = [11111, -11111], "
      "int32List = [111111111, -111111111], "
      "int64List = [1111111111111111111, -1111111111111111111], "
      "uInt8List = [111, 222], "
      "uInt16List = [33333, 44444], "
      "uInt32List = [3333333333], "
      "uInt64List = [11111111111111111111], "
      "float32List = [5555.5, inf, -inf, nan], "
      "float64List = [7777.75, inf, -inf, nan], "
      "textList = [\"plugh\", \"xyzzy\", \"thud\"], "
      "dataList = [\"oops\", \"exhausted\", \"rfc3092\"], "
      "structList = [(textField = \"structlist 1\"), "
                    "(textField = \"structlist 2\"), "
                    "(textField = \"structlist 3\")], "
      "enumList = [foo, garply])",
Kenton Varda's avatar
Kenton Varda committed
143 144 145
      kj::str(root));
}

146 147 148 149
TEST(Stringify, PrettyPrint) {
  MallocMessageBuilder builder;
  auto root = builder.initRoot<TestAllTypes>();

150
  EXPECT_EQ("()", prettyPrint(root).flatten());
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

  initTestMessage(root);

  EXPECT_EQ(
      "( boolField = true,\n"
      "  int8Field = -123,\n"
      "  int16Field = -12345,\n"
      "  int32Field = -12345678,\n"
      "  int64Field = -123456789012345,\n"
      "  uInt8Field = 234,\n"
      "  uInt16Field = 45678,\n"
      "  uInt32Field = 3456789012,\n"
      "  uInt64Field = 12345678901234567890,\n"
      "  float32Field = 1234.5,\n"
      "  float64Field = -1.23e47,\n"
      "  textField = \"foo\",\n"
      "  dataField = \"bar\",\n"
      "  structField = (\n"
      "    boolField = true,\n"
      "    int8Field = -12,\n"
      "    int16Field = 3456,\n"
      "    int32Field = -78901234,\n"
      "    int64Field = 56789012345678,\n"
      "    uInt8Field = 90,\n"
      "    uInt16Field = 1234,\n"
      "    uInt32Field = 56789012,\n"
      "    uInt64Field = 345678901234567890,\n"
      "    float32Field = -1.25e-10,\n"
      "    float64Field = 345,\n"
      "    textField = \"baz\",\n"
      "    dataField = \"qux\",\n"
      "    structField = (\n"
      "      textField = \"nested\",\n"
184 185
      "      structField = (\n"
      "        textField = \"really nested\" ) ),\n"
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
      "    enumField = baz,\n"
      "    voidList = [void, void, void],\n"
      "    boolList = [false, true, false, true, true],\n"
      "    int8List = [12, -34, -128, 127],\n"
      "    int16List = [1234, -5678, -32768, 32767],\n"
      "    int32List = [12345678, -90123456, -2147483648, 2147483647],\n"
      "    int64List = [123456789012345, -678901234567890, "
                       "-9223372036854775808, 9223372036854775807],\n"
      "    uInt8List = [12, 34, 0, 255],\n"
      "    uInt16List = [1234, 5678, 0, 65535],\n"
      "    uInt32List = [12345678, 90123456, 0, 4294967295],\n"
      "    uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615],\n"
      "    float32List = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37],\n"
      "    float64List = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306],\n"
      "    textList = [\"quux\", \"corge\", \"grault\"],\n"
      "    dataList = [\"garply\", \"waldo\", \"fred\"],\n"
      "    structList = [\n"
203 204 205 206
      "      ( textField = \"x structlist 1\" ),\n"
      "      ( textField = \"x structlist 2\" ),\n"
      "      ( textField = \"x structlist 3\" ) ],\n"
      "    enumList = [qux, bar, grault] ),\n"
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      "  enumField = corge,\n"
      "  voidList = [void, void, void, void, void, void],\n"
      "  boolList = [true, false, false, true],\n"
      "  int8List = [111, -111],\n"
      "  int16List = [11111, -11111],\n"
      "  int32List = [111111111, -111111111],\n"
      "  int64List = [1111111111111111111, -1111111111111111111],\n"
      "  uInt8List = [111, 222],\n"
      "  uInt16List = [33333, 44444],\n"
      "  uInt32List = [3333333333],\n"
      "  uInt64List = [11111111111111111111],\n"
      "  float32List = [5555.5, inf, -inf, nan],\n"
      "  float64List = [7777.75, inf, -inf, nan],\n"
      "  textList = [\"plugh\", \"xyzzy\", \"thud\"],\n"
      "  dataList = [\"oops\", \"exhausted\", \"rfc3092\"],\n"
      "  structList = [\n"
223 224 225 226 227
      "    ( textField = \"structlist 1\" ),\n"
      "    ( textField = \"structlist 2\" ),\n"
      "    ( textField = \"structlist 3\" ) ],\n"
      "  enumList = [foo, garply] )",
      prettyPrint(root).flatten());
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
}

TEST(Stringify, PrettyPrintAdvanced) {
  MallocMessageBuilder builder;

  {
    auto root = builder.initRoot<TestAllTypes>();

    auto list = root.initStructList(3);
    list[0].setInt32Field(123);
    list[0].setTextField("foo");
    list[1].setInt32Field(456);
    list[1].setTextField("bar");
    list[2].setInt32Field(789);
    list[2].setTextField("baz");

    EXPECT_EQ(
245 246 247 248 249
        "( structList = [\n"
        "    (int32Field = 123, textField = \"foo\"),\n"
        "    (int32Field = 456, textField = \"bar\"),\n"
        "    (int32Field = 789, textField = \"baz\") ] )",
        prettyPrint(root).flatten());
250 251 252 253 254 255

    root.setInt32Field(55);

    EXPECT_EQ(
        "( int32Field = 55,\n"
        "  structList = [\n"
256 257 258 259
        "    (int32Field = 123, textField = \"foo\"),\n"
        "    (int32Field = 456, textField = \"bar\"),\n"
        "    (int32Field = 789, textField = \"baz\") ] )",
        prettyPrint(root).flatten());
260 261 262 263 264
  }

  {
    auto root = builder.initRoot<test::TestLists>();
    auto ll = root.initInt32ListList(3);
265 266 267
    ll.set(0, {123, 456, 789, 1234567890});
    ll.set(1, {234, 567, 891, 1234567890});
    ll.set(2, {345, 678, 912, 1234567890});
268 269

    EXPECT_EQ(
270 271 272 273
        "[ [123, 456, 789, 1234567890],\n"
        "  [234, 567, 891, 1234567890],\n"
        "  [345, 678, 912, 1234567890] ]",
        prettyPrint(ll).flatten());
274 275

    EXPECT_EQ(
276 277 278 279 280
        "( int32ListList = [\n"
        "    [123, 456, 789, 1234567890],\n"
        "    [234, 567, 891, 1234567890],\n"
        "    [345, 678, 912, 1234567890] ] )",
        prettyPrint(root).flatten());
281 282 283 284 285 286

    root.initList8(0);

    EXPECT_EQ(
        "( list8 = [],\n"
        "  int32ListList = [\n"
287 288 289 290
        "    [123, 456, 789, 1234567890],\n"
        "    [234, 567, 891, 1234567890],\n"
        "    [345, 678, 912, 1234567890] ] )",
        prettyPrint(root).flatten());
291 292 293 294 295 296 297

    auto l8 = root.initList8(1);
    l8[0].setF(12);

    EXPECT_EQ(
        "( list8 = [(f = 12)],\n"
        "  int32ListList = [\n"
298 299 300 301
        "    [123, 456, 789, 1234567890],\n"
        "    [234, 567, 891, 1234567890],\n"
        "    [345, 678, 912, 1234567890] ] )",
        prettyPrint(root).flatten());
302 303 304 305 306 307

    l8 = root.initList8(2);
    l8[0].setF(12);
    l8[1].setF(34);

    EXPECT_EQ(
308
        "( list8 = [(f = 12), (f = 34)],\n"
309
        "  int32ListList = [\n"
310 311 312 313
        "    [123, 456, 789, 1234567890],\n"
        "    [234, 567, 891, 1234567890],\n"
        "    [345, 678, 912, 1234567890] ] )",
        prettyPrint(root).flatten());
314 315 316 317 318 319 320
  }

  {
    auto root = builder.initRoot<test::TestStructUnion>();

    auto s = root.getUn().initAllTypes();
    EXPECT_EQ(
321
        "(un = (allTypes = ()))",
322
        prettyPrint(root).flatten());
323 324 325

    s.setInt32Field(123);
    EXPECT_EQ(
326 327
        "( un = (\n"
        "    allTypes = (int32Field = 123) ) )",
328
        prettyPrint(root).flatten());
329 330

    s.setTextField("foo");
331
    s.setUInt64Field(0xffffffffffffffffull);
332
    EXPECT_EQ(
333 334 335 336 337
        "( un = (\n"
        "    allTypes = (\n"
        "      int32Field = 123,\n"
        "      uInt64Field = 18446744073709551615,\n"
        "      textField = \"foo\" ) ) )",
338
        prettyPrint(root).flatten());
339 340 341
  }
}

Kenton Varda's avatar
Kenton Varda committed
342 343 344 345
TEST(Stringify, Unions) {
  MallocMessageBuilder builder;
  auto root = builder.initRoot<TestUnion>();

346
  root.getUnion0().setU0f0s16(123);
Kenton Varda's avatar
Kenton Varda committed
347 348 349 350 351
  root.getUnion1().setU1f0sp("foo");
  root.getUnion2().setU2f0s1(true);
  root.getUnion3().setU3f0s64(123456789012345678ll);

  EXPECT_EQ("("
352
      "union0 = (u0f0s16 = 123), "
353 354 355
      "union1 = (u1f0sp = \"foo\"), "
      "union2 = (u2f0s1 = true), "
      "union3 = (u3f0s64 = 123456789012345678))",
Kenton Varda's avatar
Kenton Varda committed
356 357
      kj::str(root));

358
  EXPECT_EQ("(u0f0s16 = 123)", kj::str(root.getUnion0()));
359 360 361
  EXPECT_EQ("(u1f0sp = \"foo\")", kj::str(root.getUnion1()));
  EXPECT_EQ("(u2f0s1 = true)", kj::str(root.getUnion2()));
  EXPECT_EQ("(u3f0s64 = 123456789012345678)", kj::str(root.getUnion3()));
362 363
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
TEST(Stringify, UnionDefaults) {
  MallocMessageBuilder builder;
  auto root = builder.initRoot<TestUnion>();

  root.getUnion0().setU0f0s16(0);     // Non-default field has default value.
  root.getUnion1().setU1f0sp("foo");  // Non-default field has non-default value.
  root.getUnion2().setU2f0s1(false);  // Default field has default value.
  root.getUnion3().setU3f0s1(true);   // Default field has non-default value.

  EXPECT_EQ("("
      "union0 = (u0f0s16 = 0), "
      "union1 = (u1f0sp = \"foo\"), "
      "union3 = (u3f0s1 = true))",
      kj::str(root));

  EXPECT_EQ("(u0f0s16 = 0)", kj::str(root.getUnion0()));
  EXPECT_EQ("(u1f0sp = \"foo\")", kj::str(root.getUnion1()));
  EXPECT_EQ("()", kj::str(root.getUnion2()));
  EXPECT_EQ("(u3f0s1 = true)", kj::str(root.getUnion3()));
}

385 386 387 388 389 390
TEST(Stringify, UnnamedUnions) {
  MallocMessageBuilder builder;
  auto root = builder.initRoot<test::TestUnnamedUnion>();

  root.setBar(123);

391 392
  EXPECT_EQ("(bar = 123)", kj::str(root));
  EXPECT_EQ("(bar = 123)", prettyPrint(root).flatten());
393 394 395

  root.setAfter("foooooooooooooooooooooooooooooooo");

396
  EXPECT_EQ("(bar = 123, after = \"foooooooooooooooooooooooooooooooo\")", kj::str(root));
397
  EXPECT_EQ(
398
      "( bar = 123,\n"
399 400 401 402 403
      "  after = \"foooooooooooooooooooooooooooooooo\" )",
      prettyPrint(root).flatten());

  root.setBefore("before");

404
  EXPECT_EQ("(before = \"before\", bar = 123, "
405 406 407
      "after = \"foooooooooooooooooooooooooooooooo\")", kj::str(root));
  EXPECT_EQ(
      "( before = \"before\",\n"
408
      "  bar = 123,\n"
409 410
      "  after = \"foooooooooooooooooooooooooooooooo\" )",
      prettyPrint(root).flatten());
411 412 413 414 415 416 417 418

  root.setFoo(0);

  EXPECT_EQ("(before = \"before\", after = \"foooooooooooooooooooooooooooooooo\")", kj::str(root));
  EXPECT_EQ(
      "( before = \"before\",\n"
      "  after = \"foooooooooooooooooooooooooooooooo\" )",
      prettyPrint(root).flatten());
419 420
}

421 422 423 424 425 426 427 428
TEST(Stringify, StructUnions) {
  MallocMessageBuilder builder;
  auto root = builder.initRoot<test::TestStructUnion>();

  auto allTypes = root.getUn().initAllTypes();
  allTypes.setUInt32Field(12345);
  allTypes.setTextField("foo");

429
  EXPECT_EQ("(un = (allTypes = (uInt32Field = 12345, textField = \"foo\")))", kj::str(root));
430 431
}

432
TEST(Stringify, MoreValues) {
Kenton Varda's avatar
Kenton Varda committed
433 434 435 436
  EXPECT_EQ("123", kj::str(DynamicValue::Reader(123)));
  EXPECT_EQ("1.23e47", kj::str(DynamicValue::Reader(123e45)));
  EXPECT_EQ("\"foo\"", kj::str(DynamicValue::Reader("foo")));
  EXPECT_EQ("\"\\a\\b\\n\\t\\\"\"", kj::str(DynamicValue::Reader("\a\b\n\t\"")));
437

Kenton Varda's avatar
Kenton Varda committed
438
  EXPECT_EQ("foo", kj::str(DynamicValue::Reader(TestEnum::FOO)));
439
  EXPECT_EQ("(123)", kj::str(DynamicValue::Reader(static_cast<TestEnum>(123))));
440 441 442
}

}  // namespace
443
}  // namespace _ (private)
444
}  // namespace capnp