Skip to content

Instantly share code, notes, and snippets.

@jyfeather
Last active December 18, 2015 21:38
Show Gist options
  • Save jyfeather/5848589 to your computer and use it in GitHub Desktop.
Save jyfeather/5848589 to your computer and use it in GitHub Desktop.
Ruby工程简明模板以及自动化测试
require 'test/unit'
require_relative '../lib/game' # 用 require_relative 加载本地文件,省略后缀
class MyTest < Test::Unit::TestCase # 三个测试用例
def test_room()
gold = Room.new("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, {})
end
def test_room_paths()
center = Room.new("Center", "Test room in the center.")
north = Room.new("North", "Test room in the north.")
south = Room.new("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)
end
def test_map()
start = Room.new("Start", "You can go west and down a hole.")
west = Room.new("Trees", "There are trees here, you can go east.")
down = Room.new("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)
end
end
class Room
attr_accessor :name, :description, :paths # 相当于类变量,生成getter、setter方法
def initialize(name, description)
@name = name
@description = description
@paths = {}
end
def go(direction)
@paths[direction]
end
def add_paths(paths)
@paths.update(paths)
end
end
Run options:
# Running tests:
...
Finished tests in 0.005094s, 588.9282 tests/s, 1374.1657 assertions/s.
3 tests, 7 assertions, 0 failures, 0 errors, 0 skips
ruby -v: ruby 2.0.0p195 (2013-05-14 revision 40734) [x86_64-darwin12.4.0]
Process finished with exit code 0
├── ex47
│   ├── bin
│   ├── game.gemspec
│   ├── lib
│   │   ├── game
│   │   │   └── version.rb
│   │   └── game.rb
│   └── test
│   └── ex47_test.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment