summaryrefslogtreecommitdiff
path: root/test/irb/command/test_copy.rb
blob: 505812a1de4e0584ebf8c12e39d90155ac48d4ad (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
# frozen_string_literal: true

require 'irb'

require_relative "../helper"

module TestIRB
  class CopyTest < IntegrationTestCase
    def setup
      super
      @envs['IRB_COPY_COMMAND'] = "#{EnvUtil.rubybin} -e \"puts 'foo' + STDIN.read\""
    end

    def test_copy_with_pbcopy
      write_ruby <<~'ruby'
        class Answer
          def initialize(answer)
            @answer = answer
          end
        end

        binding.irb
      ruby

      output =  run_ruby_file do
        type "copy Answer.new(42)"
        type "exit"
      end

      assert_match(/foo#<Answer:0x[0-9a-f]+ @answer=42/, output)
      assert_match(/Copied to system clipboard/, output)
    end

    # copy puts 5 should:
    # - Print value to the console
    # - Copy nil to clipboard, since that is what the puts call evaluates to
    def test_copy_when_expression_has_side_effects
      write_ruby <<~'ruby'
        binding.irb
      ruby

      output = run_ruby_file do
        type "copy puts 42"
        type "exit"
      end

      assert_match(/^42\r\n/, output)
      assert_match(/foonil/, output)
      assert_match(/Copied to system clipboard/, output)
      refute_match(/foo42/, output)
    end

    def test_copy_when_copy_command_is_invalid
      @envs['IRB_COPY_COMMAND'] = "lulz"

      write_ruby <<~'ruby'
        binding.irb
      ruby

      output = run_ruby_file do
        type "copy 42"
        type "exit"
      end

      assert_match(/No such file or directory - lulz/, output)
      assert_match(/Is IRB\.conf\[:COPY_COMMAND\] set to a bad value/, output)
      refute_match(/Copied to system clipboard/, output)
    end
  end
end