设为首页 加入收藏

TOP

简单Elixir游戏服设计-使table测试通过(二)
2017-10-16 18:18:42 】 浏览:6409
Tags:简单 Elixir 游戏 设计 table 测试 通过
;(&1 ++ [seat_id])) def remove_seat(table, %{} = player), do: remove_seat(table, player |> Player.get_id) def remove_seat(table, player_id) do table = update_in(table.seat_map, fn m -> Map.delete(m, player_id) end) update_in(table.seat_order, fn o -> List.delete(o, player_id) end) end def start(table, player) do cond do is_playing?(table) -> {:error, ErrorMsg.can_not_start_when_playing} seat_count(table) < 2 -> {:error, ErrorMsg.player_not_enough} not is_creator?(table, player) -> {:error, ErrorMsg.just_creator_can_start} true -> table = table |> set_playing {:ok, table} end end def quit(table, player) do cond do is_playing?(table) -> {:error, ErrorMsg.can_not_quit_when_playing} is_creator?(table, player) -> {:error, ErrorMsg.can_not_quit_when_creator} end end def dismiss(table, player) do cond do is_playing?(table) -> {:error, ErrorMsg.can_not_dismiss_when_playing} not is_creator?(table, player) -> {:error, ErrorMsg.just_creator_can_dismiss} true -> table = table |> set_dismiss {:ok, table} end end def make_up(table, player) do cond do is_ready?(table) -> {:error, ErrorMsg.can_not_make_up_when_not_playing} find_seat(table, player) |> Seat.is_open? -> {:error, ErrorMsg.can_not_make_up_when_open} find_seat(table, player) |> Seat.is_full? -> {:error, ErrorMsg.can_not_make_up_when_full} end end def join(table, player) do cond do is_playing?(table) -> {:error, ErrorMsg.can_not_join_when_playing} find_seat(table, player) -> {:error, ErrorMsg.repeated_join} true -> table = table |> add_seat(player) {:ok, table} end end def open(table, player) do cond do find_seat(table, player) |> Seat.is_open? -> {:error, ErrorMsg.repeated_open} not (find_seat(table, player) |> Seat.get_cards |> SimplePoker.can_be_tian_gong?) -> {:error, ErrorMsg.just_tian_gong_can_open} end end def is_creator?(table, player), do: table.creator |> Player.get_id == player |> Player.get_id end simple_table.ex

测试果然发现需要调整,分解测试还是需要小心。

好在有测试的话,很容易就发现问题。这大概是测试的好处了吧。

还有其他的小改动, seat.ex  simple_poker.ex

具体看git吧

果然,写代码比写测试快得多, 看着测试一个一个通过,还是挺享受的。

另外,看代码 join 和 quit 的cond 语句少了true, 显然说明我们的测试还没覆盖到!!!!

测试使代码更容易编写,代码又辅助发现测试不足。相辅相成。

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇java线程锁基础 下一篇简单Elixir游戏服设计-测试驱动?

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目