Class: Cucumberator::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumberator/writer.rb

Constant Summary collapse

FULL_BACKTRACE =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(world, scenario, step_line = nil) ⇒ Writer

Returns a new instance of Writer.



7
8
9
10
11
12
13
14
15
# File 'lib/cucumberator/writer.rb', line 7

def initialize(world, scenario, step_line = nil)
  @world, @scenario = world, scenario
  @step_line = @original_step_line = step_line if step_line
  @saved_stack = []

  check_scenario
  set_autocomplete
  read_input
end

Instance Attribute Details

#exit_flagObject

Returns the value of attribute exit_flag.



3
4
5
# File 'lib/cucumberator/writer.rb', line 3

def exit_flag
  @exit_flag
end

#last_inputObject

Returns the value of attribute last_input.



3
4
5
# File 'lib/cucumberator/writer.rb', line 3

def last_input
  @last_input
end

#original_step_lineObject

Returns the value of attribute original_step_line.



3
4
5
# File 'lib/cucumberator/writer.rb', line 3

def original_step_line
  @original_step_line
end

#saved_stackObject

Returns the value of attribute saved_stack.



3
4
5
# File 'lib/cucumberator/writer.rb', line 3

def saved_stack
  @saved_stack
end

#scenarioObject

Returns the value of attribute scenario.



3
4
5
# File 'lib/cucumberator/writer.rb', line 3

def scenario
  @scenario
end

#step_lineObject

Returns the value of attribute step_line.



3
4
5
# File 'lib/cucumberator/writer.rb', line 3

def step_line
  @step_line
end

#worldObject

Returns the value of attribute world.



3
4
5
# File 'lib/cucumberator/writer.rb', line 3

def world
  @world
end

Instance Method Details

#all_defined_stepsObject



40
41
42
43
# File 'lib/cucumberator/writer.rb', line 40

def all_defined_steps
  support_code = current_visitor.runtime.instance_variable_get("@support_code")
  support_code.step_definitions.map { |sd| sd.regexp_source }
end

#check_scenarioObject



17
18
19
# File 'lib/cucumberator/writer.rb', line 17

def check_scenario
  raise "Sorry, cucumberator is not available when scenario is already failing!" if scenario.failed?
end

#current_visitorObject



36
37
38
# File 'lib/cucumberator/writer.rb', line 36

def current_visitor
  @current_visitor ||= scenario.instance_variable_get("@current_visitor")
end

#detect_next_stepObject



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/cucumberator/writer.rb', line 206

def detect_next_step
  next_step = nil

  scenario.steps.each do |step|
    if step.status == :skipped and not step.backtrace_line["Then I will write new steps"]
      next_step = step
      break
    end
  end

  next_step
end

#display_current_locationObject



173
174
175
176
177
# File 'lib/cucumberator/writer.rb', line 173

def display_current_location
  display_line(step_line - 1)
  display_line(step_line, current: true)
  display_line(step_line + 1)
end

#display_helpObject

COMMANDS



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cucumberator/writer.rb', line 105

def display_help
  puts ":: Write a step here and watch it happen on the browser."
  puts ":: Steps are automatically saved unless it raises exception. Use 'save' to force-save it anyway."
  puts ":: Available commands:"
  puts "::   save      - force-saves last step into current feature file"
  puts "::   last-step - display last executed step (to be saved on 'save' command)"
  puts "::   undo      - remove last saved line from feature file"
  puts "::   next      - execute next step and stop"
  puts "::   steps     - display available steps"
  puts "::   where     - display current location in file"
  puts "::   exit      - exits current scenario"
  puts "::   exit-all  - exists whole Cucumber feature"
  puts "::   help      - display this notification"
end

#display_line(line_number, opts = {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/cucumberator/writer.rb', line 179

def display_line(line_number, opts = {})
  lines = feature_file_lines
  line_string = sprintf("%3d", line_number)

  if opts[:current]
    line_string << ": -> "
  else
    line_string << ":    "
  end

  line_string << lines[line_number]
  puts line_string
end

#display_stepsObject



223
224
225
226
227
228
229
230
# File 'lib/cucumberator/writer.rb', line 223

def display_steps
  if @steps and @steps.size > 0
    puts ":: Yay, you have #{@steps.size} steps in your pocket:"
    @steps.each { |s| puts s }
  else
    puts ":: Sorry, no steps detected?"
  end
end

#execute_cucumber_step(input) ⇒ Object



150
151
152
153
154
155
# File 'lib/cucumberator/writer.rb', line 150

def execute_cucumber_step(input)
  return if input.blank?

  self.last_input = input
  world.steps(input)
end

#execute_next_stepObject



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/cucumberator/writer.rb', line 193

def execute_next_step
  next_step = detect_next_step

  if next_step
    puts next_step.backtrace_line
    current_visitor.visit_step(next_step)
    self.step_line = next_step.file_colon_line.split(':').last.to_i + 1
  else
    puts ":: Looks like it's the end of feature file. Happy coding! <3"
    mark_exit(true)
  end
end

#feature_fileObject

HELPERS



234
235
236
# File 'lib/cucumberator/writer.rb', line 234

def feature_file
  @feature_file ||= File.join(Rails.root, scenario.file_colon_line.split(":").first)
end

#feature_file_linesObject



219
220
221
# File 'lib/cucumberator/writer.rb', line 219

def feature_file_lines
  File.readlines(feature_file)
end

#mark_exit(totally = false) ⇒ Object



51
52
53
54
# File 'lib/cucumberator/writer.rb', line 51

def mark_exit(totally = false)
  Cucumber.wants_to_quit = true if totally
  self.exit_flag = true
end

#parse_input(input) ⇒ Object



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
# File 'lib/cucumberator/writer.rb', line 56

def parse_input(input)
  case input
  when "exit"
    mark_exit and return

  when "exit-all"
    mark_exit(true)

  when "help"
    display_help

  when "last-step"
    puts last_input

  when "save"
    save_last_input

  when "undo"
    undo

  when "next"
    execute_next_step

  when "steps"
    display_steps

  when "where", "whereami"
    display_current_location

  when ""
    save_empty_line

  else
    begin
      execute_cucumber_step(input)
      save_last_input
    rescue Exception => e
      raise e
    end

  end
rescue Exception => e
  puts e.inspect
  puts e.backtrace.join("\n") if FULL_BACKTRACE
end

#read_inputObject



45
46
47
48
49
# File 'lib/cucumberator/writer.rb', line 45

def read_input
  input = Readline.readline("> ", true)
  parse_input(input)
  read_input unless exit_flag
end

#save_empty_lineObject



145
146
147
148
# File 'lib/cucumberator/writer.rb', line 145

def save_empty_line
  save_to_feature_file("")
  self.last_input = nil
end

#save_last_inputObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cucumberator/writer.rb', line 120

def save_last_input
  if last_input.blank?
    puts "Hm... nothing to save yet?"
  else
    string_to_save = (" " * spaces_in_last_input) + last_input
    save_to_feature_file(string_to_save)

    puts "Saved `#{last_input}` to #{File.basename(feature_file)}"
    self.last_input = nil
  end
end

#save_to_feature_file(string) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cucumberator/writer.rb', line 132

def save_to_feature_file(string)
  if step_line
    lines = feature_file_lines
    lines.insert(step_line, string.to_s+$/) # $/ - default newline separator
    File.open(feature_file, 'w'){|f| f.puts(lines.join) }
    self.saved_stack << [step_line, string]
    self.step_line += 1
  else
    File.open(feature_file, 'a'){|f| f.puts(string) }
    self.saved_stack << [feature_file_lines.size, string]
  end
end

#set_autocompleteObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cucumberator/writer.rb', line 21

def set_autocomplete
  commands = %w(exit exit-all help last-step save undo next where whereami steps)

  @steps = all_defined_steps
  @steps.each do |s|
    # remove typical start/end regexp parts
    step = s.gsub(/^\/\^|\$\/$/,'')
    # every step is equal, no matter if When/Then/And, so combining everything for autocomplete
    commands << "When #{step}" << "Then #{step}" << "And #{step}"
  end

  Readline.basic_word_break_characters = ""; # no break chars = no autobreaking for completion input
  Readline.completion_proc = proc { |s| commands.grep( /^#{Regexp.escape(s)}/ ) }
end

#spaces_in_last_inputObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/cucumberator/writer.rb', line 238

def spaces_in_last_input
  lines = File.readlines(feature_file)

  if step_line
    line  = lines[step_line-1]
    lines = lines.slice(0, step_line-1) if line.blank?
  end

  if line.blank?
    for l in lines.reverse
      unless l.blank?
        line = l
        break
      end
    end
  end

  spaces = line =~ /\S/
  spaces.to_i
end

#undoObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cucumberator/writer.rb', line 157

def undo
  if saved_stack.empty?
    puts "There's nothing to revert yet"
    return
  end

  lines = feature_file_lines

  remove_line, remove_string = self.saved_stack.pop
  lines.delete_at(remove_line)
  File.open(feature_file, 'w') { |f| f.puts(lines.join) }
  self.step_line -= 1

  puts "Removed `#{remove_string.to_s.strip}` from #{File.basename(feature_file)}"
end