YAMLParserTest.cpp 9.4 KB

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