Skip to content

Commit

Permalink
raise if the byte array is the wrong size
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgolick committed Aug 16, 2010
1 parent 420ca65 commit a5e0275
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/lexical_uuid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 22 additions & 12 deletions spec/lexical_uuid_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a5e0275

Please sign in to comment.