PageRenderTime 63ms CodeModel.GetById 30ms app.highlight 11ms RepoModel.GetById 19ms app.codeStats 1ms

/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
 26import unittest
 27
 28from filtertools import filterthru
 29
 30def f_id(x):
 31    return [x]
 32
 33def f_rev(x):
 34    return [''.join(map(None, reversed(x)))]
 35
 36def f_csv(x):
 37    return x.split(',')
 38
 39def f_xxx(x):
 40    if x[0] == "x":
 41        return []
 42    return [x]
 43
 44class TestFilter(unittest.TestCase):
 45
 46    def assertNoChange(self, input, stack):
 47        output = filterthru(input, stack)
 48        if type(input) != list:
 49            self.assertEqual(output, [input])
 50        else:
 51            self.assertEqual(output, input)
 52        
 53    def testEmptyStack(self):
 54        self.assertNoChange([], [])
 55        self.assertNoChange(["abc"], [])
 56        self.assertNoChange(["abc", "def"], [])
 57    
 58    def testFilterId(self):
 59        self.assertNoChange([], [f_id])
 60        self.assertNoChange(["abc"], [f_id])
 61        self.assertNoChange(["abc", "def"], [f_id])
 62
 63        self.assertNoChange([], [f_id, f_id])
 64        self.assertNoChange(["abc"], [f_id, f_id])
 65        self.assertNoChange(["abc", "def"], [f_id, f_id])
 66
 67    def testModify(self):
 68        self.assertEqual(filterthru(["abc"], [f_rev]), ["cba"])
 69        self.assertEqual(filterthru(["abc", "def"], [f_rev]), ["cba", "fed"])
 70
 71        self.assertNoChange(["abc"], [f_rev, f_rev])
 72        self.assertNoChange(["abc", "def"], [f_rev, f_rev])
 73
 74    def testExpand(self):
 75        self.assertEqual(filterthru(["abc"], [f_csv]), ["abc"])
 76        self.assertEqual(filterthru(["abc,def"], [f_csv]), ["abc", "def"])
 77        self.assertEqual(filterthru(["abc,def,ghi", "jkl,mno"], [f_csv]),
 78            ["abc", "def", "ghi", "jkl", "mno"])
 79    
 80    def testRemove(self):
 81        self.assertEqual(filterthru(["abc"], [f_xxx]), ["abc"])
 82        self.assertEqual(filterthru(["xyz"], [f_xxx]), [])
 83        self.assertEqual(filterthru(["abc", "xyz"], [f_xxx]), ["abc"])
 84        self.assertEqual(filterthru(["xyz", "abc"], [f_xxx]), ["abc"])
 85    
 86    def testChain(self):
 87        self.assertEqual(filterthru(["abc,def,xyz"], [f_csv, f_xxx]),
 88            ["abc", "def"])
 89        self.assertEqual(filterthru(["abc,def,xyz"], [f_xxx, f_csv]),
 90            ["abc", "def", "xyz"])
 91        self.assertEqual(filterthru(["abc,def,xyz"], [f_rev, f_csv, f_xxx]),
 92            ["zyx", "fed", "cba"])
 93        self.assertEqual(filterthru(["abc,def,xyz"], [f_csv, f_rev, f_xxx]),
 94            ["cba", "fed", "zyx"])
 95        self.assertEqual(filterthru(["abc,def,xyz"], [f_csv, f_xxx, f_rev]),
 96            ["cba", "fed"])
 97        self.assertEqual(filterthru(["xxx,xyz"], [f_csv, f_xxx, f_rev]),
 98            [])
 99
100  
101if __name__ == '__main__':
102    unittest.main()
103