|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::RSpec::IdenticalEqualityAssertion do |
| 4 | + let(:msg) { 'Identical expressions on both sides of the equality may indicate a flawed test.' } |
| 5 | + |
| 6 | + it 'registers an offense when using identical expressions with `eq`' do |
| 7 | + expect_offense(<<~RUBY) |
| 8 | + expect(foo.bar).to eq(foo.bar) |
| 9 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} |
| 10 | + RUBY |
| 11 | + end |
| 12 | + |
| 13 | + it 'registers an offense when using identical expressions with `eql`' do |
| 14 | + expect_offense(<<~RUBY) |
| 15 | + expect(foo.bar.baz).to eql(foo.bar.baz) |
| 16 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} |
| 17 | + RUBY |
| 18 | + end |
| 19 | + |
| 20 | + it 'registers an offense for trivial constants' do |
| 21 | + expect_offense(<<~RUBY) |
| 22 | + expect(42).to eq(42) |
| 23 | + ^^^^^^^^^^^^^^^^^^^^ #{msg} |
| 24 | + RUBY |
| 25 | + end |
| 26 | + |
| 27 | + it 'registers an offense for complex constants' do |
| 28 | + expect_offense(<<~RUBY) |
| 29 | + expect({a: 1, b: :b}).to eql({a: 1, b: :b}) |
| 30 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} |
| 31 | + RUBY |
| 32 | + end |
| 33 | + |
| 34 | + it 'registers an offense for identical expression with be' do |
| 35 | + expect_offense(<<~RUBY) |
| 36 | + expect(foo.bar).to be(foo.bar) |
| 37 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} |
| 38 | + RUBY |
| 39 | + end |
| 40 | + |
| 41 | + it 'registers an offense for identical expression with be ==' do |
| 42 | + expect_offense(<<~RUBY) |
| 43 | + expect(foo.bar).to be == foo.bar |
| 44 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} |
| 45 | + RUBY |
| 46 | + end |
| 47 | + |
| 48 | + it 'does not register offense for different expressions' do |
| 49 | + expect_no_offenses(<<~RUBY) |
| 50 | + expect(foo.bar).to eq(bar.foo) |
| 51 | + RUBY |
| 52 | + end |
| 53 | + |
| 54 | + it 'checks for whole expression' do |
| 55 | + expect_no_offenses(<<~RUBY) |
| 56 | + expect(Foo.new(1).foo).to eql(Foo.new(2).bar) |
| 57 | + RUBY |
| 58 | + end |
| 59 | +end |
0 commit comments