module Cda::RSpec::ValidationRules

Public Instance Methods

_element() click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 59
def _element
  @_element || document.root
end
find_by_xpath(*args) click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 27
def find_by_xpath(*args)
  xpath = args.first
  if xpath.is_a?(Hash)
    xpath, condition = xpath.keys.first, xpath_condition(xpath.values.first)
  else
    condition = xpath_condition(args[1])
  end
  xpath = "#{xpath}[#{condition}]" if condition
  _element.xpath(xpath.to_s)
end
shall(matcher, *args) { |result| ... } click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 20
def shall(matcher, *args)
  opts = args.extract_options!
  result = find_by_xpath(*args)
  result.should matcher, opts[:message]
  yield result if block_given?
end
shall_contain_at_least_one(*xpath, &block) click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 10
def shall_contain_at_least_one(*xpath, &block)
  shall have_at_least(1).item, *(xpath + [{message: "Shall contain at least one #{xpath}"}]) do |result|
    validate_rules_for result.first, &block if block_given?
  end
end
shall_contain_exactly_one(*xpath, &block) click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 4
def shall_contain_exactly_one(*xpath, &block)
  shall have(1).item, *(xpath + [{message: "Shall contain exactly one #{xpath}"}]) do |result|
    validate_rules_for result.first, &block if block_given?
  end
end
shall_not_contain(*xpath) click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 16
def shall_not_contain(*xpath)
  shall be_empty, *(xpath + [{message: "Shall not contain #{xpath}"}])
end
validate_rules_for(element) { || ... } click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 63
def validate_rules_for(element)
  saved_element, @_element = @_element, element
  yield
ensure
  @_element = saved_element
end
xpath_condition(condition) click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 38
def xpath_condition(condition)
  case condition
  when Hash
    condition.map { |k, v| xpath_eq(k, v) }.join(' and ')
  when String
    %(text()="#{condition}")
  else
    nil
  end
end
xpath_eq(name, value) click to toggle source
# File lib/cda/rspec/validation_rules.rb, line 49
def xpath_eq(name, value)
  if name.to_s.include?(':')
    _, ns_less_name = name.to_s.split(':')
    %(@#{ns_less_name}="#{value}")
  else
    %(@#{name}="#{value}")
  end
end