def test__deposit__does_not_invoke__deposit_group__if_expected_is_dict_and_key_is_optional_repeat_and_length_of_value_is_zero( self): obj = GrammarObject(label=None, keywords=[]) mock_comparable = Mock(return_value=True) mock_deposit_group = Mock() obj._is_comparable = mock_comparable obj._deposit_group = mock_deposit_group obj._deposit({'optional-repeat': []}, {'optional-repeat': []}) self.assertTrue(mock_comparable.called) self.assertFalse(mock_deposit_group.called)
def test__deposit__invokes__deposit_group__if_expected_is_dict_and_key_is_optional_and_value_length_is_greater_than_zero( self): obj = GrammarObject(label=None, keywords=[]) mock_comparable = Mock(return_value=True) mock_deposit_group = Mock() obj._is_comparable = mock_comparable obj._deposit_group = mock_deposit_group obj._deposit({'optional': ['a']}, {'optional': ['a']}) self.assertTrue(mock_comparable.called) self.assertTrue(mock_deposit_group.called_with(['a'], ['a']))
def test__deposit__invokes__deposit_group__if_expected_is_dict_and_key_is_group( self): obj = GrammarObject(label=None, keywords=[]) mock_comparable = Mock(return_value=True) mock_deposit_group = Mock() obj._is_comparable = mock_comparable obj._deposit_group = mock_deposit_group obj._deposit({'group': ['a', 'b', 'c']}, {'group': ['a', 'b', 'c']}) self.assertTrue(mock_comparable.called) self.assertTrue( mock_deposit_group.called_with(['a', 'b', 'c'], ['a', 'b', 'c']))
def test__deposit__raises_exception_if_result_of__is_comparable__is_false( self): obj = GrammarObject(label=None, keywords=[]) mock_comparable = Mock(return_value=False) mock_syntax_error = Mock(side_effect=Exception) obj._is_comparable = mock_comparable obj._syntax_error = mock_syntax_error with self.assertRaises(Exception): obj._deposit(MockToken(1), MockToken(1)) self.assertTrue(mock_comparable.called) self.assertTrue(mock_syntax_error.called)
def test__deposit__does_not_raise_exception_if_expected_is_dict_and_key_is_any_and_given_object_matches( self): obj = GrammarObject(label=None, keywords=[]) mock_comparable = Mock(return_value=True) mock_syntax_error = Mock(side_effect=Exception) obj._is_comparable = mock_comparable obj._syntax_error = mock_syntax_error obj._deposit( 1, {'any': [1, 2, 3, { 'group': [MockToken(1), MockToken(5)] }]}) self.assertTrue(mock_comparable.called) self.assertEqual(obj.children[0], 1) self.assertFalse(mock_syntax_error.called)
def test_private__deposit_group__method(self): obj = GrammarObject(label=None, keywords=[]) mock_deposit = Mock() obj._deposit = mock_deposit obj._deposit_group(group=['a', 'b', 'c'], template=['a', 'b', 'c']) self.assertTrue(mock_deposit.call_count == 3)
def test__deposit__invokes__deposit_group__for_each_group_if_expected_is_dict_and_key_is_optional_repeat_and_length_of_value_is_greater_than_zero( self): obj = GrammarObject(label=None, keywords=[]) mock_comparable = Mock(return_value=True) mock_deposit_group = Mock() obj._is_comparable = mock_comparable obj._deposit_group = mock_deposit_group obj._deposit( { 'optional-repeat': [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']] }, {'optional-repeat': ['a', 'b', 'c']}) self.assertTrue(mock_comparable.called) self.assertEqual(mock_deposit_group.call_count, 3) self.assertTrue( mock_deposit_group.called_with(['a', 'b', 'c'], ['a', 'b', 'c']))
def test__deposit_group__raises_exception_if_arguments_do_not_have_the_same_length( self): obj = GrammarObject(label=None, keywords=[]) mock_deposit = Mock() obj._deposit = mock_deposit with self.assertRaisesRegex(Exception, 'length'): obj._deposit_group(group=['a', 'b'], template=['a']) self.assertTrue(mock_deposit.call_count == 0)
def test__deposit_group__raises_exception_if_any_argument_is_not_a_list( self): obj = GrammarObject(label=None, keywords=[]) mock_deposit = Mock() obj._deposit = mock_deposit for group, template in [('a', ['a']), (['a'], 'a')]: with self.subTest(group=group, template=template): with self.assertRaisesRegex(Exception, 'list'): obj._deposit_group(group=group, template=template) self.assertTrue(mock_deposit.call_count == 0)
def test__deposit__raises_exception_if_expected_is_dict_and_key_is_any_and_given_object_does_not_match( self): obj = GrammarObject(label=None, keywords=[]) mock_comparable = Mock(return_value=True) mock_syntax_error = Mock(side_effect=Exception) obj._is_comparable = mock_comparable obj._syntax_error = mock_syntax_error with self.assertRaises(Exception): obj._deposit( MockToken('a'), { 'any': [ MockToken(1), { 'group': [MockToken(2), MockToken(5)] }, 'hello' ] }) self.assertTrue(mock_comparable.called) self.assertEqual(len(obj.children), 0) self.assertTrue(mock_syntax_error.called)
def test_deposit_invokes_private_deposit_method(self): obj = GrammarObject(label=None, keywords=['a']) mock_deposit = Mock() mock_expected = Mock() obj._deposit = mock_deposit obj._expected = mock_expected self.assertEqual(obj._ptr, 0) obj.deposit('a') self.assertTrue(mock_deposit.called) self.assertTrue(mock_expected.called) self.assertEqual(obj._ptr, 1)
def test_deposit_raises_exception_if_cannot_deposit_more(self): obj = GrammarObject(label=None, keywords=['a']) mock_deposit = Mock() mock_expected = Mock() obj._deposit = mock_deposit obj._expected = mock_expected self.assertEqual(obj._ptr, 0) obj.deposit('a') with self.assertRaises(Exception): obj.deposit('b') self.assertTrue(mock_deposit.call_count == 1) self.assertTrue(mock_expected.call_count == 1) self.assertEqual(obj._ptr, 1)