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.
 
 
 
 

96 lines
3.8 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. import pytest
  21. from mwparserfromhell.nodes import Argument, Text
  22. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  23. class TestArgument(TreeEqualityTestCase):
  24. """Test cases for the Argument node."""
  25. def test_str(self):
  26. """test Argument.__str__()"""
  27. node = Argument(wraptext("foobar"))
  28. assert "{{{foobar}}}" == str(node)
  29. node2 = Argument(wraptext("foo"), wraptext("bar"))
  30. assert "{{{foo|bar}}}" == str(node2)
  31. def test_children(self):
  32. """test Argument.__children__()"""
  33. node1 = Argument(wraptext("foobar"))
  34. node2 = Argument(wraptext("foo"), wrap([Text("bar"), Text("baz")]))
  35. gen1 = node1.__children__()
  36. gen2 = node2.__children__()
  37. assert node1.name is next(gen1)
  38. assert node2.name is next(gen2)
  39. assert node2.default is next(gen2)
  40. with pytest.raises(StopIteration):
  41. next(gen1)
  42. with pytest.raises(StopIteration):
  43. next(gen2)
  44. def test_strip(self):
  45. """test Argument.__strip__()"""
  46. node1 = Argument(wraptext("foobar"))
  47. node2 = Argument(wraptext("foo"), wraptext("bar"))
  48. assert node1.__strip__() is None
  49. assert "bar" == node2.__strip__()
  50. def test_showtree(self):
  51. """test Argument.__showtree__()"""
  52. output = []
  53. getter, marker = object(), object()
  54. get = lambda code: output.append((getter, code))
  55. mark = lambda: output.append(marker)
  56. node1 = Argument(wraptext("foobar"))
  57. node2 = Argument(wraptext("foo"), wraptext("bar"))
  58. node1.__showtree__(output.append, get, mark)
  59. node2.__showtree__(output.append, get, mark)
  60. valid = [
  61. "{{{", (getter, node1.name), "}}}", "{{{", (getter, node2.name),
  62. " | ", marker, (getter, node2.default), "}}}"]
  63. assert valid == output
  64. def test_name(self):
  65. """test getter/setter for the name attribute"""
  66. name = wraptext("foobar")
  67. node1 = Argument(name)
  68. node2 = Argument(name, wraptext("baz"))
  69. assert name is node1.name
  70. assert name is node2.name
  71. node1.name = "héhehé"
  72. node2.name = "héhehé"
  73. self.assertWikicodeEqual(wraptext("héhehé"), node1.name)
  74. self.assertWikicodeEqual(wraptext("héhehé"), node2.name)
  75. def test_default(self):
  76. """test getter/setter for the default attribute"""
  77. default = wraptext("baz")
  78. node1 = Argument(wraptext("foobar"))
  79. node2 = Argument(wraptext("foobar"), default)
  80. assert None is node1.default
  81. assert default is node2.default
  82. node1.default = "buzz"
  83. node2.default = None
  84. self.assertWikicodeEqual(wraptext("buzz"), node1.default)
  85. assert None is node2.default