コード例 #1
0
ファイル: tests.py プロジェクト: kendaganio/python-underscore
class UnderscoreTestCase(unittest.TestCase):

  def setUp(self):
    self._ = Underscore()

  def tearDown(self):
    self._ = None

  def test_mixins(self):

    def test_method(x, y):
      return x + y

    self._.mixin({
        'test_method': test_method
      })
    self.assertTrue(self._.test_method)
    self.assertEqual(self._.test_method(5, 4), 9)

  def test_each(self):
    my_seq = [1,2,3,4,5]
    self.assertTrue(self._.each)
    # Test functional-style
    self.assertEqual(self._.each(my_seq, lambda x: x + 1), [2,3,4,5,6])
    # Test OOP-style
    self.assertEqual(self._(my_seq).each(lambda x: x + 2), [3,4,5,6,7])
コード例 #2
0
ファイル: test_underscore.py プロジェクト: shtakai/cd-python
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.test_list = [1,2,3,4,5]


    def testMap(self):
        self.assertEqual([1,4,9,16,25], self._.map(self.test_list, lambda x: x*x))
        self.assertNotEqual([1,4,9,16,25], self._.map(self.test_list, lambda x: x*3))



    def testReduce(self):
        self.assertEqual(15, self._.reduce(self.test_list, lambda x,y: x+y, 0))
        self.assertNotEqual(15, self._.reduce(self.test_list, lambda x,y: x*y, 0))

    def testFind(self):
        self.assertEqual(4, self._.find(self.test_list, lambda x: x==4))
        self.assertFalse(self._.find(self.test_list, lambda x: x==8))

    def testFilter(self):
        self.assertEqual([3], self._.filter(self.test_list, lambda x: x%3==0))
        self.assertEqual([2,4], self._.filter(self.test_list, lambda x: x%2==0))

    def testReject(self):
        self.assertEqual([1,2,4,5], self._.reject(self.test_list, lambda x: x%3==0))
        self.assertEqual([1,3,5], self._.reject(self.test_list, lambda x: x%2==0))
コード例 #3
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.squared = lambda x: x * x
        self.map_result = self._.map([1, 2, 3, 4, 5], self.squared)
        self.sum = lambda x, y: x + y
        self.reduce_result = self._.reduce([1, 2, 3, 4, 5], self.sum, 0)
        self.find_result1 = self._.find([0, 1, 2, 3, 4], lambda x: x == 2)
        self.filter_result1 = self._.filter([0, 1, 2, 3, 4], lambda x: x > 2)
        self.reject_result1 = self._.reject([0, 1, 2, 3, 4], lambda x: x > 2)

    def testMap(self):
        return self.assertEqual([1, 4, 9, 16, 25], self.map_result)

    def testReduce(self):
        return self.assertEqual(15, self.reduce_result)

    def testFind(self):
        return self.assertEqual(2, self.find_result1)

    def testFilter(self):
        return self.assertEqual([3, 4], self.filter_result1)

    def testReject(self):
        return self.assertEqual([0, 1, 2], self.reject_result1)
コード例 #4
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.test_list = [1, 2, 3, 4, 5]
        self.map_result = self._.map(self.test_list, lambda x: x + 1)
        self.reduce_result = self._.reduce(self.test_list, lambda x, y: x * y,
                                           1)
        self.find_result = self._.find(self.test_list, lambda x: x % 3 == 0)
        self.filter_result = self._.filter(self.test_list,
                                           lambda x: x % 2 == 0)
        self.reject_result = self._.reject(self.test_list,
                                           lambda x: x % 2 == 0)

    def testMap(self):
        return self.assertEqual([2, 3, 4, 5, 6], self.map_result)

    def testReduce(self):
        return self.assertEqual(720, self.reduce_result)

    def testFind(self):
        return self.assertEqual(3, self.find_result)

    def testFilter(self):
        return self.assertEqual([2, 4, 6], self.filter_result)

    def testReject(self):
        return self.assertEqual([3, 5], self.reject_result)
コード例 #5
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
    def testMap(self):
        self.test_list = [1, 2, 3, 4]
        self.map_result = self._.map(self.test_list, lambda x: x**2)
        return self.assertEqual([1, 4, 9, 16], self.test_list)

    def testReduce(self):
        self.test_list = [1, 2, 3, 4]
        self.reduce_result = self._.reduce(self.test_list, lambda x, y: x * y,
                                           1)
        return self.assertEqual(24, self.reduce_result)

    def testFind(self):
        self.test_list = [1, 2, 3, 4, 5, 6]
        self.find_result = self._.find(self.test_list, lambda x: x > 3)
        self.assertEqual(4, self.find_result)

    def testFilter(self):
        self.test_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        self.filter_result = self._.filter(self.test_list, lambda x: x > 6)
        self.assertEqual([7, 8, 9, 10], self.filter_result)

    def testReject(self):
        self.test_list = [1, 2, 3, 4, 5]
        self.reject_result = self._.reject(self.test_list, lambda x: x == 3)
        self.assertEqual([1, 2, 4, 5], self.reject_result)
コード例 #6
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        
        # create an instance of the Underscore module we created
        self._ = Underscore()

        
        # initialize a list to run our tests on
        self.map_test_list = [1,2,3,4,5]
        self.test_list = [1,2,3,4,5]


        # set up test functions with inputs
        self.testMaptest = self._.map(self.map_test_list, lambda num: num**2)
            #result: [1, 4, 9, 16, 25]
            #print self.test_list         this is how i debugged, the list was already changed in place
        self.testReducetest = self._.reduce(self.test_list, lambda memo, num: memo+num, 0)
            #result: 15
        self.testFindtest = self._.find(self.test_list, lambda num: num % 2 == 0 and num > 3)
            #result: 4
        self.testFiltertest = self._.filter(self.test_list, lambda num: num % 2 == 1)
            #result: [1, 3, 5]
        self.testRejecttest = self._.reject(self.test_list, lambda num: num % 2 == 1)
            #result: [2,4]
        
    def testMap(self):
        return self.assertEqual([1, 4, 9, 16, 25], self.testMaptest)
    def testReduce(self):
        return self.assertEqual(15, self.testReducetest)
    def testFind(self):
        return self.assertEqual(4, self.testFindtest)
    def testFilter(self):
        return self.assertEqual([1, 3, 5], self.testFiltertest)
    def testReject(self):
        return self.assertEqual([2,4], self.testRejecttest)
コード例 #7
0
 def setUp(self):
     # create an instance of the Underscore module we created
     self._ = Underscore()
     # initialize a list to run our tests on
     self.squared = lambda x: x * x
     self.map_result = self._.map([1, 2, 3, 4, 5], self.squared)
     self.sum = lambda x, y: x + y
     self.reduce_result = self._.reduce([1, 2, 3, 4, 5], self.sum, 0)
     self.find_result1 = self._.find([0, 1, 2, 3, 4], lambda x: x == 2)
     self.filter_result1 = self._.filter([0, 1, 2, 3, 4], lambda x: x > 2)
     self.reject_result1 = self._.reject([0, 1, 2, 3, 4], lambda x: x > 2)
コード例 #8
0
 def setUp(self):
     # create an instance of the Underscore module we created
     self._ = Underscore()
     # initialize a list to run our tests on
     self.test_list = [1, 2, 3, 4, 5]
     self.map_result = self._.map(self.test_list, lambda x: x + 1)
     self.reduce_result = self._.reduce(self.test_list, lambda x, y: x * y,
                                        1)
     self.find_result = self._.find(self.test_list, lambda x: x % 3 == 0)
     self.filter_result = self._.filter(self.test_list,
                                        lambda x: x % 2 == 0)
     self.reject_result = self._.reject(self.test_list,
                                        lambda x: x % 2 == 0)
コード例 #9
0
ファイル: tdii.py プロジェクト: aquang9124/tdd_underscore
class UnderscoreTest(unittest.TestCase):
	def setUp(self):
		# create an instance of the Underscore module we created
		self._ = Underscore()
		# initialize a list to run our tests on
		self.test_list = [1, 2, 3, 4, 5]
	def testMap(self):
		return self.assertIsInstance(self._.map(self.test_list, lambda x: x + 2), list)
	def testReduce(self):
		return self.assertEqual(1, len(self._.reduce(self.test_list)))
	def testFind(self):
		return self.assertEqual(True, self._.find(self.test_list, lambda x, num: x == num, 4))
	def testFilter(self):
		return self.assertIsInstance(self._.filter(self.test_list, lambda x: x % 2 == 0), list)
	def testReject(self):
		return self.assertIsInstance(self._.reject(self.test_list, lambda num, x: num == x, 3), list)
コード例 #10
0
ファイル: test.py プロジェクト: mlhoyt/DojoAssignments
class Underscore_test(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.test_list = [1, 2, 3, 4, 5]

    def test_map(self):
        result = self._.map(self.test_list, lambda x: x * 2)
        return (self.assertEqual([2, 4, 6, 8, 10], result))

    def test_reduce_zero_memo(self):
        result = self._.reduce(self.test_list, lambda i, x: i + x, 0)
        return (self.assertEqual(15, result))

    def test_reduce_nonzero_memo(self):
        result = self._.reduce(self.test_list, lambda i, x: i + x, 13)
        return (self.assertEqual(28, result))

    def test_find_mod2(self):
        result = self._.find(self.test_list, lambda x: x % 2 == 0)
        return (self.assertEqual(2, result))

    def test_find_mod5(self):
        result = self._.find(self.test_list, lambda x: x % 5 == 0)
        return (self.assertEqual(5, result))

    def test_filter(self):
        result = self._.filter(self.test_list, lambda x: x % 2 == 0)
        return (self.assertEqual([2, 4], result))

    def test_reject(self):
        result = self._.reject(self.test_list, lambda x: x % 2 == 0)
        return (self.assertEqual([1, 3, 5], result))
コード例 #11
0
 def setUp(self):
     # create an instance of the Underscore module we created
     self._ = Underscore()
     # initialize a list to run our tests on
     self.test_list = [1,2,3,4,5,6,7]
     self.map_result = self._.map(self.test_list, lambda x: x + 1)
     self.reduce_result = self._.reduce(self.test_list, (lambda mem, x: mem - x), 28)
     self.find_result = self._.find(self.test_list, lambda x: x%2 == 0)
     self.filter_result = self._.filter(self.test_list, lambda x: x%2 == 0)
     self.reject_result = self._.reject(self.test_list, lambda x: x%2 == 0)
コード例 #12
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.test_list = [1,2,3,4]
    def testMap(self):
        self.result = self._.map(self.test_list, lambda x: x*2)
        return self.assertEqual([2,4,6,8], self.result)
    def testReduce(self):
        self.result = self._.reduce(self.test_list, lambda x,y: x+y)
        return self.assertEqual(10, self.result)
    def testFind(self):
        self.result = self._.find(self.test_list, lambda x,y: x==y, self.test_list[2])
        return self.assertEqual([3], self.result)
    def testFilter(self):
        self.result = self._.filter(lambda x: x%2==0, self.test_list)
        return self.assertEqual([2,4], self.result)
    def testReject(self):
        self.result = self._.reject(lambda x: x%2==0, self.test_list)
        return self.assertEqual([1,3], self.result)
コード例 #13
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.test_list = [1,2,3,4,5,6,7]
        self.map_result = self._.map(self.test_list, lambda x: x + 1)
        self.reduce_result = self._.reduce(self.test_list, (lambda mem, x: mem - x), 28)
        self.find_result = self._.find(self.test_list, lambda x: x%2 == 0)
        self.filter_result = self._.filter(self.test_list, lambda x: x%2 == 0)
        self.reject_result = self._.reject(self.test_list, lambda x: x%2 == 0)
    def testMap(self):
        return self.assertEqual([2,3,4,5,6,7,8], self.map_result)
    def testReduce(self):
        return self.assertEqual(-7, self.reduce_result)
    def testFind(self):
        return self.assertEqual(2, self.find_result)
    def testFilter(self):
        return self.assertEqual([2,4,6,8], self.filter_result)
    def testReject(self):
        return self.assertEqual([3,5,7], self.reject_result)
コード例 #14
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.test_list = [1,2,3,4,5]
    def testMap(self):
        self.result = self._.map(self.test_list, lambda x: x * 3) # [3, 6, 9, 12, 15]
        return self.assertEqual([3, 6, 9, 12, 15], self.result)
        pass
    def testReduce(self):
        self.result = self._.reduce(self.test_list, lambda x, y: x + y, 0)
        return self.assertEqual(15, self.result)
    def testFind(self):
        self.result = self._.find(self.test_list, lambda x: x == 5)
        return self.assertEqual(5, self.result)
    def testFindfalse(self):
        self.result = self._.find(self.test_list, lambda x: x == 0)
        return self.assertFalse(self.result)
    def testFilter(self):
        self.result = self._.filter(self.test_list, lambda x: x % 2 == 0)
        return self.assertEqual([2,4], self.result)
    def testReject(self):
        self.result = self._.reject(self.test_list, lambda x: x % 2 == 0)
        return self.assertEqual([1,3,5], self.result)
コード例 #15
0
class UnderscoreTest(unittest.TestCase):
    def setUp(self):
        # create an instance of the Underscore module we created
        self._ = Underscore()
        # initialize a list to run our tests on
        self.test_list = [1,2,3,4,5]
    def testMap(self):
        self.assertEqual([1,4,9,16,25],self._.map(self.test_list[:], lambda x:x**2))
    def testReduce(self):
        pass
    def testFind(self):
        pass
    def testFilter(self):
        pass
    def testReject(self):
        pass
コード例 #16
0
 def setUp(self):
   self._ = Underscore()
コード例 #17
0
 def setUp(self):
     # create an instance of the Underscore module we created
     self._ = Underscore()
     # initialize a list to run our tests on
     self.test_list = [1,2,3,4]
コード例 #18
0
from underscore import Underscore

# you just created a library with 5 methods!
# let's create an instance of our class
_ = Underscore(
)  # yes we are setting our instance to a variable that is an underscore

# Map
result = _.map([1, 3, 8], lambda x: x * 2)
print result

#evens = _.filter([1, 2, 3, 4, 5, 6], lambda x: x % 2 == 0)
# should return [2, 4, 6] after you finish implementing the code above
コード例 #19
0
from math_dojo import MathDojo
import call_center
import hospital
from underscore import Underscore

bmx = Bike(100, "30 mph")
print bmx.__repr__()
toyota = Car(20000, "120 mph", "Full", 50000)
print toyota.__repr__()
mouse = Product(10, "Computer Mouse", "5 ounces", "Logitech", 5)
print mouse.__repr__()
electronics = Store("San Jose, CA", "Me Me")
print electronics.__repr__()
hamster = animal.Animal("Hammy", 50)
print hamster.__repr__()
spot = animal.Dog()
print spot.__repr__()
maggie = animal.Dragon()
print maggie.__repr__()
al = MathDojo()
print al.__repr__()
call1 = call_center.Call("Aaron", "268-347-1484", 911, "Complaint")
print call1.__repr__()
center1 = call_center.CallCenter()
print center1.__repr__()
patient1 = hospital.Patient(135, "Deborah", "sulfonamides")
print patient1.__repr__()
general = hospital.Hospital("BB General", 100)
print general.__repr__()
_ = Underscore()
print _.__repr__()
コード例 #20
0
 def setUp(self):
     # create an instance of the Underscore module we created
     self._ = Underscore()
     # initialize a list to run our tests on
     self.test_list = [1, 2, 3, 4, 5]
コード例 #21
0
from underscore import Underscore

_ = Underscore()

print _.map([1, 2], lambda x: x * 6)
コード例 #22
0
class UnderscoreTestCase(unittest.TestCase):

  def setUp(self):
    self._ = Underscore()

  def tearDown(self):
    self._ = None

  def test_mixins(self):

    def test_method(x, y):
      return x + y

    self._.mixin({
        'test_method': test_method
      })
    self.assertTrue(self._.test_method)
    self.assertEqual(self._.test_method(5, 4), 9)

  def test_each(self):
    my_seq = [1,2,3,4,5]
    self.assertTrue(self._.each)
    # Test functional-style
    self.assertEqual(self._.each(my_seq, lambda x: x + 1), [2,3,4,5,6])
    # Test OOP-style
    self.assertEqual(self._(my_seq).each(lambda x: x + 2), [3,4,5,6,7])

  def test_filter(self):
    my_seq = [1,2,3,4,5]
    self.assertTrue(self._.filter)
    # Test functional-style
    self.assertEqual(self._.filter(my_seq, lambda x: x % 2 == 0), [2,4])
    # Test OOP-style
    self.assertEqual(self._(my_seq).filter(lambda x: x % 2 == 0), [2,4])

  def test_all(self):
    my_seq = [1,2,False,None,True]
    self.assertTrue(self._.all)
    # Test functional-style
    self.assertEqual(self._.all(my_seq), [1,2,True])
    # Test OOP-style
    self.assertEqual(self._(my_seq).all(), [1,2,True])

  def test_generator(self):
    my_seq = [1,2,3,4,5]
    self.assertTrue(self._.generator)
    # Test functional-style if it returns a generator
    self.assertTrue(isinstance(self._.generator(my_seq), types.GeneratorType))
    # Test OOP-style if it returns a generator
    self.assertTrue(isinstance(self._(my_seq).generator(), types.GeneratorType))
    # Test return values
    self.assertEqual([i for i in self._.generator(my_seq)], my_seq)
    self.assertEqual([i for i in self._(my_seq).generator()], my_seq)
    # Test chaining
    self.assertEqual(self._.chain()
        .generator(my_seq)
        .each(lambda x: x + 1)
        .value(), [2,3,4,5,6])

  def test_chaining(self):
    my_seq = [1,2,3,4,5]
    self.assertTrue(self._.chain)
    # Test functional-style
    self.assertEqual(self._
      .chain(my_seq)
      .each(lambda x: x + 1)
      .filter(lambda x: x % 2 == 0)
      .value(), [2,4,6])
    # Test OOP-style
    self.assertEqual(self._(my_seq)
      .chain()
      .each(lambda x: x + 1)
      .filter(lambda x: x % 2 == 0)
      .value(), [2,4,6])
コード例 #23
0
 def setUp(self):
     # create an instance of the Underscore module we created
     self._ = Underscore()