|
| 1 | +# typed: true |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "test_helper" |
| 5 | + |
| 6 | +module RubyLsp |
| 7 | + module Rails |
| 8 | + class RailsTestStyleTest < ActiveSupport::TestCase |
| 9 | + test "discovers rails declarative tests" do |
| 10 | + source = <<~RUBY |
| 11 | + class SampleTest < ActiveSupport::TestCase |
| 12 | + test "first test" do |
| 13 | + assert true |
| 14 | + end |
| 15 | +
|
| 16 | + test "second test" do |
| 17 | + assert true |
| 18 | + end |
| 19 | + end |
| 20 | + RUBY |
| 21 | + |
| 22 | + with_active_support_declarative_tests(source) do |items| |
| 23 | + assert_equal(1, items.length) |
| 24 | + test_class = items.first |
| 25 | + assert_equal("SampleTest", test_class[:label]) |
| 26 | + assert_equal(2, test_class[:children].length) |
| 27 | + |
| 28 | + test_labels = test_class[:children].map { |i| i[:label] } |
| 29 | + assert_includes(test_labels, "first test") |
| 30 | + assert_includes(test_labels, "second test") |
| 31 | + assert_all_items_tagged_with(items, :rails) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + test "discovers rails test with empty test name" do |
| 36 | + source = <<~RUBY |
| 37 | + class EmptyTest < ActiveSupport::TestCase |
| 38 | + test "valid test" do |
| 39 | + assert true |
| 40 | + end |
| 41 | +
|
| 42 | + test "" do |
| 43 | + assert true |
| 44 | + end |
| 45 | + end |
| 46 | + RUBY |
| 47 | + |
| 48 | + with_active_support_declarative_tests(source) do |items| |
| 49 | + assert_equal(1, items.length) |
| 50 | + test_class = items.first |
| 51 | + assert_equal("EmptyTest", test_class[:label]) |
| 52 | + assert_equal(2, test_class[:children].length) |
| 53 | + |
| 54 | + test_labels = test_class[:children].map { |i| i[:label] } |
| 55 | + assert_includes(test_labels, "<empty test name>") |
| 56 | + assert_all_items_tagged_with(items, :rails) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + test "handles nested namespaces" do |
| 61 | + source = <<~RUBY |
| 62 | + class EmptyTest < ActiveSupport::TestCase |
| 63 | + test "valid test" do |
| 64 | + assert true |
| 65 | + end |
| 66 | +
|
| 67 | + module RandomModule |
| 68 | + test "not valid test" do |
| 69 | + assert false |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | + RUBY |
| 74 | + |
| 75 | + with_active_support_declarative_tests(source) do |items| |
| 76 | + assert_equal(1, items.length) |
| 77 | + test_class = items.first |
| 78 | + assert_equal("EmptyTest", test_class[:label]) |
| 79 | + assert_equal(1, test_class[:children].length) |
| 80 | + |
| 81 | + test_labels = test_class[:children].map { |i| i[:label] } |
| 82 | + refute_includes(test_labels, "not valid test") |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + test "handles test methods defined with def" do |
| 87 | + source = <<~RUBY |
| 88 | + class SampleTest < ActiveSupport::TestCase |
| 89 | + test "first test" do |
| 90 | + assert true |
| 91 | + end |
| 92 | +
|
| 93 | + def test_second_test |
| 94 | + assert true |
| 95 | + end |
| 96 | + end |
| 97 | + RUBY |
| 98 | + |
| 99 | + with_active_support_declarative_tests(source) do |items| |
| 100 | + assert_equal(1, items.length) |
| 101 | + test_class = items.first |
| 102 | + assert_equal("SampleTest", test_class[:label]) |
| 103 | + assert_equal(2, test_class[:children].length) |
| 104 | + |
| 105 | + test_labels = test_class[:children].map { |i| i[:label] } |
| 106 | + assert_includes(test_labels, "test_second_test") |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + test "handles tests with special characters in name" do |
| 111 | + source = <<~RUBY |
| 112 | + class SpecialCharsTest < ActiveSupport::TestCase |
| 113 | + test "test with spaces and punctuation!" do |
| 114 | + assert true |
| 115 | + end |
| 116 | +
|
| 117 | + test "test with unicode: 你好" do |
| 118 | + assert true |
| 119 | + end |
| 120 | + end |
| 121 | + RUBY |
| 122 | + |
| 123 | + with_active_support_declarative_tests(source) do |items| |
| 124 | + assert_equal(1, items.length) |
| 125 | + test_class = items.first |
| 126 | + assert_equal("SpecialCharsTest", test_class[:label]) |
| 127 | + assert_equal(2, test_class[:children].length) |
| 128 | + |
| 129 | + test_labels = test_class[:children].map { |i| i[:label] } |
| 130 | + assert_includes(test_labels, "test with spaces and punctuation!") |
| 131 | + assert_includes(test_labels, "test with unicode: 你好") |
| 132 | + assert_all_items_tagged_with(items, :rails) |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + private |
| 137 | + |
| 138 | + def with_active_support_declarative_tests(source, file: "/fake.rb", &block) |
| 139 | + with_server(source, URI(file)) do |server, uri| |
| 140 | + server.global_state.index.index_single(uri, <<~RUBY) |
| 141 | + module Minitest |
| 142 | + class Test; end |
| 143 | + end |
| 144 | +
|
| 145 | + module ActiveSupport |
| 146 | + module Testing |
| 147 | + module Declarative |
| 148 | + end |
| 149 | + end |
| 150 | +
|
| 151 | + class TestCase < Minitest::Test |
| 152 | + extend Testing::Declarative |
| 153 | + end |
| 154 | + end |
| 155 | + RUBY |
| 156 | + |
| 157 | + server.process_message(id: 1, method: "rubyLsp/discoverTests", params: { |
| 158 | + textDocument: { uri: uri }, |
| 159 | + }) |
| 160 | + |
| 161 | + result = pop_result(server) |
| 162 | + items = result.response |
| 163 | + yield items |
| 164 | + end |
| 165 | + end |
| 166 | + |
| 167 | + def assert_all_items_tagged_with(items, tag) |
| 168 | + items.each do |item| |
| 169 | + assert_includes(item[:tags], "framework:#{tag}") |
| 170 | + children = item[:children] |
| 171 | + assert_all_items_tagged_with(children, tag) unless children.empty? |
| 172 | + end |
| 173 | + end |
| 174 | + end |
| 175 | + end |
| 176 | +end |
0 commit comments