Class: CukeSniffer::Hook

Inherits:
RuleTarget show all
Defined in:
lib/cuke_sniffer/hook.rb

Overview

Author

Robert Cochran ([email protected])

Copyright

Copyright © 2014 Robert Cochran

License

Distributes under the MIT License

Cucumber Hook class used for evaluating rules Extends CukeSniffer::RulesEvaluator

Constant Summary

Constants included from RuleConfig

RuleConfig::ERROR, RuleConfig::FATAL, RuleConfig::INFO, RuleConfig::RULES, RuleConfig::WARNING

Constants included from Constants

Constants::COMMENT_REGEX, Constants::DATE_REGEX, Constants::DEFAULT_OUTPUT_FILE_NAME, Constants::FILE_IGNORE_LIST, Constants::HOOK_REGEX, Constants::HOOK_STYLES, Constants::MARKUP_SOURCE, Constants::SCENARIO_TITLE_STYLES, Constants::STEP_DEFINITION_REGEX, Constants::STEP_REGEX, Constants::STEP_STYLES, Constants::TAG_REGEX, Constants::THRESHOLDS

Instance Attribute Summary collapse

Attributes inherited from RuleTarget

#location, #rules_hash, #score

Instance Method Summary collapse

Methods inherited from RuleTarget

#good?, #is_comment?, #problem_percentage, #store_rule, #store_rule_many_times

Constructor Details

#initialize(location, hook_block) ⇒ Hook

location must be in the format of “file_pathfile_name.rb:line_number” raw_code is an array of strings that represents the step definition must contain the hook declaration line and the pairing end



34
35
36
37
38
39
40
# File 'lib/cuke_sniffer/hook.rb', line 34

def initialize(location, hook_block)
  super(location)
  @start_line = location.match(/:(?<line>\d*)$/)[:line].to_i
  end_match_index = (hook_block.size - 1) - hook_block.reverse.index("end")
  @code = hook_block[1...end_match_index]
  initialize_hook_signature(hook_block)
end

Instance Attribute Details

#codeObject

Array of strings that contain the code kept in the hook



28
29
30
# File 'lib/cuke_sniffer/hook.rb', line 28

def code
  @code
end

#parametersObject

The parameters that are declared on the hook



22
23
24
# File 'lib/cuke_sniffer/hook.rb', line 22

def parameters
  @parameters
end

#start_lineObject

Integer of the line in which the hook was found



25
26
27
# File 'lib/cuke_sniffer/hook.rb', line 25

def start_line
  @start_line
end

#tagsObject

The list of tags used as a filter for the hook



19
20
21
# File 'lib/cuke_sniffer/hook.rb', line 19

def tags
  @tags
end

#typeObject

The type of the hook: AfterConfiguration, After, AfterStep, Around, Before, at_exit



16
17
18
# File 'lib/cuke_sniffer/hook.rb', line 16

def type
  @type
end

Instance Method Details

#==(comparison_object) ⇒ Object

:nodoc:



42
43
44
45
46
47
48
# File 'lib/cuke_sniffer/hook.rb', line 42

def ==(comparison_object) # :nodoc:
  super(comparison_object) &&
      comparison_object.type == type &&
      comparison_object.tags == tags &&
      comparison_object.parameters == parameters &&
      comparison_object.code == code
end

#around?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/cuke_sniffer/hook.rb', line 50

def around?
  type == 'Around'
end

#calls_block?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/cuke_sniffer/hook.rb', line 54

def calls_block?
  @code.join.to_s.include?("#{@parameters[1]}.call")
end

#conflicting_tags?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
# File 'lib/cuke_sniffer/hook.rb', line 58

def conflicting_tags?
  all_tags = get_indepentent_tags
  all_tags.each do |single_tag|
    tag = single_tag.gsub("~", "")
    return true if all_tags.include?(tag) and all_tags.include?("~#{tag}")
  end
  false
end

#rescues?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/cuke_sniffer/hook.rb', line 67

def rescues?
  @code.empty? or @code.join.to_s =~ /.*begin.*rescue.*/
end