YAMLParserTest.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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, HandlesEndOfFileGracefully) {
  138. ExpectParseError("In string starting with EOF", "[\"");
  139. ExpectParseError("In string hitting EOF", "[\" ");
  140. ExpectParseError("In string escaping EOF", "[\" \\");
  141. ExpectParseError("In array starting with EOF", "[");
  142. ExpectParseError("In array element starting with EOF", "[[], ");
  143. ExpectParseError("In array hitting EOF", "[[] ");
  144. ExpectParseError("In array hitting EOF", "[[]");
  145. ExpectParseError("In object hitting EOF", "{\"\"");
  146. }
  147. TEST(YAMLParser, HandlesNullValuesInKeyValueNodesGracefully) {
  148. ExpectParseError("KeyValueNode with null value", "test: '");
  149. }
  150. // Checks that the given string can be parsed into an identical string inside
  151. // of an array.
  152. static void ExpectCanParseString(StringRef String) {
  153. std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
  154. SourceMgr SM;
  155. yaml::Stream Stream(StringInArray, SM);
  156. yaml::SequenceNode *ParsedSequence
  157. = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
  158. StringRef ParsedString
  159. = dyn_cast<yaml::ScalarNode>(
  160. static_cast<yaml::Node*>(ParsedSequence->begin()))->getRawValue();
  161. ParsedString = ParsedString.substr(1, ParsedString.size() - 2);
  162. EXPECT_EQ(String, ParsedString.str());
  163. }
  164. // Checks that parsing the given string inside an array fails.
  165. static void ExpectCannotParseString(StringRef String) {
  166. std::string StringInArray = (llvm::Twine("[\"") + String + "\"]").str();
  167. ExpectParseError((Twine("When parsing string \"") + String + "\"").str(),
  168. StringInArray);
  169. }
  170. TEST(YAMLParser, ParsesStrings) {
  171. ExpectCanParseString("");
  172. ExpectCannotParseString("\\");
  173. ExpectCannotParseString("\"");
  174. ExpectCanParseString(" ");
  175. ExpectCanParseString("\\ ");
  176. ExpectCanParseString("\\\"");
  177. ExpectCannotParseString("\"\\");
  178. ExpectCannotParseString(" \\");
  179. ExpectCanParseString("\\\\");
  180. ExpectCannotParseString("\\\\\\");
  181. ExpectCanParseString("\\\\\\\\");
  182. ExpectCanParseString("\\\" ");
  183. ExpectCannotParseString("\\\\\" ");
  184. ExpectCanParseString("\\\\\\\" ");
  185. ExpectCanParseString(" \\\\ \\\" \\\\\\\" ");
  186. }
  187. TEST(YAMLParser, WorksWithIteratorAlgorithms) {
  188. SourceMgr SM;
  189. yaml::Stream Stream("[\"1\", \"2\", \"3\", \"4\", \"5\", \"6\"]", SM);
  190. yaml::SequenceNode *Array
  191. = dyn_cast<yaml::SequenceNode>(Stream.begin()->getRoot());
  192. EXPECT_EQ(6, std::distance(Array->begin(), Array->end()));
  193. }
  194. TEST(YAMLParser, DefaultDiagnosticFilename) {
  195. SourceMgr SM;
  196. SMDiagnostic GeneratedDiag;
  197. SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
  198. // When we construct a YAML stream over an unnamed string,
  199. // the filename is hard-coded as "YAML".
  200. yaml::Stream UnnamedStream("[]", SM);
  201. UnnamedStream.printError(UnnamedStream.begin()->getRoot(), "Hello, World!");
  202. EXPECT_EQ("YAML", GeneratedDiag.getFilename());
  203. }
  204. TEST(YAMLParser, DiagnosticFilenameFromBufferID) {
  205. SourceMgr SM;
  206. SMDiagnostic GeneratedDiag;
  207. SM.setDiagHandler(CollectDiagnosticsOutput, &GeneratedDiag);
  208. // When we construct a YAML stream over a named buffer,
  209. // we get its ID as filename in diagnostics.
  210. std::unique_ptr<MemoryBuffer> Buffer =
  211. MemoryBuffer::getMemBuffer("[]", "buffername.yaml");
  212. yaml::Stream Stream(Buffer->getMemBufferRef(), SM);
  213. Stream.printError(Stream.begin()->getRoot(), "Hello, World!");
  214. EXPECT_EQ("buffername.yaml", GeneratedDiag.getFilename());
  215. }
  216. } // end namespace llvm