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])
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])
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])
def test_init_with_steps(self): state = State(steps=['a', 'b']) self.assertEqual(state.current_number, 0) self.assertEqual(state.steps, ['a', 'b'])
def test_init_with_current_number(self): state = State(current_number=10) self.assertEqual(state.current_number, 10) self.assertEqual(state.steps, [])
def test_default_init(self): state = State() self.assertEqual(state.current_number, 0) self.assertEqual(state.steps, [])
def test_positive_good_fraction(self): state = State(current_number=1) state.apply(DivideOperator(8)) self.assertEqual(state.current_number, 0.125)
def test_negative_bad_fraction(self): state = State(current_number=-1) with self.assertRaises(ValueError): state.apply(DivideOperator(16))
def test_divide_positive(self): state = State(current_number=50) state.apply(DivideOperator(5)) self.assertEqual(state.current_number, 10)
def test_multiply_negative(self): state = State(current_number=5) state.apply(MultiplyOperator(-3)) self.assertEqual(state.current_number, -15)
def test_multiply_positive(self): state = State(current_number=6) state.apply(MultiplyOperator(4)) self.assertEqual(state.current_number, 24)
def test_add_negative(self): state = State(current_number=5) state.apply(AddOperator(-8)) self.assertEqual(state.current_number, -3)
def test_add_positive(self): state = State(current_number=10) state.apply(AddOperator(5)) self.assertEqual(state.current_number, 15)
def test_add_zero(self): state = State(current_number=2) state.apply(AddOperator(0)) self.assertEqual(state.current_number, 2)
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])
def test_divide_negative(self): state = State(current_number=24) state.apply(DivideOperator(-3)) self.assertEqual(state.current_number, -8)
def test_positive_bad_fraction(self): state = State(current_number=100) with self.assertRaises(ValueError): state.apply(DivideOperator(3))