-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix block.py and add test for file block.py #25
base: master
Are you sure you want to change the base?
Changes from 5 commits
ed6a196
ff8a464
4d18907
d6db56f
9a1edc7
b69f607
9182af3
1c6ec04
810f99d
728b943
acf013c
bed58c6
11bb748
c8dad24
2d3500c
3c1fd01
f42e1f3
ffb7ca3
b677dc6
a3e3a00
8cb35c0
23f43fe
2f58179
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,68 @@ | ||
import sys | ||
if sys.version_info < (3, 4): | ||
from flufl.enum import Enum | ||
else: | ||
from enum import Enum | ||
from enum import IntEnum | ||
|
||
|
||
class Block: | ||
""" | ||
Minecraft PI block description. Can be sent to Minecraft.setBlock/s | ||
block.type = the blockID of a block (It's meterial) | ||
block.data = A unknown member that does something | ||
The default type for blocks is dirt | ||
""" | ||
Minecraft PI block description. Can be sent to Minecraft.setBlock/s | ||
block.type = the blockID of a block (It's material) | ||
block.data = The variant of the type of block. | ||
For example the colour of wool or the orientation of stairs | ||
The default type for blocks is dirt | ||
""" | ||
|
||
def __init__(self, type=3, data=0): | ||
self.type = type | ||
self.data = data | ||
|
||
def __cmp__(self, rhs): | ||
return hash(self) - hash(rhs) | ||
def __eq__(self, rhs): | ||
""" | ||
Equality override | ||
Two blocks are equal only if their hashes are equal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Except that's only true in this case where |
||
""" | ||
return hash(self) == hash(rhs) | ||
|
||
def __ne__(self, rhs): | ||
""" | ||
not equal override | ||
""" | ||
return not (self == rhs) | ||
|
||
def __hash__(self): | ||
return (self.id << 8) + self.data | ||
""" | ||
Override of hash generation | ||
Returns a unique representation of contents of block | ||
""" | ||
return (self.type << 8) + self.data | ||
|
||
# TODO: This looks wierd, does it have any use? | ||
def __iter__(self): | ||
"""Allows a Block to be sent whenever id [and data] is needed""" | ||
return iter((self.id, self.data)) | ||
""" | ||
Returns an Iterator of the contents of the Block class | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this changes a bit of the API we don't want to change, but we aren't testing thoroughly enough :s |
||
Makes the Block an Iterable object | ||
This means that Block can be treated like a list/tuple in places | ||
list/tuple of type and data | ||
For example when flattening lists or *args | ||
Allows a Block to be sent whenever id [and data] is needed | ||
""" | ||
return iter((self.type, self.data)) | ||
|
||
def __repr__(self): | ||
return 'Block({}, {:.2f})'.format(self.id, self.data) | ||
""" Override string representation """ | ||
return 'Block({:d}, {:d})'.format(self.type, self.data) | ||
|
||
# TODO: find out if this has any use | ||
def withData(self, data): | ||
return Block(self.id, data) | ||
""" | ||
Returns a new block with the same type as the invoking block but | ||
with the passed in data value | ||
""" | ||
return Block(self.type, data) | ||
|
||
@property | ||
def id(self): | ||
""" Here for backwards compatibility for previous attribute name """ | ||
return self.type | ||
|
||
|
||
class blockType(Enum): | ||
class blockType(IntEnum): | ||
AIR = 0 | ||
STONE = 1 | ||
GRASS = 2 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import unittest | ||
from minecraft.block import Block | ||
from minecraft.block import blockType | ||
|
||
|
||
class TestBlock(unittest.TestCase): | ||
|
||
def testRepresentation(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PEP8 / Pythonic method names please |
||
# Test repr | ||
b = Block(2, 8) | ||
expectedString = "Block({:d}, {:d})".format(b.type, b.data) | ||
rep = repr(b) | ||
self.assertEqual(rep, expectedString) | ||
|
||
def testInstantiationAndWithDataFunction(self): | ||
blck = Block(12) | ||
self.assertEqual(blck.type, 12) | ||
self.assertEqual(blck.data, 0) | ||
blckWithData = Block(12, 4) | ||
self.assertEqual(blckWithData.type, 12) | ||
self.assertEqual(blckWithData.data, 4) | ||
otherBlckWithData = blck.withData(8) | ||
self.assertEqual(otherBlckWithData.type, 12) | ||
self.assertEqual(otherBlckWithData.data, 8) | ||
|
||
def testBackwardsCompatibility(self): | ||
blck = Block(12) | ||
self.assertEqual(blck.type, blck.id) | ||
|
||
def testComparison(self): | ||
b1 = Block(8, 3) | ||
bSame = Block(8, 3) | ||
bDiffId = Block(12, 3) | ||
bDiffData = Block(8, 7) | ||
bDiffIdAndData = Block(51, 7) | ||
|
||
self.assertTrue(b1 == b1) | ||
self.assertTrue(b1 == bSame) | ||
self.assertTrue(b1 != bDiffId) | ||
self.assertTrue(b1 != bDiffData) | ||
self.assertTrue(b1 != bDiffIdAndData) | ||
|
||
def testIteration(self): | ||
idAndData = [35, 4] | ||
b = Block(idAndData[0], idAndData[1]) | ||
for index, attr in enumerate(b): | ||
self.assertEqual(attr, idAndData[index]) | ||
|
||
def testBlockConstants(self): | ||
self.assertEqual(blockType.AIR, 0) | ||
self.assertEqual(blockType.STONE, 1) | ||
self.assertEqual(blockType.GRASS, 2) | ||
self.assertEqual(blockType.DIRT, 3) | ||
self.assertEqual(blockType.COBBLESTONE, 4) | ||
self.assertEqual(blockType.WOOD_PLANKS, 5) | ||
self.assertEqual(blockType.SAPLING, 6) | ||
self.assertEqual(blockType.BEDROCK, 7) | ||
self.assertEqual(blockType.WATER_FLOWING, 8) | ||
self.assertEqual(blockType.WATER, blockType.WATER_FLOWING) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you pull this out into a distinct test please? (as it's somewhat different to the other "is the id the one hardcoded here" tests but easy to not notice!) |
||
self.assertEqual(blockType.WATER_STATIONARY, 9) | ||
self.assertEqual(blockType.LAVA_FLOWING, 10) | ||
self.assertEqual(blockType.LAVA, blockType.LAVA_FLOWING) | ||
self.assertEqual(blockType.LAVA_STATIONARY, 11) | ||
self.assertEqual(blockType.SAND, 12) | ||
self.assertEqual(blockType.GRAVEL, 13) | ||
self.assertEqual(blockType.GOLD_ORE, 14) | ||
self.assertEqual(blockType.IRON_ORE, 15) | ||
self.assertEqual(blockType.COAL_ORE, 16) | ||
self.assertEqual(blockType.WOOD, 17) | ||
self.assertEqual(blockType.LEAVES, 18) | ||
self.assertEqual(blockType.GLASS, 20) | ||
self.assertEqual(blockType.LAPIS_LAZULI_ORE, 21) | ||
self.assertEqual(blockType.LAPIS_LAZULI_BLOCK, 22) | ||
self.assertEqual(blockType.SANDSTONE, 24) | ||
self.assertEqual(blockType.BED, 26) | ||
self.assertEqual(blockType.COBWEB, 30) | ||
self.assertEqual(blockType.GRASS_TALL, 31) | ||
self.assertEqual(blockType.WOOL, 35) | ||
self.assertEqual(blockType.FLOWER_YELLOW, 37) | ||
self.assertEqual(blockType.FLOWER_CYAN, 38) | ||
self.assertEqual(blockType.MUSHROOM_BROWN, 39) | ||
self.assertEqual(blockType.MUSHROOM_RED, 40) | ||
self.assertEqual(blockType.GOLD_BLOCK, 41) | ||
self.assertEqual(blockType.IRON_BLOCK, 42) | ||
self.assertEqual(blockType.STONE_SLAB_DOUBLE, 43) | ||
self.assertEqual(blockType.STONE_SLAB, 44) | ||
self.assertEqual(blockType.BRICK_BLOCK, 45) | ||
self.assertEqual(blockType.TNT, 46) | ||
self.assertEqual(blockType.BOOKSHELF, 47) | ||
self.assertEqual(blockType.MOSS_STONE, 48) | ||
self.assertEqual(blockType.OBSIDIAN, 49) | ||
self.assertEqual(blockType.TORCH, 50) | ||
self.assertEqual(blockType.FIRE, 51) | ||
self.assertEqual(blockType.STAIRS_WOOD, 53) | ||
self.assertEqual(blockType.CHEST, 54) | ||
self.assertEqual(blockType.DIAMOND_ORE, 56) | ||
self.assertEqual(blockType.DIAMOND_BLOCK, 57) | ||
self.assertEqual(blockType.CRAFTING_TABLE, 58) | ||
self.assertEqual(blockType.FARMLAND, 60) | ||
self.assertEqual(blockType.FURNACE_INACTIVE, 61) | ||
self.assertEqual(blockType.FURNACE_ACTIVE, 62) | ||
self.assertEqual(blockType.DOOR_WOOD, 64) | ||
self.assertEqual(blockType.LADDER, 65) | ||
self.assertEqual(blockType.STAIRS_COBBLESTONE, 67) | ||
self.assertEqual(blockType.DOOR_IRON, 71) | ||
self.assertEqual(blockType.REDSTONE_ORE, 73) | ||
self.assertEqual(blockType.SNOW, 78) | ||
self.assertEqual(blockType.ICE, 79) | ||
self.assertEqual(blockType.SNOW_BLOCK, 80) | ||
self.assertEqual(blockType.CACTUS, 81) | ||
self.assertEqual(blockType.CLAY, 82) | ||
self.assertEqual(blockType.SUGAR_CANE, 83) | ||
self.assertEqual(blockType.FENCE, 85) | ||
self.assertEqual(blockType.GLOWSTONE_BLOCK, 89) | ||
self.assertEqual(blockType.BEDROCK_INVISIBLE, 95) | ||
self.assertEqual(blockType.STONE_BRICK, 98) | ||
self.assertEqual(blockType.GLASS_PANE, 102) | ||
self.assertEqual(blockType.MELON, 103) | ||
self.assertEqual(blockType.FENCE_GATE, 107) | ||
self.assertEqual(blockType.GLOWING_OBSIDIAN, 246) | ||
self.assertEqual(blockType.NETHER_REACTOR_CORE, 247) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the indentation change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is unnecessary, can you keep the indentation of docstrings to one level in line with PEP 257 please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ghickman Got the pep8/flake8 skillz to add this to tests so you don't need to keep hand-PEPing? ;P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unintentional will alter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hashbangstudio – Thanks!