#coding:UTF-8 | |
from nose.tools import * | |
import nose | |
import sys | |
sys.path.append("../ex47") # 把 这个文件的上级目录的 ex47 加下 python 的模组 索引路径目录 | |
from ex47.game import Room # 把 ex47这个文件夹下的 game.py 里的 Room 类导入进来 | |
def test_room(): | |
gold = Room("GoldRoom", | |
"""This room has gold in it you can grab. There's a | |
door to the north.""") # 创建一个 Room 类的对象 gold , 参数1表示name 2表示description | |
assert_equal(gold.name, "GoldRoom") | |
assert_equal(gold.paths, {}) # {} 表示空字典 |
class Room(object): | |
def __init__(self , name , description): | |
self.name = name | |
self.description = description | |
self.paths = {} | |
def go(self , direction): | |
return self.paths.get(direction , None) | |
def add_paths(self , paths): | |
self.paths.update(paths) | |
class Room(object): | |
def __init__(self , name , description): | |
self.name = name | |
self.description = description | |
self.paths = {} | |
def go(self , direction): | |
return self.paths.get(direction , None) | |
def add_paths(self , paths): | |
self.paths.update(paths) | |
from nose.tools import * | |
import nose | |
import sys | |
sys.path.append("../ex47") | |
from ex47.game import Room | |
def test_room(): | |
gold = Room("GoldRoom", | |
"""This room has gold in it you can grab. There's a | |
door to the north.""") | |
assert_equal(gold.name, "GoldRoom") | |
assert_equal(gold.paths, {}) | |
def test_room_paths(): | |
center = Room("Center", "Test room in the center.") | |
north = Room("North", "Test room in the north.") | |
south = Room("South", "Test room in the south.") | |
center.add_paths({'north': north, 'south': south}) | |
assert_equal(center.go('north'), north) | |
assert_equal(center.go('south'), south) | |
def test_map(): | |
start = Room("Start", "You can go west and down a hole.") | |
west = Room("Trees", "There are trees here, you can go east.") | |
down = Room("Dungeon", "It's dark down here, you can go up.") | |
start.add_paths({'west': west, 'down': down}) | |
west.add_paths({'east': start}) | |
down.add_paths({'up': start}) | |
assert_equal(start.go('west'), west) | |
assert_equal(start.go('west').go('east'), start) | |
assert_equal(start.go('down').go('up'), start) |
![]() |
1
rrfeng 2013-11-15 18:07:13 +08:00
help(assert_equal())
|
![]() |
4
rrfeng 2013-11-15 20:38:42 +08:00
或者直接去看源码呀。。。
|