/srsly/tests/ruamel_yaml/test_version.py

https://github.com/explosion/srsly · Python · 175 lines · 105 code · 18 blank · 52 comment · 5 complexity · 305ac43795bec783c3a5a59a4fcf6fcd MD5 · raw file

  1. # coding: utf-8
  2. import pytest # NOQA
  3. from .roundtrip import dedent, round_trip, round_trip_load
  4. def load(s, version=None):
  5. import srsly.ruamel_yaml # NOQA
  6. return srsly.ruamel_yaml.round_trip_load(dedent(s), version)
  7. class TestVersions:
  8. def test_explicit_1_2(self):
  9. r = load(
  10. """\
  11. %YAML 1.2
  12. ---
  13. - 12:34:56
  14. - 012
  15. - 012345678
  16. - 0o12
  17. - on
  18. - off
  19. - yes
  20. - no
  21. - true
  22. """
  23. )
  24. assert r[0] == "12:34:56"
  25. assert r[1] == 12
  26. assert r[2] == 12345678
  27. assert r[3] == 10
  28. assert r[4] == "on"
  29. assert r[5] == "off"
  30. assert r[6] == "yes"
  31. assert r[7] == "no"
  32. assert r[8] is True
  33. def test_explicit_1_1(self):
  34. r = load(
  35. """\
  36. %YAML 1.1
  37. ---
  38. - 12:34:56
  39. - 012
  40. - 012345678
  41. - 0o12
  42. - on
  43. - off
  44. - yes
  45. - no
  46. - true
  47. """
  48. )
  49. assert r[0] == 45296
  50. assert r[1] == 10
  51. assert r[2] == "012345678"
  52. assert r[3] == "0o12"
  53. assert r[4] is True
  54. assert r[5] is False
  55. assert r[6] is True
  56. assert r[7] is False
  57. assert r[8] is True
  58. def test_implicit_1_2(self):
  59. r = load(
  60. """\
  61. - 12:34:56
  62. - 12:34:56.78
  63. - 012
  64. - 012345678
  65. - 0o12
  66. - on
  67. - off
  68. - yes
  69. - no
  70. - true
  71. """
  72. )
  73. assert r[0] == "12:34:56"
  74. assert r[1] == "12:34:56.78"
  75. assert r[2] == 12
  76. assert r[3] == 12345678
  77. assert r[4] == 10
  78. assert r[5] == "on"
  79. assert r[6] == "off"
  80. assert r[7] == "yes"
  81. assert r[8] == "no"
  82. assert r[9] is True
  83. def test_load_version_1_1(self):
  84. inp = """\
  85. - 12:34:56
  86. - 12:34:56.78
  87. - 012
  88. - 012345678
  89. - 0o12
  90. - on
  91. - off
  92. - yes
  93. - no
  94. - true
  95. """
  96. r = load(inp, version="1.1")
  97. assert r[0] == 45296
  98. assert r[1] == 45296.78
  99. assert r[2] == 10
  100. assert r[3] == "012345678"
  101. assert r[4] == "0o12"
  102. assert r[5] is True
  103. assert r[6] is False
  104. assert r[7] is True
  105. assert r[8] is False
  106. assert r[9] is True
  107. class TestIssue62:
  108. # bitbucket issue 62, issue_62
  109. def test_00(self):
  110. import srsly.ruamel_yaml # NOQA
  111. s = dedent(
  112. """\
  113. {}# Outside flow collection:
  114. - ::vector
  115. - ": - ()"
  116. - Up, up, and away!
  117. - -123
  118. - http://example.com/foo#bar
  119. # Inside flow collection:
  120. - [::vector, ": - ()", "Down, down and away!", -456, http://example.com/foo#bar]
  121. """
  122. )
  123. with pytest.raises(srsly.ruamel_yaml.parser.ParserError):
  124. round_trip(s.format("%YAML 1.1\n---\n"), preserve_quotes=True)
  125. round_trip(s.format(""), preserve_quotes=True)
  126. def test_00_single_comment(self):
  127. import srsly.ruamel_yaml # NOQA
  128. s = dedent(
  129. """\
  130. {}# Outside flow collection:
  131. - ::vector
  132. - ": - ()"
  133. - Up, up, and away!
  134. - -123
  135. - http://example.com/foo#bar
  136. - [::vector, ": - ()", "Down, down and away!", -456, http://example.com/foo#bar]
  137. """
  138. )
  139. with pytest.raises(srsly.ruamel_yaml.parser.ParserError):
  140. round_trip(s.format("%YAML 1.1\n---\n"), preserve_quotes=True)
  141. round_trip(s.format(""), preserve_quotes=True)
  142. # round_trip(s.format('%YAML 1.2\n---\n'), preserve_quotes=True, version=(1, 2))
  143. def test_01(self):
  144. import srsly.ruamel_yaml # NOQA
  145. s = dedent(
  146. """\
  147. {}[random plain value that contains a ? character]
  148. """
  149. )
  150. with pytest.raises(srsly.ruamel_yaml.parser.ParserError):
  151. round_trip(s.format("%YAML 1.1\n---\n"), preserve_quotes=True)
  152. round_trip(s.format(""), preserve_quotes=True)
  153. # note the flow seq on the --- line!
  154. round_trip(s.format("%YAML 1.2\n--- "), preserve_quotes=True, version="1.2")
  155. def test_so_45681626(self):
  156. # was not properly parsing
  157. round_trip_load('{"in":{},"out":{}}')