summaryrefslogtreecommitdiff
path: root/test/irb/test_ruby-lex.rb
blob: b5255e9b536f99d29d1d8f7174b1cf6284a9d312 (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
# frozen_string_literal: false
require 'test/unit'
require 'irb/ruby-lex'
require 'stringio'

module TestIRB
  class TestRubyLex < Test::Unit::TestCase
    def setup
      @scanner = RubyLex.new
    end

    def test_set_input_proc
      called = false
      @scanner.set_input(nil) {called = true; nil}
      @scanner.each_top_level_statement {}
      assert(called)
    end

    def test_comment
      assert_equal([["#\n", 1]], top_level_statement("#\n"))
    end

    def top_level_statement(lines)
      input = InputLines.new(lines, "r")
      scanned = []
      @scanner.set_input(input)
      @scanner.each_top_level_statement {|*e|
        scanned << e
        yield(*e) if defined?(yield)
      }
      scanned
    end

    class InputLines < StringIO
      alias encoding external_encoding
    end
  end
end