File: equality_map_spec.rb

package info (click to toggle)
ruby-mustermann 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,224 kB
  • sloc: ruby: 7,746; makefile: 6
file content (42 lines) | stat: -rw-r--r-- 1,240 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true
require 'support'
require 'mustermann/equality_map'

RSpec.describe Mustermann::EqualityMap do
  before { GC.disable }
  after { GC.enable }

  describe :fetch do
    subject { Mustermann::EqualityMap.new }
    specify 'with existing entry' do
      next if subject.is_a? Hash
      subject.fetch("foo") { "foo" }
      result = subject.fetch("foo") { "bar" }
      expect(result).to be == "foo"
    end

    specify 'with GC-removed entry' do
      next if subject.is_a? Hash
      subject.fetch(String.new('foo')) { "foo" }
      expect(subject.map).to receive(:[]).and_return(nil)
      result = subject.fetch(String.new('foo')) { "bar" }
      expect(result).to be == "bar"
    end

    specify 'allows a frozen key and value' do
      next if subject.is_a? Hash
      key = "foo".freeze
      value = "bar".freeze
      subject.fetch(key) { value }
      result = subject.fetch("foo".dup) { raise "not executed" }
      expect(result).to be == value
      expect(result).not_to equal value
    end

    specify 'allows only a single argument to be compatible with Hash#fetch' do
      expect {
        subject.fetch("foo", "bar", "baz") { "value" }
      }.to raise_error(ArgumentError)
    end
  end
end