Sha256: f67c4c12d07bfbddb1781656fca9e21793c17b4cb83d9f727492d85c40fb6738

Contents?: true

Size: 1.93 KB

Versions: 2

Compression:

Stored size: 1.93 KB

Contents

require "spec_helper"

RSpec.describe Cachew::Hash do
  describe ".new" do
    it "accepts any #to_hash object" do
      storage = double(:to_hash => {})

      described_class.new(storage).fetch(:foo) { :bar }

      expect(storage.to_hash).not_to be_empty
    end

    it "tolerates argument-less call" do
      expect { described_class.new }.not_to raise_error
    end
  end

  describe "#[]" do
    let(:cachew) { described_class.new }

    context "when value is not cahed yet" do
      it "evaluates given block" do
        expect { |b| cachew.fetch :foo, &b }.to yield_control
      end

      it "returns evaluated value" do
        expect(cachew.fetch(:foo) { :bar }).to be :bar
      end
    end

    context "when value was already cached" do
      before { cachew.fetch(:foo) { :cached } }

      it "does not evaluates block" do
        expect { |b| cachew.fetch(:foo, &b) }.not_to yield_control
      end

      it "returns previously cached value" do
        expect(cachew.fetch(:foo) { :bar }).to be :cached
      end
    end

    context "when value with expiration still fresh" do
      before { cachew.fetch(:foo, :ttl => 84) { :cached } }

      it "does not evaluates given block" do
        Timecop.travel 42 do
          expect { |b| cachew.fetch :foo, :ttl => 84, &b }.not_to yield_control
        end
      end

      it "returns previously cached value" do
        Timecop.travel 42 do
          expect(cachew.fetch(:foo, :ttl => 84) { :bar }).to be :cached
        end
      end
    end

    context "when value with expiration became stale" do
      before { cachew.fetch(:foo, :ttl => 21) { :cached } }

      it "evaluates given block" do
        Timecop.travel 42 do
          expect { |b| cachew.fetch :foo, :ttl => 21, &b }.to yield_control
        end
      end

      it "returns evaluated value" do
        Timecop.travel 42 do
          expect(cachew.fetch(:foo, :ttl => 21) { :bar }).to be :bar
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cachew-0.2.1 spec/lib/cachew/hash_spec.rb
cachew-0.2.0 spec/lib/cachew/hash_spec.rb