summaryrefslogtreecommitdiff
path: root/spec/ruby/command_line/frozen_strings_spec.rb
blob: 334b98273b78270b7777e0de3c5c281495271845 (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
require_relative '../spec_helper'

describe "The --enable-frozen-string-literal flag causes string literals to" do

  it "produce the same object each time" do
    ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
  end

  it "produce the same object for literals with the same content" do
    ruby_exe(fixture(__FILE__, "freeze_flag_two_literals.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
  end

  it "produce the same object for literals with the same content in different files" do
    ruby_exe(fixture(__FILE__, "freeze_flag_across_files.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
  end

  it "produce different objects for literals with the same content in different files if they have different encodings" do
    ruby_exe(fixture(__FILE__, "freeze_flag_across_files_diff_enc.rb"), options: "--enable-frozen-string-literal").chomp.should == "true"
  end
end

describe "The --disable-frozen-string-literal flag causes string literals to" do

  it "produce a different object each time" do
    ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb"), options: "--disable-frozen-string-literal").chomp.should == "false"
  end

end

describe "With neither --enable-frozen-string-literal nor --disable-frozen-string-literal flag set" do
  before do
    # disable --enable-frozen-string-literal and --disable-frozen-string-literal passed in $RUBYOPT
    @rubyopt = ENV["RUBYOPT"]
    ENV["RUBYOPT"] = ""
  end

  after do
    ENV["RUBYOPT"] = @rubyopt
  end

  it "produce a different object each time" do
    ruby_exe(fixture(__FILE__, "freeze_flag_one_literal.rb")).chomp.should == "false"
  end

  ruby_version_is "3.4" do
    it "if file has no frozen_string_literal comment produce different frozen strings each time" do
      ruby_exe(fixture(__FILE__, "string_literal_raw.rb")).chomp.should == "frozen:true interned:false"
    end
  end

  ruby_version_is ""..."3.4" do
    it "if file has no frozen_string_literal comment produce different mutable strings each time" do
      ruby_exe(fixture(__FILE__, "string_literal_raw.rb")).chomp.should == "frozen:false interned:false"
    end
  end

  it "if file has frozen_string_literal:true comment produce same frozen strings each time" do
    ruby_exe(fixture(__FILE__, "string_literal_frozen_comment.rb")).chomp.should == "frozen:true interned:true"
  end

  it "if file has frozen_string_literal:false comment produce different mutable strings each time" do
    ruby_exe(fixture(__FILE__, "string_literal_mutable_comment.rb")).chomp.should == "frozen:false interned:false"
  end
end

describe "The --debug flag produces" do
  it "debugging info on attempted frozen string modification" do
    error_str = ruby_exe(fixture(__FILE__, 'debug_info.rb'), options: '--debug',  args: "2>&1")
    error_str.should include("can't modify frozen String")
    error_str.should include("created at")
    error_str.should include("command_line/fixtures/debug_info.rb:2")
  end
end