capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

stringify-test.c++ (24540B)


      1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
      2 // Licensed under the MIT License:
      3 //
      4 // Permission is hereby granted, free of charge, to any person obtaining a copy
      5 // of this software and associated documentation files (the "Software"), to deal
      6 // in the Software without restriction, including without limitation the rights
      7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 // copies of the Software, and to permit persons to whom the Software is
      9 // furnished to do so, subject to the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included in
     12 // all copies or substantial portions of the Software.
     13 //
     14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 // THE SOFTWARE.
     21 
     22 #include "message.h"
     23 #include "dynamic.h"
     24 #include "pretty-print.h"
     25 #include <kj/debug.h>
     26 #include <kj/compat/gtest.h>
     27 #include "test-util.h"
     28 
     29 namespace capnp {
     30 namespace _ {  // private
     31 namespace {
     32 
     33 TEST(Stringify, KjStringification) {
     34   MallocMessageBuilder builder;
     35   auto root = builder.initRoot<TestAllTypes>();
     36 
     37   // This test got ugly after printing was changed to always print primitives even when they have
     38   // default values...
     39 
     40   EXPECT_EQ("("
     41       "voidField = void, "
     42       "boolField = false, "
     43       "int8Field = 0, "
     44       "int16Field = 0, "
     45       "int32Field = 0, "
     46       "int64Field = 0, "
     47       "uInt8Field = 0, "
     48       "uInt16Field = 0, "
     49       "uInt32Field = 0, "
     50       "uInt64Field = 0, "
     51       "float32Field = 0, "
     52       "float64Field = 0, "
     53       "enumField = foo, "
     54       "interfaceField = void)",
     55       kj::str(root));
     56 
     57   initTestMessage(root);
     58 
     59   EXPECT_EQ("("
     60       "voidField = void, "
     61       "boolField = true, "
     62       "int8Field = -123, "
     63       "int16Field = -12345, "
     64       "int32Field = -12345678, "
     65       "int64Field = -123456789012345, "
     66       "uInt8Field = 234, "
     67       "uInt16Field = 45678, "
     68       "uInt32Field = 3456789012, "
     69       "uInt64Field = 12345678901234567890, "
     70       "float32Field = 1234.5, "
     71       "float64Field = -1.23e47, "
     72       "textField = \"foo\", "
     73       "dataField = \"bar\", "
     74       "structField = ("
     75           "voidField = void, "
     76           "boolField = true, "
     77           "int8Field = -12, "
     78           "int16Field = 3456, "
     79           "int32Field = -78901234, "
     80           "int64Field = 56789012345678, "
     81           "uInt8Field = 90, "
     82           "uInt16Field = 1234, "
     83           "uInt32Field = 56789012, "
     84           "uInt64Field = 345678901234567890, "
     85           "float32Field = -1.25e-10, "
     86           "float64Field = 345, "
     87           "textField = \"baz\", "
     88           "dataField = \"qux\", "
     89           "structField = ("
     90               "voidField = void, "
     91               "boolField = false, "
     92               "int8Field = 0, "
     93               "int16Field = 0, "
     94               "int32Field = 0, "
     95               "int64Field = 0, "
     96               "uInt8Field = 0, "
     97               "uInt16Field = 0, "
     98               "uInt32Field = 0, "
     99               "uInt64Field = 0, "
    100               "float32Field = 0, "
    101               "float64Field = 0, "
    102               "textField = \"nested\", "
    103               "structField = ("
    104                 "voidField = void, "
    105                 "boolField = false, "
    106                 "int8Field = 0, "
    107                 "int16Field = 0, "
    108                 "int32Field = 0, "
    109                 "int64Field = 0, "
    110                 "uInt8Field = 0, "
    111                 "uInt16Field = 0, "
    112                 "uInt32Field = 0, "
    113                 "uInt64Field = 0, "
    114                 "float32Field = 0, "
    115                 "float64Field = 0, "
    116                 "textField = \"really nested\", "
    117                 "enumField = foo, "
    118                 "interfaceField = void), "
    119               "enumField = foo, "
    120               "interfaceField = void), "
    121           "enumField = baz, "
    122           "interfaceField = void, "
    123           "voidList = [void, void, void], "
    124           "boolList = [false, true, false, true, true], "
    125           "int8List = [12, -34, -128, 127], "
    126           "int16List = [1234, -5678, -32768, 32767], "
    127           "int32List = [12345678, -90123456, -2147483648, 2147483647], "
    128           "int64List = [123456789012345, -678901234567890, "
    129                        "-9223372036854775808, 9223372036854775807], "
    130           "uInt8List = [12, 34, 0, 255], "
    131           "uInt16List = [1234, 5678, 0, 65535], "
    132           "uInt32List = [12345678, 90123456, 0, 4294967295], "
    133           "uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615], "
    134           "float32List = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37], "
    135           "float64List = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306], "
    136           "textList = [\"quux\", \"corge\", \"grault\"], "
    137           "dataList = [\"garply\", \"waldo\", \"fred\"], "
    138           "structList = ["
    139               "("
    140                 "voidField = void, "
    141                 "boolField = false, "
    142                 "int8Field = 0, "
    143                 "int16Field = 0, "
    144                 "int32Field = 0, "
    145                 "int64Field = 0, "
    146                 "uInt8Field = 0, "
    147                 "uInt16Field = 0, "
    148                 "uInt32Field = 0, "
    149                 "uInt64Field = 0, "
    150                 "float32Field = 0, "
    151                 "float64Field = 0, "
    152                 "textField = \"x structlist 1\", "
    153                 "enumField = foo, "
    154                 "interfaceField = void), "
    155               "("
    156                 "voidField = void, "
    157                 "boolField = false, "
    158                 "int8Field = 0, "
    159                 "int16Field = 0, "
    160                 "int32Field = 0, "
    161                 "int64Field = 0, "
    162                 "uInt8Field = 0, "
    163                 "uInt16Field = 0, "
    164                 "uInt32Field = 0, "
    165                 "uInt64Field = 0, "
    166                 "float32Field = 0, "
    167                 "float64Field = 0, "
    168                 "textField = \"x structlist 2\", "
    169                 "enumField = foo, "
    170                 "interfaceField = void), "
    171               "("
    172                 "voidField = void, "
    173                 "boolField = false, "
    174                 "int8Field = 0, "
    175                 "int16Field = 0, "
    176                 "int32Field = 0, "
    177                 "int64Field = 0, "
    178                 "uInt8Field = 0, "
    179                 "uInt16Field = 0, "
    180                 "uInt32Field = 0, "
    181                 "uInt64Field = 0, "
    182                 "float32Field = 0, "
    183                 "float64Field = 0, "
    184                 "textField = \"x structlist 3\", "
    185                 "enumField = foo, "
    186                 "interfaceField = void)], "
    187           "enumList = [qux, bar, grault]), "
    188       "enumField = corge, "
    189       "interfaceField = void, "
    190       "voidList = [void, void, void, void, void, void], "
    191       "boolList = [true, false, false, true], "
    192       "int8List = [111, -111], "
    193       "int16List = [11111, -11111], "
    194       "int32List = [111111111, -111111111], "
    195       "int64List = [1111111111111111111, -1111111111111111111], "
    196       "uInt8List = [111, 222], "
    197       "uInt16List = [33333, 44444], "
    198       "uInt32List = [3333333333], "
    199       "uInt64List = [11111111111111111111], "
    200       "float32List = [5555.5, inf, -inf, nan], "
    201       "float64List = [7777.75, inf, -inf, nan], "
    202       "textList = [\"plugh\", \"xyzzy\", \"thud\"], "
    203       "dataList = [\"oops\", \"exhausted\", \"rfc3092\"], "
    204       "structList = ["
    205           "("
    206             "voidField = void, "
    207             "boolField = false, "
    208             "int8Field = 0, "
    209             "int16Field = 0, "
    210             "int32Field = 0, "
    211             "int64Field = 0, "
    212             "uInt8Field = 0, "
    213             "uInt16Field = 0, "
    214             "uInt32Field = 0, "
    215             "uInt64Field = 0, "
    216             "float32Field = 0, "
    217             "float64Field = 0, "
    218             "textField = \"structlist 1\", "
    219             "enumField = foo, "
    220             "interfaceField = void), "
    221           "("
    222             "voidField = void, "
    223             "boolField = false, "
    224             "int8Field = 0, "
    225             "int16Field = 0, "
    226             "int32Field = 0, "
    227             "int64Field = 0, "
    228             "uInt8Field = 0, "
    229             "uInt16Field = 0, "
    230             "uInt32Field = 0, "
    231             "uInt64Field = 0, "
    232             "float32Field = 0, "
    233             "float64Field = 0, "
    234             "textField = \"structlist 2\", "
    235             "enumField = foo, "
    236             "interfaceField = void), "
    237           "("
    238             "voidField = void, "
    239             "boolField = false, "
    240             "int8Field = 0, "
    241             "int16Field = 0, "
    242             "int32Field = 0, "
    243             "int64Field = 0, "
    244             "uInt8Field = 0, "
    245             "uInt16Field = 0, "
    246             "uInt32Field = 0, "
    247             "uInt64Field = 0, "
    248             "float32Field = 0, "
    249             "float64Field = 0, "
    250             "textField = \"structlist 3\", "
    251             "enumField = foo, "
    252             "interfaceField = void)], "
    253       "enumList = [foo, garply])",
    254       kj::str(root));
    255 }
    256 
    257 TEST(Stringify, PrettyPrint) {
    258   MallocMessageBuilder builder;
    259   auto root = builder.initRoot<TestAllTypes>();
    260 
    261   EXPECT_EQ(
    262       "( voidField = void,\n"
    263       "  boolField = false,\n"
    264       "  int8Field = 0,\n"
    265       "  int16Field = 0,\n"
    266       "  int32Field = 0,\n"
    267       "  int64Field = 0,\n"
    268       "  uInt8Field = 0,\n"
    269       "  uInt16Field = 0,\n"
    270       "  uInt32Field = 0,\n"
    271       "  uInt64Field = 0,\n"
    272       "  float32Field = 0,\n"
    273       "  float64Field = 0,\n"
    274       "  enumField = foo,\n"
    275       "  interfaceField = void )", prettyPrint(root).flatten());
    276 
    277   initTestMessage(root);
    278 
    279   EXPECT_EQ(
    280       "( voidField = void,\n"
    281       "  boolField = true,\n"
    282       "  int8Field = -123,\n"
    283       "  int16Field = -12345,\n"
    284       "  int32Field = -12345678,\n"
    285       "  int64Field = -123456789012345,\n"
    286       "  uInt8Field = 234,\n"
    287       "  uInt16Field = 45678,\n"
    288       "  uInt32Field = 3456789012,\n"
    289       "  uInt64Field = 12345678901234567890,\n"
    290       "  float32Field = 1234.5,\n"
    291       "  float64Field = -1.23e47,\n"
    292       "  textField = \"foo\",\n"
    293       "  dataField = \"bar\",\n"
    294       "  structField = (\n"
    295       "    voidField = void,\n"
    296       "    boolField = true,\n"
    297       "    int8Field = -12,\n"
    298       "    int16Field = 3456,\n"
    299       "    int32Field = -78901234,\n"
    300       "    int64Field = 56789012345678,\n"
    301       "    uInt8Field = 90,\n"
    302       "    uInt16Field = 1234,\n"
    303       "    uInt32Field = 56789012,\n"
    304       "    uInt64Field = 345678901234567890,\n"
    305       "    float32Field = -1.25e-10,\n"
    306       "    float64Field = 345,\n"
    307       "    textField = \"baz\",\n"
    308       "    dataField = \"qux\",\n"
    309       "    structField = (\n"
    310       "      voidField = void,\n"
    311       "      boolField = false,\n"
    312       "      int8Field = 0,\n"
    313       "      int16Field = 0,\n"
    314       "      int32Field = 0,\n"
    315       "      int64Field = 0,\n"
    316       "      uInt8Field = 0,\n"
    317       "      uInt16Field = 0,\n"
    318       "      uInt32Field = 0,\n"
    319       "      uInt64Field = 0,\n"
    320       "      float32Field = 0,\n"
    321       "      float64Field = 0,\n"
    322       "      textField = \"nested\",\n"
    323       "      structField = (\n"
    324       "        voidField = void,\n"
    325       "        boolField = false,\n"
    326       "        int8Field = 0,\n"
    327       "        int16Field = 0,\n"
    328       "        int32Field = 0,\n"
    329       "        int64Field = 0,\n"
    330       "        uInt8Field = 0,\n"
    331       "        uInt16Field = 0,\n"
    332       "        uInt32Field = 0,\n"
    333       "        uInt64Field = 0,\n"
    334       "        float32Field = 0,\n"
    335       "        float64Field = 0,\n"
    336       "        textField = \"really nested\",\n"
    337       "        enumField = foo,\n"
    338       "        interfaceField = void ),\n"
    339       "      enumField = foo,\n"
    340       "      interfaceField = void ),\n"
    341       "    enumField = baz,\n"
    342       "    interfaceField = void,\n"
    343       "    voidList = [void, void, void],\n"
    344       "    boolList = [false, true, false, true, true],\n"
    345       "    int8List = [12, -34, -128, 127],\n"
    346       "    int16List = [1234, -5678, -32768, 32767],\n"
    347       "    int32List = [12345678, -90123456, -2147483648, 2147483647],\n"
    348       "    int64List = [123456789012345, -678901234567890, "
    349                        "-9223372036854775808, 9223372036854775807],\n"
    350       "    uInt8List = [12, 34, 0, 255],\n"
    351       "    uInt16List = [1234, 5678, 0, 65535],\n"
    352       "    uInt32List = [12345678, 90123456, 0, 4294967295],\n"
    353       "    uInt64List = [123456789012345, 678901234567890, 0, 18446744073709551615],\n"
    354       "    float32List = [0, 1234567, 1e37, -1e37, 1e-37, -1e-37],\n"
    355       "    float64List = [0, 123456789012345, 1e306, -1e306, 1e-306, -1e-306],\n"
    356       "    textList = [\"quux\", \"corge\", \"grault\"],\n"
    357       "    dataList = [\"garply\", \"waldo\", \"fred\"],\n"
    358       "    structList = [\n"
    359       "      ( voidField = void,\n"
    360       "        boolField = false,\n"
    361       "        int8Field = 0,\n"
    362       "        int16Field = 0,\n"
    363       "        int32Field = 0,\n"
    364       "        int64Field = 0,\n"
    365       "        uInt8Field = 0,\n"
    366       "        uInt16Field = 0,\n"
    367       "        uInt32Field = 0,\n"
    368       "        uInt64Field = 0,\n"
    369       "        float32Field = 0,\n"
    370       "        float64Field = 0,\n"
    371       "        textField = \"x structlist 1\",\n"
    372       "        enumField = foo,\n"
    373       "        interfaceField = void ),\n"
    374       "      ( voidField = void,\n"
    375       "        boolField = false,\n"
    376       "        int8Field = 0,\n"
    377       "        int16Field = 0,\n"
    378       "        int32Field = 0,\n"
    379       "        int64Field = 0,\n"
    380       "        uInt8Field = 0,\n"
    381       "        uInt16Field = 0,\n"
    382       "        uInt32Field = 0,\n"
    383       "        uInt64Field = 0,\n"
    384       "        float32Field = 0,\n"
    385       "        float64Field = 0,\n"
    386       "        textField = \"x structlist 2\",\n"
    387       "        enumField = foo,\n"
    388       "        interfaceField = void ),\n"
    389       "      ( voidField = void,\n"
    390       "        boolField = false,\n"
    391       "        int8Field = 0,\n"
    392       "        int16Field = 0,\n"
    393       "        int32Field = 0,\n"
    394       "        int64Field = 0,\n"
    395       "        uInt8Field = 0,\n"
    396       "        uInt16Field = 0,\n"
    397       "        uInt32Field = 0,\n"
    398       "        uInt64Field = 0,\n"
    399       "        float32Field = 0,\n"
    400       "        float64Field = 0,\n"
    401       "        textField = \"x structlist 3\",\n"
    402       "        enumField = foo,\n"
    403       "        interfaceField = void ) ],\n"
    404       "    enumList = [qux, bar, grault] ),\n"
    405       "  enumField = corge,\n"
    406       "  interfaceField = void,\n"
    407       "  voidList = [void, void, void, void, void, void],\n"
    408       "  boolList = [true, false, false, true],\n"
    409       "  int8List = [111, -111],\n"
    410       "  int16List = [11111, -11111],\n"
    411       "  int32List = [111111111, -111111111],\n"
    412       "  int64List = [1111111111111111111, -1111111111111111111],\n"
    413       "  uInt8List = [111, 222],\n"
    414       "  uInt16List = [33333, 44444],\n"
    415       "  uInt32List = [3333333333],\n"
    416       "  uInt64List = [11111111111111111111],\n"
    417       "  float32List = [5555.5, inf, -inf, nan],\n"
    418       "  float64List = [7777.75, inf, -inf, nan],\n"
    419       "  textList = [\"plugh\", \"xyzzy\", \"thud\"],\n"
    420       "  dataList = [\"oops\", \"exhausted\", \"rfc3092\"],\n"
    421       "  structList = [\n"
    422       "    ( voidField = void,\n"
    423       "      boolField = false,\n"
    424       "      int8Field = 0,\n"
    425       "      int16Field = 0,\n"
    426       "      int32Field = 0,\n"
    427       "      int64Field = 0,\n"
    428       "      uInt8Field = 0,\n"
    429       "      uInt16Field = 0,\n"
    430       "      uInt32Field = 0,\n"
    431       "      uInt64Field = 0,\n"
    432       "      float32Field = 0,\n"
    433       "      float64Field = 0,\n"
    434       "      textField = \"structlist 1\",\n"
    435       "      enumField = foo,\n"
    436       "      interfaceField = void ),\n"
    437       "    ( voidField = void,\n"
    438       "      boolField = false,\n"
    439       "      int8Field = 0,\n"
    440       "      int16Field = 0,\n"
    441       "      int32Field = 0,\n"
    442       "      int64Field = 0,\n"
    443       "      uInt8Field = 0,\n"
    444       "      uInt16Field = 0,\n"
    445       "      uInt32Field = 0,\n"
    446       "      uInt64Field = 0,\n"
    447       "      float32Field = 0,\n"
    448       "      float64Field = 0,\n"
    449       "      textField = \"structlist 2\",\n"
    450       "      enumField = foo,\n"
    451       "      interfaceField = void ),\n"
    452       "    ( voidField = void,\n"
    453       "      boolField = false,\n"
    454       "      int8Field = 0,\n"
    455       "      int16Field = 0,\n"
    456       "      int32Field = 0,\n"
    457       "      int64Field = 0,\n"
    458       "      uInt8Field = 0,\n"
    459       "      uInt16Field = 0,\n"
    460       "      uInt32Field = 0,\n"
    461       "      uInt64Field = 0,\n"
    462       "      float32Field = 0,\n"
    463       "      float64Field = 0,\n"
    464       "      textField = \"structlist 3\",\n"
    465       "      enumField = foo,\n"
    466       "      interfaceField = void ) ],\n"
    467       "  enumList = [foo, garply] )",
    468       prettyPrint(root).flatten());
    469 }
    470 
    471 TEST(Stringify, PrettyPrintAdvanced) {
    472   MallocMessageBuilder builder;
    473 
    474   {
    475     auto root = builder.initRoot<test::TestPrintInlineStructs>();
    476 
    477     auto list = root.initStructList(3);
    478     list[0].setInt32Field(123);
    479     list[0].setTextField("foo");
    480     list[1].setInt32Field(456);
    481     list[1].setTextField("bar");
    482     list[2].setInt32Field(789);
    483     list[2].setTextField("baz");
    484 
    485     EXPECT_EQ(
    486         "( structList = [\n"
    487         "    (int32Field = 123, textField = \"foo\"),\n"
    488         "    (int32Field = 456, textField = \"bar\"),\n"
    489         "    (int32Field = 789, textField = \"baz\") ] )",
    490         prettyPrint(root).flatten());
    491 
    492     root.setSomeText("foo");
    493 
    494     EXPECT_EQ(
    495         "( someText = \"foo\",\n"
    496         "  structList = [\n"
    497         "    (int32Field = 123, textField = \"foo\"),\n"
    498         "    (int32Field = 456, textField = \"bar\"),\n"
    499         "    (int32Field = 789, textField = \"baz\") ] )",
    500         prettyPrint(root).flatten());
    501   }
    502 
    503   {
    504     auto root = builder.initRoot<test::TestLists>();
    505     auto ll = root.initInt32ListList(3);
    506     ll.set(0, {123, 456, 789, 1234567890});
    507     ll.set(1, {234, 567, 891, 1234567890});
    508     ll.set(2, {345, 678, 912, 1234567890});
    509 
    510     EXPECT_EQ(
    511         "[ [123, 456, 789, 1234567890],\n"
    512         "  [234, 567, 891, 1234567890],\n"
    513         "  [345, 678, 912, 1234567890] ]",
    514         prettyPrint(ll).flatten());
    515 
    516     EXPECT_EQ(
    517         "( int32ListList = [\n"
    518         "    [123, 456, 789, 1234567890],\n"
    519         "    [234, 567, 891, 1234567890],\n"
    520         "    [345, 678, 912, 1234567890] ] )",
    521         prettyPrint(root).flatten());
    522 
    523     root.initList8(0);
    524 
    525     EXPECT_EQ(
    526         "( list8 = [],\n"
    527         "  int32ListList = [\n"
    528         "    [123, 456, 789, 1234567890],\n"
    529         "    [234, 567, 891, 1234567890],\n"
    530         "    [345, 678, 912, 1234567890] ] )",
    531         prettyPrint(root).flatten());
    532 
    533     auto l8 = root.initList8(1);
    534     l8[0].setF(12);
    535 
    536     EXPECT_EQ(
    537         "( list8 = [(f = 12)],\n"
    538         "  int32ListList = [\n"
    539         "    [123, 456, 789, 1234567890],\n"
    540         "    [234, 567, 891, 1234567890],\n"
    541         "    [345, 678, 912, 1234567890] ] )",
    542         prettyPrint(root).flatten());
    543 
    544     l8 = root.initList8(2);
    545     l8[0].setF(12);
    546     l8[1].setF(34);
    547 
    548     EXPECT_EQ(
    549         "( list8 = [(f = 12), (f = 34)],\n"
    550         "  int32ListList = [\n"
    551         "    [123, 456, 789, 1234567890],\n"
    552         "    [234, 567, 891, 1234567890],\n"
    553         "    [345, 678, 912, 1234567890] ] )",
    554         prettyPrint(root).flatten());
    555   }
    556 
    557   {
    558     auto root = builder.initRoot<test::TestStructUnion>();
    559 
    560     auto s = root.getUn().initStruct();
    561     EXPECT_EQ(
    562         "(un = (struct = ()))",
    563         prettyPrint(root).flatten());
    564 
    565     s.setSomeText("foo");
    566     EXPECT_EQ(
    567         "( un = (\n"
    568         "    struct = (someText = \"foo\") ) )",
    569         prettyPrint(root).flatten());
    570 
    571     s.setMoreText("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaar");
    572     EXPECT_EQ(
    573         "( un = (\n"
    574         "    struct = (\n"
    575         "      someText = \"foo\",\n"
    576         "      moreText = \"baaaaaaaaaaaaaaaaaaaaaaaaaaaaaar\" ) ) )",
    577         prettyPrint(root).flatten());
    578   }
    579 }
    580 
    581 TEST(Stringify, Unions) {
    582   MallocMessageBuilder builder;
    583   auto root = builder.initRoot<TestUnion>();
    584 
    585   root.getUnion0().setU0f0s16(123);
    586   root.getUnion1().setU1f0sp("foo");
    587   root.getUnion2().setU2f0s1(true);
    588   root.getUnion3().setU3f0s64(123456789012345678ll);
    589 
    590   EXPECT_EQ("("
    591       "union0 = (u0f0s16 = 123), "
    592       "union1 = (u1f0sp = \"foo\"), "
    593       "union2 = (u2f0s1 = true), "
    594       "union3 = (u3f0s64 = 123456789012345678), "
    595       "bit0 = false, bit2 = false, bit3 = false, bit4 = false, bit5 = false, "
    596       "bit6 = false, bit7 = false, byte0 = 0)",
    597       kj::str(root));
    598 
    599   EXPECT_EQ("(u0f0s16 = 123)", kj::str(root.getUnion0()));
    600   EXPECT_EQ("(u1f0sp = \"foo\")", kj::str(root.getUnion1()));
    601   EXPECT_EQ("(u2f0s1 = true)", kj::str(root.getUnion2()));
    602   EXPECT_EQ("(u3f0s64 = 123456789012345678)", kj::str(root.getUnion3()));
    603 }
    604 
    605 TEST(Stringify, UnionDefaults) {
    606   MallocMessageBuilder builder;
    607   auto root = builder.initRoot<TestUnion>();
    608 
    609   root.getUnion0().setU0f0s16(0);     // Non-default field has default value.
    610   root.getUnion1().setU1f0sp("foo");  // Non-default field has non-default value.
    611   root.getUnion2().setU2f0s1(false);  // Default field has default value.
    612   root.getUnion3().setU3f0s1(true);   // Default field has non-default value.
    613 
    614   EXPECT_EQ("("
    615       "union0 = (u0f0s16 = 0), "
    616       "union1 = (u1f0sp = \"foo\"), "
    617       "union2 = (u2f0s1 = false), "
    618       "union3 = (u3f0s1 = true), "
    619       "bit0 = false, bit2 = false, bit3 = false, bit4 = false, bit5 = false, "
    620       "bit6 = false, bit7 = false, byte0 = 0)",
    621       kj::str(root));
    622 
    623   EXPECT_EQ("(u0f0s16 = 0)", kj::str(root.getUnion0()));
    624   EXPECT_EQ("(u1f0sp = \"foo\")", kj::str(root.getUnion1()));
    625   EXPECT_EQ("(u2f0s1 = false)", kj::str(root.getUnion2()));
    626   EXPECT_EQ("(u3f0s1 = true)", kj::str(root.getUnion3()));
    627 }
    628 
    629 TEST(Stringify, UnnamedUnions) {
    630   MallocMessageBuilder builder;
    631   auto root = builder.initRoot<test::TestUnnamedUnion>();
    632 
    633   root.setBar(123);
    634 
    635   EXPECT_EQ("(middle = 0, bar = 123)", kj::str(root));
    636   EXPECT_EQ("(middle = 0, bar = 123)", prettyPrint(root).flatten());
    637 
    638   root.setAfter("foooooooooooooooooooooooooooooooo");
    639 
    640   EXPECT_EQ("(middle = 0, bar = 123, after = \"foooooooooooooooooooooooooooooooo\")",
    641             kj::str(root));
    642   EXPECT_EQ(
    643       "( middle = 0,\n"
    644       "  bar = 123,\n"
    645       "  after = \"foooooooooooooooooooooooooooooooo\" )",
    646       prettyPrint(root).flatten());
    647 
    648   root.setBefore("before");
    649 
    650   EXPECT_EQ("(before = \"before\", middle = 0, bar = 123, "
    651       "after = \"foooooooooooooooooooooooooooooooo\")", kj::str(root));
    652   EXPECT_EQ(
    653       "( before = \"before\",\n"
    654       "  middle = 0,\n"
    655       "  bar = 123,\n"
    656       "  after = \"foooooooooooooooooooooooooooooooo\" )",
    657       prettyPrint(root).flatten());
    658 
    659   root.setFoo(0);
    660 
    661   EXPECT_EQ(
    662       "(before = \"before\", foo = 0, middle = 0, after = \"foooooooooooooooooooooooooooooooo\")",
    663       kj::str(root));
    664   EXPECT_EQ(
    665       "( before = \"before\",\n"
    666       "  foo = 0,\n"
    667       "  middle = 0,\n"
    668       "  after = \"foooooooooooooooooooooooooooooooo\" )",
    669       prettyPrint(root).flatten());
    670 }
    671 
    672 TEST(Stringify, StructUnions) {
    673   MallocMessageBuilder builder;
    674   auto root = builder.initRoot<test::TestStructUnion>();
    675 
    676   auto s = root.getUn().initStruct();
    677   s.setSomeText("foo");
    678   s.setMoreText("bar");
    679 
    680   EXPECT_EQ("(un = (struct = (someText = \"foo\", moreText = \"bar\")))", kj::str(root));
    681 }
    682 
    683 TEST(Stringify, MoreValues) {
    684   EXPECT_EQ("123", kj::str(DynamicValue::Reader(123)));
    685   EXPECT_EQ("1.23e47", kj::str(DynamicValue::Reader(123e45)));
    686   EXPECT_EQ("\"foo\"", kj::str(DynamicValue::Reader("foo")));
    687   EXPECT_EQ("\"\\a\\b\\n\\t\\\"\"", kj::str(DynamicValue::Reader("\a\b\n\t\"")));
    688 
    689   EXPECT_EQ("foo", kj::str(DynamicValue::Reader(TestEnum::FOO)));
    690   EXPECT_EQ("(123)", kj::str(DynamicValue::Reader(static_cast<TestEnum>(123))));
    691 }
    692 
    693 TEST(Stringify, Generics) {
    694   MallocMessageBuilder builder;
    695   auto root = builder.initRoot<test::TestGenerics<Text, List<uint32_t>>::Inner>();
    696   root.setFoo("abcd");
    697   auto l = root.initBar(2);
    698   l.set(0, 123);
    699   l.set(1, 456);
    700 
    701   EXPECT_EQ("(foo = \"abcd\", bar = [123, 456])", kj::str(root));
    702 }
    703 
    704 }  // namespace
    705 }  // namespace _ (private)
    706 }  // namespace capnp