Class: GherkinLanguage
- Inherits:
-
Object
- Object
- GherkinLanguage
- Defined in:
- lib/gherkin_language.rb
Overview
gherkin utilities
Instance Method Summary collapse
- #accepted?(sentence) ⇒ Boolean
- #analyze(file) ⇒ Object
- #determine_readability_by_file(files) ⇒ Object
- #expand_outlines(sentence, example) ⇒ Object
- #extract_examples(examples, prototype) ⇒ Object
- #extract_sentences(parsed) ⇒ Object
- #extract_terms_from_scenario(steps, background) ⇒ Object
- #hash(value) ⇒ Object
- #ignore(exception) ⇒ Object
-
#initialize(no_cache = false, ngram = false, unknown_words = false) ⇒ GherkinLanguage
constructor
A new instance of GherkinLanguage.
- #parse(file) ⇒ Object
- #readability(sentences) ⇒ Object
- #report ⇒ Object
- #scenario_names(input) ⇒ Object
- #sentences(input) ⇒ Object
- #tag(files) ⇒ Object
- #to_json(input, file = 'generated.feature') ⇒ Object
- #uncapitalize(term) ⇒ Object
- #write_accepted_paragraphs(used_refs) ⇒ Object
Constructor Details
#initialize(no_cache = false, ngram = false, unknown_words = false) ⇒ GherkinLanguage
Returns a new instance of GherkinLanguage.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/gherkin_language.rb', line 21 def initialize(no_cache = false, ngram = false, unknown_words = false) path = "~/.gherkin_language/#{LanguageToolProcess::VERSION}/accepted_paragraphs.yml" @settings_path = File. path @accepted_paragraphs = {} begin @accepted_paragraphs = YAML.load_file @settings_path unless no_cache rescue puts 'could not read settings' end @references = {} @line_to_reference = {} @exceptions = %w(SENTENCE_FRAGMENT ENGLISH_WORD_REPEAT_BEGINNING_RULE) @ngram = ngram @unknown_words = unknown_words end |
Instance Method Details
#accepted?(sentence) ⇒ Boolean
79 80 81 82 83 84 85 86 |
# File 'lib/gherkin_language.rb', line 79 def accepted?(sentence) return false if @accepted_paragraphs.nil? key = :without_glossary key = hash(File.read('.glossary')) if File.exist? '.glossary' return false unless @accepted_paragraphs.key? key @accepted_paragraphs[key].include? hash sentence end |
#analyze(file) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gherkin_language.rb', line 41 def analyze(file) sentences = extract_sentences parse file sentences.select! { |sentence| !accepted? sentence } return if sentences.empty? sentences.each do |sentence| stripped = sentence.strip @references[stripped] = [] unless @references.include? stripped @references[stripped].push file end end |
#determine_readability_by_file(files) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gherkin_language.rb', line 52 def determine_readability_by_file(files) puts "Readability. Sorted from best to worst readable feature\n\n" if files.length > 1 readability_by_file = {} files.each do |file| sentences = extract_sentences parse(file) readability_by_file[file] = readability sentences end average_readability = 0 readability_by_file.sort { |lhs, rhs| lhs[1] <=> rhs[1] }.reverse_each do |file, | puts "#{.round}: #{file}" average_readability += / files.length end puts "\n#{files.length} files analyzed. Average readability is #{average_readability.round}" if files.length > 1 end |
#expand_outlines(sentence, example) ⇒ Object
241 242 243 244 245 246 247 248 249 250 |
# File 'lib/gherkin_language.rb', line 241 def (sentence, example) result = [] headers = example['rows'][0]['cells'] example['rows'].slice(1, example['rows'].length).each do |row| modified_sentence = sentence.dup headers.zip(row['cells']).map { |key, value| modified_sentence.gsub!("<#{key}>", value) } result.push modified_sentence end result end |
#extract_examples(examples, prototype) ⇒ Object
227 228 229 230 231 232 233 234 235 |
# File 'lib/gherkin_language.rb', line 227 def extract_examples(examples, prototype) examples.map do |example| sentences = [] sentences.push example['name'] unless example['name'].empty? sentences.push example['description'] unless example['description'].empty? sentences += (prototype, example) sentences end.flatten end |
#extract_sentences(parsed) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gherkin_language.rb', line 97 def extract_sentences(parsed) feature_names = lambda do |input| input.map { |feature| feature['name'] unless feature['name'] == '' } end descriptions = lambda do |input| input.map { |feature| feature['description'] unless feature['description'] == '' } end sentences = feature_names.call(parsed) + descriptions.call(parsed) + scenario_names(parsed) + sentences(parsed) sentences.select! { |sentence| sentence } sentences.map! { |sentence| sentence.gsub(/ an «[^«]*»/, ' a replacement') } sentences.map! { |sentence| sentence.gsub(/ ?«[^«]*»/, ' replacement') } sentences.map { |sentence| sentence.gsub('replacement replacement', 'replacement') } end |
#extract_terms_from_scenario(steps, background) ⇒ Object
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/gherkin_language.rb', line 216 def extract_terms_from_scenario(steps, background) steps.map do |step| keyword = step['keyword'] keyword = 'and ' unless background.empty? || keyword != 'Given ' terms = [keyword, step['name']].join terms = uncapitalize(terms) unless background.empty? background = terms terms end.flatten end |
#hash(value) ⇒ Object
88 89 90 |
# File 'lib/gherkin_language.rb', line 88 def hash(value) Digest::MD5.digest value.strip end |
#ignore(exception) ⇒ Object
37 38 39 |
# File 'lib/gherkin_language.rb', line 37 def ignore(exception) @exceptions.push exception end |
#parse(file) ⇒ Object
92 93 94 95 |
# File 'lib/gherkin_language.rb', line 92 def parse(file) content = File.read file to_json(content, file) end |
#readability(sentences) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/gherkin_language.rb', line 67 def readability(sentences) require 'syllables' total_words = 0 total_syllabels = 0 Syllables.new(sentences.join('\n')).to_h.each do |_word, syllabels| total_words += 1 total_syllabels += syllabels end 206.835 - 1.015 * (total_words / sentences.length) - 84.6 * (total_syllabels / total_words) end |
#report ⇒ Object
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 |
# File 'lib/gherkin_language.rb', line 119 def report return 0 if @references.keys.empty? language = LanguageToolProcess.new(@ngram, @unknown_words) language.start! @references.keys.each do |sentence| location = language.check_paragraph sentence location.map { |line| @line_to_reference[line] = sentence } end language.stop! errors = language.errors unknown_words = language.unknown_words used_refs = Set.new [] errors.each do |error| used_refs.add @line_to_reference[error.from_y] end errors.select! { |error| !@exceptions.include? error.rule } errors.each do |error| local_refs = @references[@line_to_reference[error.from_y]] puts error.str local_refs end # TODO: list references for unknown words puts red "#{unknown_words.count} unknown words: #{unknown_words * ', '}" unless unknown_words.empty? return -1 unless unknown_words.empty? write_accepted_paragraphs used_refs return -1 unless errors.empty? 0 end |
#scenario_names(input) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/gherkin_language.rb', line 176 def scenario_names(input) # TODO: scenario outlines with example values inside? scenarios = [] input.each do |features| next unless features.key? 'elements' elements = features['elements'] elements.each do |scenario| scenarios.push scenario['name'] if scenario['type'] == 'scenario' scenarios.push scenario['name'] if scenario['type'] == 'scenario_outline' scenarios.push scenario['description'] unless scenario['description'].empty? end end scenarios end |
#sentences(input) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/gherkin_language.rb', line 191 def sentences(input) sentences = [] background = [] input.each do |features| next unless features.key? 'elements' features['elements'].each do |scenario| next unless scenario.key? 'steps' terms = background.dup if scenario['type'] == 'background' background.push extract_terms_from_scenario(scenario['steps'], terms) next end terms.push extract_terms_from_scenario(scenario['steps'], background) sentence = terms.join(' ').strip if scenario.key? 'examples' sentences += extract_examples(scenario['examples'], sentence) else sentences.push sentence end end end sentences end |
#tag(files) ⇒ Object
113 114 115 116 117 |
# File 'lib/gherkin_language.rb', line 113 def tag(files) sentences = files.map { |file| extract_sentences parse file } language = LanguageToolProcess.new language.tag sentences end |
#to_json(input, file = 'generated.feature') ⇒ Object
167 168 169 170 171 172 173 174 |
# File 'lib/gherkin_language.rb', line 167 def to_json(input, file = 'generated.feature') io = StringIO.new formatter = Gherkin::Formatter::JSONFormatter.new(io) parser = Gherkin::Parser::Parser.new(formatter, true) parser.parse(input, file, 0) formatter.done MultiJson.load io.string end |
#uncapitalize(term) ⇒ Object
237 238 239 |
# File 'lib/gherkin_language.rb', line 237 def uncapitalize(term) term[0, 1].downcase + term[1..-1] end |
#write_accepted_paragraphs(used_refs) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/gherkin_language.rb', line 151 def write_accepted_paragraphs(used_refs) @references.each do |sentence, _refs| next if used_refs.include? sentence key = :without_glossary key = hash(File.read('.glossary')) if File.exist? '.glossary' @accepted_paragraphs[key] = Set.new [] unless @accepted_paragraphs.key? key @accepted_paragraphs[key].add hash sentence end FileUtils.mkdir_p File.dirname @settings_path File.open(@settings_path, 'w') do |settings_file| settings_file.write @accepted_paragraphs.to_yaml end end |