def test_returns_arguments_based_on_matches(self): func = lambda x: -x matcher = matchers.ParseMatcher(func, 'foo') results = parse.Result([1, 2, 3], {'foo': 'bar', 'baz': -45.3}, { 0: (13, 14), 1: (16, 17), 2: (22, 23), 'foo': (32, 35), 'baz': (39, 44), }) expected = [ (13, 14, '1', 1, None), (16, 17, '2', 2, None), (22, 23, '3', 3, None), (32, 35, 'bar', 'bar', 'foo'), (39, 44, '-45.3', -45.3, 'baz'), ] with patch.object(matcher.parser, 'parse') as p: p.return_value = results m = matcher.match('some numbers 1, 2 and 3 and the bar is -45.3') assert m.func is func args = m.arguments have = [(a.start, a.end, a.original, a.value, a.name) for a in args] eq_(have, expected)
def test_slice_access(self): r = parse.Result((1, 2, 3, 4), {}, None) self.assertEqual(r[1:3], (2, 3)) self.assertEqual(r[-5:5], (1, 2, 3, 4)) self.assertEqual(r[:4:2], (1, 3)) self.assertEqual(r[::-2], (4, 2)) self.assertEqual(r[5:10], tuple())
def test_contains(self): r = parse.Result(('cat',), {'spam': 'ham'}, None) self.assertTrue('spam' in r) self.assertTrue('cat' not in r) self.assertTrue('ham' not in r)
def test_named_access(self): r = parse.Result((), {'spam': 'ham'}, None) self.assertEqual(r['spam'], 'ham') self.assertRaises(KeyError, r.__getitem__, 'ham') self.assertRaises(IndexError, r.__getitem__, 0)
def test_fixed_access(self): r = parse.Result((1, 2), {}, None) self.assertEqual(r[0], 1) self.assertEqual(r[1], 2) self.assertRaises(IndexError, r.__getitem__, 2) self.assertRaises(KeyError, r.__getitem__, 'spam')