YAMLParserTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //===- unittest/Support/YAMLParserTest ------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/YAMLParser.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "llvm/Support/Casting.h"
  11. #include "llvm/Support/MemoryBuffer.h"
  12. #include "llvm/Support/SourceMgr.h"
  13. #include "gtest/gtest.h"
  14. namespace llvm {
  15. static void SuppressDiagnosticsOutput(const SMDiagnostic &, void *) {
  16. // Prevent SourceMgr from writing errors to stderr
  17. // to reduce noise in unit test runs.
  18. }
  19. // Assumes Ctx is an SMDiagnostic where Diag can be stored.
  20. static void CollectDiagnosticsOutput(const SMDiagnostic &Diag, void *Ctx) {
  21. SMDiagnostic* DiagOut = static_cast<SMDiagnostic*>(Ctx);
  22. *DiagOut = Diag;
  23. }
  24. // Checks that the given input gives a parse error. Makes sure that an error
  25. // text is available and the parse fails.
  26. static void ExpectParseError(StringRef Message, StringRef Input) {
  27. SourceMgr SM;
  28. yaml::Stream Stream(Input, SM);
  29. SM.setDiagHandler(SuppressDiagnosticsOutput);
  30. EXPECT_FALSE(Stream.validate()) << Message << ": " << Input;
  31. EXPECT_TRUE(Stream.failed()) << Message << ": " << Input;
  32. }
  33. // Checks that the given input can be parsed without error.
  34. static void ExpectParseSuccess(StringRef Message, StringRef Input) {
  35. SourceMgr SM;
  36. yaml::Stream Stream(Input, SM);
  37. EXPECT_TRUE(Stream.validate()) << Message << ": " << Input;
  38. }
  39. TEST(YAMLParser, ParsesEmptyArray) {
  40. ExpectParseSuccess("Empty array", "[]");
  41. }
  42. TEST(YAMLParser, FailsIfNotClosingArray) {
  43. ExpectParseError("Not closing array", "[");
  44. ExpectParseError("Not closing array", " [ ");
  45. ExpectParseError("Not closing array", " [x");
  46. }
  47. TEST(YAMLParser, ParsesEmptyArrayWithWhitespace) {
  48. ExpectParseSuccess("Array with spaces", " [ ] ");
  49. ExpectParseSuccess("All whitespaces", "\t\r\n[\t\n \t\r ]\t\r \n\n");
  50. }
  51. TEST(YAMLParser, ParsesEmptyObject) {
  52. ExpectParseSuccess("Empty object", "[{}]");
  53. }
  54. TEST(YAMLParser, ParsesObject) {
  55. ExpectParseSuccess("Object with an entry", "[{\"a\":\"/b\"}]");
  56. }
  57. TEST(YAMLParser, ParsesMultipleKeyValuePairsInObject) {
  58. ExpectParseSuccess("Multiple key, value pairs",
  59. "[{\"a\":\"/b\",\"c\":\"d\",\"e\":\"f\"}]");
  60. }
  61. TEST(YAMLParser, FailsIfNotClosingObject) {
  62. ExpectParseError("Missing close on empty", "[{]");
  63. ExpectParseError("Missing close after pair", "[{\"a\":\"b\"]");
  64. }
  65. TEST(YAMLParser, FailsIfMissingColon) {
  66. ExpectParseError("Missing colon between key and value", "[{\"a\"\"/b\"}]");
  67. ExpectParseError("Missing colon between key and value", "[{\"a\" \"b\"}]");
  68. }
  69. TEST(YAMLParser, FailsOnMissingQuote) {
  70. ExpectParseError("Missing open quote", "[{a\":\"b\"}]");
  71. ExpectParseError("Missing closing quote", "[{\"a\":\"b}]");
  72. }
  73. TEST(YAMLParser, ParsesEscapedQuotes) {
  74. ExpectParseSuccess("Parses escaped string in key and value",
  75. "[{\"a\":\"\\\"b\\\" \\\" \\\"\"}]");
  76. }
  77. TEST(YAMLParser, ParsesEmptyString) {
  78. ExpectParseSuccess("Parses empty string in value", "[{\"a\":\"\"}]");
  79. }
  80. TEST(YAMLParser, ParsesMultipleObjects) {
  81. ExpectParseSuccess(
  82. "Multiple objects in array",
  83. "["
  84. " { \"a\" : \"b\" },"
  85. " { \"a\" : \"b\" },"
  86. " { \"a\" : \"b\" }"
  87. "]");
  88. }
  89. TEST(YAMLParser, FailsOnMissingComma) {
  90. ExpectParseError(
  91. "Missing comma",
  92. "["
  93. " { \"a\" : \"b\" }"
  94. " { \"a\" : \"b\" }"
  95. "]");
  96. }
  97. TEST(YAMLParser, ParsesSpacesInBetweenTokens) {
  98. ExpectParseSuccess(
  99. "Various whitespace between tokens",
  100. " \t \n\n \r [ \t \n\n \r"
  101. " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
  102. " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r,\t \n\n \r"
  103. " \t \n\n \r { \t \n\n \r\"a\"\t \n\n \r :"
  104. " \t \n\n \r \"b\"\t \n\n \r } \t \n\n \r]\t \n\n \r");
  105. }
  106. TEST(YAMLParser, ParsesArrayOfArrays) {
  107. ExpectParseSuccess("Array of arrays", "[[]]");
  108. }
  109. TEST(YAMLParser, ParsesBlockLiteralScalars) {
  110. ExpectParseSuccess("Block literal scalar", "test: |\n Hello\n World\n");
  111. ExpectParseSuccess("Block literal scalar EOF", "test: |\n Hello\n World");
  112. ExpectParseSuccess("Empty block literal scalar header EOF", "test: | ");
  113. ExpectParseSuccess("Empty block literal scalar", "test: |\ntest2: 20");
  114. ExpectParseSuccess("Empty block literal scalar 2", "- | \n \n\n \n- 42");
  115. ExpectParseSuccess("Block literal scalar in sequence",
  116. "- |\n Testing\n Out\n\n- 22");
  117. ExpectParseSuccess("Block literal scalar in document",
  118. "--- |\n Document\n...");
  119. ExpectParseSuccess("Empty non indented lines still count",
  120. "- |\n First line\n \n\n Another line\n\n- 2");
  121. ExpectParseSuccess("Comment in block literal scalar header",
  122. "test: | # Comment \n No Comment\ntest 2: | # Void");
  123. ExpectParseSuccess("Chomping indicators in block literal scalar header",
  124. "test: |- \n Hello\n\ntest 2: |+ \n\n World\n\n\n");
  125. ExpectParseSuccess("Indent indicators in block literal scalar header",
  126. "test: |1 \n \n Hello \n World\n");
  127. ExpectParseSuccess("Chomping and indent indicators in block literals",
  128. "test: |-1\n Hello\ntest 2: |9+\n World");
  129. ExpectParseSuccess("Trailing comments in block literals",
  130. "test: |\n Content\n # Trailing\n #Comment\ntest 2: 3");
  131. ExpectParseError("Invalid block scalar header", "test: | failure");
  132. ExpectParseError("Invalid line indentation", "test: |\n First line\n Error");
  133. ExpectParseError("Long leading space line", "test: |\n \n Test\n");
  134. }
  135. TEST(YAMLParser, NullTerminatedBlockScalars) {
  136. SourceMgr SM;
  137. yaml::Stream Stream("test: |\n Hello\n World\n", SM);
  138. yaml::Document &Doc = *Stream.begin();
  139. yaml::MappingNode *Map = cast<yaml::MappingNode>(Doc.getRoot());
  140. StringRef Value =
  141. cast<yaml::BlockScalarNode>(Map->begin()->getValue())->getValue();
  142. EXPECT_EQ(Value, "Hello\nWorld\n");
  143. EXPECT_EQ(Value.data()[Value.size()], '\0');
  144. }
  145. TEST(YAMLParser, HandlesEndOfFileGracefully) {
  146. ExpectParseError("In string starting with EOF", "[\"");
  147. ExpectParseError("In string hitting EOF", "[\" ");
  148. ExpectParseError("In string escaping EOF", "[\" \\");
  149. ExpectParseError("In array starting with EOF", "[");
  150. ExpectParseError("In array element starting with EOF", "[[], ");
  151. ExpectParseError("In array hitting EOF", "[[] ");
  152. ExpectParseError("In array hitting EOF", "[[]");
  153. ExpectParseError("In object hitting EOF", "{\"\"");
  154. }
  155. TEST(YAMLParser, HandlesNullValuesInKeyValueNodesGracefully) {
  156. ExpectParseError("KeyValueNode with null key", "? \"\n:");
  157. ExpectParseError("KeyValueNode with null value", "test: '");
  158. }
  159. // Checks that the given string can be parsed into an identical string inside
  160. // of an array.
  161. static void ExpectCanParseString(StringRef String) {
  162. std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
  163. SourceMgr SM;
  164. yaml::Stream Stream(StringInArray, SM);
  165. yaml::SequenceNode *ParsedSequence
  166. = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
  167. StringRef ParsedString
  168. = dyn_cast<yaml::ScalarNode>(
  169. static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
  170. ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
  171. EXPECT_EQ(String, ParsedString.str());
  172. }
  173. // Checks that parsing the given string inside an array fails.
  174. static void ExpectCannotParseString(StringRef String) {
  175. std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
  176. ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
  177. StringInArray);
  178. }
  179. TEST(YAMLParser, ParsesStrings) {
  180. ExpectCanParseString("");
  181. ExpectCannotParseString("\\");
  182. ExpectCannotParseString("\"");
  183. ExpectCanParseString(" ");
  184. ExpectCanParseString("\\ ");
  185. ExpectCanParseString("\\\"");
  186. ExpectCannotParseString("\"\\");
  187. ExpectCannotParseString(" \\");
  188. ExpectCanParseString("\\\\");
  189. ExpectCannotParseString("\\\\\\");
  190. ExpectCanParseString("\\\\\\\\");
  191. ExpectCanParseString("\\\" ");
  192. ExpectCannotParseString("\\\\\" ");
  193. ExpectCanParseString("\\\\\\\" ");
  194. ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
  195. }
  196. TEST(YAMLParser, WorksWithIteratorAlgorithms) {
  197. SourceMgr SM;
  198. yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
  199. yaml::SequenceNode *Array
  200. = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
  201. EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
  202. }
  203. TEST(YAMLParser, DefaultDiagnosticFilename) {
  204. SourceMgr SM;
  205. SMDiagnostic GeneratedDiag;
  206. SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
  207. // When we construct a YAML stream over an unnamed string,
  208. // the filename is hard-coded as "YAML".
  209. yaml::Stream UnnamedStream("[]", SM);
  210. UnnamedStream.printError(UnnamedStream.begin()->getRoot(), "Hello, World!");
  211. EXPECT_EQ("YAML", GeneratedDiag.getFilename());
  212. }
  213. TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
  214. SourceMgr SM;
  215. SMDiagnostic GeneratedDiag;
  216. SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
  217. // When we construct a YAML stream over a named buffer,
  218. // we get its ID as filename in diagnostics.
  219. std::unique_ptr<MemoryBuffer> Buffer =
  220. MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
  221. yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
  222. Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
  223. EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
  224. }
  225. TEST(YAMLParser, SameNodeIteratorOperatorNotEquals) {
  226. SourceMgr SM;
  227. yaml::Stream Stream("[\"1\", \"2\"]", SM);
  228. yaml::SequenceNode *Node = dyn_cast<yaml::SequenceNode>(
  229. Stream.begin()->getRoot());
  230. auto Begin = Node->begin();
  231. auto End = Node->end();
  232. EXPECT_TRUE(Begin != End);
  233. EXPECT_FALSE(Begin != Begin);
  234. EXPECT_FALSE(End != End);
  235. }
  236. TEST(YAMLParser, SameNodeIteratorOperatorEquals) {
  237. SourceMgr SM;
  238. yaml::Stream Stream("[\"1\", \"2\"]", SM);
  239. yaml::SequenceNode *Node = dyn_cast<yaml::SequenceNode>(
  240. Stream.begin()->getRoot());
  241. auto Begin = Node->begin();
  242. auto End = Node->end();
  243. EXPECT_FALSE(Begin == End);
  244. EXPECT_TRUE(Begin == Begin);
  245. EXPECT_TRUE(End == End);
  246. }
  247. TEST(YAMLParser, DifferentNodesIteratorOperatorNotEquals) {
  248. SourceMgr SM;
  249. yaml::Stream Stream("[\"1\", \"2\"]", SM);
  250. yaml::Stream AnotherStream("[\"1\", \"2\"]", SM);
  251. yaml::SequenceNode *Node = dyn_cast<yaml::SequenceNode>(
  252. Stream.begin()->getRoot());
  253. yaml::SequenceNode *AnotherNode = dyn_cast<yaml::SequenceNode>(
  254. AnotherStream.begin()->getRoot());
  255. auto Begin = Node->begin();
  256. auto End = Node->end();
  257. auto AnotherBegin = AnotherNode->begin();
  258. auto AnotherEnd = AnotherNode->end();
  259. EXPECT_TRUE(Begin != AnotherBegin);
  260. EXPECT_TRUE(Begin != AnotherEnd);
  261. EXPECT_FALSE(End != AnotherEnd);
  262. }
  263. TEST(YAMLParser, DifferentNodesIteratorOperatorEquals) {
  264. SourceMgr SM;
  265. yaml::Stream Stream("[\"1\", \"2\"]", SM);
  266. yaml::Stream AnotherStream("[\"1\", \"2\"]", SM);
  267. yaml::SequenceNode *Node = dyn_cast<yaml::SequenceNode>(
  268. Stream.begin()->getRoot());
  269. yaml::SequenceNode *AnotherNode = dyn_cast<yaml::SequenceNode>(
  270. AnotherStream.begin()->getRoot());
  271. auto Begin = Node->begin();
  272. auto End = Node->end();
  273. auto AnotherBegin = AnotherNode->begin();
  274. auto AnotherEnd = AnotherNode->end();
  275. EXPECT_FALSE(Begin == AnotherBegin);
  276. EXPECT_FALSE(Begin == AnotherEnd);
  277. EXPECT_TRUE(End == AnotherEnd);
  278. }
  279. } // end namespace llvm