You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1136 lines
38 KiB

2 months ago
  1. #include "specexamples.h"
  2. #include "yaml-cpp/yaml.h" // IWYU pragma: keep
  3. #include "gtest/gtest.h"
  4. #define EXPECT_THROW_PARSER_EXCEPTION(statement, message) \
  5. ASSERT_THROW(statement, ParserException); \
  6. try { \
  7. statement; \
  8. } catch (const ParserException& e) { \
  9. EXPECT_EQ(e.msg, message); \
  10. }
  11. namespace YAML {
  12. namespace {
  13. TEST(NodeSpecTest, Ex2_1_SeqScalars) {
  14. Node doc = Load(ex2_1);
  15. EXPECT_TRUE(doc.IsSequence());
  16. EXPECT_EQ(3, doc.size());
  17. EXPECT_EQ("Mark McGwire", doc[0].as<std::string>());
  18. EXPECT_EQ("Sammy Sosa", doc[1].as<std::string>());
  19. EXPECT_EQ("Ken Griffey", doc[2].as<std::string>());
  20. }
  21. TEST(NodeSpecTest, Ex2_2_MappingScalarsToScalars) {
  22. Node doc = Load(ex2_2);
  23. EXPECT_TRUE(doc.IsMap());
  24. EXPECT_EQ(3, doc.size());
  25. EXPECT_EQ("65", doc["hr"].as<std::string>());
  26. EXPECT_EQ("0.278", doc["avg"].as<std::string>());
  27. EXPECT_EQ("147", doc["rbi"].as<std::string>());
  28. }
  29. TEST(NodeSpecTest, Ex2_3_MappingScalarsToSequences) {
  30. Node doc = Load(ex2_3);
  31. EXPECT_TRUE(doc.IsMap());
  32. EXPECT_EQ(2, doc.size());
  33. EXPECT_EQ(3, doc["american"].size());
  34. EXPECT_EQ("Boston Red Sox", doc["american"][0].as<std::string>());
  35. EXPECT_EQ("Detroit Tigers", doc["american"][1].as<std::string>());
  36. EXPECT_EQ("New York Yankees", doc["american"][2].as<std::string>());
  37. EXPECT_EQ(3, doc["national"].size());
  38. EXPECT_EQ("New York Mets", doc["national"][0].as<std::string>());
  39. EXPECT_EQ("Chicago Cubs", doc["national"][1].as<std::string>());
  40. EXPECT_EQ("Atlanta Braves", doc["national"][2].as<std::string>());
  41. }
  42. TEST(NodeSpecTest, Ex2_4_SequenceOfMappings) {
  43. Node doc = Load(ex2_4);
  44. EXPECT_EQ(2, doc.size());
  45. EXPECT_EQ(3, doc[0].size());
  46. EXPECT_EQ("Mark McGwire", doc[0]["name"].as<std::string>());
  47. EXPECT_EQ("65", doc[0]["hr"].as<std::string>());
  48. EXPECT_EQ("0.278", doc[0]["avg"].as<std::string>());
  49. EXPECT_EQ(3, doc[1].size());
  50. EXPECT_EQ("Sammy Sosa", doc[1]["name"].as<std::string>());
  51. EXPECT_EQ("63", doc[1]["hr"].as<std::string>());
  52. EXPECT_EQ("0.288", doc[1]["avg"].as<std::string>());
  53. }
  54. TEST(NodeSpecTest, Ex2_5_SequenceOfSequences) {
  55. Node doc = Load(ex2_5);
  56. EXPECT_EQ(3, doc.size());
  57. EXPECT_EQ(3, doc[0].size());
  58. EXPECT_EQ("name", doc[0][0].as<std::string>());
  59. EXPECT_EQ("hr", doc[0][1].as<std::string>());
  60. EXPECT_EQ("avg", doc[0][2].as<std::string>());
  61. EXPECT_EQ(3, doc[1].size());
  62. EXPECT_EQ("Mark McGwire", doc[1][0].as<std::string>());
  63. EXPECT_EQ("65", doc[1][1].as<std::string>());
  64. EXPECT_EQ("0.278", doc[1][2].as<std::string>());
  65. EXPECT_EQ(3, doc[2].size());
  66. EXPECT_EQ("Sammy Sosa", doc[2][0].as<std::string>());
  67. EXPECT_EQ("63", doc[2][1].as<std::string>());
  68. EXPECT_EQ("0.288", doc[2][2].as<std::string>());
  69. }
  70. TEST(NodeSpecTest, Ex2_6_MappingOfMappings) {
  71. Node doc = Load(ex2_6);
  72. EXPECT_EQ(2, doc.size());
  73. EXPECT_EQ(2, doc["Mark McGwire"].size());
  74. EXPECT_EQ("65", doc["Mark McGwire"]["hr"].as<std::string>());
  75. EXPECT_EQ("0.278", doc["Mark McGwire"]["avg"].as<std::string>());
  76. EXPECT_EQ(2, doc["Sammy Sosa"].size());
  77. EXPECT_EQ("63", doc["Sammy Sosa"]["hr"].as<std::string>());
  78. EXPECT_EQ("0.288", doc["Sammy Sosa"]["avg"].as<std::string>());
  79. }
  80. TEST(NodeSpecTest, Ex2_7_TwoDocumentsInAStream) {
  81. std::vector<Node> docs = LoadAll(ex2_7);
  82. EXPECT_EQ(2, docs.size());
  83. {
  84. Node doc = docs[0];
  85. EXPECT_EQ(3, doc.size());
  86. EXPECT_EQ("Mark McGwire", doc[0].as<std::string>());
  87. EXPECT_EQ("Sammy Sosa", doc[1].as<std::string>());
  88. EXPECT_EQ("Ken Griffey", doc[2].as<std::string>());
  89. }
  90. {
  91. Node doc = docs[1];
  92. EXPECT_EQ(2, doc.size());
  93. EXPECT_EQ("Chicago Cubs", doc[0].as<std::string>());
  94. EXPECT_EQ("St Louis Cardinals", doc[1].as<std::string>());
  95. }
  96. }
  97. TEST(NodeSpecTest, Ex2_8_PlayByPlayFeed) {
  98. std::vector<Node> docs = LoadAll(ex2_8);
  99. EXPECT_EQ(2, docs.size());
  100. {
  101. Node doc = docs[0];
  102. EXPECT_EQ(3, doc.size());
  103. EXPECT_EQ("20:03:20", doc["time"].as<std::string>());
  104. EXPECT_EQ("Sammy Sosa", doc["player"].as<std::string>());
  105. EXPECT_EQ("strike (miss)", doc["action"].as<std::string>());
  106. }
  107. {
  108. Node doc = docs[1];
  109. EXPECT_EQ(3, doc.size());
  110. EXPECT_EQ("20:03:47", doc["time"].as<std::string>());
  111. EXPECT_EQ("Sammy Sosa", doc["player"].as<std::string>());
  112. EXPECT_EQ("grand slam", doc["action"].as<std::string>());
  113. }
  114. }
  115. TEST(NodeSpecTest, Ex2_9_SingleDocumentWithTwoComments) {
  116. Node doc = Load(ex2_9);
  117. EXPECT_EQ(2, doc.size());
  118. EXPECT_EQ(2, doc["hr"].size());
  119. EXPECT_EQ("Mark McGwire", doc["hr"][0].as<std::string>());
  120. EXPECT_EQ("Sammy Sosa", doc["hr"][1].as<std::string>());
  121. EXPECT_EQ(2, doc["rbi"].size());
  122. EXPECT_EQ("Sammy Sosa", doc["rbi"][0].as<std::string>());
  123. EXPECT_EQ("Ken Griffey", doc["rbi"][1].as<std::string>());
  124. }
  125. TEST(NodeSpecTest, Ex2_10_SimpleAnchor) {
  126. Node doc = Load(ex2_10);
  127. EXPECT_EQ(2, doc.size());
  128. EXPECT_EQ(2, doc["hr"].size());
  129. EXPECT_EQ("Mark McGwire", doc["hr"][0].as<std::string>());
  130. EXPECT_EQ("Sammy Sosa", doc["hr"][1].as<std::string>());
  131. EXPECT_EQ(2, doc["rbi"].size());
  132. EXPECT_EQ("Sammy Sosa", doc["rbi"][0].as<std::string>());
  133. EXPECT_EQ("Ken Griffey", doc["rbi"][1].as<std::string>());
  134. }
  135. TEST(NodeSpecTest, Ex2_11_MappingBetweenSequences) {
  136. Node doc = Load(ex2_11);
  137. std::vector<std::string> tigers_cubs;
  138. tigers_cubs.push_back("Detroit Tigers");
  139. tigers_cubs.push_back("Chicago cubs");
  140. std::vector<std::string> yankees_braves;
  141. yankees_braves.push_back("New York Yankees");
  142. yankees_braves.push_back("Atlanta Braves");
  143. EXPECT_EQ(2, doc.size());
  144. EXPECT_EQ(1, doc[tigers_cubs].size());
  145. EXPECT_EQ("2001-07-23", doc[tigers_cubs][0].as<std::string>());
  146. EXPECT_EQ(3, doc[yankees_braves].size());
  147. EXPECT_EQ("2001-07-02", doc[yankees_braves][0].as<std::string>());
  148. EXPECT_EQ("2001-08-12", doc[yankees_braves][1].as<std::string>());
  149. EXPECT_EQ("2001-08-14", doc[yankees_braves][2].as<std::string>());
  150. }
  151. TEST(NodeSpecTest, Ex2_12_CompactNestedMapping) {
  152. Node doc = Load(ex2_12);
  153. EXPECT_EQ(3, doc.size());
  154. EXPECT_EQ(2, doc[0].size());
  155. EXPECT_EQ("Super Hoop", doc[0]["item"].as<std::string>());
  156. EXPECT_EQ(1, doc[0]["quantity"].as<int>());
  157. EXPECT_EQ(2, doc[1].size());
  158. EXPECT_EQ("Basketball", doc[1]["item"].as<std::string>());
  159. EXPECT_EQ(4, doc[1]["quantity"].as<int>());
  160. EXPECT_EQ(2, doc[2].size());
  161. EXPECT_EQ("Big Shoes", doc[2]["item"].as<std::string>());
  162. EXPECT_EQ(1, doc[2]["quantity"].as<int>());
  163. }
  164. TEST(NodeSpecTest, Ex2_13_InLiteralsNewlinesArePreserved) {
  165. Node doc = Load(ex2_13);
  166. EXPECT_TRUE(doc.as<std::string>() ==
  167. "\\//||\\/||\n"
  168. "// || ||__");
  169. }
  170. TEST(NodeSpecTest, Ex2_14_InFoldedScalarsNewlinesBecomeSpaces) {
  171. Node doc = Load(ex2_14);
  172. EXPECT_TRUE(doc.as<std::string>() ==
  173. "Mark McGwire's year was crippled by a knee injury.");
  174. }
  175. TEST(NodeSpecTest,
  176. Ex2_15_FoldedNewlinesArePreservedForMoreIndentedAndBlankLines) {
  177. Node doc = Load(ex2_15);
  178. EXPECT_TRUE(doc.as<std::string>() ==
  179. "Sammy Sosa completed another fine season with great stats.\n\n"
  180. " 63 Home Runs\n"
  181. " 0.288 Batting Average\n\n"
  182. "What a year!");
  183. }
  184. TEST(NodeSpecTest, Ex2_16_IndentationDeterminesScope) {
  185. Node doc = Load(ex2_16);
  186. EXPECT_EQ(3, doc.size());
  187. EXPECT_EQ("Mark McGwire", doc["name"].as<std::string>());
  188. EXPECT_TRUE(doc["accomplishment"].as<std::string>() ==
  189. "Mark set a major league home run record in 1998.\n");
  190. EXPECT_TRUE(doc["stats"].as<std::string>() ==
  191. "65 Home Runs\n0.278 Batting Average\n");
  192. }
  193. TEST(NodeSpecTest, Ex2_17_QuotedScalars) {
  194. Node doc = Load(ex2_17);
  195. EXPECT_EQ(6, doc.size());
  196. EXPECT_EQ("Sosa did fine.\xe2\x98\xba", doc["unicode"].as<std::string>());
  197. EXPECT_EQ("\b1998\t1999\t2000\n", doc["control"].as<std::string>());
  198. EXPECT_EQ("\x0d\x0a is \r\n", doc["hex esc"].as<std::string>());
  199. EXPECT_EQ("\"Howdy!\" he cried.", doc["single"].as<std::string>());
  200. EXPECT_EQ(" # Not a 'comment'.", doc["quoted"].as<std::string>());
  201. EXPECT_EQ("|\\-*-/|", doc["tie-fighter"].as<std::string>());
  202. }
  203. TEST(NodeSpecTest, Ex2_18_MultiLineFlowScalars) {
  204. Node doc = Load(ex2_18);
  205. EXPECT_EQ(2, doc.size());
  206. EXPECT_TRUE(doc["plain"].as<std::string>() ==
  207. "This unquoted scalar spans many lines.");
  208. EXPECT_TRUE(doc["quoted"].as<std::string>() ==
  209. "So does this quoted scalar.\n");
  210. }
  211. // TODO: 2.19 - 2.22 schema tags
  212. TEST(NodeSpecTest, Ex2_23_VariousExplicitTags) {
  213. Node doc = Load(ex2_23);
  214. EXPECT_EQ(3, doc.size());
  215. EXPECT_EQ("tag:yaml.org,2002:str", doc["not-date"].Tag());
  216. EXPECT_EQ("2002-04-28", doc["not-date"].as<std::string>());
  217. EXPECT_EQ("tag:yaml.org,2002:binary", doc["picture"].Tag());
  218. EXPECT_TRUE(doc["picture"].as<std::string>() ==
  219. "R0lGODlhDAAMAIQAAP//9/X\n"
  220. "17unp5WZmZgAAAOfn515eXv\n"
  221. "Pz7Y6OjuDg4J+fn5OTk6enp\n"
  222. "56enmleECcgggoBADs=\n");
  223. EXPECT_EQ("!something", doc["application specific tag"].Tag());
  224. EXPECT_TRUE(doc["application specific tag"].as<std::string>() ==
  225. "The semantics of the tag\n"
  226. "above may be different for\n"
  227. "different documents.");
  228. }
  229. TEST(NodeSpecTest, Ex2_24_GlobalTags) {
  230. Node doc = Load(ex2_24);
  231. EXPECT_EQ("tag:clarkevans.com,2002:shape", doc.Tag());
  232. EXPECT_EQ(3, doc.size());
  233. EXPECT_EQ("tag:clarkevans.com,2002:circle", doc[0].Tag());
  234. EXPECT_EQ(2, doc[0].size());
  235. EXPECT_EQ(2, doc[0]["center"].size());
  236. EXPECT_EQ(73, doc[0]["center"]["x"].as<int>());
  237. EXPECT_EQ(129, doc[0]["center"]["y"].as<int>());
  238. EXPECT_EQ(7, doc[0]["radius"].as<int>());
  239. EXPECT_EQ("tag:clarkevans.com,2002:line", doc[1].Tag());
  240. EXPECT_EQ(2, doc[1].size());
  241. EXPECT_EQ(2, doc[1]["start"].size());
  242. EXPECT_EQ(73, doc[1]["start"]["x"].as<int>());
  243. EXPECT_EQ(129, doc[1]["start"]["y"].as<int>());
  244. EXPECT_EQ(2, doc[1]["finish"].size());
  245. EXPECT_EQ(89, doc[1]["finish"]["x"].as<int>());
  246. EXPECT_EQ(102, doc[1]["finish"]["y"].as<int>());
  247. EXPECT_EQ("tag:clarkevans.com,2002:label", doc[2].Tag());
  248. EXPECT_EQ(3, doc[2].size());
  249. EXPECT_EQ(2, doc[2]["start"].size());
  250. EXPECT_EQ(73, doc[2]["start"]["x"].as<int>());
  251. EXPECT_EQ(129, doc[2]["start"]["y"].as<int>());
  252. EXPECT_EQ("0xFFEEBB", doc[2]["color"].as<std::string>());
  253. EXPECT_EQ("Pretty vector drawing.", doc[2]["text"].as<std::string>());
  254. }
  255. TEST(NodeSpecTest, Ex2_25_UnorderedSets) {
  256. Node doc = Load(ex2_25);
  257. EXPECT_EQ("tag:yaml.org,2002:set", doc.Tag());
  258. EXPECT_EQ(3, doc.size());
  259. EXPECT_TRUE(doc["Mark McGwire"].IsNull());
  260. EXPECT_TRUE(doc["Sammy Sosa"].IsNull());
  261. EXPECT_TRUE(doc["Ken Griffey"].IsNull());
  262. }
  263. TEST(NodeSpecTest, Ex2_16_OrderedMappings) {
  264. Node doc = Load(ex2_26);
  265. EXPECT_EQ("tag:yaml.org,2002:omap", doc.Tag());
  266. EXPECT_EQ(3, doc.size());
  267. EXPECT_EQ(1, doc[0].size());
  268. EXPECT_EQ(65, doc[0]["Mark McGwire"].as<int>());
  269. EXPECT_EQ(1, doc[1].size());
  270. EXPECT_EQ(63, doc[1]["Sammy Sosa"].as<int>());
  271. EXPECT_EQ(1, doc[2].size());
  272. EXPECT_EQ(58, doc[2]["Ken Griffey"].as<int>());
  273. }
  274. TEST(NodeSpecTest, Ex2_27_Invoice) {
  275. Node doc = Load(ex2_27);
  276. EXPECT_EQ("tag:clarkevans.com,2002:invoice", doc.Tag());
  277. EXPECT_EQ(8, doc.size());
  278. EXPECT_EQ(34843, doc["invoice"].as<int>());
  279. EXPECT_EQ("2001-01-23", doc["date"].as<std::string>());
  280. EXPECT_EQ(3, doc["bill-to"].size());
  281. EXPECT_EQ("Chris", doc["bill-to"]["given"].as<std::string>());
  282. EXPECT_EQ("Dumars", doc["bill-to"]["family"].as<std::string>());
  283. EXPECT_EQ(4, doc["bill-to"]["address"].size());
  284. EXPECT_TRUE(doc["bill-to"]["address"]["lines"].as<std::string>() ==
  285. "458 Walkman Dr.\nSuite #292\n");
  286. EXPECT_TRUE(doc["bill-to"]["address"]["city"].as<std::string>() ==
  287. "Royal Oak");
  288. EXPECT_EQ("MI", doc["bill-to"]["address"]["state"].as<std::string>());
  289. EXPECT_EQ("48046", doc["bill-to"]["address"]["postal"].as<std::string>());
  290. EXPECT_EQ(3, doc["ship-to"].size());
  291. EXPECT_EQ("Chris", doc["ship-to"]["given"].as<std::string>());
  292. EXPECT_EQ("Dumars", doc["ship-to"]["family"].as<std::string>());
  293. EXPECT_EQ(4, doc["ship-to"]["address"].size());
  294. EXPECT_TRUE(doc["ship-to"]["address"]["lines"].as<std::string>() ==
  295. "458 Walkman Dr.\nSuite #292\n");
  296. EXPECT_TRUE(doc["ship-to"]["address"]["city"].as<std::string>() ==
  297. "Royal Oak");
  298. EXPECT_EQ("MI", doc["ship-to"]["address"]["state"].as<std::string>());
  299. EXPECT_EQ("48046", doc["ship-to"]["address"]["postal"].as<std::string>());
  300. EXPECT_EQ(2, doc["product"].size());
  301. EXPECT_EQ(4, doc["product"][0].size());
  302. EXPECT_EQ("BL394D", doc["product"][0]["sku"].as<std::string>());
  303. EXPECT_EQ(4, doc["product"][0]["quantity"].as<int>());
  304. EXPECT_TRUE(doc["product"][0]["description"].as<std::string>() ==
  305. "Basketball");
  306. EXPECT_EQ("450.00", doc["product"][0]["price"].as<std::string>());
  307. EXPECT_EQ(4, doc["product"][1].size());
  308. EXPECT_EQ("BL4438H", doc["product"][1]["sku"].as<std::string>());
  309. EXPECT_EQ(1, doc["product"][1]["quantity"].as<int>());
  310. EXPECT_TRUE(doc["product"][1]["description"].as<std::string>() ==
  311. "Super Hoop");
  312. EXPECT_EQ("2392.00", doc["product"][1]["price"].as<std::string>());
  313. EXPECT_EQ("251.42", doc["tax"].as<std::string>());
  314. EXPECT_EQ("4443.52", doc["total"].as<std::string>());
  315. EXPECT_EQ(
  316. "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338.",
  317. doc["comments"].as<std::string>());
  318. }
  319. TEST(NodeSpecTest, Ex2_28_LogFile) {
  320. std::vector<Node> docs = LoadAll(ex2_28);
  321. EXPECT_EQ(3, docs.size());
  322. {
  323. Node doc = docs[0];
  324. EXPECT_EQ(3, doc.size());
  325. EXPECT_EQ("2001-11-23 15:01:42 -5", doc["Time"].as<std::string>());
  326. EXPECT_EQ("ed", doc["User"].as<std::string>());
  327. EXPECT_TRUE(doc["Warning"].as<std::string>() ==
  328. "This is an error message for the log file");
  329. }
  330. {
  331. Node doc = docs[1];
  332. EXPECT_EQ(3, doc.size());
  333. EXPECT_EQ("2001-11-23 15:02:31 -5", doc["Time"].as<std::string>());
  334. EXPECT_EQ("ed", doc["User"].as<std::string>());
  335. EXPECT_TRUE(doc["Warning"].as<std::string>() ==
  336. "A slightly different error message.");
  337. }
  338. {
  339. Node doc = docs[2];
  340. EXPECT_EQ(4, doc.size());
  341. EXPECT_EQ("2001-11-23 15:03:17 -5", doc["Date"].as<std::string>());
  342. EXPECT_EQ("ed", doc["User"].as<std::string>());
  343. EXPECT_EQ("Unknown variable \"bar\"", doc["Fatal"].as<std::string>());
  344. EXPECT_EQ(2, doc["Stack"].size());
  345. EXPECT_EQ(3, doc["Stack"][0].size());
  346. EXPECT_EQ("TopClass.py", doc["Stack"][0]["file"].as<std::string>());
  347. EXPECT_EQ("23", doc["Stack"][0]["line"].as<std::string>());
  348. EXPECT_TRUE(doc["Stack"][0]["code"].as<std::string>() ==
  349. "x = MoreObject(\"345\\n\")\n");
  350. EXPECT_EQ(3, doc["Stack"][1].size());
  351. EXPECT_EQ("MoreClass.py", doc["Stack"][1]["file"].as<std::string>());
  352. EXPECT_EQ("58", doc["Stack"][1]["line"].as<std::string>());
  353. EXPECT_EQ("foo = bar", doc["Stack"][1]["code"].as<std::string>());
  354. }
  355. }
  356. // TODO: 5.1 - 5.2 BOM
  357. TEST(NodeSpecTest, Ex5_3_BlockStructureIndicators) {
  358. Node doc = Load(ex5_3);
  359. EXPECT_EQ(2, doc.size());
  360. EXPECT_EQ(2, doc["sequence"].size());
  361. EXPECT_EQ("one", doc["sequence"][0].as<std::string>());
  362. EXPECT_EQ("two", doc["sequence"][1].as<std::string>());
  363. EXPECT_EQ(2, doc["mapping"].size());
  364. EXPECT_EQ("blue", doc["mapping"]["sky"].as<std::string>());
  365. EXPECT_EQ("green", doc["mapping"]["sea"].as<std::string>());
  366. }
  367. TEST(NodeSpecTest, Ex5_4_FlowStructureIndicators) {
  368. Node doc = Load(ex5_4);
  369. EXPECT_EQ(2, doc.size());
  370. EXPECT_EQ(2, doc["sequence"].size());
  371. EXPECT_EQ("one", doc["sequence"][0].as<std::string>());
  372. EXPECT_EQ("two", doc["sequence"][1].as<std::string>());
  373. EXPECT_EQ(2, doc["mapping"].size());
  374. EXPECT_EQ("blue", doc["mapping"]["sky"].as<std::string>());
  375. EXPECT_EQ("green", doc["mapping"]["sea"].as<std::string>());
  376. }
  377. TEST(NodeSpecTest, Ex5_5_CommentIndicator) {
  378. Node doc = Load(ex5_5);
  379. EXPECT_TRUE(doc.IsNull());
  380. }
  381. TEST(NodeSpecTest, Ex5_6_NodePropertyIndicators) {
  382. Node doc = Load(ex5_6);
  383. EXPECT_EQ(2, doc.size());
  384. EXPECT_TRUE(doc["anchored"].as<std::string>() ==
  385. "value"); // TODO: assert tag
  386. EXPECT_EQ("value", doc["alias"].as<std::string>());
  387. }
  388. TEST(NodeSpecTest, Ex5_7_BlockScalarIndicators) {
  389. Node doc = Load(ex5_7);
  390. EXPECT_EQ(2, doc.size());
  391. EXPECT_EQ("some\ntext\n", doc["literal"].as<std::string>());
  392. EXPECT_EQ("some text\n", doc["folded"].as<std::string>());
  393. }
  394. TEST(NodeSpecTest, Ex5_8_QuotedScalarIndicators) {
  395. Node doc = Load(ex5_8);
  396. EXPECT_EQ(2, doc.size());
  397. EXPECT_EQ("text", doc["single"].as<std::string>());
  398. EXPECT_EQ("text", doc["double"].as<std::string>());
  399. }
  400. // TODO: 5.9 directive
  401. // TODO: 5.10 reserved indicator
  402. TEST(NodeSpecTest, Ex5_11_LineBreakCharacters) {
  403. Node doc = Load(ex5_11);
  404. EXPECT_TRUE(doc.as<std::string>() ==
  405. "Line break (no glyph)\nLine break (glyphed)\n");
  406. }
  407. TEST(NodeSpecTest, Ex5_12_TabsAndSpaces) {
  408. Node doc = Load(ex5_12);
  409. EXPECT_EQ(2, doc.size());
  410. EXPECT_EQ("Quoted\t", doc["quoted"].as<std::string>());
  411. EXPECT_TRUE(doc["block"].as<std::string>() ==
  412. "void main() {\n"
  413. "\tprintf(\"Hello, world!\\n\");\n"
  414. "}");
  415. }
  416. TEST(NodeSpecTest, Ex5_13_EscapedCharacters) {
  417. Node doc = Load(ex5_13);
  418. EXPECT_TRUE(doc.as<std::string>() ==
  419. "Fun with \x5C \x22 \x07 \x08 \x1B \x0C \x0A \x0D \x09 \x0B " +
  420. std::string("\x00", 1) +
  421. " \x20 \xA0 \x85 \xe2\x80\xa8 \xe2\x80\xa9 A A A");
  422. }
  423. TEST(NodeSpecTest, Ex5_14_InvalidEscapedCharacters) {
  424. EXPECT_THROW_PARSER_EXCEPTION(Load(ex5_14),
  425. std::string(ErrorMsg::INVALID_ESCAPE) + "c");
  426. }
  427. TEST(NodeSpecTest, Ex6_1_IndentationSpaces) {
  428. Node doc = Load(ex6_1);
  429. EXPECT_EQ(1, doc.size());
  430. EXPECT_EQ(2, doc["Not indented"].size());
  431. EXPECT_TRUE(doc["Not indented"]["By one space"].as<std::string>() ==
  432. "By four\n spaces\n");
  433. EXPECT_EQ(3, doc["Not indented"]["Flow style"].size());
  434. EXPECT_TRUE(doc["Not indented"]["Flow style"][0].as<std::string>() ==
  435. "By two");
  436. EXPECT_TRUE(doc["Not indented"]["Flow style"][1].as<std::string>() ==
  437. "Also by two");
  438. EXPECT_TRUE(doc["Not indented"]["Flow style"][2].as<std::string>() ==
  439. "Still by two");
  440. }
  441. TEST(NodeSpecTest, Ex6_2_IndentationIndicators) {
  442. Node doc = Load(ex6_2);
  443. EXPECT_EQ(1, doc.size());
  444. EXPECT_EQ(2, doc["a"].size());
  445. EXPECT_EQ("b", doc["a"][0].as<std::string>());
  446. EXPECT_EQ(2, doc["a"][1].size());
  447. EXPECT_EQ("c", doc["a"][1][0].as<std::string>());
  448. EXPECT_EQ("d", doc["a"][1][1].as<std::string>());
  449. }
  450. TEST(NodeSpecTest, Ex6_3_SeparationSpaces) {
  451. Node doc = Load(ex6_3);
  452. EXPECT_EQ(2, doc.size());
  453. EXPECT_EQ(1, doc[0].size());
  454. EXPECT_EQ("bar", doc[0]["foo"].as<std::string>());
  455. EXPECT_EQ(2, doc[1].size());
  456. EXPECT_EQ("baz", doc[1][0].as<std::string>());
  457. EXPECT_EQ("baz", doc[1][1].as<std::string>());
  458. }
  459. TEST(NodeSpecTest, Ex6_4_LinePrefixes) {
  460. Node doc = Load(ex6_4);
  461. EXPECT_EQ(3, doc.size());
  462. EXPECT_EQ("text lines", doc["plain"].as<std::string>());
  463. EXPECT_EQ("text lines", doc["quoted"].as<std::string>());
  464. EXPECT_EQ("text\n \tlines\n", doc["block"].as<std::string>());
  465. }
  466. TEST(NodeSpecTest, Ex6_5_EmptyLines) {
  467. Node doc = Load(ex6_5);
  468. EXPECT_EQ(2, doc.size());
  469. EXPECT_EQ("Empty line\nas a line feed", doc["Folding"].as<std::string>());
  470. EXPECT_EQ("Clipped empty lines\n", doc["Chomping"].as<std::string>());
  471. }
  472. TEST(NodeSpecTest, Ex6_6_LineFolding) {
  473. Node doc = Load(ex6_6);
  474. EXPECT_EQ("trimmed\n\n\nas space", doc.as<std::string>());
  475. }
  476. TEST(NodeSpecTest, Ex6_7_BlockFolding) {
  477. Node doc = Load(ex6_7);
  478. EXPECT_EQ("foo \n\n\t bar\n\nbaz\n", doc.as<std::string>());
  479. }
  480. TEST(NodeSpecTest, Ex6_8_FlowFolding) {
  481. Node doc = Load(ex6_8);
  482. EXPECT_EQ(" foo\nbar\nbaz ", doc.as<std::string>());
  483. }
  484. TEST(NodeSpecTest, Ex6_9_SeparatedComment) {
  485. Node doc = Load(ex6_9);
  486. EXPECT_EQ(1, doc.size());
  487. EXPECT_EQ("value", doc["key"].as<std::string>());
  488. }
  489. TEST(NodeSpecTest, Ex6_10_CommentLines) {
  490. Node doc = Load(ex6_10);
  491. EXPECT_TRUE(doc.IsNull());
  492. }
  493. TEST(NodeSpecTest, Ex6_11_MultiLineComments) {
  494. Node doc = Load(ex6_11);
  495. EXPECT_EQ(1, doc.size());
  496. EXPECT_EQ("value", doc["key"].as<std::string>());
  497. }
  498. TEST(NodeSpecTest, Ex6_12_SeparationSpacesII) {
  499. Node doc = Load(ex6_12);
  500. std::map<std::string, std::string> sammy;
  501. sammy["first"] = "Sammy";
  502. sammy["last"] = "Sosa";
  503. EXPECT_EQ(1, doc.size());
  504. EXPECT_EQ(2, doc[sammy].size());
  505. EXPECT_EQ(65, doc[sammy]["hr"].as<int>());
  506. EXPECT_EQ("0.278", doc[sammy]["avg"].as<std::string>());
  507. }
  508. TEST(NodeSpecTest, Ex6_13_ReservedDirectives) {
  509. Node doc = Load(ex6_13);
  510. EXPECT_EQ("foo", doc.as<std::string>());
  511. }
  512. TEST(NodeSpecTest, Ex6_14_YAMLDirective) {
  513. Node doc = Load(ex6_14);
  514. EXPECT_EQ("foo", doc.as<std::string>());
  515. }
  516. TEST(NodeSpecTest, Ex6_15_InvalidRepeatedYAMLDirective) {
  517. EXPECT_THROW_PARSER_EXCEPTION(Load(ex6_15),
  518. ErrorMsg::REPEATED_YAML_DIRECTIVE);
  519. }
  520. TEST(NodeSpecTest, Ex6_16_TagDirective) {
  521. Node doc = Load(ex6_16);
  522. EXPECT_EQ("tag:yaml.org,2002:str", doc.Tag());
  523. EXPECT_EQ("foo", doc.as<std::string>());
  524. }
  525. TEST(NodeSpecTest, Ex6_17_InvalidRepeatedTagDirective) {
  526. EXPECT_THROW_PARSER_EXCEPTION(Load(ex6_17), ErrorMsg::REPEATED_TAG_DIRECTIVE);
  527. }
  528. TEST(NodeSpecTest, Ex6_18_PrimaryTagHandle) {
  529. std::vector<Node> docs = LoadAll(ex6_18);
  530. EXPECT_EQ(2, docs.size());
  531. {
  532. Node doc = docs[0];
  533. EXPECT_EQ("!foo", doc.Tag());
  534. EXPECT_EQ("bar", doc.as<std::string>());
  535. }
  536. {
  537. Node doc = docs[1];
  538. EXPECT_EQ("tag:example.com,2000:app/foo", doc.Tag());
  539. EXPECT_EQ("bar", doc.as<std::string>());
  540. }
  541. }
  542. TEST(NodeSpecTest, Ex6_19_SecondaryTagHandle) {
  543. Node doc = Load(ex6_19);
  544. EXPECT_EQ("tag:example.com,2000:app/int", doc.Tag());
  545. EXPECT_EQ("1 - 3", doc.as<std::string>());
  546. }
  547. TEST(NodeSpecTest, Ex6_20_TagHandles) {
  548. Node doc = Load(ex6_20);
  549. EXPECT_EQ("tag:example.com,2000:app/foo", doc.Tag());
  550. EXPECT_EQ("bar", doc.as<std::string>());
  551. }
  552. TEST(NodeSpecTest, Ex6_21_LocalTagPrefix) {
  553. std::vector<Node> docs = LoadAll(ex6_21);
  554. EXPECT_EQ(2, docs.size());
  555. {
  556. Node doc = docs[0];
  557. EXPECT_EQ("!my-light", doc.Tag());
  558. EXPECT_EQ("fluorescent", doc.as<std::string>());
  559. }
  560. {
  561. Node doc = docs[1];
  562. EXPECT_EQ("!my-light", doc.Tag());
  563. EXPECT_EQ("green", doc.as<std::string>());
  564. }
  565. }
  566. TEST(NodeSpecTest, Ex6_22_GlobalTagPrefix) {
  567. Node doc = Load(ex6_22);
  568. EXPECT_EQ(1, doc.size());
  569. EXPECT_EQ("tag:example.com,2000:app/foo", doc[0].Tag());
  570. EXPECT_EQ("bar", doc[0].as<std::string>());
  571. }
  572. TEST(NodeSpecTest, Ex6_23_NodeProperties) {
  573. Node doc = Load(ex6_23);
  574. EXPECT_EQ(2, doc.size());
  575. for (const_iterator it = doc.begin(); it != doc.end(); ++it) {
  576. if (it->first.as<std::string>() == "foo") {
  577. EXPECT_EQ("tag:yaml.org,2002:str", it->first.Tag());
  578. EXPECT_EQ("tag:yaml.org,2002:str", it->second.Tag());
  579. EXPECT_EQ("bar", it->second.as<std::string>());
  580. } else if (it->first.as<std::string>() == "baz") {
  581. EXPECT_EQ("foo", it->second.as<std::string>());
  582. } else
  583. FAIL() << "unknown key";
  584. }
  585. }
  586. TEST(NodeSpecTest, Ex6_24_VerbatimTags) {
  587. Node doc = Load(ex6_24);
  588. EXPECT_EQ(1, doc.size());
  589. for (const_iterator it = doc.begin(); it != doc.end(); ++it) {
  590. EXPECT_EQ("tag:yaml.org,2002:str", it->first.Tag());
  591. EXPECT_EQ("foo", it->first.as<std::string>());
  592. EXPECT_EQ("!bar", it->second.Tag());
  593. EXPECT_EQ("baz", it->second.as<std::string>());
  594. }
  595. }
  596. TEST(NodeSpecTest, DISABLED_Ex6_25_InvalidVerbatimTags) {
  597. Node doc = Load(ex6_25);
  598. // TODO: check tags (but we probably will say these are valid, I think)
  599. FAIL() << "not implemented yet";
  600. }
  601. TEST(NodeSpecTest, Ex6_26_TagShorthands) {
  602. Node doc = Load(ex6_26);
  603. EXPECT_EQ(3, doc.size());
  604. EXPECT_EQ("!local", doc[0].Tag());
  605. EXPECT_EQ("foo", doc[0].as<std::string>());
  606. EXPECT_EQ("tag:yaml.org,2002:str", doc[1].Tag());
  607. EXPECT_EQ("bar", doc[1].as<std::string>());
  608. EXPECT_EQ("tag:example.com,2000:app/tag%21", doc[2].Tag());
  609. EXPECT_EQ("baz", doc[2].as<std::string>());
  610. }
  611. TEST(NodeSpecTest, Ex6_27a_InvalidTagShorthands) {
  612. EXPECT_THROW_PARSER_EXCEPTION(Load(ex6_27a), ErrorMsg::TAG_WITH_NO_SUFFIX);
  613. }
  614. // TODO: should we reject this one (since !h! is not declared)?
  615. TEST(NodeSpecTest, DISABLED_Ex6_27b_InvalidTagShorthands) {
  616. Load(ex6_27b);
  617. FAIL() << "not implemented yet";
  618. }
  619. TEST(NodeSpecTest, Ex6_28_NonSpecificTags) {
  620. Node doc = Load(ex6_28);
  621. EXPECT_EQ(3, doc.size());
  622. EXPECT_EQ("12", doc[0].as<std::string>()); // TODO: check tags. How?
  623. EXPECT_EQ(12, doc[1].as<int>());
  624. EXPECT_EQ("12", doc[2].as<std::string>());
  625. }
  626. TEST(NodeSpecTest, Ex6_29_NodeAnchors) {
  627. Node doc = Load(ex6_29);
  628. EXPECT_EQ(2, doc.size());
  629. EXPECT_EQ("Value", doc["First occurrence"].as<std::string>());
  630. EXPECT_EQ("Value", doc["Second occurrence"].as<std::string>());
  631. }
  632. TEST(NodeSpecTest, Ex7_1_AliasNodes) {
  633. Node doc = Load(ex7_1);
  634. EXPECT_EQ(4, doc.size());
  635. EXPECT_EQ("Foo", doc["First occurrence"].as<std::string>());
  636. EXPECT_EQ("Foo", doc["Second occurrence"].as<std::string>());
  637. EXPECT_EQ("Bar", doc["Override anchor"].as<std::string>());
  638. EXPECT_EQ("Bar", doc["Reuse anchor"].as<std::string>());
  639. }
  640. TEST(NodeSpecTest, Ex7_2_EmptyNodes) {
  641. Node doc = Load(ex7_2);
  642. EXPECT_EQ(2, doc.size());
  643. for (const_iterator it = doc.begin(); it != doc.end(); ++it) {
  644. if (it->first.as<std::string>() == "foo") {
  645. EXPECT_EQ("tag:yaml.org,2002:str", it->second.Tag());
  646. EXPECT_EQ("", it->second.as<std::string>());
  647. } else if (it->first.as<std::string>() == "") {
  648. EXPECT_EQ("tag:yaml.org,2002:str", it->first.Tag());
  649. EXPECT_EQ("bar", it->second.as<std::string>());
  650. } else
  651. FAIL() << "unexpected key";
  652. }
  653. }
  654. TEST(NodeSpecTest, Ex7_3_CompletelyEmptyNodes) {
  655. Node doc = Load(ex7_3);
  656. EXPECT_EQ(2, doc.size());
  657. EXPECT_TRUE(doc["foo"].IsNull());
  658. EXPECT_EQ("bar", doc[Null].as<std::string>());
  659. }
  660. TEST(NodeSpecTest, Ex7_4_DoubleQuotedImplicitKeys) {
  661. Node doc = Load(ex7_4);
  662. EXPECT_EQ(1, doc.size());
  663. EXPECT_EQ(1, doc["implicit block key"].size());
  664. EXPECT_EQ(1, doc["implicit block key"][0].size());
  665. EXPECT_EQ(
  666. "value",
  667. doc["implicit block key"][0]["implicit flow key"].as<std::string>());
  668. }
  669. TEST(NodeSpecTest, Ex7_5_DoubleQuotedLineBreaks) {
  670. Node doc = Load(ex7_5);
  671. EXPECT_TRUE(doc.as<std::string>() ==
  672. "folded to a space,\nto a line feed, or \t \tnon-content");
  673. }
  674. TEST(NodeSpecTest, Ex7_6_DoubleQuotedLines) {
  675. Node doc = Load(ex7_6);
  676. EXPECT_TRUE(doc.as<std::string>() ==
  677. " 1st non-empty\n2nd non-empty 3rd non-empty ");
  678. }
  679. TEST(NodeSpecTest, Ex7_7_SingleQuotedCharacters) {
  680. Node doc = Load(ex7_7);
  681. EXPECT_EQ("here's to \"quotes\"", doc.as<std::string>());
  682. }
  683. TEST(NodeSpecTest, Ex7_8_SingleQuotedImplicitKeys) {
  684. Node doc = Load(ex7_8);
  685. EXPECT_EQ(1, doc.size());
  686. EXPECT_EQ(1, doc["implicit block key"].size());
  687. EXPECT_EQ(1, doc["implicit block key"][0].size());
  688. EXPECT_EQ(
  689. "value",
  690. doc["implicit block key"][0]["implicit flow key"].as<std::string>());
  691. }
  692. TEST(NodeSpecTest, Ex7_9_SingleQuotedLines) {
  693. Node doc = Load(ex7_9);
  694. EXPECT_TRUE(doc.as<std::string>() ==
  695. " 1st non-empty\n2nd non-empty 3rd non-empty ");
  696. }
  697. TEST(NodeSpecTest, Ex7_10_PlainCharacters) {
  698. Node doc = Load(ex7_10);
  699. EXPECT_EQ(6, doc.size());
  700. EXPECT_EQ("::vector", doc[0].as<std::string>());
  701. EXPECT_EQ(": - ()", doc[1].as<std::string>());
  702. EXPECT_EQ("Up, up, and away!", doc[2].as<std::string>());
  703. EXPECT_EQ(-123, doc[3].as<int>());
  704. EXPECT_EQ("http://example.com/foo#bar", doc[4].as<std::string>());
  705. EXPECT_EQ(5, doc[5].size());
  706. EXPECT_EQ("::vector", doc[5][0].as<std::string>());
  707. EXPECT_EQ(": - ()", doc[5][1].as<std::string>());
  708. EXPECT_EQ("Up, up, and away!", doc[5][2].as<std::string>());
  709. EXPECT_EQ(-123, doc[5][3].as<int>());
  710. EXPECT_EQ("http://example.com/foo#bar", doc[5][4].as<std::string>());
  711. }
  712. TEST(NodeSpecTest, Ex7_11_PlainImplicitKeys) {
  713. Node doc = Load(ex7_11);
  714. EXPECT_EQ(1, doc.size());
  715. EXPECT_EQ(1, doc["implicit block key"].size());
  716. EXPECT_EQ(1, doc["implicit block key"][0].size());
  717. EXPECT_EQ(
  718. "value",
  719. doc["implicit block key"][0]["implicit flow key"].as<std::string>());
  720. }
  721. TEST(NodeSpecTest, Ex7_12_PlainLines) {
  722. Node doc = Load(ex7_12);
  723. EXPECT_TRUE(doc.as<std::string>() ==
  724. "1st non-empty\n2nd non-empty 3rd non-empty");
  725. }
  726. TEST(NodeSpecTest, Ex7_13_FlowSequence) {
  727. Node doc = Load(ex7_13);
  728. EXPECT_EQ(2, doc.size());
  729. EXPECT_EQ(2, doc[0].size());
  730. EXPECT_EQ("one", doc[0][0].as<std::string>());
  731. EXPECT_EQ("two", doc[0][1].as<std::string>());
  732. EXPECT_EQ(2, doc[1].size());
  733. EXPECT_EQ("three", doc[1][0].as<std::string>());
  734. EXPECT_EQ("four", doc[1][1].as<std::string>());
  735. }
  736. TEST(NodeSpecTest, Ex7_14_FlowSequenceEntries) {
  737. Node doc = Load(ex7_14);
  738. EXPECT_EQ(5, doc.size());
  739. EXPECT_EQ("double quoted", doc[0].as<std::string>());
  740. EXPECT_EQ("single quoted", doc[1].as<std::string>());
  741. EXPECT_EQ("plain text", doc[2].as<std::string>());
  742. EXPECT_EQ(1, doc[3].size());
  743. EXPECT_EQ("nested", doc[3][0].as<std::string>());
  744. EXPECT_EQ(1, doc[4].size());
  745. EXPECT_EQ("pair", doc[4]["single"].as<std::string>());
  746. }
  747. TEST(NodeSpecTest, Ex7_15_FlowMappings) {
  748. Node doc = Load(ex7_15);
  749. EXPECT_EQ(2, doc.size());
  750. EXPECT_EQ(2, doc[0].size());
  751. EXPECT_EQ("two", doc[0]["one"].as<std::string>());
  752. EXPECT_EQ("four", doc[0]["three"].as<std::string>());
  753. EXPECT_EQ(2, doc[1].size());
  754. EXPECT_EQ("six", doc[1]["five"].as<std::string>());
  755. EXPECT_EQ("eight", doc[1]["seven"].as<std::string>());
  756. }
  757. TEST(NodeSpecTest, Ex7_16_FlowMappingEntries) {
  758. Node doc = Load(ex7_16);
  759. EXPECT_EQ(3, doc.size());
  760. EXPECT_EQ("entry", doc["explicit"].as<std::string>());
  761. EXPECT_EQ("entry", doc["implicit"].as<std::string>());
  762. EXPECT_TRUE(doc[Null].IsNull());
  763. }
  764. TEST(NodeSpecTest, Ex7_17_FlowMappingSeparateValues) {
  765. Node doc = Load(ex7_17);
  766. EXPECT_EQ(4, doc.size());
  767. EXPECT_EQ("separate", doc["unquoted"].as<std::string>());
  768. EXPECT_TRUE(doc["http://foo.com"].IsNull());
  769. EXPECT_TRUE(doc["omitted value"].IsNull());
  770. EXPECT_EQ("omitted key", doc[Null].as<std::string>());
  771. }
  772. TEST(NodeSpecTest, Ex7_18_FlowMappingAdjacentValues) {
  773. Node doc = Load(ex7_18);
  774. EXPECT_EQ(3, doc.size());
  775. EXPECT_EQ("value", doc["adjacent"].as<std::string>());
  776. EXPECT_EQ("value", doc["readable"].as<std::string>());
  777. EXPECT_TRUE(doc["empty"].IsNull());
  778. }
  779. TEST(NodeSpecTest, Ex7_19_SinglePairFlowMappings) {
  780. Node doc = Load(ex7_19);
  781. EXPECT_EQ(1, doc.size());
  782. EXPECT_EQ(1, doc[0].size());
  783. EXPECT_EQ("bar", doc[0]["foo"].as<std::string>());
  784. }
  785. TEST(NodeSpecTest, Ex7_20_SinglePairExplicitEntry) {
  786. Node doc = Load(ex7_20);
  787. EXPECT_EQ(1, doc.size());
  788. EXPECT_EQ(1, doc[0].size());
  789. EXPECT_EQ("baz", doc[0]["foo bar"].as<std::string>());
  790. }
  791. TEST(NodeSpecTest, Ex7_21_SinglePairImplicitEntries) {
  792. Node doc = Load(ex7_21);
  793. EXPECT_EQ(3, doc.size());
  794. EXPECT_EQ(1, doc[0].size());
  795. EXPECT_EQ(1, doc[0][0].size());
  796. EXPECT_EQ("separate", doc[0][0]["YAML"].as<std::string>());
  797. EXPECT_EQ(1, doc[1].size());
  798. EXPECT_EQ(1, doc[1][0].size());
  799. EXPECT_EQ("empty key entry", doc[1][0][Null].as<std::string>());
  800. EXPECT_EQ(1, doc[2].size());
  801. EXPECT_EQ(1, doc[2][0].size());
  802. std::map<std::string, std::string> key;
  803. key["JSON"] = "like";
  804. EXPECT_EQ("adjacent", doc[2][0][key].as<std::string>());
  805. }
  806. TEST(NodeSpecTest, Ex7_22_InvalidImplicitKeys) {
  807. EXPECT_THROW_PARSER_EXCEPTION(Load(ex7_22), ErrorMsg::END_OF_SEQ_FLOW);
  808. }
  809. TEST(NodeSpecTest, Ex7_23_FlowContent) {
  810. Node doc = Load(ex7_23);
  811. EXPECT_EQ(5, doc.size());
  812. EXPECT_EQ(2, doc[0].size());
  813. EXPECT_EQ("a", doc[0][0].as<std::string>());
  814. EXPECT_EQ("b", doc[0][1].as<std::string>());
  815. EXPECT_EQ(1, doc[1].size());
  816. EXPECT_EQ("b", doc[1]["a"].as<std::string>());
  817. EXPECT_EQ("a", doc[2].as<std::string>());
  818. EXPECT_EQ('b', doc[3].as<char>());
  819. EXPECT_EQ("c", doc[4].as<std::string>());
  820. }
  821. TEST(NodeSpecTest, Ex7_24_FlowNodes) {
  822. Node doc = Load(ex7_24);
  823. EXPECT_EQ(5, doc.size());
  824. EXPECT_EQ("tag:yaml.org,2002:str", doc[0].Tag());
  825. EXPECT_EQ("a", doc[0].as<std::string>());
  826. EXPECT_EQ('b', doc[1].as<char>());
  827. EXPECT_EQ("c", doc[2].as<std::string>());
  828. EXPECT_EQ("c", doc[3].as<std::string>());
  829. EXPECT_EQ("tag:yaml.org,2002:str", doc[4].Tag());
  830. EXPECT_EQ("", doc[4].as<std::string>());
  831. }
  832. TEST(NodeSpecTest, Ex8_1_BlockScalarHeader) {
  833. Node doc = Load(ex8_1);
  834. EXPECT_EQ(4, doc.size());
  835. EXPECT_EQ("literal\n", doc[0].as<std::string>());
  836. EXPECT_EQ(" folded\n", doc[1].as<std::string>());
  837. EXPECT_EQ("keep\n\n", doc[2].as<std::string>());
  838. EXPECT_EQ(" strip", doc[3].as<std::string>());
  839. }
  840. TEST(NodeSpecTest, Ex8_2_BlockIndentationHeader) {
  841. Node doc = Load(ex8_2);
  842. EXPECT_EQ(4, doc.size());
  843. EXPECT_EQ("detected\n", doc[0].as<std::string>());
  844. EXPECT_EQ("\n\n# detected\n", doc[1].as<std::string>());
  845. EXPECT_EQ(" explicit\n", doc[2].as<std::string>());
  846. EXPECT_EQ("\t\ndetected\n", doc[3].as<std::string>());
  847. }
  848. TEST(NodeSpecTest, Ex8_3a_InvalidBlockScalarIndentationIndicators) {
  849. EXPECT_THROW_PARSER_EXCEPTION(Load(ex8_3a), ErrorMsg::END_OF_SEQ);
  850. }
  851. TEST(NodeSpecTest, Ex8_3b_InvalidBlockScalarIndentationIndicators) {
  852. EXPECT_THROW_PARSER_EXCEPTION(Load(ex8_3b), ErrorMsg::END_OF_SEQ);
  853. }
  854. TEST(NodeSpecTest, Ex8_3c_InvalidBlockScalarIndentationIndicators) {
  855. EXPECT_THROW_PARSER_EXCEPTION(Load(ex8_3c), ErrorMsg::END_OF_SEQ);
  856. }
  857. TEST(NodeSpecTest, Ex8_4_ChompingFinalLineBreak) {
  858. Node doc = Load(ex8_4);
  859. EXPECT_EQ(3, doc.size());
  860. EXPECT_EQ("text", doc["strip"].as<std::string>());
  861. EXPECT_EQ("text\n", doc["clip"].as<std::string>());
  862. EXPECT_EQ("text\n", doc["keep"].as<std::string>());
  863. }
  864. TEST(NodeSpecTest, DISABLED_Ex8_5_ChompingTrailingLines) {
  865. Node doc = Load(ex8_5);
  866. EXPECT_EQ(3, doc.size());
  867. EXPECT_EQ("# text", doc["strip"].as<std::string>());
  868. EXPECT_EQ("# text\n", doc["clip"].as<std::string>());
  869. // NOTE: I believe this is a bug in the YAML spec -
  870. // it should be "# text\n\n"
  871. EXPECT_EQ("# text\n", doc["keep"].as<std::string>());
  872. }
  873. TEST(NodeSpecTest, Ex8_6_EmptyScalarChomping) {
  874. Node doc = Load(ex8_6);
  875. EXPECT_EQ(3, doc.size());
  876. EXPECT_EQ("", doc["strip"].as<std::string>());
  877. EXPECT_EQ("", doc["clip"].as<std::string>());
  878. EXPECT_EQ("\n", doc["keep"].as<std::string>());
  879. }
  880. TEST(NodeSpecTest, Ex8_7_LiteralScalar) {
  881. Node doc = Load(ex8_7);
  882. EXPECT_EQ("literal\n\ttext\n", doc.as<std::string>());
  883. }
  884. TEST(NodeSpecTest, Ex8_8_LiteralContent) {
  885. Node doc = Load(ex8_8);
  886. EXPECT_EQ("\n\nliteral\n \n\ntext\n", doc.as<std::string>());
  887. }
  888. TEST(NodeSpecTest, Ex8_9_FoldedScalar) {
  889. Node doc = Load(ex8_9);
  890. EXPECT_EQ("folded text\n", doc.as<std::string>());
  891. }
  892. TEST(NodeSpecTest, Ex8_10_FoldedLines) {
  893. Node doc = Load(ex8_10);
  894. EXPECT_TRUE(doc.as<std::string>() ==
  895. "\nfolded line\nnext line\n * bullet\n\n * list\n * "
  896. "lines\n\nlast line\n");
  897. }
  898. TEST(NodeSpecTest, Ex8_11_MoreIndentedLines) {
  899. Node doc = Load(ex8_11);
  900. EXPECT_TRUE(doc.as<std::string>() ==
  901. "\nfolded line\nnext line\n * bullet\n\n * list\n * "
  902. "lines\n\nlast line\n");
  903. }
  904. TEST(NodeSpecTest, Ex8_12_EmptySeparationLines) {
  905. Node doc = Load(ex8_12);
  906. EXPECT_TRUE(doc.as<std::string>() ==
  907. "\nfolded line\nnext line\n * bullet\n\n * list\n * "
  908. "lines\n\nlast line\n");
  909. }
  910. TEST(NodeSpecTest, Ex8_13_FinalEmptyLines) {
  911. Node doc = Load(ex8_13);
  912. EXPECT_TRUE(doc.as<std::string>() ==
  913. "\nfolded line\nnext line\n * bullet\n\n * list\n * "
  914. "lines\n\nlast line\n");
  915. }
  916. TEST(NodeSpecTest, Ex8_14_BlockSequence) {
  917. Node doc = Load(ex8_14);
  918. EXPECT_EQ(1, doc.size());
  919. EXPECT_EQ(2, doc["block sequence"].size());
  920. EXPECT_EQ("one", doc["block sequence"][0].as<std::string>());
  921. EXPECT_EQ(1, doc["block sequence"][1].size());
  922. EXPECT_EQ("three", doc["block sequence"][1]["two"].as<std::string>());
  923. }
  924. TEST(NodeSpecTest, Ex8_15_BlockSequenceEntryTypes) {
  925. Node doc = Load(ex8_15);
  926. EXPECT_EQ(4, doc.size());
  927. EXPECT_TRUE(doc[0].IsNull());
  928. EXPECT_EQ("block node\n", doc[1].as<std::string>());
  929. EXPECT_EQ(2, doc[2].size());
  930. EXPECT_EQ("one", doc[2][0].as<std::string>());
  931. EXPECT_EQ("two", doc[2][1].as<std::string>());
  932. EXPECT_EQ(1, doc[3].size());
  933. EXPECT_EQ("two", doc[3]["one"].as<std::string>());
  934. }
  935. TEST(NodeSpecTest, Ex8_16_BlockMappings) {
  936. Node doc = Load(ex8_16);
  937. EXPECT_EQ(1, doc.size());
  938. EXPECT_EQ(1, doc["block mapping"].size());
  939. EXPECT_EQ("value", doc["block mapping"]["key"].as<std::string>());
  940. }
  941. TEST(NodeSpecTest, Ex8_17_ExplicitBlockMappingEntries) {
  942. Node doc = Load(ex8_17);
  943. EXPECT_EQ(2, doc.size());
  944. EXPECT_TRUE(doc["explicit key"].IsNull());
  945. EXPECT_EQ(2, doc["block key\n"].size());
  946. EXPECT_EQ("one", doc["block key\n"][0].as<std::string>());
  947. EXPECT_EQ("two", doc["block key\n"][1].as<std::string>());
  948. }
  949. TEST(NodeSpecTest, Ex8_18_ImplicitBlockMappingEntries) {
  950. Node doc = Load(ex8_18);
  951. EXPECT_EQ(3, doc.size());
  952. EXPECT_EQ("in-line value", doc["plain key"].as<std::string>());
  953. EXPECT_TRUE(doc[Null].IsNull());
  954. EXPECT_EQ(1, doc["quoted key"].size());
  955. EXPECT_EQ("entry", doc["quoted key"][0].as<std::string>());
  956. }
  957. TEST(NodeSpecTest, Ex8_19_CompactBlockMappings) {
  958. Node doc = Load(ex8_19);
  959. EXPECT_EQ(2, doc.size());
  960. EXPECT_EQ(1, doc[0].size());
  961. EXPECT_EQ("yellow", doc[0]["sun"].as<std::string>());
  962. EXPECT_EQ(1, doc[1].size());
  963. std::map<std::string, std::string> key;
  964. key["earth"] = "blue";
  965. EXPECT_EQ(1, doc[1][key].size());
  966. EXPECT_EQ("white", doc[1][key]["moon"].as<std::string>());
  967. }
  968. TEST(NodeSpecTest, Ex8_20_BlockNodeTypes) {
  969. Node doc = Load(ex8_20);
  970. EXPECT_EQ(3, doc.size());
  971. EXPECT_EQ("flow in block", doc[0].as<std::string>());
  972. EXPECT_EQ("Block scalar\n", doc[1].as<std::string>());
  973. EXPECT_EQ(1, doc[2].size());
  974. EXPECT_EQ("bar", doc[2]["foo"].as<std::string>());
  975. }
  976. TEST(NodeSpecTest, DISABLED_Ex8_21_BlockScalarNodes) {
  977. Node doc = Load(ex8_21);
  978. EXPECT_EQ(2, doc.size());
  979. // NOTE: I believe this is a bug in the YAML spec -
  980. // it should be "value\n"
  981. EXPECT_EQ("value", doc["literal"].as<std::string>());
  982. EXPECT_EQ("value", doc["folded"].as<std::string>());
  983. EXPECT_EQ("!foo", doc["folded"].Tag());
  984. }
  985. TEST(NodeSpecTest, Ex8_22_BlockCollectionNodes) {
  986. Node doc = Load(ex8_22);
  987. EXPECT_EQ(2, doc.size());
  988. EXPECT_EQ(2, doc["sequence"].size());
  989. EXPECT_EQ("entry", doc["sequence"][0].as<std::string>());
  990. EXPECT_EQ(1, doc["sequence"][1].size());
  991. EXPECT_EQ("nested", doc["sequence"][1][0].as<std::string>());
  992. EXPECT_EQ(1, doc["mapping"].size());
  993. EXPECT_EQ("bar", doc["mapping"]["foo"].as<std::string>());
  994. }
  995. TEST(NodeSpecTest, FlowMapNotClosed) {
  996. EXPECT_THROW_PARSER_EXCEPTION(Load("{x:"), ErrorMsg::UNKNOWN_TOKEN);
  997. }
  998. }
  999. }