\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/rspec-its-1.2.0/spec/rspec/

Viewing File: its_spec.rb

require 'spec_helper'

module RSpec
  describe Its do
    describe "#its" do
      context "with implicit subject" do
        context "preserves described_class" do
          its(:symbol) { expect(described_class).to be Its }
          its([]) { expect(described_class).to be Its }
        end
      end
      context "with explicit subject" do
        subject do
          Class.new do
            def initialize
              @call_count = 0
            end

            def call_count
              @call_count += 1
            end
          end.new
        end

        before(:each, :meta) do
          subject.call_count
        end

        context "with some metadata" do
          its(:call_count, :meta) { should eq(2) }
        end

        context "with a call counter" do
          its(:call_count) { should eq(1) }
        end

        context "with nil value" do
          subject do
            Class.new do
              def nil_value
                nil
              end
            end.new
          end
          its(:nil_value) { should be_nil }
        end

        context "with nested attributes" do
          subject do
            Class.new do
              def name
                "John"
              end
            end.new
          end
          its("name") { should eq("John") }
          its("name.size") { should eq(4) }
          its("name.size.class") { should eq(Fixnum) }

          context "using should_not" do
            its("name") { should_not eq("Paul") }
          end

          context "using is_expected" do
            its("name") { is_expected.to eq("John") }
          end

          context "using are_expected" do
            its("name.chars.to_a") { are_expected.to eq(%w[J o h n]) }
          end
        end

        context "when it responds to #[]" do
          subject do
            Class.new do
              def [](*objects)
                objects.map do |object|
                  "#{object.class}: #{object.to_s}"
                end.join("; ")
              end

              def name
                "George"
              end
            end.new
          end
          its([:a]) { should eq("Symbol: a") }
          its(['a']) { should eq("String: a") }
          its([:b, 'c', 4]) { should eq("Symbol: b; String: c; Fixnum: 4") }
          its(:name) { should eq("George") }
          context "when referring to an attribute that doesn't exist" do
            context "it raises an error" do
              its(:age) do
                expect do
                  should eq(64)
                end.to raise_error(NoMethodError)
              end
            end
          end

          context "when it's a hash" do
            subject { {:a => {:deep => {:key => "value"}}} }

            its([:a]) { should eq({:deep => {:key => "value"}}) }
            its([:a, :deep]) { should eq({:key => "value"}) }
            its([:a, :deep, :key]) { should eq("value") }

            context "when referring to a key that doesn't exist" do
              its([:not_here]) { should be_nil }
              its([:a, :ghost]) { should be_nil }
              its([:deep, :ghost]) { expect { should eq("missing") }.to raise_error(NoMethodError) }
            end
          end
        end

        context "when it does not respond to #[]" do
          subject { Object.new }

          context "it raises an error" do
            its([:a]) do
              expect do
                should eq("Symbol: a")
              end.to raise_error(NoMethodError)
            end
          end
        end

        context "calling and overriding super" do
          it "calls to the subject defined in the parent group" do
            group = RSpec::Core::ExampleGroup.describe(Array) do
              subject { [1, 'a'] }

              its(:last) { should eq("a") }

              describe '.first' do
                def subject;
                  super().first;
                end

                its(:next) { should eq(2) }
              end
            end

            expect(group.run(NullFormatter.new)).to be_truthy
          end
        end

        context "with nil subject" do
          subject do
            Class.new do
              def initialize
                @counter = -1
              end

              def nil_if_first_time
                @counter += 1
                @counter == 0 ? nil : true
              end
            end.new
          end
          its(:nil_if_first_time) { should be(nil) }
        end

        context "with false subject" do
          subject do
            Class.new do
              def initialize
                @counter = -1
              end

              def false_if_first_time
                @counter += 1
                @counter > 0
              end
            end.new
          end
          its(:false_if_first_time) { should be(false) }
        end

        describe 'accessing `subject` in `before` and `let`' do
          subject { 'my subject' }
          before { @subject_in_before = subject }
          let(:subject_in_let) { subject }
          let!(:eager_loaded_subject_in_let) { subject }

          # These examples read weird, because we're actually
          # specifying the behaviour of `its` itself
          its(nil) { expect(subject).to eq('my subject') }
          its(nil) { expect(@subject_in_before).to eq('my subject') }
          its(nil) { expect(subject_in_let).to eq('my subject') }
          its(nil) { expect(eager_loaded_subject_in_let).to eq('my subject') }
        end

        describe "in shared_context" do
          shared_context "shared stuff" do
            subject { Array }
            its(:name) { should eq "Array" }
          end

          include_context "shared stuff"
        end

        describe "when extending SharedContext" do
          it 'works with an implicit subject' do
            shared = Module.new do
              extend RSpec::SharedContext
              its(:size) { should eq 0 }
            end
            group = RSpec::Core::ExampleGroup.describe(Array) do
              include shared
            end

            group.run(NullFormatter.new)

            result = group.children.first.examples.first.execution_result
            # Following conditional needed to work across mix of RSpec and ruby versions without warning
            status = result.respond_to?(:status) ? result.status : result[:status].to_sym
            expect(status).to eq(:passed)
          end
        end
      end
      context "with metadata" do
        context "preserves access to metadata that doesn't end in hash" do
          its([], :foo) do |example|
            expect(example.metadata[:foo]).to be(true)
          end
        end
        context "preserves access to metadata that ends in hash" do
          its([], :foo, :bar => 17) do |example|
            expect(example.metadata[:foo]).to be(true)
            expect(example.metadata[:bar]).to be(17)
          end
        end
      end
    end
  end
end