简单Elixir游戏服设计- 给table增加测试代码
测试驱动,先simple_table_test.exs 里增加测试吧。
目前想到的这么多,如果遗漏后续增加, 写测试代码比业务代码还是难,
好处是提前想下模块的api接口。
像我这样一下子写那么多测试,其实也不对,反馈周期太长,测试驱动就没乐趣了,
而且代码调整可能导致测试要重新调整。
(这里只是示例,以后就不这么搞了)
另外想了下, 像table这样的整装(意思是协调调用其他模块)模块,
为了方便测试,还是应该分出2种不同风格的api。
一种是返回 {:ok, table} 和 {:error, reason}的,对接逻辑
一种是纯粹操作table的, 方便测试用。
上测试代码先,测试肯定会失败
test "开局:少于2个人的时候不能", %{table: table, player1: player1} do assert {:error, ErrorMsg.player_not_enough} == table |> SimpleTable.add_seat |> SimpleTable.start(player1) end test "开局:正在玩中不能", %{table: table, player1: player1} do assert {:error, ErrorMsg.can_not_start_when_playing} == table |> SimpleTable.set_playing |> SimpleTable.start(player1) end test "开局: 准备阶段且人数至少2, 且只有房主能开局", %{table: table, player1: player1, player2: player2} do table = table |> SimpleTable.set_creator(player1) |> SimpleTable.add_seat(player1) |> SimpleTable.add_seat(player2) assert {:error, ErrorMsg.just_creator_can_start} == table |> SimpleTable.start(player2) assert {:ok, new_table} = table |> SimpleTable.start(player1) assert table |> SimpleTable.is_playing? end test "解散:正在玩中不能", %{table: table, player1: player1} do assert {:error, ErrorMsg.can_not_dismiss_when_playing} == table |> SimpleTable.set_playing |> SimpleTable.dismiss(player1) end test "解散:准备阶段只有房主可以", %{table: table, player1: player1, player2: player2} do table = table |> SimpleTable.set_creator(player1) assert {:error, ErrorMsg.just_creator_can_dismiss} == table |> SimpleTable.dismiss(player2) assert {:ok, new_table} = table |> SimpleTable.dismiss(player1) assert new_table |> SimpleTable.is_dismiss? end test "加入:正在玩中不能(以后支持?)", %{table: table, player1: player1} do assert {:error, ErrorMsg.can_not_join_new_player_when_playing} == table |> SimpleTable.set_playing |> SimpleTable.join(player1) end test "加入:准备阶段可以", %{table: table, player1: player1} do assert {:ok, _new_table} = table |> SimpleTable.join(player1) end test "加入: 重复不可以", %{table: table, player1: player1} do assert {:error, ErrorMsg.repeated_join} == table |> SimpleTable.add_seat(player1) |> SimpleTable.join(player1) end test "退出:正在玩中不能(以后支持?)", %{table: table, player1: player1} do assert {:error, ErrorMsg.can_not_quit_when_playing} == table |> SimpleTable.set_playing |> SimpleTable.quit(player1) end test "退出:房主不能(以后支持?)", %{table: table, player1: player1} do assert {:error, ErrorMsg.can_not_quit_when_creator} == table |> SimpleTable.set_creator(player1) |> SimpleTable.quit(player1) end test "测试发牌", %{table: table, player1: player1, player2: player2} do cards = [{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}] table = table |> SimpleTable.set_cards(cards) |> SimpleTable.add_seat(player1) |> SimpleTable.add_seat(player2) |> SimpleTable.init_deal assert [{1, 1}, {1, 2}] == table |> SimpleTable.find_seat(player1) |> Seat.get_cards assert [{1, 3}, {1, 4}] == table |> SimpleTable.find_seat(player2) |> Seat.get_cards end test "补牌: 非玩中不能", %{table: table, player1: player1} do assert {:error, ErrorMsg.can_not_deal_when_not_playing} == table |> SimpleTable.deal_one(player1) end test "补牌: 已经翻牌不能", %{table: table, player1: player1} do seat = Seat.init(player1) |> Seat.open assert {:error, ErrorMsg.can_not_make_up_when_open} == table |> SimpleTable.update_seat(seat) |> SimpleTable.make_up(player1) end test "补牌: 已经三张了不能再补", %{table: table, player1: player1} do seat = Seat.init(player1) |> Seat.add_cards([1, 2, 3]) assert {:error, ErrorMsg.can_not_make_up_when_full} == table |> SimpleTable.update_seat(seat) |> SimpleTable.make_up(player1) end test "翻牌: 不是天公牌不能", %{table: table, player1: player1} do seat = Seat.init(player1) |> Seat.add_cards([{1, 2}, {1, 5}]) assert {:error, ErrorMsg.just_tian_gong_can_open} == table |> SimpleTable.update_seat(seat) |> SimpleTable.open(player1) end test "翻牌: 已经翻过了不能", %{table: table, player1: player1} do seat = Seat.init(player1) |> Seat.open assert {:error, ErrorMsg.repeated_open} == table |> SimpleTable.update_seat(seat) |> SimpleTable.open(player1) end
回头我们补上代码,让测试通过。
其实数据操作的测试和逻辑操作的测试分成2个测试文件会更清晰点,
我就不做了,还是用1个文件。