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.

пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2014 Ben Kurtovic <ben.kurtovic@gmail.com>
  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. try:
  24. import unittest2 as unittest
  25. except ImportError:
  26. import unittest
  27. from mwparserfromhell.compat import py3k
  28. from mwparserfromhell.parser import tokens
  29. class TestTokens(unittest.TestCase):
  30. """Test cases for the Token class and its subclasses."""
  31. def test_issubclass(self):
  32. """check that all classes within the tokens module are really Tokens"""
  33. for name in tokens.__all__:
  34. klass = getattr(tokens, name)
  35. self.assertTrue(issubclass(klass, tokens.Token))
  36. self.assertIsInstance(klass(), klass)
  37. self.assertIsInstance(klass(), tokens.Token)
  38. def test_attributes(self):
  39. """check that Token attributes can be managed properly"""
  40. token1 = tokens.Token()
  41. token2 = tokens.Token(foo="bar", baz=123)
  42. self.assertEqual("bar", token2.foo)
  43. self.assertEqual(123, token2.baz)
  44. self.assertFalse(token1.foo)
  45. self.assertFalse(token2.bar)
  46. token1.spam = "eggs"
  47. token2.foo = "ham"
  48. del token2.baz
  49. self.assertEqual("eggs", token1.spam)
  50. self.assertEqual("ham", token2.foo)
  51. self.assertFalse(token2.baz)
  52. self.assertRaises(KeyError, delattr, token2, "baz")
  53. def test_repr(self):
  54. """check that repr() on a Token works as expected"""
  55. token1 = tokens.Token()
  56. token2 = tokens.Token(foo="bar", baz=123)
  57. token3 = tokens.Text(text="earwig" * 100)
  58. hundredchars = ("earwig" * 100)[:97] + "..."
  59. self.assertEqual("Token()", repr(token1))
  60. if py3k:
  61. token2repr1 = "Token(foo='bar', baz=123)"
  62. token2repr2 = "Token(baz=123, foo='bar')"
  63. token3repr = "Text(text='" + hundredchars + "')"
  64. else:
  65. token2repr1 = "Token(foo=u'bar', baz=123)"
  66. token2repr2 = "Token(baz=123, foo=u'bar')"
  67. token3repr = "Text(text=u'" + hundredchars + "')"
  68. token2repr = repr(token2)
  69. self.assertTrue(token2repr == token2repr1 or token2repr == token2repr2)
  70. self.assertEqual(token3repr, repr(token3))
  71. def test_equality(self):
  72. """check that equivalent tokens are considered equal"""
  73. token1 = tokens.Token()
  74. token2 = tokens.Token()
  75. token3 = tokens.Token(foo="bar", baz=123)
  76. token4 = tokens.Text(text="asdf")
  77. token5 = tokens.Text(text="asdf")
  78. token6 = tokens.TemplateOpen(text="asdf")
  79. self.assertEqual(token1, token2)
  80. self.assertEqual(token2, token1)
  81. self.assertEqual(token4, token5)
  82. self.assertEqual(token5, token4)
  83. self.assertNotEqual(token1, token3)
  84. self.assertNotEqual(token2, token3)
  85. self.assertNotEqual(token4, token6)
  86. self.assertNotEqual(token5, token6)
  87. def test_repr_equality(self):
  88. "check that eval(repr(token)) == token"
  89. tests = [
  90. tokens.Token(),
  91. tokens.Token(foo="bar", baz=123),
  92. tokens.Text(text="earwig")
  93. ]
  94. for token in tests:
  95. self.assertEqual(token, eval(repr(token), vars(tokens)))
  96. if __name__ == "__main__":
  97. unittest.main(verbosity=2)