PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/historical/filtertools_test.py

https://bitbucket.org/lindenlab/apiary/
Python | 103 lines | 75 code | 4 blank | 24 comment | 0 complexity | 97deb93950381d71aba8d85cb4e31ef5 MD5 | raw file
  1. #
  2. # $LicenseInfo:firstyear=2010&license=mit$
  3. #
  4. # Copyright (c) 2010, Linden Research, Inc.
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. # THE SOFTWARE.
  23. # $/LicenseInfo$
  24. #
  25. import unittest
  26. from filtertools import filterthru
  27. def f_id(x):
  28. return [x]
  29. def f_rev(x):
  30. return [''.join(map(None, reversed(x)))]
  31. def f_csv(x):
  32. return x.split(',')
  33. def f_xxx(x):
  34. if x[0] == "x":
  35. return []
  36. return [x]
  37. class TestFilter(unittest.TestCase):
  38. def assertNoChange(self, input, stack):
  39. output = filterthru(input, stack)
  40. if type(input) != list:
  41. self.assertEqual(output, [input])
  42. else:
  43. self.assertEqual(output, input)
  44. def testEmptyStack(self):
  45. self.assertNoChange([], [])
  46. self.assertNoChange(["abc"], [])
  47. self.assertNoChange(["abc", "def"], [])
  48. def testFilterId(self):
  49. self.assertNoChange([], [f_id])
  50. self.assertNoChange(["abc"], [f_id])
  51. self.assertNoChange(["abc", "def"], [f_id])
  52. self.assertNoChange([], [f_id, f_id])
  53. self.assertNoChange(["abc"], [f_id, f_id])
  54. self.assertNoChange(["abc", "def"], [f_id, f_id])
  55. def testModify(self):
  56. self.assertEqual(filterthru(["abc"], [f_rev]), ["cba"])
  57. self.assertEqual(filterthru(["abc", "def"], [f_rev]), ["cba", "fed"])
  58. self.assertNoChange(["abc"], [f_rev, f_rev])
  59. self.assertNoChange(["abc", "def"], [f_rev, f_rev])
  60. def testExpand(self):
  61. self.assertEqual(filterthru(["abc"], [f_csv]), ["abc"])
  62. self.assertEqual(filterthru(["abc,def"], [f_csv]), ["abc", "def"])
  63. self.assertEqual(filterthru(["abc,def,ghi", "jkl,mno"], [f_csv]),
  64. ["abc", "def", "ghi", "jkl", "mno"])
  65. def testRemove(self):
  66. self.assertEqual(filterthru(["abc"], [f_xxx]), ["abc"])
  67. self.assertEqual(filterthru(["xyz"], [f_xxx]), [])
  68. self.assertEqual(filterthru(["abc", "xyz"], [f_xxx]), ["abc"])
  69. self.assertEqual(filterthru(["xyz", "abc"], [f_xxx]), ["abc"])
  70. def testChain(self):
  71. self.assertEqual(filterthru(["abc,def,xyz"], [f_csv, f_xxx]),
  72. ["abc", "def"])
  73. self.assertEqual(filterthru(["abc,def,xyz"], [f_xxx, f_csv]),
  74. ["abc", "def", "xyz"])
  75. self.assertEqual(filterthru(["abc,def,xyz"], [f_rev, f_csv, f_xxx]),
  76. ["zyx", "fed", "cba"])
  77. self.assertEqual(filterthru(["abc,def,xyz"], [f_csv, f_rev, f_xxx]),
  78. ["cba", "fed", "zyx"])
  79. self.assertEqual(filterthru(["abc,def,xyz"], [f_csv, f_xxx, f_rev]),
  80. ["cba", "fed"])
  81. self.assertEqual(filterthru(["xxx,xyz"], [f_csv, f_xxx, f_rev]),
  82. [])
  83. if __name__ == '__main__':
  84. unittest.main()