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.
 
 
 
 

97 regels
3.7 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 unittest
  21. from mwparserfromhell.parser import tokens
  22. class TestTokens(unittest.TestCase):
  23. """Test cases for the Token class and its subclasses."""
  24. def test_issubclass(self):
  25. """check that all classes within the tokens module are really Tokens"""
  26. for name in tokens.__all__:
  27. klass = getattr(tokens, name)
  28. self.assertTrue(issubclass(klass, tokens.Token))
  29. self.assertIsInstance(klass(), klass)
  30. self.assertIsInstance(klass(), tokens.Token)
  31. def test_attributes(self):
  32. """check that Token attributes can be managed properly"""
  33. token1 = tokens.Token()
  34. token2 = tokens.Token(foo="bar", baz=123)
  35. self.assertEqual("bar", token2.foo)
  36. self.assertEqual(123, token2.baz)
  37. self.assertFalse(token1.foo)
  38. self.assertFalse(token2.bar)
  39. token1.spam = "eggs"
  40. token2.foo = "ham"
  41. del token2.baz
  42. self.assertEqual("eggs", token1.spam)
  43. self.assertEqual("ham", token2.foo)
  44. self.assertFalse(token2.baz)
  45. self.assertRaises(KeyError, delattr, token2, "baz")
  46. def test_repr(self):
  47. """check that repr() on a Token works as expected"""
  48. token1 = tokens.Token()
  49. token2 = tokens.Token(foo="bar", baz=123)
  50. token3 = tokens.Text(text="earwig" * 100)
  51. hundredchars = ("earwig" * 100)[:97] + "..."
  52. self.assertEqual("Token()", repr(token1))
  53. self.assertTrue(repr(token2) in (
  54. "Token(foo='bar', baz=123)", "Token(baz=123, foo='bar')"))
  55. self.assertEqual("Text(text='" + hundredchars + "')", repr(token3))
  56. def test_equality(self):
  57. """check that equivalent tokens are considered equal"""
  58. token1 = tokens.Token()
  59. token2 = tokens.Token()
  60. token3 = tokens.Token(foo="bar", baz=123)
  61. token4 = tokens.Text(text="asdf")
  62. token5 = tokens.Text(text="asdf")
  63. token6 = tokens.TemplateOpen(text="asdf")
  64. self.assertEqual(token1, token2)
  65. self.assertEqual(token2, token1)
  66. self.assertEqual(token4, token5)
  67. self.assertEqual(token5, token4)
  68. self.assertNotEqual(token1, token3)
  69. self.assertNotEqual(token2, token3)
  70. self.assertNotEqual(token4, token6)
  71. self.assertNotEqual(token5, token6)
  72. def test_repr_equality(self):
  73. "check that eval(repr(token)) == token"
  74. tests = [
  75. tokens.Token(),
  76. tokens.Token(foo="bar", baz=123),
  77. tokens.Text(text="earwig")
  78. ]
  79. for token in tests:
  80. self.assertEqual(token, eval(repr(token), vars(tokens)))
  81. if __name__ == "__main__":
  82. unittest.main(verbosity=2)