forked from facebook/buck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_test.py
More file actions
86 lines (67 loc) · 2.66 KB
/
Copy pathstruct_test.py
File metadata and controls
86 lines (67 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import with_statement
from . import struct
import copy
import unittest
class StructTest(unittest.TestCase):
def testPropertyAccess(self):
self.assertEqual(struct.struct(foo="bar").foo, "bar")
def testJsonSerialization(self):
self.assertEqual(
struct.struct(foo="bar").to_json(),
"{\"foo\":\"bar\"}")
def testJsonKeysAreSorted(self):
self.assertEqual(
struct.struct(c="c", b="b", a="a").to_json(),
"{\"a\":\"a\",\"b\":\"b\",\"c\":\"c\"}")
def testNestedJsonSerialization(self):
self.assertEqual(
struct.struct(foo=struct.struct(bar="baz")).to_json(),
"{\"foo\":{\"bar\":\"baz\"}}")
def testCannotMutateAField(self):
with self.assertRaisesRegexp(
AttributeError,
"Mutation of struct attributes \('foo'\) is not allowed."):
struct.struct(foo="foo").foo = "bar"
def testCanCopy(self):
original = struct.struct(foo="bar")
copied = copy.copy(original)
self.assertEqual(original, copied)
self.assertIsNot(original, copied)
def testCanDeepCopy(self):
original = struct.struct(foo="bar")
deepcopied = copy.deepcopy(original)
self.assertEqual(original, deepcopied)
self.assertIsNot(original, deepcopied)
def testInequality(self):
x = struct.struct(foo="bar")
y = struct.struct(foo="baz")
self.assertNotEqual(x, y)
def testCanUseAsAHashKey(self):
x = struct.struct(foo="bar")
y = struct.struct(foo="baz")
dictionary = {
x: "x",
y: "y",
}
self.assertEqual(dictionary[x], "x")
self.assertNotEqual(dictionary[y], "x")
def testHasAttrForExistingAttribute(self):
self.assertTrue(hasattr(struct.struct(foo="bar"), "foo"))
def testHasAttrForNonExistingAttribute(self):
self.assertFalse(hasattr(struct.struct(foo="bar"), "does_not_exist"))
def testGetAttrForExistingAttribute(self):
x = struct.struct(foo="bar")
self.assertEqual("bar", getattr(x, "foo"))
def testGetAttrForNonExistingAttribute(self):
x = struct.struct(foo="bar")
self.assertEqual("default", getattr(x, "does_not_exist", "default"))
def testRepr(self):
x = struct.struct(foo="bar")
self.assertEqual("struct(foo='bar')", repr(x))
def testNestedRepr(self):
x = struct.struct(foo="bar")
y = struct.struct(x=x)
self.assertEqual("struct(x=struct(foo='bar'))", repr(y))