def test__sorts_sequences(self): self.expectThat( describe_canonical([3, 1, 2]), Equals((1, 2, 3))) self.expectThat( describe_canonical([[1, 2], [1, 1]]), Equals(((1, 1), (1, 2))))
def test__passes_numbers_through(self): self.expectThat(describe_canonical(1), MatchesAll(IsInstance(int), Equals(1))) self.expectThat(describe_canonical(1), MatchesAll(IsInstance(int), Equals(1))) self.expectThat(describe_canonical(1.0), MatchesAll(IsInstance(float), Equals(1.0)))
def test__sorts_mappings_by_key_and_value(self): class inth(int): """An `int` that hashes independently from its value. This lets us use the same numeric key twice in a dict. Strictly this is an abuse, but it helps to demonstrate a point here, that values are considered when sorting. """ __hash__ = object.__hash__ mapping = { (1, inth(2)): "foo", (1, inth(2)): "bar", (1, inth(1)): "foo", (1, inth(1)): "bar", } expected = ( ((1, 1), "bar"), ((1, 1), "foo"), ((1, 2), "bar"), ((1, 2), "foo"), ) self.expectThat( describe_canonical(mapping), Equals(expected))
def test__recursively_calls_mapping_keys_and_values(self): mapping = {"key\u1234".encode("utf-8"): ["b", "a", "r"]} expected = ( ("key\u1234", ("a", "b", "r")), ) self.expectThat( describe_canonical(mapping), Equals(expected))
def test__decodes_byte_strings(self): string = factory.make_string().encode("utf-8") self.expectThat( describe_canonical(string), MatchesAll( IsInstance(str), Not(Is(string)), Equals(string.decode("utf-8")), ), )
def test__returns_mappings_as_tuples(self): self.expectThat(describe_canonical({1: 2}), Equals(((1, 2),)))
def test__recursively_calls_sequence_elements(self): self.expectThat(describe_canonical([1, [2, 3]]), Equals((1, (2, 3))))
def test__returns_sequences_as_tuples(self): self.expectThat(describe_canonical([1, 2, 3]), Equals((1, 2, 3)))
def test__passes_unicode_strings_through(self): string = factory.make_string() self.assertThat(string, IsInstance(str)) self.expectThat(describe_canonical(string), Is(string))
def test__passes_True_False_and_None_through(self): self.expectThat(describe_canonical(True), Is(True)) self.expectThat(describe_canonical(False), Is(False)) self.expectThat(describe_canonical(None), Is(None))