Skip to content

Commit

Permalink
Refactor to add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DonoA committed Jan 5, 2019
1 parent 3452dee commit e026905
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
world/
.vscode/
__pycache__/
.pytest_cache/
5 changes: 1 addition & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/bin/python3
import sys
from world import World, BlockState
from canvas import Canvas
from materials import Material
from biomes import Biome
from pyanvil import World, BlockState, Material

with World('A', save_location='/home/dallen/.minecraft/saves', debug=True) as wrld:
print('World loaded!')
Expand Down
7 changes: 7 additions & 0 deletions pyanvil/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pyanvil.biomes import Biome
from pyanvil.materials import Material
from pyanvil.world import World, Chunk, Block, BlockState
from pyanvil.canvas import Canvas
from pyanvil.schematic import Schematic

import pyanvil.nbt
File renamed without changes.
2 changes: 1 addition & 1 deletion canvas.py → pyanvil/canvas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import sys, math
from schematic import Schematic
from pyanvil.schematic import Schematic

class WorldTask:
def __init__(self, location, new_state):
Expand Down
Empty file added pyanvil/conftest.py
Empty file.
File renamed without changes.
6 changes: 6 additions & 0 deletions nbt.py → pyanvil/nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def serialize(self, stream, include_name=True):
def clone(self):
return type(self)(self.tag_name, self.tag_value)

def __repr__(self):
return f'{type(self).clazz_name}Tag(\'{self.tag_name}\', {str(self.tag_value)})'

def __eq__(self, other):
return self.tag_name == other.tag_name and self.tag_value == other.tag_value

register_parser(tag_id, DataNBTTag)

return DataNBTTag
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 5 additions & 3 deletions world.py → pyanvil/world.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sys, math, nbt, gzip, zlib, stream, time, os
from biomes import Biome
from canvas import Canvas
import sys, math, gzip, zlib, time, os
import pyanvil.nbt as nbt
import pyanvil.stream as stream
from pyanvil.biomes import Biome
from pyanvil.canvas import Canvas

class BlockState:
def __init__(self, name, props):
Expand Down
44 changes: 44 additions & 0 deletions test/nbt_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from pyanvil.nbt import ByteTag, ShortTag, IntTag, LongTag, FloatTag, DoubleTag
from pyanvil.nbt import ByteArrayTag, StringTag, ListTag, CompoundTag, IntArrayTag, LongArrayTag
from pyanvil.nbt import parse_nbt
from pyanvil.stream import OutputStream, InputStream

def build_simple_nbt_tag_test(clz, tag_id, test_val, test_bin):
class TestSimpleNBTTag:
def test_serializing_with_name(args):
tag = clz('Test', test_val)
stream = OutputStream()
tag.serialize(stream, include_name=True)
binary_tag = list(stream.get_data())
expected_tag = [tag_id, 0x0, 0x4] + \
list(b'Test') + \
test_bin
assert binary_tag == expected_tag, f'Tag {clz.clazz_name}'

def test_serializing_without_name(args):
tag = clz('Test', test_val)
stream = OutputStream()
tag.serialize(stream, include_name=False)
binary_tag = list(stream.get_data())
expected_tag = test_bin
assert binary_tag == expected_tag, f'Tag {clz.clazz_name}'

def test_deserializing(args):
raw_tag = InputStream(bytearray([tag_id, 0x0, 0x4] + \
list(b'Test') + \
test_bin))
parsed_tag = parse_nbt(raw_tag)
expected_tag = clz('Test', test_val)
assert parsed_tag == expected_tag, f'Tag {clz.clazz_name}'

return TestSimpleNBTTag

TestByteTag = build_simple_nbt_tag_test(ByteTag, 0x1, 75, [75])
TestShortTag = build_simple_nbt_tag_test(ShortTag, 0x2, 3*10**4, [117, 48])
TestIntTag = build_simple_nbt_tag_test(IntTag, 0x3, 2*10**9, [119, 53, 148, 0])
TestLongTag = build_simple_nbt_tag_test(LongTag, 0x4, 5*10**12, [0, 0, 4, 140, 39, 57, 80, 0])
TestFloat = build_simple_nbt_tag_test(FloatTag, 0x5, 1.5, [63, 192, 0, 0])
TestDouble = build_simple_nbt_tag_test(DoubleTag, 0x6, 125.135, [64, 95, 72, 163, 215, 10, 61, 113])



26 changes: 26 additions & 0 deletions test/stream_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pyanvil.stream import InputStream, OutputStream

class TestInputStream:

def test_reading_data(args):
stream = InputStream(b'Hello World')
read_hello = stream.read(5)
stream.read(1)
read_world = stream.read(5)
assert read_hello == b'Hello'
assert read_world == b'World'

def test_peeking_data(args):
stream = InputStream(b'Hello World')
read_hello = stream.read(5)
peek_space = stream.peek()
assert read_hello == b'Hello'
assert peek_space == 32

class TestOutputStream:

def test_writing_data(args):
stream = OutputStream()
stream.write(b'Hello World')
stream_data = stream.get_data()
assert stream_data == b'Hello World'

0 comments on commit e026905

Please sign in to comment.