Skip to content

Commit ed93bfd

Browse files
committed
Add Solutions#variable_names= to override automated variable name count.
Add `Solutions#==` and `Solutions`#eql?` to test solutions including variable names.
1 parent a4971d9 commit ed93bfd

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

lib/rdf/query/solution.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,12 @@ def to_h
317317
def hash
318318
@bindings.hash
319319
end
320-
320+
321321
##
322322
# Equivalence of solution
323323
def eql?(other)
324324
other.is_a?(Solution) && @bindings.eql?(other.bindings)
325325
end
326-
alias_method :==, :eql?
327326

328327
##
329328
# Equals of solution

lib/rdf/query/solutions.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ def variable_names
7777
end
7878
end
7979

80+
##
81+
# Sets variable names used in these solutions. If not set, the default is determined by the variables used in each solution.
82+
#
83+
# @param [Array<Symbol, RDF::Query::Variable>] vars
84+
# @return [Array<Symbol>]
85+
def variable_names=(vars)
86+
@variable_names = vars.map(&:to_sym)
87+
end
88+
8089
##
8190
# @overload variable?
8291
# Returns `false`.
@@ -294,5 +303,17 @@ def limit(length)
294303
self
295304
end
296305
alias_method :limit!, :limit
306+
307+
##
308+
# Equivalence of solution
309+
def eql?(other)
310+
super && (!other.respond_to?(:variable_names) || variable_names.eql?(other.variable_names))
311+
end
312+
313+
##
314+
# Equals of solution
315+
def ==(other)
316+
super && (!other.respond_to?(:variable_names) || variable_names.eql?(other.variable_names))
317+
end
297318
end # Solutions
298319
end; end # RDF::Query

spec/query_solutions_spec.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,20 @@
305305
specify {is_expected.to include(:author, :age, :name, :description, :updated, :created, :title, :price, :date)}
306306
end
307307

308+
describe "#variable_names=" do
309+
it "can set variable names from constants" do
310+
solutions.variable_names = %i{author age foo}
311+
expect(solutions.variable_names).to include(:author, :age, :foo)
312+
expect(solutions.variable_names).not_to include(:name)
313+
end
314+
315+
it "can set variable names from variables" do
316+
solutions.variable_names = %w{author age foo}.map {|n| RDF::Query::Variable.new(n)}
317+
expect(solutions.variable_names).to include(:author, :age, :foo)
318+
expect(solutions.variable_names).not_to include(:name)
319+
end
320+
end
321+
308322
describe "#count" do
309323
its(:count) {is_expected.to eq 2}
310324
it "Counting the number of matching solutions" do
@@ -318,6 +332,40 @@
318332
end
319333
end
320334

335+
describe "#eql?" do
336+
it "is true for equivalent solutions" do
337+
expect(solutions).to eql solutions.dup
338+
end
339+
340+
it "is false for different solutions" do
341+
solns2 = RDF::Query::Solutions(uri)
342+
expect(solutions).not_to eql solns2
343+
end
344+
345+
it "is false for the same solution with different variable_names" do
346+
solns2 = solutions.dup
347+
solns2.variable_names = %i{foo bar}
348+
expect(solutions).not_to eql solns2
349+
end
350+
end
351+
352+
describe "#==" do
353+
it "is true for equivalent solutions" do
354+
expect(solutions).to eq solutions.dup
355+
end
356+
357+
it "is false for different solutions" do
358+
solns2 = RDF::Query::Solutions(uri)
359+
expect(solutions).not_to eq solns2
360+
end
361+
362+
it "is false for the same solution with different variable_names" do
363+
solns2 = solutions.dup
364+
solns2.variable_names = %i{foo bar}
365+
expect(solutions).not_to eq solns2
366+
end
367+
end
368+
321369
describe "#bindings" do
322370
subject {
323371
RDF::Query::Solutions(

0 commit comments

Comments
 (0)