| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | require "rubocops/blank" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  | describe RuboCop::Cop::Homebrew::Blank, :config do | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |   shared_examples "offense" do |source, correction, message| | 
					
						
							|  |  |  |     it "registers an offense and corrects" do | 
					
						
							|  |  |  |       expect_offense(<<~RUBY, source: source, message: message) | 
					
						
							|  |  |  |         #{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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Bug: https://github.com/rubocop/rubocop/issues/4171 | 
					
						
							|  |  |  |   it "does not break when RHS of `or` is a naked falsiness check" do | 
					
						
							|  |  |  |     expect_no_offenses("foo.empty? || bar") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   it "does not break when LHS of `or` is a naked falsiness check" do | 
					
						
							|  |  |  |     expect_no_offenses("bar || foo.empty?") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Bug: https://github.com/rubocop/rubocop/issues/4814 | 
					
						
							|  |  |  |   it "does not break when LHS of `or` is a send node with an argument" do | 
					
						
							|  |  |  |     expect_no_offenses("x(1) || something") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "when nil or empty" do | 
					
						
							|  |  |  |     it_behaves_like "offense", "foo.nil? || foo.empty?", | 
					
						
							|  |  |  |                     "foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.blank?` instead of `foo.nil? || foo.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "nil? || empty?", "blank?", "Use `blank?` instead of `nil? || empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "foo == nil || foo.empty?", | 
					
						
							|  |  |  |                     "foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.blank?` instead of `foo == nil || foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "nil == foo || foo.empty?", | 
					
						
							|  |  |  |                     "foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.blank?` instead of `nil == foo || foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "!foo || foo.empty?", "foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.blank?` instead of `!foo || foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it_behaves_like "offense", "foo.nil? || !!foo.empty?", | 
					
						
							|  |  |  |                     "foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.blank?` instead of `foo.nil? || !!foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "foo == nil || !!foo.empty?", | 
					
						
							|  |  |  |                     "foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.blank?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`foo == nil || !!foo.empty?`." | 
					
						
							|  |  |  |     it_behaves_like "offense", "nil == foo || !!foo.empty?", | 
					
						
							|  |  |  |                     "foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.blank?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`nil == foo || !!foo.empty?`." | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   context "when checking all variable types" do | 
					
						
							|  |  |  |     it_behaves_like "offense", "foo.bar.nil? || foo.bar.empty?", | 
					
						
							|  |  |  |                     "foo.bar.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo.bar.blank?` 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.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `FOO.blank?` instead of `FOO.nil? || FOO.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "Foo.nil? || Foo.empty?", | 
					
						
							|  |  |  |                     "Foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `Foo.blank?` instead of `Foo.nil? || Foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "Foo::Bar.nil? || Foo::Bar.empty?", | 
					
						
							|  |  |  |                     "Foo::Bar.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `Foo::Bar.blank?` 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.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `@foo.blank?` instead of `@foo.nil? || @foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "$foo.nil? || $foo.empty?", | 
					
						
							|  |  |  |                     "$foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `$foo.blank?` instead of `$foo.nil? || $foo.empty?`." | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |     it_behaves_like "offense", "@@foo.nil? || @@foo.empty?", | 
					
						
							|  |  |  |                     "@@foo.blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `@@foo.blank?` 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].blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo[bar].blank?` 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).blank?", | 
					
						
							| 
									
										
										
										
											2024-01-26 13:32:29 -08:00
										 |  |  |                     "Use `foo(bar).blank?` instead of " \ | 
					
						
							| 
									
										
										
										
											2024-01-19 13:54:52 -08:00
										 |  |  |                     "`foo(bar).nil? || foo(bar).empty?`." | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |