Sha256: 30e001a504680a032b5693f1229d7ad72ffe647f89d8dea1a530597bd454b46c

Contents?: true

Size: 1.61 KB

Versions: 6

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

RSpec.describe RuboCop::RSpec::Example do
  include RuboCop::AST::Sexp

  def example(source)
    described_class.new(parse_source(source).ast)
  end

  it 'extracts doc string' do
    expect(example("it('does x') { foo }").doc_string)
      .to eql(s(:str, 'does x'))
  end

  it 'extracts doc string for unimplemented examples' do
    expect(example("it('does x')").doc_string)
      .to eql(s(:str, 'does x'))
  end

  it 'returns nil for examples without doc strings' do
    expect(example('it { foo }').doc_string).to be(nil)
  end

  it 'extracts keywords' do
    expect(example("it('foo', :bar, baz: :qux) { a }").metadata)
      .to eq([s(:sym, :bar), s(:hash, s(:pair, s(:sym, :baz), s(:sym, :qux)))])
  end

  it 'extracts implementation' do
    expect(example('it("foo") { bar; baz }').implementation)
      .to eq(s(:begin, s(:send, nil, :bar), s(:send, nil, :baz)))
  end

  it 'returns node' do
    node = s(:sym, :node)
    expect(described_class.new(node).to_node).to be(node)
  end

  context 'value object semantics' do
    it 'compares by value' do
      aggregate_failures 'equality semantics' do
        expect(example('it("foo")')).to eq(example('it("foo")'))
        expect(example('it("foo")')).not_to eq(example('it("bar")'))
      end
    end

    it 'can be used as a key in a hash' do
      hash = {}

      hash[example('it("foo")')] = 123

      expect(hash[example('it("foo")')]).to be(123)
    end

    it 'computes #hash based on class and node' do
      node = s(:node)

      expect(described_class.new(node).hash)
        .to eql([described_class, node].hash)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-rspec-1.19.0 spec/rubocop/rspec/example_spec.rb
rubocop-rspec-1.18.0 spec/rubocop/rspec/example_spec.rb
rubocop-rspec-1.17.1 spec/rubocop/rspec/example_spec.rb
rubocop-rspec-1.17.0 spec/rubocop/rspec/example_spec.rb
rubocop-rspec-1.16.0 spec/rubocop/rspec/example_spec.rb
rubocop-rspec-1.15.1 spec/rubocop/rspec/example_spec.rb