File: uuid_creation_spec.rb

package info (click to toggle)
ruby-uuidtools 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: ruby: 1,284; makefile: 16
file content (131 lines) | stat: -rw-r--r-- 4,467 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require File.expand_path("../../spec_helper.rb", __FILE__)

describe UUIDTools::UUID, "when generating" do
  it "should correctly generate SHA1 variant UUIDs" do
    expect(UUIDTools::UUID.sha1_create(
      UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
    ).to_s).to eql('f2d04685-b787-55da-8644-9bd28a6f5a53')
  end

  it "should correctly generate MD5 variant UUIDs" do
    expect(UUIDTools::UUID.md5_create(
      UUIDTools::UUID_URL_NAMESPACE, 'http://sporkmonger.com'
    ).to_s).to eql('15074785-9071-3fe3-89bd-876e4b9e919b')
  end

  it "should correctly generate timestamp variant UUIDs" do
    allow(UUIDTools::UUID).to receive(:mac_address) { "aa:bb:cc:dd:ee:ff" }
    expect(UUIDTools::UUID.timestamp_create).not_to be_random_node_id
    expect(UUIDTools::UUID.timestamp_create.to_s).not_to eql(
      UUIDTools::UUID.timestamp_create.to_s
    )
    current_time = Time.now
    expect(UUIDTools::UUID.timestamp_create(current_time).to_s).not_to eql(
      UUIDTools::UUID.timestamp_create(current_time).to_s
    )
    uuids = []
    1000.times do
      uuids << UUIDTools::UUID.timestamp_create
    end
    # Check to make sure that none of the 1,000 UUIDs were duplicates
    expect((uuids.map {|x| x.to_s}).uniq.size).to eql(uuids.size)
  end

  it "should correctly generate UUIDs without a MAC address" do
    mac_address = UUIDTools::UUID.mac_address
    UUIDTools::UUID.mac_address = nil
    expect(UUIDTools::UUID.timestamp_create).to be_random_node_id
    UUIDTools::UUID.mac_address = mac_address
  end

  it "should correctly generate random number variant UUIDs" do
    uuids = []
    1000.times do
      uuids << UUIDTools::UUID.random_create
    end
    # Check to make sure that none of the 1,000 UUIDs were duplicates
    expect((uuids.map {|x| x.to_s}).uniq.size).to eql(uuids.size)
  end

  it "should not have internal state used in string representations" do
    uuid = UUIDTools::UUID.random_create
    uuid_string = uuid.to_s.dup
    uuid.to_s.gsub!("-", "/")
    expect(uuid.to_s).to eql(uuid_string)
  end

  it "should throw an exception if a segment has an invalid value" do
    expect do
      UUIDTools::UUID.new(-1, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
    expect do
      UUIDTools::UUID.new(4294967296, 0, 0, 0, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
  end

  it "should throw an exception if a segment has an invalid value" do
    expect do
      UUIDTools::UUID.new(0, -1, 0, 0, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
    expect do
      UUIDTools::UUID.new(0, 65536, 0, 0, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
  end

  it "should throw an exception if a segment has an invalid value" do
    expect do
      UUIDTools::UUID.new(0, 0, -1, 0, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
    expect do
      UUIDTools::UUID.new(0, 0, 65536, 0, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
  end

  it "should throw an exception if a segment has an invalid value" do
    expect do
      UUIDTools::UUID.new(0, 0, 0, -1, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
    expect do
      UUIDTools::UUID.new(0, 0, 0, 256, 0, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
  end

  it "should throw an exception if a segment has an invalid value" do
    expect do
      UUIDTools::UUID.new(0, 0, 0, 0, -1, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
    expect do
      UUIDTools::UUID.new(0, 0, 0, 0, 256, [0, 0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
  end

  it "should throw an exception if nodes are not a collection" do
    expect do
      UUIDTools::UUID.new(0, 0, 0, 0, 0, :bogus)
    end.to raise_error(TypeError)
  end

  it "should throw an exception if nodes are the wrong size" do
    expect do
      UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0])
    end.to raise_error(ArgumentError)
  end

  it "should throw an exception if any nodes have invalid values" do
    expect do
      UUIDTools::UUID.new(0, 0, 0, 0, 0, [0, 0, 0, 0, 0, 256])
    end.to raise_error(ArgumentError)
  end

  it "should throw an exception if parsing anything but a String" do
    expect do
      UUIDTools::UUID.parse(:bogus)
    end.to raise_error(TypeError)
  end

  it "should throw an exception if raw parsing anything but a String" do
    expect do
      UUIDTools::UUID.parse_raw(:bogus)
    end.to raise_error(TypeError)
  end
end