File: select_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 (62 lines) | stat: -rw-r--r-- 1,980 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
require "spec_helper"
require "hamster/sorted_set"

describe Hamster::SortedSet do
  [:select, :find_all].each do |method|
    describe "##{method}" do
      let(:sorted_set) { SS["A", "B", "C"] }

      context "when everything matches" do
        it "preserves the original" do
          sorted_set.send(method) { true }
          sorted_set.should eql(SS["A", "B", "C"])
        end

        it "returns self" do
          sorted_set.send(method) { |item| true }.should equal(sorted_set)
        end
      end

      context "when only some things match" do
        context "with a block" do
          it "preserves the original" do
            sorted_set.send(method) { |item| item == "A" }
            sorted_set.should eql(SS["A", "B", "C"])
          end

          it "returns a set with the matching values" do
            sorted_set.send(method) { |item| item == "A" }.should eql(SS["A"])
          end
        end

        context "with no block" do
          it "returns an Enumerator" do
            sorted_set.send(method).class.should be(Enumerator)
            sorted_set.send(method).each { |item| item == "A" }.should eql(SS["A"])
          end
        end
      end

      context "when nothing matches" do
        it "preserves the original" do
          sorted_set.send(method) { |item| false }
          sorted_set.should eql(SS["A", "B", "C"])
        end

        it "returns the canonical empty set" do
          sorted_set.send(method) { |item| false }.should equal(Hamster::EmptySortedSet)
        end
      end

      context "from a subclass" do
        it "returns an instance of the same class" do
          subclass = Class.new(Hamster::SortedSet)
          instance = subclass.new(['A', 'B', 'C'])
          instance.send(method) { true }.class.should be(subclass)
          instance.send(method) { false }.class.should be(subclass)
          instance.send(method) { rand(2) == 0 }.class.should be(subclass)
        end
      end
    end
  end
end