Example #1
0
    def test_apply_once(self):
        state = State(current_number=1)
        op1 = Mock()
        op1.operate = Mock(return_value=3)
        state.apply(op1)
        op1.operate.assert_called_with(1)

        self.assertEqual(state.current_number, 3)
        self.assertEqual(state.steps, [op1])
Example #2
0
    def test_clone_is_separate_instance(self):
        state = State(current_number=3, steps=[2, 4, 8])
        cloned_state = state.clone()

        op1 = Mock()
        op1.operate.side_effect = add_four
        state.apply(op1)

        self.assertEqual(cloned_state.current_number, 3)
        self.assertEqual(cloned_state.steps, [2, 4, 8])
Example #3
0
    def test_apply_twice(self):
        state = State()

        op1 = Mock()
        op1.operate.side_effect = add_four
        state.apply(op1)
        op1.operate.assert_called_with(0)

        op2 = Mock()
        op2.operate = Mock(return_value=7)
        state.apply(op2)
        op2.operate.assert_called_with(4)

        self.assertEqual(state.current_number, 7)
        self.assertEqual(state.steps, [op1, op2])
Example #4
0
 def test_init_with_steps(self):
     state = State(steps=['a', 'b'])
     self.assertEqual(state.current_number, 0)
     self.assertEqual(state.steps, ['a', 'b'])
Example #5
0
 def test_init_with_current_number(self):
     state = State(current_number=10)
     self.assertEqual(state.current_number, 10)
     self.assertEqual(state.steps, [])
Example #6
0
 def test_default_init(self):
     state = State()
     self.assertEqual(state.current_number, 0)
     self.assertEqual(state.steps, [])
Example #7
0
 def test_positive_good_fraction(self):
     state = State(current_number=1)
     state.apply(DivideOperator(8))
     self.assertEqual(state.current_number, 0.125)
Example #8
0
 def test_negative_bad_fraction(self):
     state = State(current_number=-1)
     with self.assertRaises(ValueError):
         state.apply(DivideOperator(16))
Example #9
0
 def test_divide_positive(self):
     state = State(current_number=50)
     state.apply(DivideOperator(5))
     self.assertEqual(state.current_number, 10)
Example #10
0
 def test_multiply_negative(self):
     state = State(current_number=5)
     state.apply(MultiplyOperator(-3))
     self.assertEqual(state.current_number, -15)
Example #11
0
 def test_multiply_positive(self):
     state = State(current_number=6)
     state.apply(MultiplyOperator(4))
     self.assertEqual(state.current_number, 24)
Example #12
0
 def test_add_negative(self):
     state = State(current_number=5)
     state.apply(AddOperator(-8))
     self.assertEqual(state.current_number, -3)
Example #13
0
 def test_add_positive(self):
     state = State(current_number=10)
     state.apply(AddOperator(5))
     self.assertEqual(state.current_number, 15)
Example #14
0
 def test_add_zero(self):
     state = State(current_number=2)
     state.apply(AddOperator(0))
     self.assertEqual(state.current_number, 2)
Example #15
0
    def test_clone_values(self):
        state = State(current_number=3, steps=[2, 4, 8])
        cloned_state = state.clone()

        self.assertEqual(cloned_state.current_number, 3)
        self.assertEqual(cloned_state.steps, [2, 4, 8])
Example #16
0
 def test_divide_negative(self):
     state = State(current_number=24)
     state.apply(DivideOperator(-3))
     self.assertEqual(state.current_number, -8)
Example #17
0
 def test_positive_bad_fraction(self):
     state = State(current_number=100)
     with self.assertRaises(ValueError):
         state.apply(DivideOperator(3))