File: new_spec.rb

package info (click to toggle)
ruby-hamster 3.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,932 kB
  • sloc: ruby: 16,915; makefile: 4
file content (72 lines) | stat: -rw-r--r-- 2,025 bytes parent folder | download | duplicates (2)
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
require "spec_helper"
require "hamster/sorted_set"

describe Hamster::SortedSet do
  describe ".new" do
    it "accepts a single enumerable argument and creates a new sorted set" do
      sorted_set = SS.new([1,2,3])
      sorted_set.size.should be(3)
      sorted_set[0].should be(1)
      sorted_set[1].should be(2)
      sorted_set[2].should be(3)
    end

    it "also works with a Range" do
      sorted_set = SS.new(1..3)
      sorted_set.size.should be(3)
      sorted_set[0].should be(1)
      sorted_set[1].should be(2)
      sorted_set[2].should be(3)
    end

    it "is amenable to overriding of #initialize" do
      class SnazzySortedSet < Hamster::SortedSet
        def initialize
          super(['SNAZZY!!!'])
        end
      end

      sorted_set = SnazzySortedSet.new
      sorted_set.size.should be(1)
      sorted_set.to_a.should == ['SNAZZY!!!']
    end

    it "accepts a block with arity 1" do
      sorted_set = SS.new(1..3) { |a| -a }
      sorted_set[0].should be(3)
      sorted_set[1].should be(2)
      sorted_set[2].should be(1)
    end

    it "accepts a block with arity 2" do
      sorted_set = SS.new(1..3) { |a,b| b <=> a }
      sorted_set[0].should be(3)
      sorted_set[1].should be(2)
      sorted_set[2].should be(1)
    end

    it "can use a block produced by Symbol#to_proc" do
      sorted_set = SS.new([Object, BasicObject], &:name.to_proc)
      sorted_set[0].should be(BasicObject)
      sorted_set[1].should be(Object)
    end

    context "from a subclass" do
      it "returns a frozen instance of the subclass" do
        subclass = Class.new(Hamster::SortedSet)
        instance = subclass.new(["some", "values"])
        instance.class.should be subclass
        instance.frozen?.should be true
      end
    end
  end

  describe ".[]" do
    it "accepts a variable number of items and creates a new sorted set" do
      sorted_set = SS['a', 'b']
      sorted_set.size.should be(2)
      sorted_set[0].should == 'a'
      sorted_set[1].should == 'b'
    end
  end
end