|
| 1 | +# typed: false |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require_relative "spec_helper" |
| 5 | + |
| 6 | +RSpec.describe RubyLsp::RSpec do |
| 7 | + include RubyLsp::TestHelper |
| 8 | + |
| 9 | + let(:uri) { URI("file:///fake_spec.rb") } |
| 10 | + |
| 11 | + describe "code lens" do |
| 12 | + it "generates code lens for basic tests" do |
| 13 | + source = <<~RUBY |
| 14 | + RSpec.describe Foo do |
| 15 | + context "when something" do |
| 16 | + it "does something" do |
| 17 | + end |
| 18 | + end |
| 19 | + end |
| 20 | + RUBY |
| 21 | + |
| 22 | + with_server(source, uri) do |server, uri| |
| 23 | + server.process_message( |
| 24 | + { |
| 25 | + id: 1, |
| 26 | + method: "textDocument/codeLens", |
| 27 | + params: { |
| 28 | + textDocument: { uri: uri }, |
| 29 | + position: { line: 0, character: 0 }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + ) |
| 33 | + |
| 34 | + response = server.pop_response.response |
| 35 | + |
| 36 | + expect(response.count).to eq(9) |
| 37 | + |
| 38 | + expect(response[0].data).to eq({ type: "test", kind: :group, group_id: nil, id: 1 }) |
| 39 | + expect(response[1].data).to eq({ type: "test_in_terminal", kind: :group, group_id: nil, id: 1 }) |
| 40 | + expect(response[2].data).to eq({ type: "debug", kind: :group, group_id: nil, id: 1 }) |
| 41 | + |
| 42 | + 0.upto(2) do |i| |
| 43 | + expect(response[i].command.arguments).to eq([ |
| 44 | + "/fake_spec.rb", |
| 45 | + "Foo", |
| 46 | + "bundle exec rspec /fake_spec.rb:1", |
| 47 | + { start_line: 0, start_column: 0, end_line: 5, end_column: 3 }, |
| 48 | + ]) |
| 49 | + end |
| 50 | + |
| 51 | + expect(response[3].data).to eq({ type: "test", kind: :group, group_id: 1, id: 2 }) |
| 52 | + expect(response[4].data).to eq({ type: "test_in_terminal", kind: :group, group_id: 1, id: 2 }) |
| 53 | + expect(response[5].data).to eq({ type: "debug", kind: :group, group_id: 1, id: 2 }) |
| 54 | + |
| 55 | + 3.upto(5) do |i| |
| 56 | + expect(response[i].command.arguments).to eq([ |
| 57 | + "/fake_spec.rb", |
| 58 | + "when something", |
| 59 | + "bundle exec rspec /fake_spec.rb:2", |
| 60 | + { start_line: 1, start_column: 2, end_line: 4, end_column: 5 }, |
| 61 | + ]) |
| 62 | + end |
| 63 | + |
| 64 | + expect(response[6].data).to eq({ type: "test", kind: :example, group_id: 2 }) |
| 65 | + expect(response[7].data).to eq({ type: "test_in_terminal", kind: :example, group_id: 2 }) |
| 66 | + expect(response[8].data).to eq({ type: "debug", kind: :example, group_id: 2 }) |
| 67 | + |
| 68 | + 6.upto(8) do |i| |
| 69 | + expect(response[i].command.arguments).to eq([ |
| 70 | + "/fake_spec.rb", |
| 71 | + "does something", |
| 72 | + "bundle exec rspec /fake_spec.rb:3", |
| 73 | + { start_line: 2, start_column: 4, end_line: 3, end_column: 7 }, |
| 74 | + ]) |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + it "recognizes different example, it, and specify declarations" do |
| 80 | + source = <<~RUBY |
| 81 | + RSpec.describe Foo do |
| 82 | + it { do_something } |
| 83 | + it var1 do |
| 84 | + do_something |
| 85 | + end |
| 86 | + specify { do_something } |
| 87 | + example var2 do |
| 88 | + do_something |
| 89 | + end |
| 90 | + end |
| 91 | + RUBY |
| 92 | + |
| 93 | + with_server(source, uri) do |server, uri| |
| 94 | + server.process_message( |
| 95 | + { |
| 96 | + id: 1, |
| 97 | + method: "textDocument/codeLens", |
| 98 | + params: { |
| 99 | + textDocument: { uri: uri }, |
| 100 | + position: { line: 0, character: 0 }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + ) |
| 104 | + |
| 105 | + response = server.pop_response.response |
| 106 | + |
| 107 | + expect(response.count).to eq(15) |
| 108 | + |
| 109 | + expect(response[3].command.arguments[1]).to eq("<unnamed-1>") |
| 110 | + expect(response[6].command.arguments[1]).to eq("<var1>") |
| 111 | + expect(response[9].command.arguments[1]).to eq("<unnamed-2>") |
| 112 | + expect(response[12].command.arguments[1]).to eq("<var2>") |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + it "recognizes different context and describe declarations" do |
| 117 | + source = <<~RUBY |
| 118 | + RSpec.describe(Foo::Bar) do |
| 119 | + end |
| 120 | +
|
| 121 | + RSpec.describe Foo::Bar do |
| 122 | + end |
| 123 | +
|
| 124 | + context(Foo) do |
| 125 | + end |
| 126 | +
|
| 127 | + describe Foo do |
| 128 | + end |
| 129 | +
|
| 130 | + context "Foo" do |
| 131 | + end |
| 132 | +
|
| 133 | + describe var do |
| 134 | + end |
| 135 | +
|
| 136 | + # these should bot be recognized |
| 137 | + context |
| 138 | + describe |
| 139 | + context("foo") |
| 140 | + RUBY |
| 141 | + |
| 142 | + with_server(source, uri) do |server, uri| |
| 143 | + server.process_message( |
| 144 | + { |
| 145 | + id: 1, |
| 146 | + method: "textDocument/codeLens", |
| 147 | + params: { |
| 148 | + textDocument: { uri: uri }, |
| 149 | + position: { line: 0, character: 0 }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + ) |
| 153 | + |
| 154 | + response = server.pop_response.response |
| 155 | + |
| 156 | + expect(response.count).to eq(18) |
| 157 | + |
| 158 | + expect(response[11].command.arguments[1]).to eq("Foo") |
| 159 | + expect(response[13].command.arguments[1]).to eq("Foo") |
| 160 | + expect(response[15].command.arguments[1]).to eq("<var>") |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + context "when the file is not a test file" do |
| 165 | + let(:uri) { URI("file:///not_spec_file.rb") } |
| 166 | + |
| 167 | + it "ignores file" do |
| 168 | + source = <<~RUBY |
| 169 | + class FooBar |
| 170 | + context "when something" do |
| 171 | + end |
| 172 | + end |
| 173 | + RUBY |
| 174 | + |
| 175 | + with_server(source, uri) do |server, uri| |
| 176 | + server.process_message( |
| 177 | + { |
| 178 | + id: 1, |
| 179 | + method: "textDocument/codeLens", |
| 180 | + params: { |
| 181 | + textDocument: { uri: uri }, |
| 182 | + position: { line: 0, character: 0 }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + ) |
| 186 | + |
| 187 | + response = server.pop_response.response |
| 188 | + |
| 189 | + expect(response.count).to eq(0) |
| 190 | + end |
| 191 | + end |
| 192 | + end |
| 193 | + |
| 194 | + context "when there's a binstub" do |
| 195 | + let(:binstub_path) { File.expand_path("../bin/rspec", __dir__) } |
| 196 | + |
| 197 | + before do |
| 198 | + File.write(binstub_path, <<~RUBY) |
| 199 | + #!/usr/bin/env ruby |
| 200 | + puts "binstub is called" |
| 201 | + RUBY |
| 202 | + end |
| 203 | + |
| 204 | + after do |
| 205 | + FileUtils.rm(binstub_path) if File.exist?(binstub_path) |
| 206 | + end |
| 207 | + |
| 208 | + it "uses the binstub" do |
| 209 | + source = <<~RUBY |
| 210 | + RSpec.describe(Foo::Bar) do |
| 211 | + end |
| 212 | + RUBY |
| 213 | + |
| 214 | + with_server(source, uri) do |server, uri| |
| 215 | + server.process_message( |
| 216 | + { |
| 217 | + id: 1, |
| 218 | + method: "textDocument/codeLens", |
| 219 | + params: { |
| 220 | + textDocument: { uri: uri }, |
| 221 | + position: { line: 0, character: 0 }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + ) |
| 225 | + |
| 226 | + response = server.pop_response.response |
| 227 | + |
| 228 | + expect(response.count).to eq(3) |
| 229 | + expect(response[0].command.arguments[2]).to eq("bundle exec bin/rspec /fake_spec.rb:1") |
| 230 | + end |
| 231 | + end |
| 232 | + end |
| 233 | + end |
| 234 | +end |
0 commit comments