summaryrefslogtreecommitdiff
path: root/spec/syntax_suggest/unit/api_spec.rb
blob: e900b9e10b8ef4d77330fd2e8b2da49aacf2d0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# frozen_string_literal: true

require_relative "../spec_helper"
begin
  require "ruby-prof"
rescue LoadError
end

module SyntaxSuggest
  RSpec.describe "Top level SyntaxSuggest api" do
    it "doesn't load prism if env var is set" do
      skip("SYNTAX_SUGGEST_DISABLE_PRISM not set") unless ENV["SYNTAX_SUGGEST_DISABLE_PRISM"]

      expect(SyntaxSuggest.use_prism_parser?).to be_falsey
    end

    it "has a `handle_error` interface" do
      fake_error = Object.new
      def fake_error.message
        "#{__FILE__}:216: unterminated string meets end of file "
      end

      def fake_error.is_a?(v)
        true
      end

      io = StringIO.new
      SyntaxSuggest.handle_error(
        fake_error,
        re_raise: false,
        io: io
      )

      expect(io.string.strip).to eq("")
    end

    it "raises original error with warning if a non-syntax error is passed" do
      error = NameError.new("blerg")
      io = StringIO.new
      expect {
        SyntaxSuggest.handle_error(
          error,
          re_raise: false,
          io: io
        )
      }.to raise_error { |e|
        expect(io.string).to include("Must pass a SyntaxError")
        expect(e).to eq(error)
      }
    end

    it "raises original error with warning if file is not found" do
      fake_error = SyntaxError.new
      def fake_error.message
        "#does/not/exist/lol/doesnotexist:216: unterminated string meets end of file "
      end

      io = StringIO.new
      expect {
        SyntaxSuggest.handle_error(
          fake_error,
          re_raise: false,
          io: io
        )
      }.to raise_error { |e|
        expect(io.string).to include("Could not find filename")
        expect(e).to eq(fake_error)
      }
    end

    it "respects highlight API" do
      skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2")

      core_ext_file = lib_dir.join("syntax_suggest").join("core_ext.rb")
      require_relative core_ext_file

      error_klass = Class.new do
        def path
          fixtures_dir.join("this_project_extra_def.rb.txt")
        end

        def detailed_message(**kwargs)
          "error"
        end
      end
      error_klass.prepend(SyntaxSuggest.module_for_detailed_message)
      error = error_klass.new

      expect(error.detailed_message(highlight: true)).to include(SyntaxSuggest::DisplayCodeWithLineNumbers::TERMINAL_HIGHLIGHT)
      expect(error.detailed_message(highlight: false)).to_not include(SyntaxSuggest::DisplayCodeWithLineNumbers::TERMINAL_HIGHLIGHT)
    end

    it "can be disabled via falsey kwarg" do
      skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2")

      core_ext_file = lib_dir.join("syntax_suggest").join("core_ext.rb")
      require_relative core_ext_file

      error_klass = Class.new do
        def path
          fixtures_dir.join("this_project_extra_def.rb.txt")
        end

        def detailed_message(**kwargs)
          "error"
        end
      end
      error_klass.prepend(SyntaxSuggest.module_for_detailed_message)
      error = error_klass.new

      expect(error.detailed_message(syntax_suggest: true)).to_not eq(error.detailed_message(syntax_suggest: false))
    end
  end
end