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