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

serialize-text-test.c++ (4828B)


      1 // Copyright (c) 2015 Philip Quinn.
      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 "serialize-text.h"
     23 #include <kj/compat/gtest.h>
     24 #include <kj/string.h>
     25 #include <capnp/pretty-print.h>
     26 #include <capnp/message.h>
     27 #include "test-util.h"
     28 
     29 #include <capnp/test.capnp.h>
     30 
     31 namespace capnp {
     32 namespace _ {  // private
     33 namespace {
     34 
     35 KJ_TEST("TextCodec TestAllTypes") {
     36   MallocMessageBuilder builder;
     37   initTestMessage(builder.initRoot<TestAllTypes>());
     38 
     39   {
     40     // Plain output
     41     TextCodec codec;
     42     codec.setPrettyPrint(false);
     43     auto text = codec.encode(builder.getRoot<TestAllTypes>());
     44 
     45     auto stringify = kj::str(builder.getRoot<TestAllTypes>());
     46     KJ_EXPECT(text == stringify);
     47 
     48     MallocMessageBuilder reader;
     49     auto orphan = codec.decode<TestAllTypes>(text, reader.getOrphanage());
     50     auto structReader = orphan.getReader();
     51     checkTestMessage(structReader);
     52   }
     53   {
     54     // Pretty output
     55     TextCodec codec;
     56     codec.setPrettyPrint(true);
     57     auto text = codec.encode(builder.getRoot<TestAllTypes>());
     58 
     59     auto stringify = prettyPrint(builder.getRoot<TestAllTypes>()).flatten();
     60     KJ_EXPECT(text == stringify);
     61 
     62     MallocMessageBuilder reader;
     63     auto orphan = codec.decode<TestAllTypes>(text, reader.getOrphanage());
     64     auto structReader = orphan.getReader();
     65     checkTestMessage(structReader);
     66   }
     67 }
     68 
     69 KJ_TEST("TextCodec TestDefaults") {
     70   MallocMessageBuilder builder;
     71   initTestMessage(builder.initRoot<TestDefaults>());
     72 
     73   TextCodec codec;
     74   auto text = codec.encode(builder.getRoot<TestDefaults>());
     75 
     76   MallocMessageBuilder reader;
     77   auto orphan = codec.decode<TestDefaults>(text, reader.getOrphanage());
     78   auto structReader = orphan.getReader();
     79   checkTestMessage(structReader);
     80 }
     81 
     82 KJ_TEST("TextCodec TestListDefaults") {
     83   MallocMessageBuilder builder;
     84   initTestMessage(builder.initRoot<TestListDefaults>());
     85 
     86   TextCodec codec;
     87   auto text = codec.encode(builder.getRoot<TestListDefaults>());
     88 
     89   MallocMessageBuilder reader;
     90   auto orphan = codec.decode<TestListDefaults>(text, reader.getOrphanage());
     91   auto structReader = orphan.getReader();
     92   checkTestMessage(structReader);
     93 }
     94 
     95 KJ_TEST("TextCodec raw text") {
     96   using TestType = capnproto_test::capnp::test::TestLateUnion;
     97 
     98   kj::String message =
     99       kj::str(R"((
    100         foo = -123, bar = "bar", baz = 456,
    101         # Test Comment
    102         theUnion = ( qux = "qux" ),
    103         anotherUnion = ( corge = [ 7, 8, 9 ] ),
    104       ))");
    105 
    106   MallocMessageBuilder builder;
    107   auto testType = builder.initRoot<TestType>();
    108 
    109   TextCodec codec;
    110   codec.decode(message, testType);
    111 
    112   auto reader = testType.asReader();
    113   KJ_EXPECT(reader.getFoo() == -123);
    114   KJ_EXPECT(reader.getBar() == "bar");
    115   KJ_EXPECT(reader.getBaz() == 456);
    116 
    117   KJ_EXPECT(reader.getTheUnion().isQux());
    118   KJ_EXPECT(reader.getTheUnion().hasQux());
    119   KJ_EXPECT(reader.getTheUnion().getQux() == "qux");
    120 
    121   KJ_EXPECT(reader.getAnotherUnion().isCorge());
    122   KJ_EXPECT(reader.getAnotherUnion().hasCorge());
    123   KJ_EXPECT(reader.getAnotherUnion().getCorge().size() == 3);
    124   KJ_EXPECT(reader.getAnotherUnion().getCorge()[0] == 7);
    125   KJ_EXPECT(reader.getAnotherUnion().getCorge()[1] == 8);
    126   KJ_EXPECT(reader.getAnotherUnion().getCorge()[2] == 9);
    127 }
    128 
    129 KJ_TEST("TextCodec parse error") {
    130   auto message = "\n  (,)"_kj;
    131 
    132   MallocMessageBuilder builder;
    133   auto root = builder.initRoot<TestAllTypes>();
    134 
    135   TextCodec codec;
    136   auto exception = KJ_ASSERT_NONNULL(kj::runCatchingExceptions(
    137       [&]() { codec.decode(message, root); }));
    138 
    139   KJ_EXPECT(exception.getFile() == "(capnp text input)"_kj);
    140   KJ_EXPECT(exception.getLine() == 2);
    141   KJ_EXPECT(exception.getDescription() == "3-6: Parse error: Empty list item.",
    142             exception.getDescription());
    143 }
    144 
    145 }  // namespace
    146 }  // namespace _ (private)
    147 }  // namespace capnp