Electroneum
cursorstreamwrappertest.cpp
Go to the documentation of this file.
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/document.h"
18 
19 using namespace rapidjson;
20 
21 // static const char json[] = "{\"string\"\n\n:\"my string\",\"array\"\n:[\"1\", \"2\", \"3\"]}";
22 
23 bool testJson(const char *json, size_t &line, size_t &col) {
24  StringStream ss(json);
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, 3);
42  EXPECT_EQ(col, 0);
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, 1);
51  EXPECT_EQ(col, 8);
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, 3);
60  EXPECT_EQ(col, 0);
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, 3);
69  EXPECT_EQ(col, 1);
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, 3);
78  EXPECT_EQ(col, 12);
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, 4);
87  EXPECT_EQ(col, 9);
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, 4);
96  EXPECT_EQ(col, 6);
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, 4);
105  EXPECT_EQ(col, 15);
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, 4);
114  EXPECT_EQ(col, 16);
115 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
size_t GetLine() const
Get the error line number, if error exists.
bool HasParseError() const
Whether a parse error has occurred in the last parsing.
Definition: document.h:2394
Read-only string stream.
Definition: fwd.h:47
TEST(CursorStreamWrapper, MissingFirstBracket)
main RapidJSON namespace
bool testJson(const char *json, size_t &line, size_t &col)
size_t GetColumn() const
Get the error column number, if error exists.
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition: document.h:2265
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
rapidjson::Document json
Definition: transport.cpp:49
Cursor stream wrapper for counting line and column number if error exists.