Commits

Anonymous committed 8737e7e

assertions for tests

  • Participants
  • Parent commits 1f7f1db

Comments (0)

Files changed (5)

nbt2yaml/parse.py

 import struct
 import gzip
 from collections import namedtuple
+import re
 
 class Tag(object):
     _tags = {}
         raise NotImplementedError()
 
     def __repr__(self):
-        return "TAG_%s" % self.name
+        return "TAG_%s" % re.sub(r'^(\w)|_(\w)', lambda m:m.group(0).upper(), self.name)
 
 class FixedTag(Tag):
     def __init__(self, name, id, size, format):
     stream.write(
         struct.pack(">" + length_type.format, length)
     )
+
 def parse_nbt(stream, gzipped=True):
     if gzipped:
         stream = gzip.GzipFile(fileobj=stream)
     return type_.parse(stream)
 
 def dump_nbt(nbt, stream, gzipped=True):
+    if gzipped:
+        stream = gzip.GzipFile(mode='w', fileobj=stream)
     type_ = nbt[0]
     type_.dump(nbt, stream)

tests/test_dump_nbt.py

         parsed = parse_nbt(datafile(fname))
         out = StringIO.StringIO()
         dump_nbt(parsed, out)
-        eq_(unzipped_data, out.getvalue())
+        out = gzip.GzipFile(fileobj=StringIO.StringIO(out.getvalue())).read()
+        eq_(unzipped_data, out)
 
     def test_basic(self):
         self._assert_data("test.nbt")

tests/test_dump_yaml.py

+import unittest
+from nbt2yaml import parse_nbt, to_yaml
+from tests import datafile, eq_
+
+class ToYamlTest(unittest.TestCase):
+    def test_basic(self):
+        data = parse_nbt(datafile("test.nbt"))
+        eq_(to_yaml(data),
+"""hello world:
+- name: Bananrama
+""")
+
+    def test_large(self):
+        data = parse_nbt(datafile("bigtest.nbt"))
+        eq_(to_yaml(data),
+r"""Level:
+- longTest: !long "9223372036854775807L"
+- shortTest: !short "32767"
+- stringTest: !!python/str "HELLO WORLD THIS IS A TEST STRING \xC5\xC4\xD6!"
+- floatTest: 0.4982314705848694
+- intTest: 2147483647
+- nested compound test:
+  - ham:
+    - name: Hampus
+    - value: 0.75
+  - egg:
+    - name: Eggbert
+    - value: 0.5
+- listTest (long):
+  - !long "11"
+  - !long "12"
+  - !long "13"
+  - !long "14"
+  - !long "15"
+- listTest (compound):
+  - - name: 'Compound tag #0'
+    - created-on: !long "1264099775885L"
+  - - name: 'Compound tag #1'
+    - created-on: !long "1264099775885L"
+- byteTest: !byte "127"
+- byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...)): "\0\
+    >\"\x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1C\
+    P*\x0E`XZ\x02\x188b2\fTB:<H^\x1AD\x14R6$\x1C\x1E*@`&Z4\x18\x06b\0\f\"B\b<\x16\
+    ^LDFR\x04$N\x1E\\@.&(4J\x060\0>\"\x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10\
+    :\nH,\x1A\x12\x14 6V\x1CP*\x0E`XZ\x02\x188b2\fTB:<H^\x1AD\x14R6$\x1C\x1E*@`&Z4\x18\
+    \x06b\0\f\"B\b<\x16^LDFR\x04$N\x1E\\@.&(4J\x060\0>\"\x10\b\n\x16,L\x12F \x04VNP\\\
+    \x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1CP*\x0E`XZ\x02\x188b2\fTB:<H^\x1A\
+    D\x14R6$\x1C\x1E*@`&Z4\x18\x06b\0\f\"B\b<\x16^LDFR\x04$N\x1E\\@.&(4J\x060\0>\"\
+    \x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1CP*\x0E\
+    `XZ\x02\x188b2\fTB:<H^\x1AD\x14R6$\x1C\x1E*@`&Z4\x18\x06b\0\f\"B\b<\x16^LDFR\x04\
+    $N\x1E\\@.&(4J\x060\0>\"\x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10:\n\
+    H,\x1A\x12\x14 6V\x1CP*\x0E`XZ\x02\x188b2\fTB:<H^\x1AD\x14R6$\x1C\x1E*@`&Z4\x18\
+    \x06b\0\f\"B\b<\x16^LDFR\x04$N\x1E\\@.&(4J\x060\0>\"\x10\b\n\x16,L\x12F \x04VNP\\\
+    \x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1CP*\x0E`XZ\x02\x188b2\fTB:<H^\x1A\
+    D\x14R6$\x1C\x1E*@`&Z4\x18\x06b\0\f\"B\b<\x16^LDFR\x04$N\x1E\\@.&(4J\x060\0>\"\
+    \x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1CP*\x0E\
+    `XZ\x02\x188b2\fTB:<H^\x1AD\x14R6$\x1C\x1E*@`&Z4\x18\x06b\0\f\"B\b<\x16^LDFR\x04\
+    $N\x1E\\@.&(4J\x060\0>\"\x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10:\n\
+    H,\x1A\x12\x14 6V\x1CP*\x0E`XZ\x02\x188b2\fTB:<H^\x1AD\x14R6$\x1C\x1E*@`&Z4\x18\
+    \x06b\0\f\"B\b<\x16^LDFR\x04$N\x1E\\@.&(4J\x060\0>\"\x10\b\n\x16,L\x12F \x04VNP\\\
+    \x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1CP*\x0E`XZ\x02\x188b2\fTB:<H^\x1A\
+    D\x14R6$\x1C\x1E*@`&Z4\x18\x06b\0\f\"B\b<\x16^LDFR\x04$N\x1E\\@.&(4J\x060\0>\"\
+    \x10\b\n\x16,L\x12F \x04VNP\\\x0E.X(\x02J802>T\x10:\nH,\x1A\x12\x14 6V\x1CP*\x0E\
+    `XZ\x02\x188b2\fTB:<H^\x1AD\x14R6$\x1C\x1E*@`&Z4\x18\x06b\0\f\"B\b<\x16^LDFR\x04\
+    $N\x1E\\@.&(4J\x060"
+- doubleTest: !double "0.4931287132182315"
+""")

tests/test_parse_nbt.py

 import unittest
-from nbt2yaml import parse_nbt
-from tests import datafile
+from nbt2yaml import parse_nbt, parse
+from tests import datafile, eq_
+import struct
 
 class ParseNBTTest(unittest.TestCase):
     def test_basic(self):
-        print parse_nbt(datafile("test.nbt"))
+        assert_ = (
+                    parse.TAG_Compound, 
+                    u'hello world', 
+                    [(parse.TAG_String, u'name', u'Bananrama')]
+                )
+        eq_(parse_nbt(datafile("test.nbt")), assert_)
 
     def test_large(self):
-        print parse_nbt(datafile("bigtest.nbt"))
+        assert_ = (
+                    parse.TAG_Compound, u'Level', [
+                        (parse.TAG_Long, u'longTest', 9223372036854775807L), 
+                        (parse.TAG_Short, u'shortTest', 32767), 
+                        (parse.TAG_String, u'stringTest', u'HELLO WORLD THIS IS A TEST STRING \xc5\xc4\xd6!'), 
+                        (parse.TAG_Float, u'floatTest', 0.4982314705848694), 
+                        (parse.TAG_Int, u'intTest', 2147483647), 
+                        (parse.TAG_Compound, u'nested compound test', 
+                            [
+                                (parse.TAG_Compound, u'ham', 
+                                    [
+                                        (parse.TAG_String, u'name', u'Hampus'), 
+                                        (parse.TAG_Float, u'value', 0.75)
+                                    ]), 
+                                    (parse.TAG_Compound, u'egg', 
+                                    [
+                                        (parse.TAG_String, u'name', u'Eggbert'), 
+                                        (parse.TAG_Float, u'value', 0.5)
+                                    ])
+                                ]), 
+                                (parse.TAG_List, u'listTest (long)', (parse.TAG_Long, [11, 12, 13, 14, 15])), 
+                    (parse.TAG_List, u'listTest (compound)', 
+                        (parse.TAG_Compound, [
+                            [
+                                (parse.TAG_String, u'name', u'Compound tag #0'), 
+                                (parse.TAG_Long, u'created-on', 1264099775885L)
+                            ], 
+                            [
+                                (parse.TAG_String, u'name', u'Compound tag #1'), 
+                                (parse.TAG_Long, u'created-on', 1264099775885L)
+                            ]
+                        ])), 
+                        (parse.TAG_Byte, u'byteTest', 127), 
+                        (parse.TAG_Byte_Array, 
+                            u'byteArrayTest (the first 1000 values of '
+                                    '(n*n*255+n*7)%100, starting with n=0 '
+                                    '(0, 62, 34, 16, 8, ...))', 
+                                    ''.join([struct.pack('>b', (n*n*255+n*7)%100) for n in range(0, 1000)])
+                            ),
+                        (parse.TAG_Double, u'doubleTest', 0.4931287132182315)
+                    ])
+        eq_(parse_nbt(datafile("bigtest.nbt")), assert_)

tests/test_yaml.py

-import unittest
-from nbt2yaml import parse_nbt, to_yaml
-from tests import datafile
-
-class ToYamlTest(unittest.TestCase):
-    def test_basic(self):
-        data = parse_nbt(datafile("test.nbt"))
-        print "\n", to_yaml(data)
-
-    def test_large(self):
-        data = parse_nbt(datafile("bigtest.nbt"))
-        print "\n", to_yaml(data)