Class: Spinach::Generators::FeatureGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/spinach/generators/feature_generator.rb

Overview

A feature generator generates and/or writes an example feature steps class given the parsed feture data

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ FeatureGenerator

Returns a new instance of FeatureGenerator.

Parameters:

  • data (Hash)

    the parsed feature data returned from the Parser



9
10
11
# File 'lib/spinach/generators/feature_generator.rb', line 9

def initialize(data)
  @data = data
end

Instance Method Details

#filenameString

Returns an example filename for this feature steps.

Returns:

  • (String)

    an example filename for this feature steps



61
62
63
64
65
# File 'lib/spinach/generators/feature_generator.rb', line 61

def filename
  Spinach::Support.underscore (
    Spinach::Support.camelize name
  ) + ".rb"
end

#filename_with_pathString

Returns the expanded path where this feature steps may be saved.

Returns:

  • (String)

    the expanded path where this feature steps may be saved



75
76
77
# File 'lib/spinach/generators/feature_generator.rb', line 75

def filename_with_path
  File.expand_path File.join(path, filename)
end

#generateString

Returns an example feature steps definition.

Returns:

  • (String)

    an example feature steps definition



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spinach/generators/feature_generator.rb', line 44

def generate
  result = StringIO.new
  result.puts "class #{Spinach::Support.camelize name} < Spinach::FeatureSteps"
  result.puts "  feature \'#{Spinach::Support.escape_single_commas name}\'\n"
  generated_steps = steps.map do |step|
    step_generator = Generators::StepGenerator.new(step)
    step_generator.generate.split("\n").map do |line|
      "  #{line}"
    end.join("\n")
  end
  result.puts generated_steps.join("\n\n")
  result.puts "end"
  result.string
end

#nameString

Returns this feature’s name.

Returns:

  • (String)

    this feature’s name



38
39
40
# File 'lib/spinach/generators/feature_generator.rb', line 38

def name
  @data['name'].strip if @data['name']
end

#pathString

Returns the path where this feature steps may be saved.

Returns:

  • (String)

    the path where this feature steps may be saved



69
70
71
# File 'lib/spinach/generators/feature_generator.rb', line 69

def path
  Spinach.config[:step_definitions_path]
end

#stepsArray<Hash>

Returns an array of unique steps found in this scenario, avoiding name repetition.

Returns:

  • (Array<Hash>)

    an array of unique steps found in this scenario, avoiding name repetition



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/spinach/generators/feature_generator.rb', line 16

def steps
  return @steps if @steps
  @steps = []
  if scenarios = @data['elements']
    scenarios.each do  |scenario|
      if scenario_steps = scenario['steps']
        scenario_steps.each do |step|
          unless @steps.any?{|s| s['name'] == step['name']}
            @steps << {
              'keyword' => step['keyword'].strip,
              'name' => step['name'].strip
            }
          end
        end
      end
    end
  end
  @steps
end

#storeObject

Stores the example feature steps definition into an expected path



81
82
83
84
85
86
87
88
89
90
# File 'lib/spinach/generators/feature_generator.rb', line 81

def store
  if File.exists?(filename_with_path)
    raise FeatureGeneratorException.new("File already exists")
  else
    FileUtils.mkdir_p path
    File.open(filename_with_path, 'w') do |file|
      file.write(generate)
    end
  end
end