Home | History | Annotate | Download | only in unittest
      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 
     15 #include "unittest.h"
     16 #include "rapidjson/reader.h"
     17 #include "rapidjson/prettywriter.h"
     18 #include "rapidjson/stringbuffer.h"
     19 #include "rapidjson/filewritestream.h"
     20 
     21 using namespace rapidjson;
     22 
     23 static const char kJson[] = "{\"hello\":\"world\",\"t\":true,\"f\":false,\"n\":null,\"i\":123,\"pi\":3.1416,\"a\":[1,2,3,-1],\"u64\":1234567890123456789,\"i64\":-1234567890123456789}";
     24 static const char kPrettyJson[] =
     25 "{\n"
     26 "    \"hello\": \"world\",\n"
     27 "    \"t\": true,\n"
     28 "    \"f\": false,\n"
     29 "    \"n\": null,\n"
     30 "    \"i\": 123,\n"
     31 "    \"pi\": 3.1416,\n"
     32 "    \"a\": [\n"
     33 "        1,\n"
     34 "        2,\n"
     35 "        3,\n"
     36 "        -1\n"
     37 "    ],\n"
     38 "    \"u64\": 1234567890123456789,\n"
     39 "    \"i64\": -1234567890123456789\n"
     40 "}";
     41 
     42 TEST(PrettyWriter, Basic) {
     43     StringBuffer buffer;
     44     PrettyWriter<StringBuffer> writer(buffer);
     45     Reader reader;
     46     StringStream s(kJson);
     47     reader.Parse(s, writer);
     48     EXPECT_STREQ(kPrettyJson, buffer.GetString());
     49 }
     50 
     51 TEST(PrettyWriter, SetIndent) {
     52     StringBuffer buffer;
     53     PrettyWriter<StringBuffer> writer(buffer);
     54     writer.SetIndent('\t', 1);
     55     Reader reader;
     56     StringStream s(kJson);
     57     reader.Parse(s, writer);
     58     EXPECT_STREQ(
     59         "{\n"
     60         "\t\"hello\": \"world\",\n"
     61         "\t\"t\": true,\n"
     62         "\t\"f\": false,\n"
     63         "\t\"n\": null,\n"
     64         "\t\"i\": 123,\n"
     65         "\t\"pi\": 3.1416,\n"
     66         "\t\"a\": [\n"
     67         "\t\t1,\n"
     68         "\t\t2,\n"
     69         "\t\t3,\n"
     70         "\t\t-1\n"
     71         "\t],\n"
     72         "\t\"u64\": 1234567890123456789,\n"
     73         "\t\"i64\": -1234567890123456789\n"
     74         "}",
     75         buffer.GetString());
     76 }
     77 
     78 TEST(PrettyWriter, String) {
     79     StringBuffer buffer;
     80     PrettyWriter<StringBuffer> writer(buffer);
     81     EXPECT_TRUE(writer.StartArray());
     82     EXPECT_TRUE(writer.String("Hello\n"));
     83     EXPECT_TRUE(writer.EndArray());
     84     EXPECT_STREQ("[\n    \"Hello\\n\"\n]", buffer.GetString());
     85 }
     86 
     87 #if RAPIDJSON_HAS_STDSTRING
     88 TEST(PrettyWriter, String_STDSTRING) {
     89     StringBuffer buffer;
     90     PrettyWriter<StringBuffer> writer(buffer);
     91     EXPECT_TRUE(writer.StartArray());
     92     EXPECT_TRUE(writer.String(std::string("Hello\n")));
     93     EXPECT_TRUE(writer.EndArray());
     94     EXPECT_STREQ("[\n    \"Hello\\n\"\n]", buffer.GetString());
     95 }
     96 #endif
     97 
     98 #include <sstream>
     99 
    100 class OStreamWrapper {
    101 public:
    102     typedef char Ch;
    103 
    104     OStreamWrapper(std::ostream& os) : os_(os) {}
    105 
    106     Ch Peek() const { assert(false); return '\0'; }
    107     Ch Take() { assert(false); return '\0'; }
    108     size_t Tell() const { return 0; }
    109 
    110     Ch* PutBegin() { assert(false); return 0; }
    111     void Put(Ch c) { os_.put(c); }
    112     void Flush() { os_.flush(); }
    113     size_t PutEnd(Ch*) { assert(false); return 0; }
    114 
    115 private:
    116     OStreamWrapper(const OStreamWrapper&);
    117     OStreamWrapper& operator=(const OStreamWrapper&);
    118 
    119     std::ostream& os_;
    120 };
    121 
    122 // For covering PutN() generic version
    123 TEST(PrettyWriter, OStreamWrapper) {
    124     StringStream s(kJson);
    125 
    126     std::stringstream ss;
    127     OStreamWrapper os(ss);
    128 
    129     PrettyWriter<OStreamWrapper> writer(os);
    130 
    131     Reader reader;
    132     reader.Parse(s, writer);
    133 
    134     std::string actual = ss.str();
    135     EXPECT_STREQ(kPrettyJson, actual.c_str());
    136 }
    137 
    138 // For covering FileWriteStream::PutN()
    139 TEST(PrettyWriter, FileWriteStream) {
    140     char filename[L_tmpnam];
    141     FILE* fp = TempFile(filename);
    142     char buffer[16];
    143     FileWriteStream os(fp, buffer, sizeof(buffer));
    144     PrettyWriter<FileWriteStream> writer(os);
    145     Reader reader;
    146     StringStream s(kJson);
    147     reader.Parse(s, writer);
    148     fclose(fp);
    149 
    150     fp = fopen(filename, "rb");
    151     fseek(fp, 0, SEEK_END);
    152     size_t size = (size_t)ftell(fp);
    153     fseek(fp, 0, SEEK_SET);
    154     char* json = (char*)malloc(size + 1);
    155     size_t readLength = fread(json, 1, size, fp);
    156     json[readLength] = '\0';
    157     fclose(fp);
    158     remove(filename);
    159     EXPECT_STREQ(kPrettyJson, json);
    160     free(json);
    161 }