| 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. |
| 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/document.h" |
| 17 |
#include "rapidjson/cursorstreamwrapper.h" |
| 18 |
|
| 19 |
using namespace rapidjson; |
| 20 |
|
| 21 |
// static const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; |
| 22 |
|
| 23 |
static bool testJson(const char *json, size_t &line, size_t &col) { |
| 24 |
StringStream ss(json); |
| 25 |
CursorStreamWrapper<StringStream> csw(ss); |
| 26 |
Document document; |
| 27 |
document.ParseStream(csw); |
| 28 |
bool ret = document.HasParseError(); |
| 29 |
if (ret) { |
| 30 |
col = csw.GetColumn(); |
| 31 |
line = csw.GetLine(); |
| 32 |
} |
| 33 |
return ret; |
| 34 |
} |
| 35 |
|
| 36 |
TEST(CursorStreamWrapper, MissingFirstBracket) { |
| 37 |
const char json[] = "\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; |
| 38 |
size_t col, line; |
| 39 |
bool ret = testJson(json, line, col); |
| 40 |
EXPECT_TRUE(ret); |
| 41 |
EXPECT_EQ(line, 3u); |
| 42 |
EXPECT_EQ(col, 0u); |
| 43 |
} |
| 44 |
|
| 45 |
TEST(CursorStreamWrapper, MissingQuotes) { |
| 46 |
const char json[] = "{\"string\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; |
| 47 |
size_t col, line; |
| 48 |
bool ret = testJson(json, line, col); |
| 49 |
EXPECT_TRUE(ret); |
| 50 |
EXPECT_EQ(line, 1u); |
| 51 |
EXPECT_EQ(col, 8u); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(CursorStreamWrapper, MissingColon) { |
| 55 |
const char json[] = "{\"string\"\n\n\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; |
| 56 |
size_t col, line; |
| 57 |
bool ret = testJson(json, line, col); |
| 58 |
EXPECT_TRUE(ret); |
| 59 |
EXPECT_EQ(line, 3u); |
| 60 |
EXPECT_EQ(col, 0u); |
| 61 |
} |
| 62 |
|
| 63 |
TEST(CursorStreamWrapper, MissingSecondQuotes) { |
| 64 |
const char json[] = "{\"string\"\n\n:my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}"; |
| 65 |
size_t col, line; |
| 66 |
bool ret = testJson(json, line, col); |
| 67 |
EXPECT_TRUE(ret); |
| 68 |
EXPECT_EQ(line, 3u); |
| 69 |
EXPECT_EQ(col, 1u); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(CursorStreamWrapper, MissingComma) { |
| 73 |
const char json[] = "{\"string\"\n\n:\"my string\"\"array\"\n:[\"1\", \"2\", \"3\"]}"; |
| 74 |
size_t col, line; |
| 75 |
bool ret = testJson(json, line, col); |
| 76 |
EXPECT_TRUE(ret); |
| 77 |
EXPECT_EQ(line, 3u); |
| 78 |
EXPECT_EQ(col, 12u); |
| 79 |
} |
| 80 |
|
| 81 |
TEST(CursorStreamWrapper, MissingArrayBracket) { |
| 82 |
const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:\"1\", \"2\", \"3\"]}"; |
| 83 |
size_t col, line; |
| 84 |
bool ret = testJson(json, line, col); |
| 85 |
EXPECT_TRUE(ret); |
| 86 |
EXPECT_EQ(line, 4u); |
| 87 |
EXPECT_EQ(col, 9u); |
| 88 |
} |
| 89 |
|
| 90 |
TEST(CursorStreamWrapper, MissingArrayComma) { |
| 91 |
const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\" \"2\", \"3\"]}"; |
| 92 |
size_t col, line; |
| 93 |
bool ret = testJson(json, line, col); |
| 94 |
EXPECT_TRUE(ret); |
| 95 |
EXPECT_EQ(line, 4u); |
| 96 |
EXPECT_EQ(col, 6u); |
| 97 |
} |
| 98 |
|
| 99 |
TEST(CursorStreamWrapper, MissingLastArrayBracket) { |
| 100 |
const char json8[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"}"; |
| 101 |
size_t col, line; |
| 102 |
bool ret = testJson(json8, line, col); |
| 103 |
EXPECT_TRUE(ret); |
| 104 |
EXPECT_EQ(line, 4u); |
| 105 |
EXPECT_EQ(col, 15u); |
| 106 |
} |
| 107 |
|
| 108 |
TEST(CursorStreamWrapper, MissingLastBracket) { |
| 109 |
const char json9[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]"; |
| 110 |
size_t col, line; |
| 111 |
bool ret = testJson(json9, line, col); |
| 112 |
EXPECT_TRUE(ret); |
| 113 |
EXPECT_EQ(line, 4u); |
| 114 |
EXPECT_EQ(col, 16u); |
| 115 |
} |