| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "rubocops/present" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-18 15:11:11 -08:00
										 |  |  | RSpec.describe RuboCop::Cop::Homebrew::Present, :config do | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |   shared_examples "offense" do |source, correction, message| | 
					
						
							|  |  |  |     it "registers an offense and corrects" do | 
					
						
							| 
									
										
										
										
											2024-03-07 16:20:20 +00:00
										 |  |  |       expect_offense(<<~RUBY, source:, message:) | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |         #{source} | 
					
						
							|  |  |  |         ^{source} #{message} | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect_correction(<<~RUBY) | 
					
						
							|  |  |  |         #{correction} | 
					
						
							|  |  |  |       RUBY | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "accepts checking nil?" do | 
					
						
							|  |  |  |     expect_no_offenses("foo.nil?") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "accepts checking empty?" do | 
					
						
							|  |  |  |     expect_no_offenses("foo.empty?") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "accepts checking nil? || empty? on different objects" do | 
					
						
							|  |  |  |     expect_no_offenses("foo.nil? || bar.empty?") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "accepts checking existence && not empty? on different objects" do | 
					
						
							|  |  |  |     expect_no_offenses("foo && !bar.empty?") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it_behaves_like "offense", "foo && !foo.empty?", | 
					
						
							|  |  |  |                   "foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                   "Use `foo.present?` instead of `foo && !foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |   it_behaves_like "offense", "!foo.nil? && !foo.empty?", | 
					
						
							|  |  |  |                   "foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                   "Use `foo.present?` instead of `!foo.nil? && !foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |   it_behaves_like "offense", "!nil? && !empty?", "present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                   "Use `present?` instead of `!nil? && !empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |   it_behaves_like "offense", "foo != nil && !foo.empty?", | 
					
						
							|  |  |  |                   "foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                   "Use `foo.present?` instead of `foo != nil && !foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |   it_behaves_like "offense", "!!foo && !foo.empty?", | 
					
						
							|  |  |  |                   "foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                   "Use `foo.present?` instead of `!!foo && !foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   context "when checking all variable types" do | 
					
						
							|  |  |  |     it_behaves_like "offense", "!foo.nil? && !foo.empty?", | 
					
						
							|  |  |  |                     "foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!foo.nil? && !foo.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!foo.bar.nil? && !foo.bar.empty?", | 
					
						
							|  |  |  |                     "foo.bar.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.bar.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!foo.bar.nil? && !foo.bar.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!FOO.nil? && !FOO.empty?", | 
					
						
							|  |  |  |                     "FOO.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `FOO.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!FOO.nil? && !FOO.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!Foo.nil? && !Foo.empty?", | 
					
						
							|  |  |  |                     "Foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `Foo.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!Foo.nil? && !Foo.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!@foo.nil? && !@foo.empty?", | 
					
						
							|  |  |  |                     "@foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `@foo.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!@foo.nil? && !@foo.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!$foo.nil? && !$foo.empty?", | 
					
						
							|  |  |  |                     "$foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `$foo.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!$foo.nil? && !$foo.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!@@foo.nil? && !@@foo.empty?", | 
					
						
							|  |  |  |                     "@@foo.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `@@foo.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!@@foo.nil? && !@@foo.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!foo[bar].nil? && !foo[bar].empty?", | 
					
						
							|  |  |  |                     "foo[bar].present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo[bar].present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!foo[bar].nil? && !foo[bar].empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!Foo::Bar.nil? && !Foo::Bar.empty?", | 
					
						
							|  |  |  |                     "Foo::Bar.present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `Foo::Bar.present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!Foo::Bar.nil? && !Foo::Bar.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "!foo(bar).nil? && !foo(bar).empty?", | 
					
						
							|  |  |  |                     "foo(bar).present?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo(bar).present?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`!foo(bar).nil? && !foo(bar).empty?`." | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |