github.com/nycdavid/zeus@v0.0.0-20201208104106-9ba439429e03/rubygem/spec/m_spec.rb (about) 1 require 'zeus/rails' 2 require 'fake_mini_test' 3 4 module Zeus::M 5 describe Runner do 6 let(:test_method) { fake_test_method } 7 8 matcher :exit_with_code do |exp_code| 9 actual = nil 10 match do |block| 11 begin 12 block.call 13 rescue SystemExit => e 14 actual = e.status 15 end 16 actual and actual == exp_code 17 end 18 failure_message do |_block| 19 "expected block to call exit(#{exp_code}) but exit" + 20 (actual.nil? ? " not called" : "(#{actual}) was called") 21 end 22 failure_message_when_negated do |_block| 23 "expected block not to call exit(#{exp_code})" 24 end 25 description do 26 "expect block to call exit(#{exp_code})" 27 end 28 end 29 30 before(:each) do 31 allow(Dir).to receive(:glob).and_return(["path/to/file.rb"]) 32 allow(Kernel).to receive(:load) 33 34 stub_mini_test_methods 35 end 36 37 context "given a test with a question mark" do 38 let(:test_method) { fake_special_characters_test_method } 39 40 it "escapes the question mark when using line number" do 41 argv = ["path/to/file.rb:2"] 42 43 expect(lambda { Runner.new(argv).run }).to exit_with_code(0) 44 expect(ARGV).to eq(["-n", "/(test_my_test_method\\?)/"]) 45 end 46 47 it "does not escape regex on explicit names" do 48 argv = ["path/to/file.rb", "--name", fake_special_characters_test_method] 49 50 expect(lambda { Runner.new(argv).run }).to exit_with_code(0) 51 52 expect(ARGV).to eq(["-n", "test_my_test_method?"]) 53 end 54 end 55 56 context "given no option" do 57 it "runs the test" do 58 argv = ["path/to/file.rb"] 59 60 expect(lambda { Runner.new(argv).run }).to exit_with_code(0) 61 62 expect(ARGV).to eq([]) 63 end 64 end 65 66 context "given a line number" do 67 it "aborts if no test is found on that line number" do 68 argv = ["path/to/file.rb:100"] 69 70 expect(STDERR).to receive(:write).with(/No tests found on line 100/) 71 72 expect(lambda { Runner.new(argv).run }).to_not exit_with_code(0) 73 end 74 75 it "runs the test if the correct line number is given" do 76 argv = ["path/to/file.rb:2"] 77 78 expect(lambda { Runner.new(argv).run }).to exit_with_code(0) 79 expect(ARGV).to eq(["-n", "/(#{fake_test_method})/"]) 80 end 81 end 82 83 context "given a specific test name" do 84 it "runs the specified tests when using a pattern in --name option" do 85 argv = ["path/to/file.rb", "--name", "/#{fake_test_method}/"] 86 87 expect(lambda { Runner.new(argv).run }).to exit_with_code(0) 88 expect(ARGV).to eq(["-n", "/#{fake_test_method}/"]) 89 end 90 91 it "runs the specified tests when using a pattern in -n option" do 92 argv = ["path/to/file.rb", "-n", "/method/"] 93 94 expect(lambda { Runner.new(argv).run }).to exit_with_code(0) 95 expect(ARGV).to eq(["-n", "/method/"]) 96 end 97 98 it "aborts if no test matches the given pattern" do 99 argv = ["path/to/file.rb", "-n", "/garbage/"] 100 101 expect(STDERR).to receive(:write).with(%r{No test name matches \'/garbage/\'}) 102 expect(fake_runner).to_not receive :run 103 104 expect(lambda { Runner.new(argv).run }).to_not exit_with_code(0) 105 end 106 107 it "runs the specified tests when using a name (no pattern)" do 108 argv = ["path/to/file.rb", "-n", "#{fake_test_method}"] 109 110 expect(lambda { Runner.new(argv).run }).to exit_with_code(0) 111 expect(ARGV).to eq(["-n", fake_test_method]) 112 end 113 114 it "aborts if no test matches the given test name" do 115 argv = ["path/to/file.rb", "-n", "method"] 116 117 expect(STDERR).to receive(:write).with(%r{No test name matches \'method\'}) 118 expect(fake_runner).to_not receive :run 119 120 expect(lambda { Runner.new(argv).run }).to_not exit_with_code(0) 121 end 122 end 123 end 124 125 end