Class: RoadForest::Testing::MatchesQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/test-support/matchers.rb

Instance Method Summary collapse

Constructor Details

#initialize(pattern = nil, &block) ⇒ MatchesQuery

Returns a new instance of MatchesQuery.



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/roadforest/test-support/matchers.rb', line 184

def initialize(pattern = nil, &block)
  if pattern.nil? and block.nil?
    raise "Matches query (e.g. should match_query) created with no patterns: probably used a do block..."
  end
  pattern ||= []
  if Hash === pattern
    pattern = [pattern]
  end
  pattern = pattern.map do |item|
    ::RDF::Query::Pattern.from(item)
  end
  @query = ::RDF::Query.new(pattern, &block)
end

Instance Method Details

#failure_message_for_shouldObject



208
209
210
211
# File 'lib/roadforest/test-support/matchers.rb', line 208

def failure_message_for_should
  require 'pp'
  "expected: \n#{indent(@query.patterns.pretty_inspect)} \nto return solutions on \n\n#{indent(@actual.dump(:nquads))}\n but didn't"
end

#failure_message_for_should_notObject



213
214
215
# File 'lib/roadforest/test-support/matchers.rb', line 213

def failure_message_for_should_not
require 'pp'
"expected: \n#{indent(@query.patterns.pretty_inspect)} \nnot to return solutions on \n\n#{indent(@actual.dump(:nquads))}\n but does" end

#indent(string) ⇒ Object



204
205
206
# File 'lib/roadforest/test-support/matchers.rb', line 204

def indent(string)
  string.split("\n").map{|line| "  " + line}.join("\n")
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
201
202
# File 'lib/roadforest/test-support/matchers.rb', line 198

def matches?(actual)
  @actual = actual
  solutions = @query.execute(actual)
  not solutions.empty?
end