test_sexpr.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Flexlay - A Generic 2D Game Editor
  2. # Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import io
  17. import unittest
  18. from flexlay.util.sexpr import parse as sexpr_parse, SExprParseError
  19. from flexlay.util.sexpr_reader import get_value_from_tree, assoc_ref
  20. from flexlay.util.sexpr_writer import SExprWriter
  21. class SExprTestCase(unittest.TestCase):
  22. def setUp(self) -> None:
  23. pass
  24. def tearDown(self) -> None:
  25. pass
  26. def test_assoc_ref(self) -> None:
  27. lst = [["one", 1],
  28. ["two", 2],
  29. ["three", 3]]
  30. self.assertEqual(assoc_ref(lst, "one"), [1])
  31. self.assertEqual(assoc_ref(lst, "two"), [2])
  32. self.assertEqual(assoc_ref(lst, "three"), [3])
  33. def test_get_value_from_tree(self) -> None:
  34. sexpr = [["supertux-level",
  35. ["bool", False],
  36. ["sublist",
  37. ["int", 20]],
  38. ["int", 15]]]
  39. result = get_value_from_tree(["supertux-level", "bool", "_"], sexpr, None)
  40. self.assertEqual(result, False)
  41. result = get_value_from_tree(["supertux-level", "int", "_"], sexpr, None)
  42. self.assertEqual(result, 15)
  43. result = get_value_from_tree(["supertux-level", "sublist", "int", "_"], sexpr, None)
  44. self.assertEqual(result, 20)
  45. def test_sexpr_parser(self) -> None:
  46. result = sexpr_parse("")
  47. self.assertEqual(result, [])
  48. result = sexpr_parse(" ")
  49. self.assertEqual(result, [])
  50. result = sexpr_parse(" 123 ")
  51. self.assertEqual(result, [123])
  52. result = sexpr_parse("512323")
  53. self.assertEqual(result, [512323])
  54. result = sexpr_parse("(#t)")
  55. self.assertEqual(result, [[True]])
  56. result = sexpr_parse("(8(8)8)")
  57. self.assertEqual(result, [[8, [8], 8]])
  58. result = sexpr_parse('"\\n\\t\\"abc\\\\"')
  59. self.assertEqual(result, ["\n\t\"abc\\"])
  60. result = sexpr_parse("(_ \"Test\")")
  61. self.assertEqual(result, [["_", "Test"]])
  62. result = sexpr_parse("(_ \"Test\")")
  63. self.assertEqual(result, [["_", "Test"]])
  64. result = sexpr_parse("symbol")
  65. self.assertEqual(result, ["symbol"])
  66. result = sexpr_parse(r'(() ("bar" foo) ()) () bar ')
  67. self.assertEqual(result, [[[], ["bar", "foo"], []], [], "bar"])
  68. result = sexpr_parse((';;comment\n'
  69. '("Hello World" 5 1 123) ("Hello" 123 123 "foobar") ;; comment'))
  70. self.assertEqual(result, [["Hello World", 5, 1, 123], ["Hello", 123, 123, "foobar"]])
  71. with self.assertRaises(SExprParseError):
  72. sexpr_parse("(")
  73. with self.assertRaises(SExprParseError):
  74. sexpr_parse("# ")
  75. with self.assertRaises(SExprParseError):
  76. sexpr_parse("#b")
  77. with self.assertRaises(SExprParseError):
  78. sexpr_parse("\"unterminated string")
  79. def test_sexpr_writer(self) -> None:
  80. with io.StringIO() as out:
  81. writer = SExprWriter(out)
  82. writer.write_comment("This is a comment")
  83. writer.begin_list("supertux-level")
  84. writer.begin_list("empty-section")
  85. writer.end_list("empty-section")
  86. writer.begin_list("test-section")
  87. writer.write_bool("falsebool", False)
  88. writer.write_bool("falsebool", True)
  89. writer.write_int("intvalue", 45)
  90. writer.write_float("floatvalue", 45.0)
  91. writer.write_string("astring", "a string")
  92. writer.write_vector("astring", [1, 2, 3, 5, 10, "a string"])
  93. writer.end_list("test-section")
  94. writer.end_list("supertux-level")
  95. result = out.getvalue()
  96. expected = (";; This is a comment\n"
  97. "(supertux-level\n"
  98. " (empty-section\n"
  99. " )\n"
  100. " (test-section\n"
  101. " (falsebool #f)\n"
  102. " (falsebool #t)\n"
  103. " (intvalue 45)\n"
  104. " (floatvalue 45.0)\n"
  105. " (astring \"a string\")\n"
  106. " (astring 1 2 3 5 10 'a string')\n"
  107. " )\n"
  108. ")\n")
  109. self.assertEqual(result, expected)
  110. if __name__ == '__main__':
  111. unittest.main()
  112. # EOF #