aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDamien George2014-03-03 22:12:32 +0000
committerDamien George2014-03-03 22:12:32 +0000
commit2ab1797d1d6f6b7590f7a0a8146b46ffe7bcb2f2 (patch)
treea8e45a23c469ea56390535001f423892ad8d572a /tests
parent61f9b1c6217d2371e01638354c852913e071fbea (diff)
parente74f52b76c2be9aefca689e34cd05f8f64be02f8 (diff)
Merge pull request #332 from pfalcon/namedtuple
Implement collections.namedtuple
Diffstat (limited to 'tests')
-rw-r--r--tests/basics/namedtuple1.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/tests/basics/namedtuple1.py b/tests/basics/namedtuple1.py
new file mode 100644
index 000000000..1e194472f
--- /dev/null
+++ b/tests/basics/namedtuple1.py
@@ -0,0 +1,46 @@
+from collections import namedtuple
+
+T = namedtuple("Tup", "foo bar")
+# CPython prints fully qualified name, what we don't bother to do so far
+#print(T)
+t = T(1, 2)
+print(t)
+print(t[0], t[1])
+print(t.foo, t.bar)
+
+print(len(t))
+print(bool(t))
+print(t + t)
+print(t * 3)
+
+print(isinstance(t, tuple))
+
+try:
+ t[0] = 200
+except TypeError:
+ print("TypeError")
+try:
+ t.bar = 200
+except AttributeError:
+ print("AttribiteError")
+
+try:
+ t = T(1)
+except TypeError:
+ print("TypeError")
+
+try:
+ t = T(1, 2, 3)
+except TypeError:
+ print("TypeError")
+
+# Try comma field separator
+T2 = namedtuple("TupComma", "foo,bar")
+t = T2(1, 2)
+print(t)
+print(t.foo, t.bar)
+
+# Try list of fields
+# Not implemented so far
+#T3 = namedtuple("TupComma", ["foo", "bar"])
+#t = T3(1, 2)