File: assoc_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 (46 lines) | stat: -rw-r--r-- 1,290 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
require "spec_helper"
require "hamster/vector"

describe Hamster::Vector do
  let(:vector) { V[[:a, 3], [:b, 2], [:c, 1]] }

  describe "#assoc" do
    it "searches for a 2-element array with a given 1st item" do
      vector.assoc(:b).should == [:b, 2]
    end

    it "returns nil if a matching 1st item is not found" do
      vector.assoc(:d).should be_nil
    end

    it "uses #== to compare 1st items with provided object" do
      vector.assoc(EqualNotEql.new).should_not be_nil
      vector.assoc(EqlNotEqual.new).should be_nil
    end

    it "skips elements which are not indexable" do
      V[false, true, nil].assoc(:b).should be_nil
      V[[1,2], nil].assoc(3).should be_nil
    end
  end

  describe "#rassoc" do
    it "searches for a 2-element array with a given 2nd item" do
      vector.rassoc(1).should == [:c, 1]
    end

    it "returns nil if a matching 2nd item is not found" do
      vector.rassoc(4).should be_nil
    end

    it "uses #== to compare 2nd items with provided object" do
      vector.rassoc(EqualNotEql.new).should_not be_nil
      vector.rassoc(EqlNotEqual.new).should be_nil
    end

    it "skips elements which are not indexable" do
      V[false, true, nil].rassoc(:b).should be_nil
      V[[1,2], nil].rassoc(3).should be_nil
    end
  end
end