def test_it_can_be_used_as_a_document_to_resolve_a_pointer_in(self): ctx = _TestContext(self.id(), {'a': 1, 'b': 2, 'c': 3}) ref = Reference(ctx, '') self.assertEqual(Pointer.resolve_in('/a', ref), 1) self.assertEqual(Pointer.resolve_in('/b', ref), 2) self.assertEqual(Pointer.resolve_in('/c', ref), 3)
def test_it_offers_support_for_lazy_loaded_values(self): class LazyValue: def __lazy_eval__(self): return {'a': 1, 'b': 2, 'c': 3} value = LazyValue() self.assertEqual(Pointer.resolve_in('/a', value), 1) self.assertEqual(Pointer.resolve_in('/b', value), 2) self.assertEqual(Pointer.resolve_in('/c', value), 3)
def test_pointer_resolve_in_can_be_called_as_a_static_method(self): self.assertEqual(Pointer.resolve_in('key', {'key': self.sentinel}), self.sentinel) self.assertEqual( Pointer.resolve_in('key', document={'key': self.sentinel}), self.sentinel)
def test_pointer_resolve_in_can_be_called_as_an_instance_method(self): self.assertEqual( Pointer('key').resolve_in({'key': self.sentinel}), self.sentinel) self.assertEqual( Pointer('key').resolve_in(document={'key': self.sentinel}), self.sentinel)
def test_it_raises_an_error_if_unable_to_resolve_token(self): with self.assertRaises(error.UnreferenceableValue): Pointer.resolve_in('/key', document=object()) with self.assertRaises(error.UnreferenceableValue): Pointer.resolve_in('key', document=object())
def test_it_raises_an_error_on_unescaped_tilde(self): with self.assertRaises(error.UnescapedTilde): Pointer.resolve_in('/~', document={}) with self.assertRaises(error.UnescapedTilde): Pointer.resolve_in('~', document={})
def test_it_raises_an_error_on_unrecognized_escape_sequences(self): with self.assertRaises(error.UnrecognizedEscapeSequence): Pointer.resolve_in('/~2', document={}) with self.assertRaises(error.UnrecognizedEscapeSequence): Pointer.resolve_in('~2', document={})
def check_pointer_equal(self, document, pointer, value): self.assertEqual(Pointer.resolve_in(pointer, document), value) # test that starting slash in non-empty pointer is optional if (len(pointer) > 1 and pointer[0] == '/'): self.assertEqual(Pointer.resolve_in(pointer[1:], document), value)
def test_it_raises_an_error_if_key_not_in_document(self): with self.assertRaises(error.MemberNotDefined): Pointer.resolve_in('/key', document={}) with self.assertRaises(error.MemberNotDefined): Pointer.resolve_in('key', document={})
def test_it_raises_an_error_for_non_numeric_array_index(self): with self.assertRaises(error.InvalidArrayIndex): Pointer.resolve_in('/key', document=[]) with self.assertRaises(error.InvalidArrayIndex): Pointer.resolve_in('key', document=[])
def test_it_raises_an_error_for_array_index_out_of_range(self): with self.assertRaises(error.IndexOutOfRange): Pointer.resolve_in('/5', document=[]) with self.assertRaises(error.IndexOutOfRange): Pointer.resolve_in('5', document=[])
def test_it_raises_an_error_for_dash_as_an_array_index(self): with self.assertRaises(error.DashArrayIndexNotSupported): Pointer.resolve_in('/-', document=[]) with self.assertRaises(error.DashArrayIndexNotSupported): Pointer.resolve_in('-', document=[])