A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_tokens.py 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. import unittest
  24. from mwparserfromhell.compat import py3k
  25. from mwparserfromhell.parser import tokens
  26. class TestTokens(unittest.TestCase):
  27. """Test cases for the Token class and its subclasses."""
  28. def test_issubclass(self):
  29. """check that all classes within the tokens module are really Tokens"""
  30. for name in tokens.__all__:
  31. klass = getattr(tokens, name)
  32. self.assertTrue(issubclass(klass, tokens.Token))
  33. self.assertIsInstance(klass(), klass)
  34. self.assertIsInstance(klass(), tokens.Token)
  35. def test_attributes(self):
  36. """check that Token attributes can be managed properly"""
  37. token1 = tokens.Token()
  38. token2 = tokens.Token(foo="bar", baz=123)
  39. self.assertEqual("bar", token2.foo)
  40. self.assertEqual(123, token2.baz)
  41. self.assertRaises(KeyError, lambda: token1.foo)
  42. self.assertRaises(KeyError, lambda: token2.bar)
  43. token1.spam = "eggs"
  44. token2.foo = "ham"
  45. del token2.baz
  46. self.assertEqual("eggs", token1.spam)
  47. self.assertEqual("ham", token2.foo)
  48. self.assertRaises(KeyError, lambda: token2.baz)
  49. self.assertRaises(KeyError, delattr, token2, "baz")
  50. def test_repr(self):
  51. """check that repr() on a Token works as expected"""
  52. token1 = tokens.Token()
  53. token2 = tokens.Token(foo="bar", baz=123)
  54. token3 = tokens.Text(text="earwig" * 100)
  55. hundredchars = ("earwig" * 100)[:97] + "..."
  56. self.assertEqual("Token()", repr(token1))
  57. if py3k:
  58. token2repr = "Token(foo='bar', baz=123)"
  59. token3repr = "Text(text='" + hundredchars + "')"
  60. else:
  61. token2repr = "Token(foo=u'bar', baz=123)"
  62. token3repr = "Text(text=u'" + hundredchars + "')"
  63. self.assertEqual(token2repr, repr(token2))
  64. self.assertEqual(token3repr, repr(token3))
  65. def test_equality(self):
  66. """check that equivalent tokens are considered equal"""
  67. token1 = tokens.Token()
  68. token2 = tokens.Token()
  69. token3 = tokens.Token(foo="bar", baz=123)
  70. token4 = tokens.Text(text="asdf")
  71. token5 = tokens.Text(text="asdf")
  72. token6 = tokens.TemplateOpen(text="asdf")
  73. self.assertEqual(token1, token2)
  74. self.assertEqual(token2, token1)
  75. self.assertEqual(token4, token5)
  76. self.assertEqual(token5, token4)
  77. self.assertNotEquals(token1, token3)
  78. self.assertNotEquals(token2, token3)
  79. self.assertNotEquals(token4, token6)
  80. self.assertNotEquals(token5, token6)
  81. def test_repr_equality(self):
  82. "check that eval(repr(token)) == token"
  83. tests = [
  84. tokens.Token(),
  85. tokens.Token(foo="bar", baz=123),
  86. tokens.Text(text="earwig")
  87. ]
  88. for token in tests:
  89. self.assertEqual(token, eval(repr(token), vars(tokens)))
  90. if __name__ == "__main__":
  91. unittest.main(verbosity=2)