Module: Cukable::Helper

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

Overview

Common/shared methods supporting the Cukable library

Instance Method Summary (collapse)

Instance Method Details

- (String) clean_cell(string)

Return the given string, cleaned of any HTML tags and status indicators added by the JSON output formatter. The intent here is to take a table cell from JSON output, and make it match the original FitNesse table cell.

Examples:

clean_cell('pass:Given some <b>bold text</b>')
  #=> 'Given some bold text'
clean_cell('pass:Given a step <br>with line break')
  #=> 'Given a step'


207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/cukable/helper.rb', line 207

def clean_cell(string)
  # FIXME: This may not be terribly efficient...
  # strip first to get a copy of the string
  result = string.strip
  # Remove extra stuff added by JSON formatter
  result.gsub!(/^[^:]*:(.*)$/, '\1')        # status indicator
  result.gsub!(/<b>|<\/b>/, '')             # all bold tags
  result.gsub!(/<br\/?>.*/, '')             # <br> and anything that follows
  result.gsub!(/<span[^>]*>.*<\/span>/, '') # spans and their content
  result.gsub!(/\(Undefined Step\)/, '')    # (Undefined Step)
  return result.strip
end

- (String) clean_filename(filename, prefix, suffix)

Return filename with prefix and suffix removed, and any path-separators converted to underscores.

Examples:

clean_filename('abc/some/path/xyz', 'abc', 'xyz')
  #=> 'some_path'


28
29
30
31
# File 'lib/cukable/helper.rb', line 28

def clean_filename(filename, prefix, suffix)
  middle = filename.gsub(/^#{prefix}\/(.+)\/#{suffix}$/, '\1')
  return middle.gsub('/', '_')
end

- (String) literalize(string)

Return the given string with any CamelCase words, email addresses, and URLs escaped with FitNesse's !-...-! string-literal markup.

FIXME: Literals inside other literals will cause too much escaping!

Examples:

literalize('With a CamelCase word')
  #=> 'With a !-CamelCase-! word'


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cukable/helper.rb', line 101

def literalize(string)
  result = string.strip

  # Literalize email addresses
  # FitNesse pattern for email addresses, per TextMaker.java:
  #   [\w\-_.]+@[\w\-_.]+\.[\w\-_.]+
  result.gsub!(/([\w\-_.]+@[\w\-_.]+\.[\w\-_.]+)/, '!-\1-!')


  # Literalize CamelCase words
  # Regex for matching wiki words, according to FitNesse.UserGuide.WikiWord
  #   \b[A-Z](?:[a-z0-9]+[A-Z][a-z0-9]*)+
  result.gsub!(/(\b[A-Z](?:[a-z0-9]+[A-Z][a-z0-9]*)+)/, '!-\1-!')

  # Literalize URLs
  # Brain-dead URL matcher, should do the trick in most cases though
  # (Better to literalize too much than not enough)
  result.gsub!(/(http[^ ]+)/, '!-\1-!')

  return result
end

- (String) remove_cruft(string)

Remove FitNesse-generated link cruft from a string. Strips <a ...></a> tags, keeping the inner content unless that content is '[?]'.

Examples:

remove_cruft('Go to <a href="SomePage">this page</a>')
  #=> 'Go to this page'
remove_cruft('See SomePage<a href="SomePage">[?]</a>')
  #=> 'See SomePage'


49
50
51
# File 'lib/cukable/helper.rb', line 49

def remove_cruft(string)
  string.gsub(/<a [^>]*>([^<]*)<\/a>/, '\1').gsub('[?]', '')
end

- (String) table_digest(table)

Return an MD5 digest string for table. Any HTML entities and FitNesse markup in the table is unescaped before the digest is calculated.

Examples:

table_digest(['foo'], ['bar'])
  #=> '3858f62230ac3c915f300c664312c63f'
table_digest(['foo', 'baz'])
  #=> '80338e79d2ca9b9c090ebaaa2ef293c7'


161
162
163
164
165
166
167
# File 'lib/cukable/helper.rb', line 161

def table_digest(table)
  digest = Digest::MD5.new
  table.flatten.each do |cell|
    digest.update(unescape(cell))
  end
  return digest.to_s
end

- (String) unescape(string)

Unescape any HTML entities and FitNesse markup in the given string.

Examples:

unescape('Some &lt;stuff&gt; to !-unescape-!')
  #=> 'Some <stuff> to unescape'


183
184
185
186
187
# File 'lib/cukable/helper.rb', line 183

def unescape(string)
  result = CGI.unescapeHTML(string)
  result.gsub!(/!-(.*?)-!/, '\1')
  return result
end

- (String) wikify(string)

Wikify (CamelCase) the given string, removing spaces, underscores, dashes and periods, and CamelCasing the remaining words. If this does not result in a CamelCase word with at least two words in it (that is, if the input was only a single word), then the last letter in the word is capitalized so as to make FitNesse happy.

FIXME: This will not generate valid FitNesse wiki page names for pathological cases, such as any input that would result in consecutive capital letters, including words having only two letters in them.

Examples:

wikify('file.extension')   #=> 'FileExtension'
wikify('with_underscore')  #=> 'WithUnderscore'
wikify('having spaces')    #=> 'HavingSpaces'
wikify('foo')              #=> 'FoO'


76
77
78
79
80
81
82
83
84
# File 'lib/cukable/helper.rb', line 76

def wikify(string)
  string.gsub!(/^[a-z]|[_.\s\-]+[a-z]/) { |a| a.upcase }
  string.gsub!(/[_.\s\-]/, '')
  if string =~ /(([A-Z][a-z]*){2})/
    return string
  else
    return string.gsub(/.\b/) { |c| c.upcase }
  end
end

- (String) wikify_path(path)

Wikify the given path name, and return a path that's suitable for use as a FitNesse wiki page path. Any path component having only a single word in it will have the last letter in that word capitalized.

Examples:

wikify_path('features/account/create.feature')
  #=> 'FeatureS/AccounT/CreateFeature'


138
139
140
141
142
143
# File 'lib/cukable/helper.rb', line 138

def wikify_path(path)
  wiki_parts = path.split(File::SEPARATOR).collect do |part|
    wikify(part)
  end
  return File.join(wiki_parts)
end