/spec/model_uri_spec.rb

http://github.com/bendiken/rdf · Ruby · 197 lines · 158 code · 37 blank · 2 comment · 14 complexity · 14609961131f82a233b20d77652e3b41 MD5 · raw file

  1. require File.join(File.dirname(__FILE__), 'spec_helper')
  2. describe RDF::URI do
  3. before :each do
  4. @new = Proc.new { |*args| RDF::URI.new(*args) }
  5. end
  6. it "should be instantiable" do
  7. lambda { @new.call('http://rdf.rubyforge.org/') }.should_not raise_error
  8. end
  9. it "should recognize URNs" do
  10. urns = %w(urn:isbn:0451450523 urn:isan:0000-0000-9E59-0000-O-0000-0000-2 urn:issn:0167-6423 urn:ietf:rfc:2648 urn:mpeg:mpeg7:schema:2001 urn:oid:2.16.840 urn:uuid:6e8bc430-9c3a-11d9-9669-0800200c9a66 urn:uci:I001+SBSi-B10000083052)
  11. urns.each do |urn|
  12. uri = @new.call(urn)
  13. uri.should be_a_uri
  14. uri.should respond_to(:urn?)
  15. uri.should be_a_urn
  16. uri.should_not be_a_url
  17. end
  18. end
  19. it "should recognize URLs" do
  20. urls = %w(mailto:jhacker@example.org http://example.org/ ftp://example.org/)
  21. urls.each do |url|
  22. uri = @new.call(url)
  23. uri.should be_a_uri
  24. uri.should respond_to(:url?)
  25. uri.should be_a_url
  26. uri.should_not be_a_urn
  27. end
  28. end
  29. it "should return the root URI" do
  30. uri = @new.call('http://rdf.rubyforge.org/RDF/URI.html')
  31. uri.should respond_to(:root)
  32. uri.root.should be_a_uri
  33. uri.root.should == @new.call('http://rdf.rubyforge.org/')
  34. end
  35. it "should find the parent URI" do
  36. uri = @new.call('http://rdf.rubyforge.org/RDF/URI.html')
  37. uri.should respond_to(:parent)
  38. uri.parent.should be_a_uri
  39. uri.parent.should == @new.call('http://rdf.rubyforge.org/RDF/')
  40. uri.parent.parent.should == @new.call('http://rdf.rubyforge.org/')
  41. uri.parent.parent.parent.should be_nil
  42. end
  43. it "should have a consistent hash code" do
  44. hash1 = @new.call('http://rdf.rubyforge.org/').hash
  45. hash2 = @new.call('http://rdf.rubyforge.org/').hash
  46. hash1.should == hash2
  47. end
  48. it "should be duplicable" do
  49. url = Addressable::URI.parse('http://rdf.rubyforge.org/')
  50. uri2 = (uri1 = @new.call(url)).dup
  51. uri1.should_not be_equal(uri2)
  52. uri1.should be_eql(uri2)
  53. uri1.should == uri2
  54. url.path = '/rdf/'
  55. uri1.should_not be_equal(uri2)
  56. uri1.should_not be_eql(uri2)
  57. uri1.should_not == uri2
  58. end
  59. it "should not be #anonymous?" do
  60. @new.call('http://example.org').should_not be_anonymous
  61. end
  62. context "using the smart separator (/)" do
  63. {
  64. # #!! means that I'm not sure I like the semantics, but they are cases for
  65. # arguably invalid input, probably without 'correct' answers.
  66. %w(http://foo a) => "http://foo/a",
  67. %w(http://foo /a) => "http://foo/a",
  68. %w(http://foo #a) => "http://foo#a",
  69. %w(http://foo/ a) => "http://foo/a",
  70. %w(http://foo/ /a) => "http://foo/a",
  71. %w(http://foo/ #a) => "http://foo#a", #!!
  72. %w(http://foo# a) => "http://foo#a",
  73. %w(http://foo# /a) => "http://foo/a", #!!
  74. %w(http://foo# #a) => "http://foo#a",
  75. %w(http://foo/bar a) => "http://foo/bar/a",
  76. %w(http://foo/bar /a) => "http://foo/bar/a",
  77. %w(http://foo/bar #a) => "http://foo/bar#a",
  78. %w(http://foo/bar/ a) => "http://foo/bar/a",
  79. %w(http://foo/bar/ /a) => "http://foo/bar/a",
  80. %w(http://foo/bar/ #a) => "http://foo/bar#a", #!!
  81. %w(http://foo/bar# a) => "http://foo/bar#a",
  82. %w(http://foo/bar# /a) => "http://foo/bar/a", #!!
  83. %w(http://foo/bar# #a) => "http://foo/bar#a",
  84. %w(urn:isbn: 0451450523) => "urn:isbn:0451450523",
  85. %w(urn:isbn: :0451450523) => "urn:isbn:0451450523",
  86. %w(urn:isbn 0451450523) => "urn:isbn:0451450523",
  87. %w(urn:isbn :0451450523) => "urn:isbn:0451450523",
  88. }.each_pair do |input, result|
  89. it "should create <#{result}> from <#{input[0]}> and '#{input[1]}'" do
  90. (RDF::URI.new(input[0]) / input[1]).to_s.should == result
  91. (RDF::URI.new(input[0]) / RDF::URI.new(input[1])).to_s.should == result unless input[1][0,1] == ':'
  92. end
  93. end
  94. it "should raise an ArgumentError when receiving an absolute URI as a fragment" do
  95. lambda { RDF::URI.new('http://example.org') / RDF::URI.new('http://example.com') }.should raise_error ArgumentError
  96. end
  97. end
  98. context "using concatenation (#+)" do
  99. {
  100. %w(http://foo/ a) => "http://foo/a",
  101. %w(http://foo/ /a) => "http://foo//a",
  102. %w(http://foo/ #a) => "http://foo/#a",
  103. %w(urn:isbn :0451450523) => "urn:isbn:0451450523",
  104. %w(urn:isbn 0451450523) => "urn:isbn0451450523",
  105. %w(http://example.org/test test) => "http://example.org/testtest",
  106. }.each_pair do |input, result|
  107. it "should create <#{result}> from <#{input[0]}> and '#{input[1]}'" do
  108. (RDF::URI.new(input[0]) + input[1]).to_s.should == result
  109. (RDF::URI.new(input[0]) + RDF::URI.new(input[1])).to_s.should == result unless input[1][0,1] == ':'
  110. end
  111. end
  112. end
  113. context "using normalized merging (#join)" do
  114. before :all do
  115. @writer = RDF::Writer.for(:ntriples)
  116. end
  117. before :each do
  118. @subject = RDF::URI.new("http://example.org")
  119. end
  120. it "appends fragment to uri" do
  121. @subject.join("foo").to_s.should == "http://example.org/foo"
  122. end
  123. it "appends another fragment" do
  124. @subject.join("foo#bar").to_s.should == "http://example.org/foo#bar"
  125. end
  126. it "appends another URI" do
  127. @subject.join(RDF::URI.new("foo#bar")).to_s.should == "http://example.org/foo#bar"
  128. end
  129. {
  130. %w(http://foo ) => "<http://foo>",
  131. %w(http://foo a) => "<http://foo/a>",
  132. %w(http://foo /a) => "<http://foo/a>",
  133. %w(http://foo #a) => "<http://foo#a>",
  134. %w(http://foo/ ) => "<http://foo/>",
  135. %w(http://foo/ a) => "<http://foo/a>",
  136. %w(http://foo/ /a) => "<http://foo/a>",
  137. %w(http://foo/ #a) => "<http://foo/#a>",
  138. %w(http://foo# ) => "<http://foo#>",
  139. %w(http://foo# a) => "<http://foo/a>",
  140. %w(http://foo# /a) => "<http://foo/a>",
  141. %w(http://foo# #a) => "<http://foo#a>",
  142. %w(http://foo/bar ) => "<http://foo/bar>",
  143. %w(http://foo/bar a) => "<http://foo/a>",
  144. %w(http://foo/bar /a) => "<http://foo/a>",
  145. %w(http://foo/bar #a) => "<http://foo/bar#a>",
  146. %w(http://foo/bar/ ) => "<http://foo/bar/>",
  147. %w(http://foo/bar/ a) => "<http://foo/bar/a>",
  148. %w(http://foo/bar/ /a) => "<http://foo/a>",
  149. %w(http://foo/bar/ #a) => "<http://foo/bar/#a>",
  150. %w(http://foo/bar# ) => "<http://foo/bar#>",
  151. %w(http://foo/bar# a) => "<http://foo/a>",
  152. %w(http://foo/bar# /a) => "<http://foo/a>",
  153. %w(http://foo/bar# #a) => "<http://foo/bar#a>",
  154. }.each_pair do |input, result|
  155. it "creates #{result} from <#{input[0]}> and '#{input[1]}'" do
  156. @writer.serialize(RDF::URI.new(input[0]).join(input[1].to_s)).should == result
  157. end
  158. end
  159. end
  160. end