namespacetest.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Tencent is pleased to support the open source community by making RapidJSON available.
  2. //
  3. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
  4. //
  5. // Licensed under the MIT License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // http://opensource.org/licenses/MIT
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "unittest.h"
  15. // test another instantiation of RapidJSON in a different namespace
  16. #define RAPIDJSON_NAMESPACE my::rapid::json
  17. #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapid { namespace json {
  18. #define RAPIDJSON_NAMESPACE_END } } }
  19. // include lots of RapidJSON files
  20. #include "rapidjson/document.h"
  21. #include "rapidjson/writer.h"
  22. #include "rapidjson/filereadstream.h"
  23. #include "rapidjson/filewritestream.h"
  24. #include "rapidjson/encodedstream.h"
  25. #include "rapidjson/stringbuffer.h"
  26. static const char json[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,4]}";
  27. TEST(NamespaceTest,Using) {
  28. using namespace RAPIDJSON_NAMESPACE;
  29. typedef GenericDocument<UTF8<>, CrtAllocator> DocumentType;
  30. DocumentType doc;
  31. doc.Parse(json);
  32. EXPECT_TRUE(!doc.HasParseError());
  33. }
  34. TEST(NamespaceTest,Direct) {
  35. typedef RAPIDJSON_NAMESPACE::Document Document;
  36. typedef RAPIDJSON_NAMESPACE::Reader Reader;
  37. typedef RAPIDJSON_NAMESPACE::StringStream StringStream;
  38. typedef RAPIDJSON_NAMESPACE::StringBuffer StringBuffer;
  39. typedef RAPIDJSON_NAMESPACE::Writer<StringBuffer> WriterType;
  40. StringStream s(json);
  41. StringBuffer buffer;
  42. WriterType writer(buffer);
  43. buffer.ShrinkToFit();
  44. Reader reader;
  45. reader.Parse(s, writer);
  46. EXPECT_STREQ(json, buffer.GetString());
  47. EXPECT_EQ(sizeof(json)-1, buffer.GetSize());
  48. EXPECT_TRUE(writer.IsComplete());
  49. Document doc;
  50. doc.Parse(buffer.GetString());
  51. EXPECT_TRUE(!doc.HasParseError());
  52. buffer.Clear();
  53. writer.Reset(buffer);
  54. doc.Accept(writer);
  55. EXPECT_STREQ(json, buffer.GetString());
  56. EXPECT_TRUE(writer.IsComplete());
  57. }