lapis-common 0.1.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.
@@ -0,0 +1,450 @@
1
+ require_relative '../spec_helper'
2
+
3
+ RSpec::Matchers.define :be_a_valid_uuid_string do
4
+ match do |actual|
5
+ !!/^[0-9a-f]{8}(-?)[0-9a-f]{4}\1[1-5][0-9a-f]{3}\1[89ab][0-9a-f]{3}\1[0-9a-f]{12}$/mi.match(actual)
6
+ end
7
+
8
+ description do
9
+ 'be a valid UUID string'
10
+ end
11
+
12
+ failure_message do |actual|
13
+ "expected that #{actual} would be a valid UUID string"
14
+ end
15
+
16
+ failure_message_when_negated do |actual|
17
+ "expected that #{actual} would not be a valid UUID string"
18
+ end
19
+ end
20
+
21
+ RSpec.describe Lapis::Uuid do
22
+
23
+ describe '::DEFAULT' do
24
+ subject { Lapis::Uuid::DEFAULT }
25
+
26
+ it 'is all zeroes' do
27
+ is_expected.to eq("\x0" * 16)
28
+ end
29
+
30
+ it 'is frozen' do
31
+ is_expected.to be_frozen
32
+ end
33
+ end
34
+
35
+ describe '#new' do
36
+ context 'with a valid value' do
37
+ let(:uuid_value) { build(:uuid_value) }
38
+ subject(:uuid) { Lapis::Uuid.new(uuid_value) }
39
+
40
+ describe 'the value' do
41
+ subject { uuid.value }
42
+
43
+ it 'is stored' do
44
+ is_expected.to eq uuid_value
45
+ end
46
+ end
47
+ end
48
+
49
+ context 'with an invalid value' do
50
+ let(:uuid_value) { 'foobar' }
51
+
52
+ it 'throws an ArgumentError' do
53
+ expect { subject }.to raise_error(ArgumentError)
54
+ end
55
+ end
56
+ end
57
+
58
+ context '.generate' do
59
+ subject(:uuid) { Lapis::Uuid.generate }
60
+
61
+ describe 'the value' do
62
+ let(:other) { build(:uuid) }
63
+ subject { uuid.value }
64
+
65
+ # The UUID standards don't allow generated UUID to be all zeroes.
66
+ it 'is not DEFAULT' do
67
+ is_expected.to_not eq("\x0" * 16)
68
+ end
69
+
70
+ # The chances of two randomly generated UUIDs being identical is virtually impossible.
71
+ it 'is random' do
72
+ is_expected.to_not eq other
73
+ end
74
+ end
75
+
76
+ describe 'the string' do
77
+ subject { uuid.to_s }
78
+
79
+ it 'is valid' do
80
+ is_expected.to be_a_valid_uuid_string
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '.parse' do
86
+ let(:uuid_str) { build(:uuid_str) }
87
+ subject { Lapis::Uuid.parse(uuid_str) }
88
+
89
+ context 'with a valid UUID' do
90
+ it 'parses correctly' do
91
+ is_expected.to eq uuid_str
92
+ end
93
+
94
+ context 'without dashes' do
95
+ let(:uuid_str) { build(:uuid_str, :no_dashes) }
96
+
97
+ it 'parses correctly' do
98
+ is_expected.to eq uuid_str
99
+ end
100
+ end
101
+ end
102
+
103
+ context 'with a malformed string' do
104
+ let(:uuid_str) { 'foobar' }
105
+
106
+ it 'returns nil' do
107
+ is_expected.to be_nil
108
+ end
109
+ end
110
+
111
+ context 'with an invalid argument' do
112
+ let(:uuid_str) { 5 }
113
+
114
+ it 'raises an ArgumentError' do
115
+ expect { subject }.to raise_error(ArgumentError)
116
+ end
117
+ end
118
+ end
119
+
120
+ describe '#value' do
121
+ let(:uuid) { build(:uuid) }
122
+ subject(:value) { uuid.value }
123
+
124
+ it 'is a string' do
125
+ is_expected.to be_a String
126
+ end
127
+
128
+ it 'is frozen' do
129
+ is_expected.to be_frozen
130
+ end
131
+
132
+ describe 'its length' do
133
+ subject { value.length }
134
+
135
+ it 'is 16 bytes' do
136
+ is_expected.to be 16
137
+ end
138
+ end
139
+ end
140
+
141
+ describe '#eql?' do
142
+ let(:uuid) { build(:uuid) }
143
+ subject { uuid.eql?(other) }
144
+
145
+ context 'with two identical UUIDs' do
146
+ let(:other) { build(:uuid, :source => uuid) }
147
+
148
+ it 'returns true' do
149
+ is_expected.to eq true
150
+ end
151
+ end
152
+
153
+ context 'with two different UUIDs' do
154
+ let(:other) { build(:uuid) }
155
+
156
+ it 'returns false' do
157
+ is_expected.to eq false
158
+ end
159
+ end
160
+
161
+ context 'without a UUID' do
162
+ let(:other) { 'foobar' }
163
+
164
+ it 'returns false' do
165
+ is_expected.to eq false
166
+ end
167
+ end
168
+ end
169
+
170
+ describe '#==' do
171
+ let(:uuid) { build(:uuid) }
172
+ subject { uuid == other }
173
+
174
+ context 'with equal UUIDs' do
175
+ let(:other) { build(:uuid, :source => uuid) }
176
+
177
+ it 'is true' do
178
+ is_expected.to be true
179
+ end
180
+ end
181
+
182
+ context 'with the same instance' do
183
+ let(:other) { uuid }
184
+
185
+ it 'is true' do
186
+ is_expected.to be true
187
+ end
188
+ end
189
+
190
+ context 'with unequal UUIDs' do
191
+ let(:other) { build(:uuid) }
192
+
193
+ it 'is false' do
194
+ is_expected.to be false
195
+ end
196
+ end
197
+
198
+ context 'with nil' do
199
+ let(:other) { nil }
200
+
201
+ it 'is false' do
202
+ is_expected.to be false
203
+ end
204
+ end
205
+
206
+ context 'with a malformed string' do
207
+ let(:other) { 'foobar' }
208
+
209
+ it 'is false' do
210
+ is_expected.to be false
211
+ end
212
+ end
213
+
214
+ context 'with a non-string' do
215
+ let(:other) { 5 }
216
+
217
+ it 'is false' do
218
+ is_expected.to be false
219
+ end
220
+ end
221
+
222
+ context 'with identical value' do
223
+ let(:other) { uuid.value }
224
+
225
+ it 'is true' do
226
+ is_expected.to be true
227
+ end
228
+ end
229
+
230
+ context 'with identical string' do
231
+ let(:other) { uuid.to_s }
232
+
233
+ it 'is true' do
234
+ is_expected.to be true
235
+ end
236
+ end
237
+ end
238
+
239
+ describe '#<=>' do
240
+ let(:lesser) { 'de305d54-75b4-431b-adb2-eb6b9e546014' }
241
+ let(:greater) { 'de305d54-75b4-431b-adb2-eb6b9e546020' }
242
+ subject { first <=> second }
243
+
244
+ context 'with an equal UUID' do
245
+ let(:first) { build(:uuid) }
246
+ let(:second) { build(:uuid, :source => first) }
247
+
248
+ it 'is 0' do
249
+ is_expected.to eq 0
250
+ end
251
+ end
252
+
253
+ context 'with the same instance' do
254
+ let(:first) { build(:uuid) }
255
+ let(:second) { first }
256
+
257
+ it 'is 0' do
258
+ is_expected.to eq 0
259
+ end
260
+ end
261
+
262
+ context 'with a lesser UUID' do
263
+ let(:first) { build(:uuid, :source => greater) }
264
+ let(:second) { build(:uuid, :source => lesser) }
265
+
266
+ it 'is 1' do
267
+ is_expected.to eq 1
268
+ end
269
+ end
270
+
271
+ context 'with a greater UUID' do
272
+ let(:first) { build(:uuid, :source => lesser) }
273
+ let(:second) { build(:uuid, :source => greater) }
274
+
275
+ it 'is -1' do
276
+ is_expected.to eq(-1)
277
+ end
278
+ end
279
+
280
+ context 'with nil' do
281
+ let(:first) { build(:uuid) }
282
+ let(:second) { nil }
283
+
284
+ it 'is nil' do
285
+ is_expected.to be_nil
286
+ end
287
+ end
288
+
289
+ context 'with a number' do
290
+ let(:first) { build(:uuid) }
291
+ let(:second) { 500 }
292
+
293
+ it 'is nil' do
294
+ is_expected.to be_nil
295
+ end
296
+ end
297
+
298
+ context 'with an identical value' do
299
+ let(:first) { build(:uuid) }
300
+ let(:second) { first.value }
301
+
302
+ it 'is 0' do
303
+ is_expected.to eq 0
304
+ end
305
+ end
306
+
307
+ context 'with a lesser value' do
308
+ let(:first) { build(:uuid, :source => greater) }
309
+ let(:second) { build(:uuid_value, :uuid_str => lesser) }
310
+
311
+ it 'is 1' do
312
+ is_expected.to eq 1
313
+ end
314
+ end
315
+
316
+ context 'with a greater value' do
317
+ let(:first) { build(:uuid, :source => lesser) }
318
+ let(:second) { build(:uuid_value, :uuid_str => greater) }
319
+
320
+ it 'is -1' do
321
+ is_expected.to eq(-1)
322
+ end
323
+ end
324
+
325
+ context 'with an equal string' do
326
+ let(:first) { build(:uuid) }
327
+ let(:second) { first.to_s }
328
+
329
+ it 'is 0' do
330
+ is_expected.to eq 0
331
+ end
332
+ end
333
+
334
+ context 'with a lesser string' do
335
+ let(:first) { build(:uuid, :source => greater) }
336
+ let(:second) { lesser }
337
+
338
+ it 'is 1' do
339
+ is_expected.to eq 1
340
+ end
341
+ end
342
+
343
+ context 'with a greater string' do
344
+ let(:first) { build(:uuid, :source => lesser) }
345
+ let(:second) { greater }
346
+
347
+ it 'is -1' do
348
+ is_expected.to eq(-1)
349
+ end
350
+ end
351
+
352
+ context 'with an invalid string' do
353
+ let(:first) { build(:uuid) }
354
+ let(:second) { 'foobar' }
355
+
356
+ it 'is nil' do
357
+ is_expected.to be_nil
358
+ end
359
+ end
360
+ end
361
+
362
+ describe '#hash' do
363
+ let(:uuid) { build(:uuid) }
364
+ subject { uuid.hash }
365
+
366
+ it 'is a Fixnum' do
367
+ is_expected.to be_a Fixnum
368
+ end
369
+
370
+ context 'with identical UUIDs' do
371
+ let(:first) { build(:uuid) }
372
+ let(:second) { build(:uuid, :source => first) }
373
+
374
+ it 'returns identical values' do
375
+ expect(first.hash).to eq(second.hash)
376
+ end
377
+ end
378
+
379
+ context 'with different UUIDs' do
380
+ let(:first) { build(:uuid) }
381
+ let(:second) { build(:uuid) }
382
+
383
+ it 'returns different values' do
384
+ expect(first.hash).to_not eq(second.hash)
385
+ end
386
+ end
387
+ end
388
+
389
+ describe '#to_s' do
390
+ let(:uuid_str) { build(:uuid_str) }
391
+ let(:uuid) { build(:uuid, :source => uuid_str) }
392
+ subject { uuid.to_s }
393
+
394
+ it 'is a String' do
395
+ is_expected.to be_a String
396
+ end
397
+
398
+ it 'is a valid UUID string' do
399
+ is_expected.to be_a_valid_uuid_string
400
+ end
401
+
402
+ it 'equals the initial value' do
403
+ is_expected.to eq uuid_str
404
+ end
405
+
406
+ it 'contains dashes' do
407
+ is_expected.to include '-'
408
+ end
409
+
410
+ context 'with bytes less than 16' do
411
+ let(:uuid_str) { '05305d54-7502-431b-adb2-eb6b9e546000' }
412
+ let(:uuid) { build(:uuid, :source => uuid_str) }
413
+
414
+ it 'pads with zeroes' do
415
+ is_expected.to eq uuid_str
416
+ end
417
+ end
418
+
419
+ context 'with dashes set to false' do
420
+ let(:uuid_str) { build(:uuid_str, :no_dashes) }
421
+ subject { uuid.to_s(false) }
422
+
423
+ it 'is a String' do
424
+ is_expected.to be_a String
425
+ end
426
+
427
+ it 'is a valid UUID string' do
428
+ is_expected.to be_a_valid_uuid_string
429
+ end
430
+
431
+ it 'equals the initial value' do
432
+ is_expected.to eq uuid_str
433
+ end
434
+
435
+ it 'does not contain dashes' do
436
+ is_expected.not_to include '-'
437
+ end
438
+
439
+ context 'with bytes less than 16' do
440
+ let(:uuid_str) { '05305d547502431badb2eb6b9e546000' }
441
+ let(:uuid) { build(:uuid, :source => uuid_str) }
442
+
443
+ it 'pads with zeroes' do
444
+ is_expected.to eq uuid_str
445
+ end
446
+ end
447
+ end
448
+ end
449
+
450
+ end
@@ -0,0 +1,85 @@
1
+ if RUBY_PLATFORM != 'java'
2
+ # Code Climate test coverage.
3
+ begin
4
+ require 'codeclimate-test-reporter'
5
+ CodeClimate::TestReporter.start
6
+ rescue
7
+ # Continue without Code Climate test coverage.
8
+ end
9
+ end
10
+
11
+ require 'rspec'
12
+ require 'factory_girl'
13
+
14
+ RSpec.configure do |config|
15
+ # rspec-expectations config goes here. You can use an alternate
16
+ # assertion/expectation library such as wrong or the stdlib/minitest
17
+ # assertions if you prefer.
18
+ config.expect_with :rspec do |expectations|
19
+ # This option will default to `true` in RSpec 4. It makes the `description`
20
+ # and `failure_message` of custom matchers include text for helper methods
21
+ # defined using `chain`, e.g.:
22
+ # be_bigger_than(2).and_smaller_than(4).description
23
+ # # => "be bigger than 2 and smaller than 4"
24
+ # ...rather than:
25
+ # # => "be bigger than 2"
26
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
27
+ end
28
+
29
+ # rspec-mocks config goes here. You can use an alternate test double
30
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
31
+ config.mock_with :rspec do |mocks|
32
+ # Prevents you from mocking or stubbing a method that does not exist on
33
+ # a real object. This is generally recommended, and will default to
34
+ # `true` in RSpec 4.
35
+ mocks.verify_partial_doubles = true
36
+ end
37
+
38
+ # Allows RSpec to persist some state between runs in order to support
39
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
40
+ # you configure your source control system to ignore this file.
41
+ config.example_status_persistence_file_path = 'spec/examples.txt'
42
+
43
+ # Limits the available syntax to the non-monkey patched syntax that is
44
+ # recommended. For more details, see:
45
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
46
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
47
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
48
+ config.disable_monkey_patching!
49
+
50
+ # This setting enables warnings. It's recommended, but in some cases may
51
+ # be too noisy due to issues in dependencies.
52
+ config.warnings = true
53
+
54
+ # Many RSpec users commonly either run the entire suite or an individual
55
+ # file, and it's useful to allow more verbose output when running an
56
+ # individual spec file.
57
+ if config.files_to_run.one?
58
+ # Use the documentation formatter for detailed output,
59
+ # unless a formatter has already been configured
60
+ # (e.g. via a command-line flag).
61
+ config.default_formatter = 'doc'
62
+ end
63
+
64
+ # Run specs in random order to surface order dependencies. If you find an
65
+ # order dependency and want to debug it, you can fix the order by providing
66
+ # the seed, which is printed after each run.
67
+ # --seed 1234
68
+ config.order = :random
69
+
70
+ # Seed global randomization in this process using the `--seed` CLI option.
71
+ # Setting this allows you to use `--seed` to deterministically reproduce
72
+ # test failures related to randomization by passing the same `--seed` value
73
+ # as the one that triggered the failure.
74
+ Kernel.srand config.seed
75
+
76
+ # Include FactoryGirl for building test objects.
77
+ config.include FactoryGirl::Syntax::Methods
78
+ end
79
+
80
+ # Include the library.
81
+ require_relative '../lib/lapis'
82
+
83
+ # Include factories.
84
+ FactoryGirl.definition_file_paths << File.join(File.dirname(__FILE__), 'factories')
85
+ FactoryGirl.find_definitions
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lapis-common
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Miller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_girl
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: reek
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.38.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.38.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: codeclimate-test-reporter
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: yard
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Core classes and methods used by various Lapis modules.
126
+ email:
127
+ - bluepixelmike@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".codeclimate.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - ".travis.yml"
137
+ - Gemfile
138
+ - LICENSE.md
139
+ - README.md
140
+ - Rakefile
141
+ - bin/console
142
+ - bin/setup
143
+ - lapis-common.gemspec
144
+ - lib/lapis.rb
145
+ - lib/lapis/uuid.rb
146
+ - lib/lapis/version.rb
147
+ - spec/factories/uuid_factory.rb
148
+ - spec/lapis/uuid_spec.rb
149
+ - spec/spec_helper.rb
150
+ homepage: https://github.com/lapis-mc/common
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.5.1
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Shared library and utilities for Lapis modules
174
+ test_files:
175
+ - spec/factories/uuid_factory.rb
176
+ - spec/lapis/uuid_spec.rb
177
+ - spec/spec_helper.rb
178
+ has_rdoc: