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 година
пре 11 година
пре 11 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 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. 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.assertFalse(token1.foo)
  42. self.assertFalse(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.assertFalse(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. token2repr1 = "Token(foo='bar', baz=123)"
  59. token2repr2 = "Token(baz=123, foo='bar')"
  60. token3repr = "Text(text='" + hundredchars + "')"
  61. else:
  62. token2repr1 = "Token(foo=u'bar', baz=123)"
  63. token2repr2 = "Token(baz=123, foo=u'bar')"
  64. token3repr = "Text(text=u'" + hundredchars + "')"
  65. token2repr = repr(token2)
  66. self.assertTrue(token2repr == token2repr1 or token2repr == token2repr2)
  67. self.assertEqual(token3repr, repr(token3))
  68. def test_equality(self):
  69. """check that equivalent tokens are considered equal"""
  70. token1 = tokens.Token()
  71. token2 = tokens.Token()
  72. token3 = tokens.Token(foo="bar", baz=123)
  73. token4 = tokens.Text(text="asdf")
  74. token5 = tokens.Text(text="asdf")
  75. token6 = tokens.TemplateOpen(text="asdf")
  76. self.assertEqual(token1, token2)
  77. self.assertEqual(token2, token1)
  78. self.assertEqual(token4, token5)
  79. self.assertEqual(token5, token4)
  80. self.assertNotEqual(token1, token3)
  81. self.assertNotEqual(token2, token3)
  82. self.assertNotEqual(token4, token6)
  83. self.assertNotEqual(token5, token6)
  84. def test_repr_equality(self):
  85. "check that eval(repr(token)) == token"
  86. tests = [
  87. tokens.Token(),
  88. tokens.Token(foo="bar", baz=123),
  89. tokens.Text(text="earwig")
  90. ]
  91. for token in tests:
  92. self.assertEqual(token, eval(repr(token), vars(tokens)))
  93. if __name__ == "__main__":
  94. unittest.main(verbosity=2)