summaryrefslogtreecommitdiff
path: root/jni/ruby/test/misc/test_ruby_mode.rb
blob: 5be01747f184eba0abad36b8bdb054a26e53aeac (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
require 'test/unit'
require 'tempfile'

class TestRubyMode < Test::Unit::TestCase
  MISCDIR = File.expand_path("../../../misc", __FILE__)
  e = ENV["EMACS"] || "emacs"
  emacs = %W"#{e} -q --no-site-file --batch --load #{MISCDIR}/ruby-mode.el"
  begin
    raise if IO.popen([e, "--version", :err=>[:child, :out]]) {|f| f.read}[/[0-9]+/].to_i < 23
    IO.popen([*emacs, :err=>[:child, :out]]) {|f| f.read}
  rescue
    EMACS = nil
  else
    EMACS = (emacs if $? and $?.success?)
  end
end

class TestRubyMode
  EVAL_OPT = "--eval"
  EXPR_SAVE = "(save-buffer)"
  EXPR_RUBYMODE = "(ruby-mode)"
  EXPR_NOBACKUP = "(progn" \
  " (set (make-local-variable \'backup-inhibited) t)" \
  " (set-buffer-modified-p t)" \
  ")"

  def run_emacs(src, *exprs)
    tmp = Tempfile.new(%w"ruby-mode.test. .rb")
    tmp.puts(src)
    tmp.close
    exprs = exprs.map {|expr| [EVAL_OPT, expr]}.flatten
    exprs.unshift(EVAL_OPT, EXPR_RUBYMODE)
    exprs.unshift(EVAL_OPT, EXPR_NOBACKUP)
    output = IO.popen([*EMACS, tmp.path, *exprs, err:[:child, :out]], "r") {|e| e.read}
    tmp.open
    result = tmp.read
    return result, output
  ensure
    tmp.close!
  end

  class TestIndent < self
    EXPR_INDENT = "(indent-region (point-min) (point-max))"

    def assert_indent(expected, source, *message)
      if space = expected[/\A\n?(\s*\|)/, 1]
        space = /^#{Regexp.quote(space)}/m
        expected.gsub!(space, '')
        source.gsub!(space, '')
      end
      result, output = run_emacs(source, EXPR_INDENT, EXPR_SAVE)
      assert_match(/^Wrote /, output)
      assert_equal(expected, result, message(*message) {diff expected, result})
    end

    def test_simple
      assert_indent('
      |if foo
      |  bar
      |end
      |zot
      |', '
      |if foo
      |bar
      |  end
      |    zot
      |')
    end

    def test_keyword_label
      assert_indent('
      |bar(class: XXX) do
      |  foo
      |end
      |bar
      |', '
      |bar(class: XXX) do
      |     foo
      |  end
      |    bar
      |')
    end

    def test_method_with_question_mark
      assert_indent('
      |if x.is_a?(XXX)
      |  foo
      |end
      |', '
      |if x.is_a?(XXX)
      | foo
      |   end
      |')
    end

    def test_expr_in_regexp
      assert_indent('
      |if /#{foo}/ =~ s
      |  x = 1
      |end
      |', '
      |if /#{foo}/ =~ s
      | x = 1
      |  end
      |')
    end

    def test_singleton_class
      skip("pending")
      assert_indent('
      |class<<bar
      |  foo
      |end
      |', '
      |class<<bar
      |foo
      |   end
      |')
    end

    def test_array_literal
      assert_indent('
      |foo = [
      |  bar
      |]
      |', '
      |foo = [
      | bar
      |  ]
      |')
      assert_indent('
      |foo do
      |  [bar]
      |end
      |', '
      |foo do
      |[bar]
      |  end
      |')
    end

    def test_begin_end
      assert_indent('
      |begin
      |  a[b]
      |end
      |', '
      |begin
      | a[b]
      |  end
      |')
    end

    def test_array_after_paren_and_space
      assert_indent('
      |class A
      |  def foo
      |    foo( [])
      |  end
      |end
      |', '
      |class A
      | def foo
      |foo( [])
      |end
      |  end
      |')
    end

    def test_spread_arguments
      assert_indent('
      |foo(1,
      |    2,
      |    3)
      |', '
      |foo(1,
      | 2,
      |  3)
      |')
    end
  end
end if TestRubyMode::EMACS