A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
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.
 
 
 
 

115 line
3.6 KiB

  1. # Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. """
  21. Test cases for the Argument node.
  22. """
  23. import pytest
  24. from mwparserfromhell.nodes import Argument, Text
  25. from .conftest import assert_wikicode_equal, wrap, wraptext
  26. def test_str():
  27. """test Argument.__str__()"""
  28. node = Argument(wraptext("foobar"))
  29. assert "{{{foobar}}}" == str(node)
  30. node2 = Argument(wraptext("foo"), wraptext("bar"))
  31. assert "{{{foo|bar}}}" == str(node2)
  32. def test_children():
  33. """test Argument.__children__()"""
  34. node1 = Argument(wraptext("foobar"))
  35. node2 = Argument(wraptext("foo"), wrap([Text("bar"), Text("baz")]))
  36. gen1 = node1.__children__()
  37. gen2 = node2.__children__()
  38. assert node1.name is next(gen1)
  39. assert node2.name is next(gen2)
  40. assert node2.default is next(gen2)
  41. with pytest.raises(StopIteration):
  42. next(gen1)
  43. with pytest.raises(StopIteration):
  44. next(gen2)
  45. def test_strip():
  46. """test Argument.__strip__()"""
  47. node1 = Argument(wraptext("foobar"))
  48. node2 = Argument(wraptext("foo"), wraptext("bar"))
  49. assert node1.__strip__() is None
  50. assert "bar" == node2.__strip__()
  51. def test_showtree():
  52. """test Argument.__showtree__()"""
  53. output = []
  54. getter, marker = object(), object()
  55. def get(code):
  56. return output.append((getter, code))
  57. def mark():
  58. return output.append(marker)
  59. node1 = Argument(wraptext("foobar"))
  60. node2 = Argument(wraptext("foo"), wraptext("bar"))
  61. node1.__showtree__(output.append, get, mark)
  62. node2.__showtree__(output.append, get, mark)
  63. valid = [
  64. "{{{",
  65. (getter, node1.name),
  66. "}}}",
  67. "{{{",
  68. (getter, node2.name),
  69. " | ",
  70. marker,
  71. (getter, node2.default),
  72. "}}}",
  73. ]
  74. assert valid == output
  75. def test_name():
  76. """test getter/setter for the name attribute"""
  77. name = wraptext("foobar")
  78. node1 = Argument(name)
  79. node2 = Argument(name, wraptext("baz"))
  80. assert name is node1.name
  81. assert name is node2.name
  82. node1.name = "héhehé"
  83. node2.name = "héhehé"
  84. assert_wikicode_equal(wraptext("héhehé"), node1.name)
  85. assert_wikicode_equal(wraptext("héhehé"), node2.name)
  86. def test_default():
  87. """test getter/setter for the default attribute"""
  88. default = wraptext("baz")
  89. node1 = Argument(wraptext("foobar"))
  90. node2 = Argument(wraptext("foobar"), default)
  91. assert None is node1.default
  92. assert default is node2.default
  93. node1.default = "buzz"
  94. node2.default = None
  95. assert_wikicode_equal(wraptext("buzz"), node1.default)
  96. assert None is node2.default