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.
 
 
 
 

100 line
3.3 KiB

  1. # Copyright (C) 2012-2025 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 Token class and its subclasses.
  22. """
  23. from __future__ import annotations
  24. import pytest
  25. from mwparserfromhell.parser import tokens
  26. @pytest.mark.parametrize("name", tokens.__all__)
  27. def test_issubclass(name):
  28. """check that all classes within the tokens module are really Tokens"""
  29. klass = getattr(tokens, name)
  30. assert issubclass(klass, tokens.Token) is True
  31. assert isinstance(klass(), klass)
  32. assert isinstance(klass(), tokens.Token)
  33. def test_attributes():
  34. """check that Token attributes can be managed properly"""
  35. token1 = tokens.Token()
  36. token2 = tokens.Token(foo="bar", baz=123)
  37. assert "bar" == token2.foo
  38. assert 123 == token2.baz
  39. assert token1.foo is None
  40. assert token2.bar is None
  41. token1.spam = "eggs"
  42. token2.foo = "ham"
  43. del token2.baz
  44. assert "eggs" == token1.spam
  45. assert "ham" == token2.foo
  46. assert token2.baz is None
  47. with pytest.raises(KeyError):
  48. token2.__delattr__("baz")
  49. def test_repr():
  50. """check that repr() on a Token works as expected"""
  51. token1 = tokens.Token()
  52. token2 = tokens.Token(foo="bar", baz=123)
  53. token3 = tokens.Text(text="earwig" * 100)
  54. hundredchars = ("earwig" * 100)[:97] + "..."
  55. assert "Token()" == repr(token1)
  56. assert repr(token2) in ("Token(foo='bar', baz=123)", "Token(baz=123, foo='bar')")
  57. assert "Text(text='" + hundredchars + "')" == repr(token3)
  58. def test_equality():
  59. """check that equivalent tokens are considered equal"""
  60. token1 = tokens.Token()
  61. token2 = tokens.Token()
  62. token3 = tokens.Token(foo="bar", baz=123)
  63. token4 = tokens.Text(text="asdf")
  64. token5 = tokens.Text(text="asdf")
  65. token6 = tokens.TemplateOpen(text="asdf")
  66. assert token1 == token2
  67. assert token2 == token1
  68. assert token4 == token5
  69. assert token5 == token4
  70. assert token1 != token3
  71. assert token2 != token3
  72. assert token4 != token6
  73. assert token5 != token6
  74. @pytest.mark.parametrize(
  75. "token",
  76. [tokens.Token(), tokens.Token(foo="bar", baz=123), tokens.Text(text="earwig")],
  77. )
  78. def test_repr_equality(token):
  79. """check that eval(repr(token)) == token"""
  80. assert token == eval(repr(token), vars(tokens))