cmdparse_test.py 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright (C) 2009 Internet Systems Consortium.
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM
  8. # DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  9. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  10. # INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  12. # FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. # NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  14. # WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. import unittest
  16. from bindctl import cmdparse
  17. class TestCmdParse(unittest.TestCase):
  18. def test_remove_unquoted_whitespace(self):
  19. self.assertEqual(cmdparse._remove_unquoted_whitespace("a"), "a")
  20. self.assertEqual(cmdparse._remove_unquoted_whitespace(" a"), "a")
  21. self.assertEqual(cmdparse._remove_unquoted_whitespace("a "), "a")
  22. self.assertEqual(cmdparse._remove_unquoted_whitespace(" a "), "a")
  23. self.assertNotEqual(cmdparse._remove_unquoted_whitespace("a"), "a ")
  24. self.assertNotEqual(cmdparse._remove_unquoted_whitespace(" a"), " a")
  25. self.assertNotEqual(cmdparse._remove_unquoted_whitespace("a "), "a ")
  26. self.assertNotEqual(cmdparse._remove_unquoted_whitespace(" a "), " a ")
  27. self.assertNotEqual(cmdparse._remove_unquoted_whitespace(" a "), "b")
  28. self.assertEqual(cmdparse._remove_unquoted_whitespace("\"abc\""), "\"abc\"")
  29. self.assertEqual(cmdparse._remove_unquoted_whitespace(" \"abc\""), "\"abc\"")
  30. self.assertEqual(cmdparse._remove_unquoted_whitespace("\"abc\" "), "\"abc\"")
  31. self.assertEqual(cmdparse._remove_unquoted_whitespace(" \"abc\" "), "\"abc\"")
  32. self.assertEqual(cmdparse._remove_unquoted_whitespace("\" abc\""), "\" abc\"")
  33. self.assertEqual(cmdparse._remove_unquoted_whitespace(" \"a bc\""), "\"a bc\"")
  34. self.assertEqual(cmdparse._remove_unquoted_whitespace("\"ab c\" "), "\"ab c\"")
  35. self.assertEqual(cmdparse._remove_unquoted_whitespace(" \"abc \" "), "\"abc \"")
  36. self.assertEqual(cmdparse._remove_unquoted_whitespace(" \" a b c \" "), "\" a b c \"")
  37. self.assertEqual(cmdparse._remove_unquoted_whitespace("a\" abc\"a"), "a\" abc\"a")
  38. self.assertEqual(cmdparse._remove_unquoted_whitespace("a \"a bc\"a"), "a\"a bc\"a")
  39. self.assertEqual(cmdparse._remove_unquoted_whitespace("a\"ab c\" a"), "a\"ab c\"a")
  40. self.assertEqual(cmdparse._remove_unquoted_whitespace("a \"abc \" a"), "a\"abc \"a")
  41. self.assertEqual(cmdparse._remove_unquoted_whitespace("a \" a b c \" a"), "a\" a b c \"a")
  42. # short-hand function to make the set of tests more readable
  43. def rws(self, a, b):
  44. self.assertEqual(cmdparse._remove_list_and_map_whitespace(a), b)
  45. def test_remove_list_whitespace(self):
  46. self.rws("a", "a")
  47. self.rws(" a ", " a ")
  48. self.rws(" [a] ", " [a] ")
  49. self.rws(" [ a] ", " [a] ")
  50. self.rws(" [ a ] ", " [a] ")
  51. self.rws(" [ a b c ] ", " [abc] ")
  52. self.rws(" [ a \"b c\" ] ", " [a\"b c\"] ")
  53. self.rws("a [ a \"b c\" ] a", "a [a\"b c\"] a")
  54. self.rws("a] [ a \"b c\" ] a", "a] [a\"b c\"] a")
  55. self.rws(" [ a [b c] ] ", " [a[bc]] ")
  56. self.rws(" [ a b][ c d ] ", " [ab][cd] ")
  57. self.rws(" [ a b] [ c d ] ", " [ab] [cd] ")
  58. self.rws("a", "a")
  59. self.rws(" a ", " a ")
  60. self.rws(" {a} ", " {a} ")
  61. self.rws(" { a} ", " {a} ")
  62. self.rws(" { a } ", " {a} ")
  63. self.rws(" { a b c } ", " {abc} ")
  64. self.rws(" { a \"b c\" } ", " {a\"b c\"} ")
  65. self.rws("a { a \"b c\" } a", "a {a\"b c\"} a")
  66. self.rws("a} { a \"b c\" } a", "a} {a\"b c\"} a")
  67. self.rws(" { a {b c} } ", " {a{bc}} ")
  68. self.rws(" { a b}{ c d } ", " {ab}{cd} ")
  69. self.rws(" { a b} { c d } ", " {ab} {cd} ")
  70. self.rws(" [ a b]{ c d } ", " [ab]{cd} ")
  71. self.rws(" [ a b{ c d }] ", " [ab{cd}] ")
  72. self.rws(" [ a b{ \"c d\" }] ", " [ab{\"c d\"}] ")
  73. if __name__== "__main__":
  74. unittest.main()