Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

raise if the byte array is the wrong size

  • Loading branch information...
commit a5e0275e27d1d592a076c546bc326bee719b2a8a 1 parent 420ca65
@jamesgolick authored
Showing with 25 additions and 12 deletions.
  1. +3 −0  lib/lexical_uuid.rb
  2. +22 −12 spec/lexical_uuid_spec.rb
View
3  lib/lexical_uuid.rb
@@ -48,6 +48,9 @@ def create_worker_id
def initialize(bytes = nil)
case bytes
when String
+ if bytes.size != 16
+ raise ArgumentError, "#{bytes} was incorrectly sized. Must be 16 bytes."
+ end
time_high, time_low, worker_high, worker_low = bytes.unpack("NNNN")
@timestamp = (time_high << 32) | time_low
@worker_id = (worker_high << 32) | worker_low
View
34 spec/lexical_uuid_spec.rb
@@ -24,20 +24,30 @@
end
describe "reinitializing the uuid from bytes" do
- before do
- @bytes = [1234567890 >> 32,
- 1234567890 & 0xffffffff,
- 9876543210 >> 32,
- 9876543210 & 0xffffffff].pack("NNNN")
- @uuid = LexicalUUID.new(@bytes)
- end
+ describe "with a correctly sized byte array" do
+ before do
+ @bytes = [1234567890 >> 32,
+ 1234567890 & 0xffffffff,
+ 9876543210 >> 32,
+ 9876543210 & 0xffffffff].pack("NNNN")
+ @uuid = LexicalUUID.new(@bytes)
+ end
- it "correctly extracts the timestamp" do
- @uuid.timestamp.should == 1234567890
- end
+ it "correctly extracts the timestamp" do
+ @uuid.timestamp.should == 1234567890
+ end
- it "correctly extracts the worker id" do
- @uuid.worker_id.should == 9876543210
+ it "correctly extracts the worker id" do
+ @uuid.worker_id.should == 9876543210
+ end
+ end
+
+ describe "with a mis-sized byte array" do
+ it "raises ArgumentError" do
+ lambda {
+ LexicalUUID.new("asdf")
+ }.should raise_error(ArgumentError)
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.