Skip to content

Commit e8a1e8d

Browse files
committed
Code coverage and remove dead code.
1 parent bf91451 commit e8a1e8d

File tree

18 files changed

+125
-184
lines changed

18 files changed

+125
-184
lines changed

lib/shex.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module ShEx
2424
# @param [Hash{Symbol => Object}] options
2525
# @return [ShEx::Algebra::Schema] The executable parsed expression.
2626
# @raise [ShEx::ParseError] when a syntax error is detected
27-
# @raise [ShEx::StructureError, ShEx::OperandError] on structural problems with schema
27+
# @raise [ShEx::StructureError, ArgumentError] on structural problems with schema
2828
def self.parse(expression, format: 'shexc', **options)
2929
case format
3030
when 'shexc' then Parser.new(expression, options).parse
@@ -49,10 +49,10 @@ def self.parse(expression, format: 'shexc', **options)
4949
# @yieldreturn [void] ignored
5050
# @return [ShEx::Algebra::Schema] The executable parsed expression.
5151
# @raise [ShEx::ParseError] when a syntax error is detected
52-
# @raise [ShEx::StructureError, ShEx::OperandError] on structural problems with schema
52+
# @raise [ShEx::StructureError, ArgumentError] on structural problems with schema
5353
def self.open(filename, format: 'shexc', **options, &block)
5454
RDF::Util::File.open_file(filename, options) do |file|
55-
self.parse(file, options)
55+
self.parse(file, options.merge(format: format))
5656
end
5757
end
5858

@@ -71,9 +71,9 @@ def self.open(filename, format: 'shexc', **options, &block)
7171
# @return [Boolean] `true` if satisfied, `false` if it does not apply
7272
# @raise [ShEx::NotSatisfied] if not satisfied
7373
# @raise [ShEx::ParseError] when a syntax error is detected
74-
# @raise [ShEx::StructureError, ShEx::OperandError] on structural problems with schema
75-
def self.execute(expression, queryable, focus, shape, format: 'shexc', **options, &block)
76-
shex = self.parse(expression, options)
74+
# @raise [ShEx::StructureError, ArgumentError] on structural problems with schema
75+
def self.execute(expression, queryable, focus, shape, format: 'shexc', **options)
76+
shex = self.parse(expression, options.merge(format: format))
7777
queryable = queryable || RDF::Graph.new
7878

7979
shex.satisfies?(focus, queryable, {focus => shape}, options)
@@ -102,9 +102,6 @@ class StructureError < Error; end
102102
# Shape expectation not satisfied
103103
class NotSatisfied < Error; end
104104

105-
# An error found on an operand
106-
class OperandError < Error; end
107-
108105
# Indicates bad syntax found in LD Patch document
109106
class ParseError < Error
110107
##

lib/shex/algebra/and.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ class And < Operator
77
def initialize(*args, **options)
88
case
99
when args.length <= 1
10-
raise ShEx::OperandError, "Expected at least one operand, found #{args.length}"
10+
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1..)"
1111
end
1212
super
1313
end
1414

1515
#
1616
# S is a ShapeAnd and for every shape expression se2 in shapeExprs, satisfies(n, se2, G, m).
1717
# @param [RDF::Resource] n
18-
# @return [Boolean] `true` if satisfied, `false` if it does not apply
18+
# @return [Boolean] `true` when satisfied
1919
# @raise [ShEx::NotSatisfied] if not satisfied
2020
def satisfies?(n)
2121
status ""
22-
unless operands.select {|o| o.is_a?(Satisfiable)}.all? {|op| op.satisfies?(n)}
23-
not_satisfied "Expected all to match"
24-
end
22+
23+
# Operand raises NotSatisfied, so no need to check here.
24+
operands.select {|o| o.is_a?(Satisfiable)}.each {|op| op.satisfies?(n)}
2525
status("satisfied")
26+
true
27+
rescue ShEx::NotSatisfied => e
28+
not_satisfied(e.message)
29+
raise
2630
end
2731
end
2832
end

lib/shex/algebra/each_of.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def matches(t)
3939
num_iters < minimum
4040

4141
# Last, evaluate semantic acts
42-
operands.select {|o| o.is_a?(SemAct)}.all? do |op|
42+
semantic_actions.all? do |op|
4343
op.satisfies?(results)
4444
end unless results.empty?
4545

lib/shex/algebra/inclusion.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Inclusion < Operator
55
NAME = :inclusion
66

77
def initialize(arg, **options)
8-
raise ShEx::OperandError, "Shape inclusion must be an IRI or BNode: #{arg}" unless arg.is_a?(RDF::Resource)
8+
raise ArgumentError, "Shape inclusion must be an IRI or BNode: #{arg}" unless arg.is_a?(RDF::Resource)
99
super
1010
end
1111

@@ -25,11 +25,6 @@ def matches(t)
2525
not_matched "Minimum Cardinality Violation: #{results.length} < #{minimum}" if
2626
results.length < minimum
2727

28-
# Last, evaluate semantic acts
29-
operands.select {|o| o.is_a?(SemAct)}.all? do |op|
30-
op.satisfies?(results)
31-
end unless results.empty?
32-
3328
results
3429
end
3530

lib/shex/algebra/not.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ class Not < Operator::Unary
77
#
88
# S is a ShapeNot and for the shape expression se2 at shapeExpr, notSatisfies(n, se2, G, m).
99
# @param [RDF::Resource] n
10-
# @return [Boolean]
10+
# @return [Boolean] `true` when satisfied, meaning that the operand was not satisfied
11+
# @raise [ShEx::NotSatisfied] if not satisfied, meaning that the operand was satisfied
1112
def satisfies?(n)
1213
status ""
1314
operands.last.not_satisfies?(n)
1415
status "not satisfied"
16+
true
1517
end
1618
end
1719
end

lib/shex/algebra/one_of.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def matches(t)
3939
num_iters < minimum
4040

4141
# Last, evaluate semantic acts
42-
operands.select {|o| o.is_a?(SemAct)}.all? do |op|
42+
semantic_actions.all? do |op|
4343
op.satisfies?(results)
4444
end unless results.empty?
4545

lib/shex/algebra/operator.rb

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -17,52 +17,6 @@ class Operator
1717
# Initialization options
1818
attr_accessor :options
1919

20-
##
21-
# Returns an operator class for the given operator `name`.
22-
#
23-
# @param [Symbol, #to_s] name
24-
# @param [Integer] arity
25-
# @return [Class] an operator class, or `nil` if an operator was not found
26-
def self.for(name, arity = nil)
27-
{
28-
and: And,
29-
annotation: Annotation,
30-
base: Base,
31-
each_of: EachOf,
32-
inclusion: Inclusion,
33-
nodeConstraint: NodeConstraint,
34-
not: Not,
35-
one_of: OneOf,
36-
or: Or,
37-
prefix: Prefix,
38-
schema: Schema,
39-
semact: SemAct,
40-
external: External,
41-
shape_ref: ShapeRef,
42-
shape: Shape,
43-
start: Start,
44-
stem: Stem,
45-
stemRange: StemRange,
46-
tripleConstraint: TripleConstraint,
47-
value: Value,
48-
}.fetch(name.to_s.downcase.to_sym)
49-
end
50-
51-
##
52-
# Returns the arity of this operator class.
53-
#
54-
# @example
55-
# Operator.arity #=> -1
56-
# Operator::Nullary.arity #=> 0
57-
# Operator::Unary.arity #=> 1
58-
# Operator::Binary.arity #=> 2
59-
# Operator::Ternary.arity #=> 3
60-
#
61-
# @return [Integer] an integer in the range `(-1..3)`
62-
def self.arity
63-
self.const_get(:ARITY)
64-
end
65-
6620
ARITY = -1 # variable arity
6721

6822
##
@@ -93,9 +47,8 @@ def initialize(*operands)
9347
when TrueClass, FalseClass, Numeric, String, DateTime, Date, Time
9448
RDF::Literal(operand)
9549
when NilClass
96-
raise ShEx::OperandError, "Found nil operand for #{self.class.name}"
97-
nil
98-
else raise TypeError, "invalid SPARQL::Algebra::Operator operand: #{operand.inspect}"
50+
raise ArgumentError, "Found nil operand for #{self.class.name}"
51+
else raise TypeError, "invalid ShEx::Algebra::Operator operand: #{operand.inspect}"
9952
end
10053
end
10154

@@ -110,67 +63,13 @@ def initialize(*operands)
11063
end
11164
end
11265

113-
##
114-
# Base URI used for reading data sources with relative URIs
115-
#
116-
# @return [RDF::URI]
117-
def base_uri
118-
Operator.base_uri
119-
end
120-
121-
##
122-
# Base URI used for reading data sources with relative URIs
123-
#
124-
# @return [RDF::URI]
125-
def self.base_uri
126-
@base_uri
127-
end
128-
12966
##
13067
# Is this shape closed?
13168
# @return [Boolean]
13269
def closed?
13370
operands.include?(:closed)
13471
end
13572

136-
##
137-
# Set Base URI associated with SPARQL document, typically done
138-
# when reading SPARQL from a URI
139-
#
140-
# @param [RDF::URI] uri
141-
# @return [RDF::URI]
142-
def self.base_uri=(uri)
143-
@base_uri = RDF::URI(uri)
144-
end
145-
146-
##
147-
# Prefixes useful for future serialization
148-
#
149-
# @return [Hash{Symbol => RDF::URI}]
150-
# Prefix definitions
151-
def prefixes
152-
Operator.prefixes
153-
end
154-
155-
##
156-
# Prefixes useful for future serialization
157-
#
158-
# @return [Hash{Symbol => RDF::URI}]
159-
# Prefix definitions
160-
def self.prefixes
161-
@prefixes
162-
end
163-
164-
##
165-
# Prefixes useful for future serialization
166-
#
167-
# @param [Hash{Symbol => RDF::URI}] hash
168-
# Prefix definitions
169-
# @return [Hash{Symbol => RDF::URI}]
170-
def self.prefixes=(hash)
171-
@prefixes = hash
172-
end
173-
17473
##
17574
# Semantic Actions
17675
# @return [Array<SemAct>]
@@ -344,6 +243,7 @@ class Unary < Operator
344243
# @param [Hash{Symbol => Object}] options
345244
# any additional options (see {Operator#initialize})
346245
def initialize(arg1, options = {})
246+
raise ArgumentError, "wrong number of arguments (given 2, expected 1)" unless options.is_a?(Hash)
347247
super
348248
end
349249
end # Unary
@@ -365,6 +265,7 @@ class Binary < Operator
365265
# @param [Hash{Symbol => Object}] options
366266
# any additional options (see {Operator#initialize})
367267
def initialize(arg1, arg2, options = {})
268+
raise ArgumentError, "wrong number of arguments (given 3, expected 2)" unless options.is_a?(Hash)
368269
super
369270
end
370271
end # Binary

lib/shex/algebra/or.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Or < Operator
77
def initialize(*args, **options)
88
case
99
when args.length <= 1
10-
structure_error("Expected at least one operand, found #{args.length}", exception: ShEx::OperandError)
10+
raise ArgumentError, "wrong number of arguments (given #{args.length}, expected 1..)"
1111
end
1212
super
1313
end
@@ -32,7 +32,6 @@ def satisfies?(n)
3232
end
3333

3434
not_satisfied "Expected some expression to be satisfied"
35-
true
3635
end
3736
end
3837
end

lib/shex/algebra/schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def satisfies?(n, g, m, shapeExterns: [], **options)
3030
@map = m.inject({}) {|memo, (k,v)| memo.merge(k.to_s => v.to_s)}
3131

3232
# First, evaluate semantic acts
33-
operands.select {|o| o.is_a?(SemAct)}.all? do |op|
33+
semantic_actions.all? do |op|
3434
op.satisfies?([])
3535
end
3636

lib/shex/algebra/semact.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ def satisfies?(statements)
2525
$stdout.puts str
2626
status str
2727
not_satisfied "fail" if md && md[1] == 'fail'
28+
true
2829
else
2930
status("unknown SemAct name #{operands.first}") {"expression: #{self.to_sxp}"}
31+
false
3032
end
31-
true
3233
end
3334

3435
# Does This operator is SemAct

0 commit comments

Comments
 (0)