A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

101 rader
3.9 KiB

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