cursorstreamwrappertest.cpp 3.7 KB

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