Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Streamline literals by removing extra XSD definitions and using inher…

…ited hooks for looking up the appropriate literal type to use.
  • Loading branch information...
commit 8bada218502e5b2875f5039d56b96e2e420f340d 1 parent 0a9023d
@gkellogg gkellogg authored
View
111 lib/rdf/model/literal.rb
@@ -2,6 +2,10 @@ module RDF
##
# An RDF literal.
#
+ # Subclasses of {RDF::Literal} should define DATATYPE and GRAMMAR constants, which are used
+ # for identifying the appropriate class to use for a datatype URI and to perform lexical
+ # matching on the value.
+ #
# @example Creating a plain literal
# value = RDF::Literal.new("Hello, world!")
# value.plain? #=> true
@@ -39,87 +43,48 @@ module RDF
# @see http://www.w3.org/TR/rdf-concepts/#section-Literals
# @see http://www.w3.org/TR/rdf-concepts/#section-Datatypes-intro
class Literal
- autoload :Boolean, 'rdf/model/literal/boolean'
- autoload :Numeric, 'rdf/model/literal/numeric'
- autoload :Integer, 'rdf/model/literal/integer'
- autoload :NonPositiveInteger, 'rdf/model/literal/integer'
- autoload :NegativeInteger, 'rdf/model/literal/integer'
- autoload :Long, 'rdf/model/literal/integer'
- autoload :Int, 'rdf/model/literal/integer'
- autoload :Short, 'rdf/model/literal/integer'
- autoload :Byte, 'rdf/model/literal/integer'
- autoload :NonNegativeInteger, 'rdf/model/literal/integer'
- autoload :UnsignedLong, 'rdf/model/literal/integer'
- autoload :UnsignedInt, 'rdf/model/literal/integer'
- autoload :UnsignedShort, 'rdf/model/literal/integer'
- autoload :UnsignedInteger, 'rdf/model/literal/integer'
- autoload :UnsignedByte, 'rdf/model/literal/integer'
- autoload :PositiveInteger, 'rdf/model/literal/integer'
- autoload :Double, 'rdf/model/literal/double'
- autoload :Decimal, 'rdf/model/literal/decimal'
- autoload :Date, 'rdf/model/literal/date'
- autoload :DateTime, 'rdf/model/literal/datetime'
- autoload :Float, 'rdf/model/literal/double'
- autoload :Time, 'rdf/model/literal/time'
- autoload :Token, 'rdf/model/literal/token'
- autoload :XML, 'rdf/model/literal/xml'
+
+ private
+ @@subclasses = [] # @private
+
+ ##
+ # @private
+ # @return [void]
+ def self.inherited(child)
+ @@subclasses << child
+ super
+ end
+
+ public
+
+ require 'rdf/model/literal/numeric'
+ require 'rdf/model/literal/boolean'
+ require 'rdf/model/literal/decimal'
+ require 'rdf/model/literal/integer'
+ require 'rdf/model/literal/double'
+ require 'rdf/model/literal/date'
+ require 'rdf/model/literal/dateTime'
+ require 'rdf/model/literal/time'
+ require 'rdf/model/literal/token'
+ require 'rdf/model/literal/xml'
include RDF::Term
##
# @private
+ # Return datatype class for uri, or nil if none is found
+ def self.datatyped_class(uri)
+ @@subclasses.detect {|klass| klass.const_defined?(:DATATYPE) && klass.const_get(:DATATYPE) == uri}
+ end
+
+ ##
+ # @private
def self.new(value, options = {})
klass = case
when !self.equal?(RDF::Literal)
self # subclasses can be directly constructed without type dispatch
- when datatype = options[:datatype]
- case RDF::URI(datatype)
- when XSD.boolean
- RDF::Literal::Boolean
- when XSD.integer
- RDF::Literal::Integer
- when XSD.long
- RDF::Literal::Long
- when XSD.int
- RDF::Literal::Int
- when XSD.short
- RDF::Literal::Short
- when XSD.byte
- RDF::Literal::Byte
- when XSD.float
- RDF::Literal::Float
- when XSD.double
- RDF::Literal::Double
- when XSD.decimal
- RDF::Literal::Decimal
- when XSD.date
- RDF::Literal::Date
- when XSD.dateTime
- RDF::Literal::DateTime
- when XSD.time
- RDF::Literal::Time
- when XSD.nonPositiveInteger
- RDF::Literal::NonPositiveInteger
- when XSD.negativeInteger
- RDF::Literal::NegativeInteger
- when XSD.nonNegativeInteger
- RDF::Literal::NonNegativeInteger
- when XSD.positiveInteger
- RDF::Literal::PositiveInteger
- when XSD.unsignedLong
- RDF::Literal::UnsignedLong
- when XSD.unsignedInt
- RDF::Literal::UnsignedInt
- when XSD.unsignedShort
- RDF::Literal::UnsignedShort
- when XSD.unsignedByte
- RDF::Literal::UnsignedByte
- when XSD.token, XSD.language
- RDF::Literal::Token
- when RDF.XMLLiteral
- RDF::Literal::XML
- else self
- end
+ when typed_literal = datatyped_class(RDF::URI(options[:datatype]))
+ typed_literal
else case value
when ::TrueClass then RDF::Literal::Boolean
when ::FalseClass then RDF::Literal::Boolean
@@ -157,8 +122,10 @@ def self.new(value, options = {})
def initialize(value, options = {})
@object = value
@string = options[:lexical] if options[:lexical]
+ @string = value if !defined?(@string) && value.is_a?(String)
@language = options[:language].to_s.to_sym if options[:language]
@datatype = RDF::URI(options[:datatype]) if options[:datatype]
+ @datatype ||= self.class.const_get(:DATATYPE) if self.class.const_defined?(:DATATYPE)
end
##
View
4 lib/rdf/model/literal/boolean.rb
@@ -14,9 +14,7 @@ class Boolean < Literal
# @param [Boolean] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = case
when true.equal?(value) then true
when false.equal?(value) then false
View
4 lib/rdf/model/literal/date.rb
@@ -12,9 +12,7 @@ class Date < Literal
# @param [Date] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = case
when value.is_a?(::Date) then value
when value.respond_to?(:to_date) then value.to_date # Ruby 1.9+
View
4 lib/rdf/model/literal/datetime.rb
@@ -12,9 +12,7 @@ class DateTime < Literal
# @param [DateTime] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = case
when value.is_a?(::DateTime) then value
when value.respond_to?(:to_datetime) then value.to_datetime # Ruby 1.9+
View
4 lib/rdf/model/literal/decimal.rb
@@ -18,9 +18,7 @@ class Decimal < Numeric
# @param [BigDecimal] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = case
when value.is_a?(BigDecimal) then value
else BigDecimal(value.to_s)
View
29 lib/rdf/model/literal/double.rb
@@ -18,9 +18,7 @@ class Double < Numeric
# @param [Float, #to_f] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = case
when value.is_a?(::String) then case value
when 'INF' then 1/0.0
@@ -186,29 +184,4 @@ def to_s
end
end
end # Double
-
- # Derived types
- # @see http://www.w3.org/TR/xpath-functions/#datatypes
-
- # Note that in XML Schema, Float is not really derived from Double,
- # but implementations are identical in Ruby
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#float
- class Float < Double
- DATATYPE = XSD.float
-
- ##
- # @param [Float, #to_f] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => DATATYPE))
- end
-
- ##
- # Returns the value as a rational number.
- #
- # @return [Rational]
- def to_r
- @object.to_r # only available on Ruby 1.9+
- end
- end # Double
end; end # RDF::Literal
View
137 lib/rdf/model/literal/integer.rb
@@ -19,9 +19,7 @@ class Integer < Decimal
# @param [Integer, #to_i] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = case
when value.is_a?(::String) then Integer(value) rescue nil
when value.is_a?(::Integer) then value
@@ -123,137 +121,4 @@ def to_bn
OpenSSL::BN.new(to_s)
end
end # Integer
-
- # Derived types
- # @see http://www.w3.org/TR/xpath-functions/#datatypes
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#nonPositiveInteger
- class NonPositiveInteger < Integer
- GRAMMAR = /^(?:[\+\-]?0)|(?:-\d+)$/.freeze
-
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.nonPositiveInteger))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#negativeInteger
- class NegativeInteger < NonPositiveInteger
- GRAMMAR = /^\-\d+$/.freeze
-
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.negativeInteger))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#long
- class Long < Integer
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.long))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#int
- class Int < Long
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.int))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#short
- class Short < Int
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.short))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#byte
- class Byte < Short
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.byte))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#nonNegativeInteger
- class NonNegativeInteger < Integer
- GRAMMAR = /^(?:[\+\-]?0)|(?:\+?\d+)$/.freeze
-
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.nonNegativeInteger))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#unsignedLong
- class UnsignedLong < NonNegativeInteger
- GRAMMAR = /^\d+$/.freeze
-
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.unsignedLong))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#unsignedInt
- class UnsignedInt < UnsignedLong
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.unsignedInt))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#unsignedShort
- class UnsignedShort < UnsignedInt
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.unsignedShort))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#unsignedByte
- class UnsignedByte < UnsignedShort
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.unsignedByte))
- end
- end
-
- # @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#positiveInteger
- class PositiveInteger < NonNegativeInteger
- GRAMMAR = /^\+?\d+$/.freeze
-
- ##
- # @param [#to_i] value
- # @option options [String] :lexical (nil)
- def initialize(value, options = {})
- super(value, options.merge(:datatype => XSD.positiveInteger))
- end
- end
end; end # RDF::Literal
View
3  lib/rdf/model/literal/numeric.rb
@@ -57,9 +57,6 @@ def +@
# @return [RDF::Literal::Numeric]
# @since 0.2.3
def -@
- if (self.class == NonPositiveInteger || self.class == NegativeInteger) && object != 0
- # XXX Raise error?
- end
self.class.new(-self.object)
end
View
4 lib/rdf/model/literal/time.rb
@@ -17,9 +17,7 @@ class Time < Literal
# @param [Time] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = case
when value.is_a?(::Time) then value
when value.respond_to?(:to_time) then value.to_time # Ruby 1.9+
View
4 lib/rdf/model/literal/token.rb
@@ -12,9 +12,7 @@ class Token < Literal
# @param [Symbol, #to_s] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
- @string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ super
@object = value.is_a?(Symbol) ? value : value.to_s
end
View
297 spec/model_literal_spec.rb
@@ -627,239 +627,89 @@ def self.literals(*selector)
end
end
- describe RDF::Literal::Numeric do
- context "type-promotion" do
- context "for numbers" do
- {
- :integer => {
- :integer => :integer,
- :nonPositiveInteger => :integer,
- :negativeInteger => :integer,
- :long => :integer,
- :int => :integer,
- :short => :integer,
- :byte => :integer,
- :nonNegativeInteger => :integer,
- :unsignedLong => :integer,
- :unsignedInt => :integer,
- :unsignedShort => :integer,
- :unsignedByte => :integer,
- :positiveInteger => :integer,
- :decimal => :decimal,
- :float => :float,
- :double => :double,
- },
- :decimal => {
- :integer => :decimal,
- :nonPositiveInteger => :decimal,
- :negativeInteger => :decimal,
- :long => :decimal,
- :int => :decimal,
- :short => :decimal,
- :byte => :decimal,
- :nonNegativeInteger => :decimal,
- :unsignedLong => :decimal,
- :unsignedInt => :decimal,
- :unsignedShort => :decimal,
- :unsignedByte => :decimal,
- :positiveInteger => :decimal,
- :decimal => :decimal,
- :float => :float,
- :double => :double,
- },
- :float => {
- :integer => :float,
- :nonPositiveInteger => :float,
- :negativeInteger => :float,
- :long => :float,
- :int => :float,
- :short => :float,
- :byte => :float,
- :nonNegativeInteger => :float,
- :unsignedLong => :float,
- :unsignedInt => :float,
- :unsignedShort => :float,
- :unsignedByte => :float,
- :positiveInteger => :float,
- :decimal => :float,
- :float => :float,
- :double => :double,
- },
- :double => {
- :integer => :double,
- :nonPositiveInteger => :double,
- :negativeInteger => :double,
- :long => :double,
- :int => :double,
- :short => :double,
- :byte => :double,
- :nonNegativeInteger => :double,
- :unsignedLong => :double,
- :unsignedInt => :double,
- :unsignedShort => :double,
- :unsignedByte => :double,
- :positiveInteger => :double,
- :decimal => :double,
- :float => :double,
- :double => :double,
- },
- }.each do |left, right_result|
- if left == :integer
- # Type promotion is equivalent for sub-types of xsd:integer
- (right_result.keys - [:integer, :decimal, :float, :double]).each do |l|
- o_l = RDF::Literal.new(([:nonPositiveInteger, :negativeInteger].include?(l) ? "-1" : "1"), :datatype => RDF::XSD.send(l))
- right_result.each do |right, result|
- o_r = RDF::Literal.new(([:nonPositiveInteger, :negativeInteger].include?(right) ? "-1" : "1"), :datatype => RDF::XSD.send(right))
-
- it "returns #{result} for #{l} + #{right}" do
- (o_l + o_r).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{l} - #{right}" do
- (o_l - o_r).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{l} * #{right}" do
- (o_l * o_r).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{l} / #{right}" do
- (o_l / o_r).datatype.should == RDF::XSD.send(result)
- end
-
- it "returns #{result} for #{right} + #{l}" do
- (o_r + o_l).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{right} - #{l}" do
- (o_r - o_l).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{right} * #{l}" do
- (o_r * o_l).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{right} / #{l}" do
- (o_r / o_l).datatype.should == RDF::XSD.send(result)
- end
- end
- end
- end
-
- o_l = RDF::Literal.new("1", :datatype => RDF::XSD.send(left))
- right_result.each do |right, result|
- o_r = RDF::Literal.new(([:nonPositiveInteger, :negativeInteger].include?(right) ? "-1" : "1"), :datatype => RDF::XSD.send(right))
-
- it "returns #{result} for #{left} + #{right}" do
- (o_l + o_r).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{left} - #{right}" do
- (o_l - o_r).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{left} * #{right}" do
- (o_l * o_r).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{left} / #{right}" do
- (o_l / o_r).datatype.should == RDF::XSD.send(result)
- end
-
- it "returns #{result} for #{right} + #{left}" do
- (o_r + o_l).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{right} - #{left}" do
- (o_r - o_l).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{right} * #{left}" do
- (o_r * o_l).datatype.should == RDF::XSD.send(result)
- end
- it "returns #{result} for #{right} / #{left}" do
- (o_r / o_l).datatype.should == RDF::XSD.send(result)
- end
- end
- end
- end
+ describe RDF::Literal::Double do
+ before(:each) do
+ @nan = RDF::Literal::Double.new("NaN")
+ @inf = RDF::Literal::Double.new("INF")
end
- [RDF::Literal::Float, RDF::Literal::Double].each do |c|
- describe c do
- before(:each) do
- @nan = c.new("NaN")
- @inf = c.new("INF")
- end
-
- it "recognizes INF" do
- @inf.should be_infinite
- RDF::Literal.new('INF', :datatype => c::DATATYPE).should == @inf
- end
-
- it "recognizes -INF" do
- @inf.should be_infinite
- RDF::Literal.new('-INF', :datatype => c::DATATYPE).should == -@inf
- end
-
- it "recognizes NaN" do
- @nan.should be_nan
- RDF::Literal.new('NaN', :datatype => c::DATATYPE).should be_nan
+ it "recognizes INF" do
+ @inf.should be_infinite
+ RDF::Literal.new('INF', :datatype => RDF::Literal::Double::DATATYPE).should == @inf
+ end
+
+ it "recognizes -INF" do
+ @inf.should be_infinite
+ RDF::Literal.new('-INF', :datatype => RDF::Literal::Double::DATATYPE).should == -@inf
+ end
+
+ it "recognizes NaN" do
+ @nan.should be_nan
+ RDF::Literal.new('NaN', :datatype => RDF::Literal::Double::DATATYPE).should be_nan
+ end
+
+ [-1, 0, 1].map {|n| RDF::Literal::Double.new(n)}.each do |n|
+ {
+ :"+" => [RDF::Literal::Double.new("INF"), RDF::Literal::Double.new("INF"), RDF::Literal::Double.new("-INF"), RDF::Literal::Double.new("-INF")],
+ :"-" => [RDF::Literal::Double.new("INF"), RDF::Literal::Double.new("-INF"), RDF::Literal::Double.new("-INF"), RDF::Literal::Double.new("INF")],
+ }.each do |op, (lp, rp, lm, rm)|
+ it "returns #{lp} for INF #{op} #{n}" do
+ @inf.send(op, n).should == lp
end
-
- [-1, 0, 1].map {|n| c.new(n)}.each do |n|
- {
- :"+" => [c.new("INF"), c.new("INF"), c.new("-INF"), c.new("-INF")],
- :"-" => [c.new("INF"), c.new("-INF"), c.new("-INF"), c.new("INF")],
- }.each do |op, (lp, rp, lm, rm)|
- it "returns #{lp} for INF #{op} #{n}" do
- @inf.send(op, n).should == lp
- end
-
- it "returns #{rp} for #{n} #{op} INF" do
- n.send(op, @inf).should == rp
- end
-
- it "returns #{lm} for -INF #{op} #{n}" do
- (-@inf).send(op, n).should == lm
- end
-
- it "returns #{rm} for #{n} #{op} -INF" do
- n.send(op, -@inf).should == rm
- end
- end
-
- it "#{n} + NaN" do
- (n + -@nan).should be_nan
- (-@nan + n).should be_nan
- end
+
+ it "returns #{rp} for #{n} #{op} INF" do
+ n.send(op, @inf).should == rp
end
- # Multiplication
- {
- -1 => [c.new("-INF"), c.new("-INF")],
- 0 => [:nan, :nan],
- 1 => [c.new("INF"), c.new("INF")],
- }.each do |n, (p, m)|
- it "returns #{p} for #{n} * INF" do
- if p == :nan
- (c.new(n) * @inf).should be_nan
- else
- (c.new(n) * @inf).should == p
- end
- end
-
- it "returns #{p} for INF * #{n}" do
- if p == :nan
- (@inf * c.new(n)).should be_nan
- else
- (@inf * c.new(n)).should == p
- end
- end
+ it "returns #{lm} for -INF #{op} #{n}" do
+ (-@inf).send(op, n).should == lm
end
+
+ it "returns #{rm} for #{n} #{op} -INF" do
+ n.send(op, -@inf).should == rm
+ end
+ end
+
+ it "#{n} + NaN" do
+ (n + -@nan).should be_nan
+ (-@nan + n).should be_nan
+ end
+ end
- it "adds infinities" do
- (@inf + @inf).should == @inf
- (@inf + -@inf).should be_nan
- (-@inf + -@inf).should == -@inf
- (-@inf + @inf).should be_nan
+ # Multiplication
+ {
+ -1 => [RDF::Literal::Double.new("-INF"), RDF::Literal::Double.new("-INF")],
+ 0 => [:nan, :nan],
+ 1 => [RDF::Literal::Double.new("INF"), RDF::Literal::Double.new("INF")],
+ }.each do |n, (p, m)|
+ it "returns #{p} for #{n} * INF" do
+ if p == :nan
+ (RDF::Literal::Double.new(n) * @inf).should be_nan
+ else
+ (RDF::Literal::Double.new(n) * @inf).should == p
end
+ end
- it "adds NaN" do
- (@inf + @nan).should be_nan
- (@nan + @nan).should be_nan
+ it "returns #{p} for INF * #{n}" do
+ if p == :nan
+ (@inf * RDF::Literal::Double.new(n)).should be_nan
+ else
+ (@inf * RDF::Literal::Double.new(n)).should == p
end
end
end
+
+ it "adds infinities" do
+ (@inf + @inf).should == @inf
+ (@inf + -@inf).should be_nan
+ (-@inf + -@inf).should == -@inf
+ (-@inf + @inf).should be_nan
+ end
+
+ it "adds NaN" do
+ (@inf + @nan).should be_nan
+ (@nan + @nan).should be_nan
+ end
end
describe "SPARQL tests" do
@@ -883,10 +733,7 @@ def self.literals(*selector)
"eq-2-1 1^^xsd:decimal=1^^xsd:decimal" => [RDF::Literal::Decimal.new(1), RDF::Literal::Decimal.new(1)],
"eq-3 '1'='1'" => [RDF::Literal("1"), RDF::Literal("1")],
"eq-4 'zzz'='zzz'" => [RDF::Literal("zzz"), RDF::Literal("zzz")],
- "numeric '1'^xsd:int=1" => [RDF::Literal::Int.new("1"), RDF::Literal(1)],
"numeric -INF=-INF" => [-RDF::Literal::Double.new("INF"), -RDF::Literal::Double.new("INF")],
- "numeric 1='1'^xsd:int" => [RDF::Literal(1), RDF::Literal::Int.new("1")],
- "numeric 1='1'^xsd:int" => [RDF::Literal(1), RDF::Literal::Int.new("1")],
"numeric INF=INF" => [RDF::Literal::Double.new("INF"), RDF::Literal::Double.new("INF")],
"open-eq-02 'xyz'^^<unknown>='xyz'^^<unknown>" => [RDF::Literal("xyz", :datatype => RDF::URI("unknown")), RDF::Literal("xyz", :datatype => RDF::URI("unknown"))],
"open-eq-03 '01'^xsd:integer=1" => [RDF::Literal::Integer.new("01"), RDF::Literal(1)],
@@ -1051,8 +898,6 @@ def self.literals(*selector)
"eq-1 1=1.0" => [RDF::Literal(1), RDF::Literal(1.0)],
"eq-2-1 1.0e0=1.0" => [RDF::Literal::Double.new("1.0e0"), RDF::Literal::Double.new("1.0")],
"eq-2-1 1='1'^xsd:decimal" => [RDF::Literal(1), RDF::Literal::Decimal.new("1")],
- "numeric '1'^xsd:int=1" => [RDF::Literal::Int.new("1"), RDF::Literal(1)],
- "numeric 1='1'^xsd:int" => [RDF::Literal(1), RDF::Literal::Int.new("1")],
"open-eq-03 '01'^xsd:integer=1" => [RDF::Literal::Integer.new("01"), RDF::Literal(1)],
"open-eq-07 'xyz'='xyz'^^xsd:string" => [RDF::Literal("xyz"), RDF::Literal("xyz", :datatype => XSD.string)],
"open-eq-07 'xyz'xsd:string='xyz'" => [RDF::Literal("xyz", :datatype => XSD.string), RDF::Literal("xyz")],
Please sign in to comment.
Something went wrong with that request. Please try again.