rdf-spec 0.3.8 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,88 +1,117 @@
1
1
  require 'rdf/spec'
2
2
 
3
- share_as :RDF_Writable do
3
+ module RDF_Writable
4
+ extend RSpec::SharedContext
4
5
  include RDF::Spec::Matchers
5
6
 
6
7
  before :each do
7
8
  raise '+@writable+ must be defined in a before(:each) block' unless instance_variable_get('@writable')
8
9
 
9
- @filename = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'etc', 'doap.nt'))
10
+ @filename = File.expand_path("../../../../etc/doap.nt", __FILE__)
11
+ @statements = RDF::NTriples::Reader.new(File.open(@filename)).to_a
10
12
 
11
13
  @supports_context = @writable.respond_to?(:supports?) && @writable.supports?(:context)
12
14
  end
13
-
14
- it "responds to #writable?" do
15
- @writable.respond_to?(:readable?)
16
- end
17
-
18
- it "implements #writable?" do
19
- !!@writable.writable?.should == @writable.writable?
20
- end
21
15
 
22
- describe "#<<" do
23
- it "inserts a reader"
24
- it "inserts a graph"
25
- it "inserts an enumerable"
26
- it "inserts data responding to #to_rdf"
27
- it "inserts a statement"
28
- end
29
-
30
- context "when inserting statements" do
31
- before :each do
32
- @statements = RDF::NTriples::Reader.new(File.open(@filename)).to_a
16
+ describe RDF::Writable do
17
+ it "responds to #writable?" do
18
+ @writable.respond_to?(:readable?)
33
19
  end
34
-
35
- it "should support #insert" do
36
- @writable.should respond_to(:insert)
20
+
21
+ it "implements #writable?" do
22
+ !!@writable.writable?.should == @writable.writable?
37
23
  end
38
24
 
39
- it "should not raise errors" do
40
- if @writable.writable?
41
- lambda { @writable.insert(@statements.first) }.should_not raise_error
25
+ describe "#<<" do
26
+ it "inserts a reader" do
27
+ reader = RDF::NTriples::Reader.new(File.open(@filename)).to_a
28
+ @writable << reader
29
+ @writable.should have_statement(@statements.first)
30
+ @writable.count.should == @statements.size
42
31
  end
43
- end
44
32
 
45
- it "should support inserting one statement at a time" do
46
- if @writable.writable?
47
- @writable.insert(@statements.first)
33
+ it "inserts a graph" do
34
+ graph = RDF::Graph.new << @statements
35
+ @writable << graph
48
36
  @writable.should have_statement(@statements.first)
37
+ @writable.count.should == @statements.size
49
38
  end
50
- end
51
39
 
52
- it "should support inserting multiple statements at a time" do
53
- if @writable.writable?
54
- @writable.insert(*@statements)
40
+ it "inserts an enumerable" do
41
+ enumerable = @statements.dup.extend(RDF::Enumerable)
42
+ @writable << enumerable
43
+ @writable.should have_statement(@statements.first)
44
+ @writable.count.should == @statements.size
55
45
  end
56
- end
57
46
 
58
- it "should insert statements successfully" do
59
- if @writable.writable?
60
- @writable.insert(*@statements)
47
+ it "inserts data responding to #to_rdf" do
48
+ mock = double('mock')
49
+ mock.stub(:to_rdf).and_return(@statements)
50
+ @writable << mock
51
+ @writable.should have_statement(@statements.first)
61
52
  @writable.count.should == @statements.size
62
53
  end
63
- end
64
54
 
65
- it "should not insert a statement twice" do
66
- if @writable.writable?
67
- @writable.insert(@statements.first)
68
- @writable.insert(@statements.first)
55
+ it "inserts a statement" do
56
+ @writable << @statements.first
57
+ @writable.should have_statement(@statements.first)
69
58
  @writable.count.should == 1
70
59
  end
71
60
  end
72
61
 
73
- it "should treat statements with a different context as distinct" do
74
- if @writable.writable?
75
- s1 = @statements.first.dup
76
- s1.context = nil
77
- s2 = @statements.first.dup
78
- s2.context = RDF::URI.new("urn:context:1")
79
- s3 = @statements.first.dup
80
- s3.context = RDF::URI.new("urn:context:2")
81
- @writable.insert(s1)
82
- @writable.insert(s2)
83
- @writable.insert(s3)
84
- # If contexts are not suported, all three are redundant
85
- @writable.count.should == (@supports_context ? 3 : 1)
62
+ context "when inserting statements" do
63
+ it "should support #insert" do
64
+ @writable.should respond_to(:insert)
65
+ end
66
+
67
+ it "should not raise errors" do
68
+ if @writable.writable?
69
+ lambda { @writable.insert(@statements.first) }.should_not raise_error
70
+ end
71
+ end
72
+
73
+ it "should support inserting one statement at a time" do
74
+ if @writable.writable?
75
+ @writable.insert(@statements.first)
76
+ @writable.should have_statement(@statements.first)
77
+ end
78
+ end
79
+
80
+ it "should support inserting multiple statements at a time" do
81
+ if @writable.writable?
82
+ @writable.insert(*@statements)
83
+ end
84
+ end
85
+
86
+ it "should insert statements successfully" do
87
+ if @writable.writable?
88
+ @writable.insert(*@statements)
89
+ @writable.count.should == @statements.size
90
+ end
91
+ end
92
+
93
+ it "should not insert a statement twice" do
94
+ if @writable.writable?
95
+ @writable.insert(@statements.first)
96
+ @writable.insert(@statements.first)
97
+ @writable.count.should == 1
98
+ end
99
+ end
100
+
101
+ it "should treat statements with a different context as distinct" do
102
+ if @writable.writable?
103
+ s1 = @statements.first.dup
104
+ s1.context = nil
105
+ s2 = @statements.first.dup
106
+ s2.context = RDF::URI.new("urn:context:1")
107
+ s3 = @statements.first.dup
108
+ s3.context = RDF::URI.new("urn:context:2")
109
+ @writable.insert(s1)
110
+ @writable.insert(s2)
111
+ @writable.insert(s3)
112
+ # If contexts are not suported, all three are redundant
113
+ @writable.count.should == (@supports_context ? 3 : 1)
114
+ end
86
115
  end
87
116
  end
88
117
  end
@@ -2,7 +2,8 @@ require 'rdf/spec'
2
2
  require 'fileutils'
3
3
  require 'tmpdir'
4
4
 
5
- share_as :RDF_Writer do
5
+ module RDF_Writer
6
+ extend RSpec::SharedContext
6
7
  include RDF::Spec::Matchers
7
8
 
8
9
  before(:each) do
@@ -10,165 +11,167 @@ share_as :RDF_Writer do
10
11
  @writer_class = @writer.class
11
12
  end
12
13
 
13
- describe ".each" do
14
- it "yields each writer" do
15
- @writer_class.each do |r|
16
- r.should_not be_nil
14
+ describe RDF::Writer do
15
+ describe ".each" do
16
+ it "yields each writer" do
17
+ @writer_class.each do |r|
18
+ r.should_not be_nil
19
+ end
17
20
  end
18
21
  end
19
- end
20
22
 
21
- describe ".buffer" do
22
- it "calls .new with buffer and other arguments" do
23
- @writer_class.should_receive(:new)
24
- @writer_class.buffer do |r|
25
- r.should be_a(@writer_class)
23
+ describe ".buffer" do
24
+ it "calls .new with buffer and other arguments" do
25
+ @writer_class.should_receive(:new)
26
+ @writer_class.buffer do |r|
27
+ r.should be_a(@writer_class)
28
+ end
26
29
  end
27
30
  end
28
- end
29
31
 
30
- describe ".open" do
31
- before(:each) do
32
- RDF::Util::File.stub!(:open_file).and_yield(StringIO.new("foo"))
33
- @dir = Dir.tmpdir
34
- @basename = File.join(@dir, "foo")
35
- end
32
+ describe ".open" do
33
+ before(:each) do
34
+ RDF::Util::File.stub!(:open_file).and_yield(StringIO.new("foo"))
35
+ @dir = Dir.mktmpdir
36
+ @basename = File.join(@dir, "foo")
37
+ end
36
38
 
37
- after(:each) do
38
- FileUtils.rm_rf(@dir)
39
- end
40
-
41
- it "yields writer given file_name" do
42
- @writer_class.format.each do |f|
43
- f.file_extensions.each_pair do |sym, content_type|
44
- writer_mock = mock("writer")
45
- writer_mock.should_receive(:got_here)
46
- @writer_class.should_receive(:for).with(:file_name => "#{@basename}.#{sym}").and_return(@writer_class)
47
- @writer_class.open("#{@basename}.#{sym}") do |r|
48
- r.should be_a(RDF::Writer)
49
- writer_mock.got_here
50
- end
51
- end
39
+ after(:each) do
40
+ FileUtils.rm_rf(@dir)
52
41
  end
53
- end
54
42
 
55
- it "yields writer given symbol" do
56
- @writer_class.format.each do |f|
57
- sym = f.to_sym # Like RDF::NTriples::Format => :ntriples
58
- writer_mock = mock("writer")
59
- writer_mock.should_receive(:got_here)
60
- @writer_class.should_receive(:for).with(sym).and_return(@writer_class)
61
- @writer_class.open("#{@basename}.#{sym}", :format => sym) do |r|
62
- r.should be_a(RDF::Writer)
63
- writer_mock.got_here
43
+ it "yields writer given file_name" do
44
+ @writer_class.format.each do |f|
45
+ f.file_extensions.each_pair do |sym, content_type|
46
+ writer_mock = mock("writer")
47
+ writer_mock.should_receive(:got_here)
48
+ @writer_class.should_receive(:for).with(:file_name => "#{@basename}.#{sym}").and_return(@writer_class)
49
+ @writer_class.open("#{@basename}.#{sym}") do |r|
50
+ r.should be_a(RDF::Writer)
51
+ writer_mock.got_here
52
+ end
53
+ end
64
54
  end
65
55
  end
66
- end
67
56
 
68
- it "yields writer given {:file_name => file_name}" do
69
- @writer_class.format.each do |f|
70
- f.file_extensions.each_pair do |sym, content_type|
57
+ it "yields writer given symbol" do
58
+ @writer_class.format.each do |f|
59
+ sym = f.to_sym # Like RDF::NTriples::Format => :ntriples
71
60
  writer_mock = mock("writer")
72
61
  writer_mock.should_receive(:got_here)
73
- @writer_class.should_receive(:for).with(:file_name => "#{@basename}.#{sym}").and_return(@writer_class)
74
- @writer_class.open("#{@basename}.#{sym}", :file_name => "#{@basename}.#{sym}") do |r|
62
+ @writer_class.should_receive(:for).with(sym).and_return(@writer_class)
63
+ @writer_class.open("#{@basename}.#{sym}", :format => sym) do |r|
75
64
  r.should be_a(RDF::Writer)
76
65
  writer_mock.got_here
77
66
  end
78
67
  end
79
68
  end
80
- end
81
69
 
82
- it "yields writer given {:content_type => 'a/b'}" do
83
- @writer_class.format.each do |f|
84
- f.content_types.each_pair do |content_type, formats|
85
- writer_mock = mock("writer")
86
- writer_mock.should_receive(:got_here)
87
- @writer_class.should_receive(:for).with(:content_type => content_type, :file_name => @basename).and_return(@writer_class)
88
- @writer_class.open(@basename, :content_type => content_type) do |r|
89
- r.should be_a(RDF::Writer)
90
- writer_mock.got_here
70
+ it "yields writer given {:file_name => file_name}" do
71
+ @writer_class.format.each do |f|
72
+ f.file_extensions.each_pair do |sym, content_type|
73
+ writer_mock = mock("writer")
74
+ writer_mock.should_receive(:got_here)
75
+ @writer_class.should_receive(:for).with(:file_name => "#{@basename}.#{sym}").and_return(@writer_class)
76
+ @writer_class.open("#{@basename}.#{sym}", :file_name => "#{@basename}.#{sym}") do |r|
77
+ r.should be_a(RDF::Writer)
78
+ writer_mock.got_here
79
+ end
80
+ end
81
+ end
82
+ end
83
+
84
+ it "yields writer given {:content_type => 'a/b'}" do
85
+ @writer_class.format.each do |f|
86
+ f.content_types.each_pair do |content_type, formats|
87
+ writer_mock = mock("writer")
88
+ writer_mock.should_receive(:got_here)
89
+ @writer_class.should_receive(:for).with(:content_type => content_type, :file_name => @basename).and_return(@writer_class)
90
+ @writer_class.open(@basename, :content_type => content_type) do |r|
91
+ r.should be_a(RDF::Writer)
92
+ writer_mock.got_here
93
+ end
91
94
  end
92
95
  end
93
96
  end
94
97
  end
95
- end
96
98
 
97
- describe ".format" do
98
- it "returns itself even if given explicit format" do
99
- other_format = @writer_class == RDF::NTriples::Writer ? :nquads : :ntriples
100
- @writer_class.for(other_format).should == @writer_class
99
+ describe ".format" do
100
+ it "returns itself even if given explicit format" do
101
+ other_format = @writer_class == RDF::NTriples::Writer ? :nquads : :ntriples
102
+ @writer_class.for(other_format).should == @writer_class
103
+ end
101
104
  end
102
- end
103
105
 
104
- describe ".new" do
105
- it "sets @output to $stdout by default" do
106
- writer_mock = mock("writer")
107
- writer_mock.should_receive(:got_here)
108
- save, $stdout = $stdout, StringIO.new
106
+ describe ".new" do
107
+ it "sets @output to $stdout by default" do
108
+ writer_mock = mock("writer")
109
+ writer_mock.should_receive(:got_here)
110
+ save, $stdout = $stdout, StringIO.new
109
111
 
110
- @writer_class.new do |r|
111
- writer_mock.got_here
112
- r.instance_variable_get(:@output).should == $stdout
112
+ @writer_class.new do |r|
113
+ writer_mock.got_here
114
+ r.instance_variable_get(:@output).should == $stdout
115
+ end
116
+ $stdout = save
113
117
  end
114
- $stdout = save
115
- end
116
118
 
117
- it "sets @output to file given something other than a string" do
118
- writer_mock = mock("writer")
119
- writer_mock.should_receive(:got_here)
120
- file = StringIO.new
121
- file.should_receive(:write).any_number_of_times
122
- @writer_class.new(file) do |r|
123
- writer_mock.got_here
124
- r.instance_variable_get(:@output).should == file
119
+ it "sets @output to file given something other than a string" do
120
+ writer_mock = mock("writer")
121
+ writer_mock.should_receive(:got_here)
122
+ file = StringIO.new
123
+ file.should_receive(:write).any_number_of_times
124
+ @writer_class.new(file) do |r|
125
+ writer_mock.got_here
126
+ r.instance_variable_get(:@output).should == file
127
+ end
125
128
  end
126
- end
127
129
 
128
- it "sets prefixes given :prefixes => {}" do
129
- writer_mock = mock("writer")
130
- writer_mock.should_receive(:got_here)
131
- @writer_class.new(StringIO.new, :prefixes => {:a => "b"}) do |r|
132
- writer_mock.got_here
133
- r.prefixes.should == {:a => "b"}
130
+ it "sets prefixes given :prefixes => {}" do
131
+ writer_mock = mock("writer")
132
+ writer_mock.should_receive(:got_here)
133
+ @writer_class.new(StringIO.new, :prefixes => {:a => "b"}) do |r|
134
+ writer_mock.got_here
135
+ r.prefixes.should == {:a => "b"}
136
+ end
134
137
  end
135
- end
136
138
 
137
- #it "calls #write_prologue" do
138
- # writer_mock = mock("writer")
139
- # writer_mock.should_receive(:got_here)
140
- # @writer_class.any_instance.should_receive(:write_prologue)
141
- # @writer_class.new(StringIO.new) do |r|
142
- # writer_mock.got_here
143
- # end
144
- #end
145
- #
146
- #it "calls #write_epilogue" do
147
- # writer_mock = mock("writer")
148
- # writer_mock.should_receive(:got_here)
149
- # @writer_class.any_instance.should_receive(:write_epilogue)
150
- # @writer_class.new(StringIO.new) do |r|
151
- # writer_mock.got_here
152
- # end
153
- #end
154
- end
139
+ #it "calls #write_prologue" do
140
+ # writer_mock = mock("writer")
141
+ # writer_mock.should_receive(:got_here)
142
+ # @writer_class.any_instance.should_receive(:write_prologue)
143
+ # @writer_class.new(StringIO.new) do |r|
144
+ # writer_mock.got_here
145
+ # end
146
+ #end
147
+ #
148
+ #it "calls #write_epilogue" do
149
+ # writer_mock = mock("writer")
150
+ # writer_mock.should_receive(:got_here)
151
+ # @writer_class.any_instance.should_receive(:write_epilogue)
152
+ # @writer_class.new(StringIO.new) do |r|
153
+ # writer_mock.got_here
154
+ # end
155
+ #end
156
+ end
155
157
 
156
- describe "#prefixes=" do
157
- it "sets prefixes from hash" do
158
- @writer.prefixes = {:a => "b"}
159
- @writer.prefixes.should == {:a => "b"}
158
+ describe "#prefixes=" do
159
+ it "sets prefixes from hash" do
160
+ @writer.prefixes = {:a => "b"}
161
+ @writer.prefixes.should == {:a => "b"}
162
+ end
160
163
  end
161
- end
162
164
 
163
- describe "#prefix" do
164
- {
165
- nil => "nil",
166
- :a => "b",
167
- "foo" => "bar",
168
- }.each_pair do |pfx, uri|
169
- it "sets prefix(#{pfx}) to #{uri}" do
170
- @writer.prefix(pfx, uri).should == uri
171
- @writer.prefix(pfx).should == uri
165
+ describe "#prefix" do
166
+ {
167
+ nil => "nil",
168
+ :a => "b",
169
+ "foo" => "bar",
170
+ }.each_pair do |pfx, uri|
171
+ it "sets prefix(#{pfx}) to #{uri}" do
172
+ @writer.prefix(pfx, uri).should == uri
173
+ @writer.prefix(pfx).should == uri
174
+ end
172
175
  end
173
176
  end
174
177
  end