/
cucumber_js_mappings.rb
238 lines (201 loc) · 6.9 KB
/
cucumber_js_mappings.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
module CucumberJsMappings
STEP_DEFINITIONS_FILE = "features/step_definitions/cucumber_steps.js"
COFFEE_SCRIPT_DEFINITIONS_FILE = "features/step_definitions/cucumber_steps.coffee"
FEATURE_FILE = "features/a_feature.feature"
WORLD_VARIABLE_LOG_FILE = "world_variable.log"
WORLD_FUNCTION_LOG_FILE = "world_function.log"
DATA_TABLE_LOG_FILE = "data_table.log"
CYCLE_LOG_FILE = "cycle.log"
attr_accessor :support_code
def features_dir
'features'
end
def run_feature
write_main_step_definitions_file
run_simple "#{cucumber_bin} #{FEATURE_FILE}", false
end
def cucumber_bin
File.expand_path(File.dirname(__FILE__) + '/../../bin/cucumber.js')
end
def write_passing_mapping(step_name)
append_step_definition(step_name, "// no-op, pass gently\ncallback();")
end
def write_pending_mapping(step_name)
append_step_definition(step_name, "callback.pending();")
end
def write_failing_mapping(step_name)
write_failing_mapping_with_message(step_name, "I was supposed to fail.")
end
def write_failing_mapping_with_message(step_name, message)
append_step_definition(step_name, "throw(new Error('#{message}'));")
end
def write_mapping_incrementing_world_variable_by_value(step_name, increment_value)
append_step_definition(step_name, "this.variable += #{increment_value}; callback();")
end
def write_mapping_logging_world_variable_value(step_name, time = "1")
step_def = <<-EOF
fs.writeFileSync("#{WORLD_VARIABLE_LOG_FILE}.#{time}", "" + this.variable); callback();
EOF
append_step_definition(step_name, step_def)
end
def write_mapping_calling_world_function(step_name)
step_def = <<-EOF
this.someFunction(); callback();
EOF
append_step_definition(step_name, step_def)
end
def write_mapping_receiving_data_table_as_raw(step_name)
body = <<-EOF
var dataTableArray = dataTable.raw();
var dataTableJSON = JSON.stringify(dataTableArray);
fs.writeFileSync("#{DATA_TABLE_LOG_FILE}", "" + dataTableJSON);
callback();
EOF
append_step_definition(step_name, body, ["dataTable"])
end
def write_mapping_receiving_data_table_as_hashes(step_name)
body = <<-EOF
var dataTableHashes = dataTable.hashes();
var dataTableJSON = JSON.stringify(dataTableHashes);
fs.writeFileSync("#{DATA_TABLE_LOG_FILE}", "" + dataTableJSON);
callback();
EOF
append_step_definition(step_name, body, ["dataTable"])
end
def write_calculator_code
rpn_calculator_code = get_file_contents('../support/rpn_calculator.js')
create_dir 'features/support'
write_file 'features/support/rpn_calculator.js', rpn_calculator_code
end
def write_mappings_for_calculator
calculator_steps = get_file_contents('./calculator_steps.js')
write_file 'features/step_definitions/calculator_steps.js', calculator_steps
append_support_code <<-EOF
var RpnCalculator = require('../support/rpn_calculator');
var calculatorSteps = require('./calculator_steps');
calculatorSteps.initialize.call(this, RpnCalculator);
EOF
end
def write_world_variable_with_numeric_value(value)
append_support_code <<-EOF
this.World.prototype.variable = #{value};
EOF
end
def write_custom_world_constructor
append_support_code "this.World = function CustomWorld() {};"
end
def write_world_function
append_support_code <<-EOF
this.World.prototype.someFunction = function() {
fs.writeFileSync("#{WORLD_FUNCTION_LOG_FILE}", "");
};
EOF
end
def assert_passing_scenario
assert_partial_output("1 scenario (1 passed)", all_output)
assert_success true
end
def assert_failing_scenario
assert_partial_output("1 scenario (1 failed)", all_output)
assert_success false
end
def assert_pending_scenario
assert_partial_output("1 scenario (1 pending)", all_output)
assert_success true
end
def assert_undefined_scenario
assert_partial_output("1 scenario (1 undefined)", all_output)
assert_success true
end
def assert_scenario_reported_as_failing(scenario_name)
assert_partial_output("# Scenario: #{scenario_name}", all_output)
assert_success false
end
def assert_scenario_not_reported_as_failing(scenario_name)
assert_no_partial_output("# Scenario: #{scenario_name}", all_output)
end
def assert_world_variable_held_value_at_time(value, time)
check_exact_file_content "#{WORLD_VARIABLE_LOG_FILE}.#{time}", value
end
def assert_world_function_called
check_file_presence [WORLD_FUNCTION_LOG_FILE], true
end
def assert_data_table_equals_json(json)
prep_for_fs_check do
log_file_contents = IO.read(DATA_TABLE_LOG_FILE)
actual_array = JSON.parse(log_file_contents)
expected_array = JSON.parse(json)
actual_array.should == expected_array
end
end
def assert_suggested_step_definition_snippet(stepdef_type, stepdef_name, parameter_count = 0, doc_string = false, data_table = false)
parameter_count ||= 0
params = Array.new(parameter_count) { |i| "arg#{i+1}" }
params << "string" if doc_string
params << "table" if data_table
params << "callback"
params = params.join ", "
expected_snippet = <<-EOF
this.#{stepdef_type}(#{stepdef_name}, function(#{params}) {
// express the regexp above with the code you wish you had
callback.pending();
});
EOF
assert_partial_output(expected_snippet, all_output)
end
def failed_output
"failed"
end
protected
def append_step_definition(step_name, code, params = [])
params.push("callback");
params_string = params.join(", ")
indented_code = indent_code(code).rstrip
append_support_code <<-EOF
this.defineStep(/#{step_name}/, function(#{params_string}) {
fs.writeFileSync("#{step_file(step_name)}", "");
#{indented_code}
});
EOF
end
def append_support_code(code)
@support_code ||= ''
@support_code += indent_code(code)
end
def write_main_step_definitions_file
append_to_file(STEP_DEFINITIONS_FILE, "var fs = require('fs');\nvar stepDefinitions = function() {\n");
append_to_file(STEP_DEFINITIONS_FILE, world_code)
append_to_file(STEP_DEFINITIONS_FILE, support_code);
append_to_file(STEP_DEFINITIONS_FILE, "};\nmodule.exports = stepDefinitions;")
end
def world_code
<<-EOF
this.World = function() {
this.callCount = 0;
};
this.World.prototype.logCycleEvent = function logCycleEvent(name) {
fd = fs.openSync('#{CYCLE_LOG_FILE}', 'a');
fs.writeSync(fd, " -> " + name, null);
fs.closeSync(fd);
};
EOF
end
def write_coffee_script_definition_file
append_to_file COFFEE_SCRIPT_DEFINITIONS_FILE, <<-EOF
fs = require('fs')
stepDefinitions = () ->
this.defineStep(/^a mapping$/, (callback) ->
fs.writeFileSync('a_mapping.step', '')
callback()
)
module.exports = stepDefinitions
EOF
end
def get_file_contents(file_path)
file_realpath = File.expand_path(file_path, File.dirname(__FILE__))
File.open(file_realpath, 'rb') do |f|
f.read
end
end
end
World(CucumberJsMappings)