Module: Cukable::Conversion

Included in:
Converter
Defined in:
lib/cukable/conversion.rb

Overview

Supporting functions for converting Cucumber features to FitNesse wiki pages and vice-versa.

Instance Method Summary collapse

Instance Method Details

#feature_to_fitnesse(feature) ⇒ Array

Wikify the given feature, and return lines of FitNesse wikitext.



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
# File 'lib/cukable/conversion.rb', line 24

def feature_to_fitnesse(feature)

  # Unparsed text (between 'Feature:' line and the first Background/Scenario)
  unparsed = []
  # Table (all Background, Scenario, and Scenario Outlines with steps
  table = ["!| Table: Cuke |"]

  # Are we in the unparsed-text section of the .feature file?
  in_unparsed = false

  feature.each do |line|
    line.strip!

    # The Feature: line starts the table, and also starts the unparsed
    # section of the feature file
    if line =~ /^Feature:.*$/
      table << "| #{line} |"
      in_unparsed = true

    # When the first Background/Scenario block is reached, end the unparsed
    # text section
    elsif line =~ /^(Background:|Scenario:|Scenario Outline:)/
      in_unparsed = false
      table << "| #{line} |"

    # If line contains one or more tags, output them on separate lines
    elsif line =~ /^\s*@\w+/
      for tag in line.split
        table << "| #{tag} |"
      end

    # Between 'Feature:...' and the first Background/Scenario/Scenario Outline
    # block, we're in the unparsed text section
    elsif in_unparsed and !line.empty?
      unparsed << line

    # If line contains a table row, insert a '|' at the beginning
    elsif line =~ /^\|.+\|$/
      table << "| #{line}"

    # If line is commented out, skip it
    elsif line =~ /^#.*$/
      nil

    # Otherwise, if line is non-empty, insert a '|' at beginning and end
    elsif !line.empty?
      table << "| #{line} |"

    end
  end
  # If there was unparsed text, include an empty line after it
  if !unparsed.empty?
    unparsed << ''
  end
  return unparsed + table
end

#fitnesse_to_features(wiki_page) ⇒ Array

Return an array of Cucumber tables found in the given FitNesse content file, or an empty array if no tables are found. Each table in the array is in the same format as a table passed to the do_table method; that is, a table is an array of rows, where each row is an array of strings found in each cell of the table.



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
# File 'lib/cukable/conversion.rb', line 96

def fitnesse_to_features(wiki_page)

  tables = []         # List of all tables parsed so far
  current_table = []  # List of lines in the current table
  in_table = false    # Are we inside a table right now?

  wiki_page.each do |line|
    # Strip newline
    line = line.strip

    # Beginning of a new table?
    if line =~ /\| *Table *: *Cuke *\| *$/
      in_table = true
      current_table = []

    # Already in a table?
    elsif in_table
      # Append a new row to the current table, with pipes
      # and leading/trailing whitespace removed
      if line =~ /\| *(.*) *\| *$/
        row = $1.split('|').collect { |cell| unescape(cell.strip) }
        current_table << row
      # No more rows; end this table and append to the results
      else
        in_table = false
        tables << current_table
        current_table = []
      end

    # Ignore all non-table lines in the content
    else
      nil
    end
  end

  # If we're still inside a table, append it (this means that the last line
  # of the table was the last line of the file, and there were no more
  # lines after the table to terminate it)
  if in_table
    tables << current_table
  end

  return tables
end