summaryrefslogtreecommitdiff
path: root/test/irb/command/test_cd.rb
blob: 10f77f66911041de073869fff7c557a1e983680d (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
require "tempfile"
require_relative "../helper"

module TestIRB
  class CDTest < IntegrationTestCase
    def setup
      super

      write_ruby <<~'RUBY'
        class Foo
          class Bar
            def bar
              "this is bar"
            end
          end

          def foo
            "this is foo"
          end
        end

        class BO < BasicObject
          def baz
            "this is baz"
          end
        end

        binding.irb
      RUBY
    end

    def test_cd
      out = run_ruby_file do
        type "cd Foo"
        type "ls"
        type "cd Bar"
        type "ls"
        type "cd .."
        type "exit"
      end

      assert_match(/irb\(Foo\):002>/, out)
      assert_match(/Foo#methods: foo/, out)
      assert_match(/irb\(Foo::Bar\):004>/, out)
      assert_match(/Bar#methods: bar/, out)
      assert_match(/irb\(Foo\):006>/, out)
    end

    def test_cd_basic_object_or_frozen
      out = run_ruby_file do
        type "cd BO.new"
        type "cd 1"
        type "cd Object.new.freeze"
        type "exit"
      end

      assert_match(/irb\(#<BO:.+\):002>/, out)
      assert_match(/irb\(1\):003>/, out)
      assert_match(/irb\(#<Object:.+\):004>/, out)
    end

    def test_cd_moves_top_level_with_no_args
      out = run_ruby_file do
        type "cd Foo"
        type "cd Bar"
        type "cd"
        type "exit"
      end

      assert_match(/irb\(Foo::Bar\):003>/, out)
      assert_match(/irb\(main\):004>/, out)
    end

    def test_cd_with_error
      out = run_ruby_file do
        type "cd Baz"
        type "exit"
      end

      assert_match(/Error: uninitialized constant Baz/, out)
      assert_match(/irb\(main\):002>/, out) # the context should not change
    end
  end
end