forked from bcdice/BCDice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_game_system_commands.rb
95 lines (75 loc) · 2.47 KB
/
test_game_system_commands.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# frozen_string_literal: true
require "test/unit"
require "tomlrb"
require "json"
require "bcdice"
require "bcdice/game_system"
require_relative "randomizer_mock"
class TestGameSystemCommands < Test::Unit::TestCase
class << self
def target_files
env_target = ENV["target"]
unless env_target
return Dir.glob("test/data/*.toml")
end
files = env_target.split(",").map do |target|
target_with_extension = target.end_with?(".toml") ? target : "#{target}.toml"
path = "test/data/#{target_with_extension}"
unless File.exist?(path)
warn("Unknown target: #{path}")
next nil
end
path
end
return files.compact
end
end
data do
data_set = {}
files = target_files()
if files.empty?
warn("No target found!")
exit(1)
end
files.each do |filename|
filename_base = File.basename(filename, ".toml")
data = Tomlrb.load_file(filename, symbolize_keys: true)
systems = data[:test].map { |t| t[:game_system] }.uniq
unless systems.count == 1
warn("Multiple game_system (#{systems.join(',')}) in #{filename}.")
exit(1)
end
data[:test].each.with_index(1) do |test_case, index|
test_case[:filename] = filename
test_case[:output] = nil if test_case[:output].empty? # TOMLではnilを表現できないので空文字で代用
test_case[:secret] ||= false
test_case[:success] ||= false
test_case[:failure] ||= false
test_case[:critical] ||= false
test_case[:fumble] ||= false
key = [filename_base, index, test_case[:input]].join(":")
data_set[key] = test_case
end
end
data_set
end
def test_diceroll(data)
klass = BCDice.game_system_class(data[:game_system])
game_system = klass.new(data[:input])
rands = data[:rands].map { |r| [r[:value], r[:sides]] }
game_system.randomizer = RandomizerMock.new(rands)
msg = JSON.pretty_generate(data)
result = game_system.eval()
if result.nil?
assert_nil(data[:output])
return
end
assert_equal(data[:output], result.text, msg)
assert_equal(data[:secret], result.secret?, msg)
assert_equal(data[:success], result.success?, msg)
assert_equal(data[:failure], result.failure?, msg)
assert_equal(data[:critical], result.critical?, msg)
assert_equal(data[:fumble], result.fumble?, msg)
assert_match(klass.command_pattern, data[:input])
end
end