libcdb-ruby 0.1.1 → 0.2.0

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,42 +1,27 @@
1
- describe LibCDB::CDB::Reader do
2
-
3
- before :all do
4
- @temp = tempfile
1
+ #encoding: utf-8
5
2
 
6
- db = LibCDB::CDB::Writer.new(@temp.to_io)
7
- TEST_DATA.each { |k, v| v.each { |w| db.store(k, w) } }
8
- db.close
9
- end
10
-
11
- after :all do
12
- @temp.unlink
13
- end
3
+ describe LibCDB::CDB::Reader do
14
4
 
15
- describe "#initialize" do
5
+ describe '#initialize' do
16
6
 
17
- it "should not accept integers" do
7
+ it 'should not accept integers' do
18
8
  lambda { LibCDB::CDB::Reader.new(12345) }.should raise_error(TypeError)
19
9
  end
20
10
 
21
- it "should not accept pipes" do
11
+ it 'should not accept pipes' do
22
12
  lambda { LibCDB::CDB::Reader.new(STDIN) }.should raise_error(SystemCallError)
23
13
  end
24
14
 
25
- end
26
-
27
- describe "empty" do
28
-
29
- before :all do
30
- @empt = tempfile
31
- LibCDB::CDB::Writer.new(@empt.to_io).close
15
+ it 'should not accept empty files' do
16
+ lambda { reader('empty.dump') }.should raise_error(SystemCallError)
32
17
  end
33
18
 
34
- after :all do
35
- @empt.unlink
36
- end
19
+ end
20
+
21
+ describe 'empty' do
37
22
 
38
23
  before :each do
39
- @db = LibCDB::CDB::Reader.new(File.open(@empt.path))
24
+ @db = reader(:empty)
40
25
  end
41
26
 
42
27
  after :each do
@@ -51,19 +36,19 @@ describe LibCDB::CDB::Reader do
51
36
  @db.should be_empty
52
37
  end
53
38
 
54
- it "should know its size" do
39
+ it 'should know its size' do
55
40
  @db.size.should == 0
56
41
  end
57
42
 
58
- it "should know its total" do
43
+ it 'should know its total' do
59
44
  @db.total.should == 0
60
45
  end
61
46
 
62
- it "should know its keys" do
47
+ it 'should know its keys' do
63
48
  @db.keys.should == []
64
49
  end
65
50
 
66
- it "should know its values" do
51
+ it 'should know its values' do
67
52
  @db.values.should == []
68
53
  end
69
54
 
@@ -75,29 +60,21 @@ describe LibCDB::CDB::Reader do
75
60
  @db.should_not have_value('v3.2')
76
61
  end
77
62
 
78
- it "should dump itself" do
63
+ it 'should dump itself' do
79
64
  @db.dump.should == ''
80
65
  end
81
66
 
82
- it "should convert itself into a hash" do
67
+ it 'should convert itself into a hash' do
83
68
  @db.to_h.should == {}
84
69
  end
85
70
 
86
- it "should convert itself into an array" do
71
+ it 'should convert itself into an array' do
87
72
  @db.to_a.should == []
88
73
  end
89
74
 
90
75
  end
91
76
 
92
- describe "non-empty" do
93
-
94
- before :each do
95
- @db = LibCDB::CDB::Reader.new(File.open(@temp.path))
96
- end
97
-
98
- after :each do
99
- @db.close
100
- end
77
+ shared_examples 'non-empty' do
101
78
 
102
79
  it "should know that it's not closed" do
103
80
  @db.should_not be_closed
@@ -107,47 +84,59 @@ describe LibCDB::CDB::Reader do
107
84
  @db.should_not be_empty
108
85
  end
109
86
 
110
- it "should know its size" do
87
+ it 'should know its size' do
111
88
  @db.size.should == TEST_DATA.size
112
89
  end
113
90
 
114
- it "should know its total" do
91
+ it 'should know its total' do
115
92
  @db.total.should == TEST_DATA.values.flatten.size
116
93
  end
117
94
 
118
- it "should know its keys" do
95
+ it 'should know its keys' do
119
96
  @db.keys.should == TEST_DATA.map { |k, _| k }.uniq
120
97
  end
121
98
 
122
- it "should know its values" do
99
+ it 'should know its values' do
123
100
  @db.values.should == TEST_DATA.map { |_, v| v }.flatten
124
101
  end
125
102
 
126
- it "should know if it has a key" do
103
+ it 'should know if it has a key' do
127
104
  @db.should have_key('k3')
128
105
  end
129
106
 
107
+ it 'should know if it has a wide-char key' do
108
+ @db.should have_key('€ürö')
109
+ end
110
+
130
111
  it "should know if it doesn't have a key" do
131
112
  @db.should_not have_key('none')
132
113
  end
133
114
 
134
- it "should know if it has a value" do
115
+ it 'should know if it has a value' do
135
116
  @db.should have_value('v3.2')
136
117
  end
137
118
 
119
+ it 'should know if it has a wide-char value' do
120
+ @db.should have_value('½a×¾b')
121
+ end
122
+
138
123
  it "should know if it doesn't have a value" do
139
124
  @db.should_not have_value('none')
140
125
  end
141
126
 
142
- it "should get a single value" do
127
+ it 'should get a single value' do
143
128
  @db['k1'].should == 'v1.1'
144
129
  end
145
130
 
146
- it "should not get non-existent value" do
131
+ it 'should get a single wide-char value' do
132
+ @db['€ürö'].should == '½a×¾b'
133
+ end
134
+
135
+ it 'should not get non-existent value' do
147
136
  @db['none'].should be_nil
148
137
  end
149
138
 
150
- it "should get each value" do
139
+ it 'should get each value' do
151
140
  TEST_DATA.each { |k, o|
152
141
  r = []
153
142
  @db.each(k) { |v| r << v }
@@ -155,42 +144,104 @@ describe LibCDB::CDB::Reader do
155
144
  }
156
145
  end
157
146
 
158
- it "should get all values" do
159
- TEST_DATA.each { |k, o|
160
- @db.fetch(k).should == o
161
- }
147
+ it 'should get all values' do
148
+ TEST_DATA.each { |k, o| @db.fetch(k).should == o }
162
149
  end
163
150
 
164
- it "should get last value" do
165
- @db.fetch_last('k10').should == 'v10.10'
151
+ it 'should get first values' do
152
+ TEST_DATA.each { |k, o| @db.fetch_first(k).should == Array(o).first }
166
153
  end
167
154
 
168
- it "should find the key for a value" do
155
+ it 'should get last values' do
156
+ TEST_DATA.each { |k, o| @db.fetch_last(k).should == Array(o).last }
157
+ end
158
+
159
+ it 'should find the key for a value' do
169
160
  @db.key('v3.2').should == 'k3'
170
161
  end
171
162
 
172
- it "should not find the key for a non-existent value" do
163
+ it 'should find the key for a wide-char value' do
164
+ @db.key('½a×¾b').should == '€ürö'
165
+ end
166
+
167
+ it 'should not find the key for a non-existent value' do
173
168
  @db.key('none').should be_nil
174
169
  end
175
170
 
176
- it "should dump records for key" do
171
+ it 'should dump itself' do
172
+ @db.dump.should == File.read(data('test.dump'))
173
+ end
174
+
175
+ it 'should dump records for key' do
177
176
  d = []
178
177
  @db.each_dump('k3') { |e| d << e }
179
178
  d.should == %w[+2,4:k3->v3.1 +2,4:k3->v3.2 +2,4:k3->v3.3]
180
179
  end
181
180
 
182
- it "should convert itself into a hash" do
181
+ it 'should dump records for wide-char key' do
182
+ d = []
183
+ @db.each_dump('€ürö') { |e| d << e }
184
+ d.should == %w[+8,8:€ürö->½a×¾b]
185
+ end
186
+
187
+ it 'should convert itself into a hash' do
183
188
  h = {}
184
189
  TEST_DATA.each { |k, v| h[k] = v.size > 1 ? v : v.first }
185
190
  @db.to_h.should == h
186
191
  end
187
192
 
188
- it "should convert itself into an array" do
193
+ it 'should convert itself into an array' do
189
194
  a = []
190
195
  TEST_DATA.each { |k, v| v.each { |w| a << [k, w] } }
191
196
  @db.to_a.should == a
192
197
  end
193
198
 
199
+ it 'should yield keys' do
200
+ @db.each_key { |k| break k }.should == 'k1'
201
+ end
202
+
203
+ it 'should return an enumerator for #each_key without block' do
204
+ @db.each_key.should be_a(Enumerator)
205
+ end
206
+
207
+ it 'should yield values' do
208
+ @db.each_value { |v| break v }.should == 'v1.1'
209
+ end
210
+
211
+ it 'should return an enumerator for #each_value without block' do
212
+ @db.each_value.should be_a(Enumerator)
213
+ end
214
+
215
+ end
216
+
217
+ describe 'reader' do
218
+
219
+ before do
220
+ @db = reader
221
+ end
222
+
223
+ after do
224
+ @db.close
225
+ end
226
+
227
+ include_examples 'non-empty'
228
+
229
+ end
230
+
231
+ describe '#load_file' do
232
+
233
+ before :all do
234
+ @temp = tempfile
235
+ @db = LibCDB::CDB.load_file(@temp.path, data('test.dump'))
236
+ end
237
+
238
+ after :all do
239
+ @db.close
240
+ @temp.unlink
241
+ end
242
+
243
+ include_examples 'non-empty'
244
+
194
245
  end
195
246
 
196
247
  end
@@ -1,33 +1,38 @@
1
+ require 'tempfile'
2
+
1
3
  describe LibCDB::CDB::Writer do
2
4
 
3
- describe "#initialize" do
5
+ describe '#initialize' do
4
6
 
5
- it "should not accept strings" do
7
+ it 'should not accept strings' do
6
8
  lambda { LibCDB::CDB::Writer.new('test') }.should raise_error(TypeError)
7
9
  end
8
10
 
9
- it "should not accept pipes" do
11
+ it 'should not accept pipes' do
10
12
  lambda { LibCDB::CDB::Writer.new(STDOUT) }.should raise_error(SystemCallError)
11
13
  end
12
14
 
13
15
  end
14
16
 
15
- describe "#add" do
17
+ describe 'when writing' do
16
18
 
17
- before :each do
19
+ before do
18
20
  @temp = tempfile
19
21
  @db = LibCDB::CDB::Writer.new(@temp.to_io)
20
22
  end
21
23
 
22
- after :each do
24
+ after do
23
25
  @temp.unlink
24
26
  end
25
27
 
26
- it "should add data" do
27
- TEST_DATA.each { |k, v| v.each { |w| @db.add(k, w) } }
28
- @db.close
29
- @db.should be_closed
30
- end
28
+ %w[insert replace store].each { |m|
29
+ it "should #{m} data" do
30
+ TEST_DATA.each { |k, v| v.each { |w| @db.send(m, k, w) } }
31
+
32
+ @db.close
33
+ @db.should be_closed
34
+ end
35
+ }
31
36
 
32
37
  end
33
38
 
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  $:.unshift('lib') unless $:.first == 'lib'
2
4
 
3
5
  require 'libcdb'
@@ -14,10 +16,26 @@ RSpec.configure { |config|
14
16
  1.upto(i) { |j| v << "v#{i}.#{j}" }
15
17
  }
16
18
 
17
- TEST_DATA['a' * 1024] = Array('b' * 1024 ** 2)
19
+ TEST_DATA['€ürö'] = ['½a×¾b']
20
+
21
+ TEST_DATA['a' * 1024] = ['b' * 1024 ** 2]
18
22
 
19
23
  def tempfile
20
24
  Tempfile.open("libcdb_spec_#{object_id}_temp")
21
25
  end
26
+
27
+ def data(file)
28
+ File.join(File.dirname(__FILE__), 'data', file)
29
+ end
30
+
31
+ def reader(cdb = :test)
32
+ cdb = "#{cdb}.cdb" if cdb.is_a?(Symbol)
33
+ LibCDB::CDB::Reader.new(File.open(data(cdb)))
34
+ end
22
35
  })
23
36
  }
37
+
38
+ class String
39
+ alias_method :_original_inspect, :inspect
40
+ def inspect; size > 100 ? "#{self.class}(#{size})" : _original_inspect; end
41
+ end
metadata CHANGED
@@ -1,17 +1,37 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libcdb-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-25 00:00:00.000000000 Z
11
+ date: 2014-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hen
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.8.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.8'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.8.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
15
35
  requirement: !ruby/object:Gem::Requirement
16
36
  requirements:
17
37
  - - ">="
@@ -25,7 +45,7 @@ dependencies:
25
45
  - !ruby/object:Gem::Version
26
46
  version: '0'
27
47
  - !ruby/object:Gem::Dependency
28
- name: rake
48
+ name: rake-compiler
29
49
  requirement: !ruby/object:Gem::Requirement
30
50
  requirements:
31
51
  - - ">="
@@ -85,6 +105,10 @@ files:
85
105
  - lib/libcdb-ruby.rb
86
106
  - lib/libcdb.rb
87
107
  - lib/libcdb/version.rb
108
+ - spec/data/empty.cdb
109
+ - spec/data/empty.dump
110
+ - spec/data/test.cdb
111
+ - spec/data/test.dump
88
112
  - spec/libcdb/reader_spec.rb
89
113
  - spec/libcdb/writer_spec.rb
90
114
  - spec/spec_helper.rb
@@ -94,14 +118,19 @@ licenses:
94
118
  metadata: {}
95
119
  post_install_message: |2+
96
120
 
97
- libcdb-ruby-0.1.1 [2014-04-25]:
121
+ libcdb-ruby-0.2.0 [2014-12-05]:
98
122
 
99
- * <b>Dropped support for Ruby 1.9.2.</b>
100
- * Housekeeping.
123
+ * Added encoding support to LibCDB::CDB::Reader.
124
+ * Added LibCDB::CDB.load and LibCDB::CDB.load_file to create a database from a
125
+ dump.
126
+ * Added LibCDB::CDB.stats and LibCDB::CDB.print_stats to collect stats from a
127
+ database.
128
+ * Fixed that LibCDB::CDB::Reader#each_key and LibCDB::CDB::Reader#each_value
129
+ would not return an enumerator when no block was given.
101
130
 
102
131
  rdoc_options:
103
132
  - "--title"
104
- - libcdb-ruby Application documentation (v0.1.1)
133
+ - libcdb-ruby Application documentation (v0.2.0)
105
134
  - "--charset"
106
135
  - UTF-8
107
136
  - "--line-numbers"
@@ -122,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
151
  version: '0'
123
152
  requirements: []
124
153
  rubyforge_project:
125
- rubygems_version: 2.2.2.x
154
+ rubygems_version: 2.4.5
126
155
  signing_key:
127
156
  specification_version: 4
128
157
  summary: Ruby bindings for CDB Constant Databases.