|
| 1 | +require_relative '../../spec_helper' |
| 2 | + |
| 3 | +ruby_version_is "3.4" do |
| 4 | + describe "GC.config" do |
| 5 | + context "without arguments" do |
| 6 | + it "returns a hash of current settings" do |
| 7 | + GC.config.should be_kind_of(Hash) |
| 8 | + end |
| 9 | + |
| 10 | + it "includes the name of currently loaded GC implementation as a global key" do |
| 11 | + GC.config.should include(:implementation) |
| 12 | + GC.config[:implementation].should be_kind_of(String) |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + context "with a hash of options" do |
| 17 | + it "allows to set GC implementation's options" do |
| 18 | + # Can't set anything if there are no options. |
| 19 | + skip if GC.config(foo_bar_nonexistent_option_for_real!: nil).empty? |
| 20 | + |
| 21 | + # Remove global keys which are read-only. |
| 22 | + config = GC.config(foo_bar_nonexistent_option_for_real!: nil) |
| 23 | + # Try to find a boolean setting to reliably test changing it. |
| 24 | + key, value = config.find { |_k, v| v == true } |
| 25 | + skip unless key |
| 26 | + |
| 27 | + GC.config(key => false).should == config.merge(key => false) |
| 28 | + GC.config[key].should == false |
| 29 | + GC.config(key => true).should == config |
| 30 | + GC.config[key].should == true |
| 31 | + ensure |
| 32 | + GC.config(config) |
| 33 | + end |
| 34 | + |
| 35 | + it "does not change settings that aren't present in the hash" do |
| 36 | + GC.config({}).should == GC.config.except(:implementation) |
| 37 | + end |
| 38 | + |
| 39 | + it "ignores unknown keys" do |
| 40 | + GC.config(foo: "bar").should == GC.config.except(:implementation) |
| 41 | + end |
| 42 | + |
| 43 | + it "raises an ArgumentError if options include global keys" do |
| 44 | + -> { GC.config(implementation: "default") }.should raise_error(ArgumentError, 'Attempting to set read-only key "Implementation"') |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context "with a non-hash argument" do |
| 49 | + it "returns current settings if argument is nil" do |
| 50 | + GC.config(nil).should == GC.config |
| 51 | + end |
| 52 | + |
| 53 | + it "raises ArgumentError for all other arguments" do |
| 54 | + -> { GC.config([]) }.should raise_error(ArgumentError, "ArgumentError") |
| 55 | + -> { GC.config("default") }.should raise_error(ArgumentError, "ArgumentError") |
| 56 | + -> { GC.config(1) }.should raise_error(ArgumentError, "ArgumentError") |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + guard -> { PlatformGuard.standard? && GC.config[:implementation] == "default" } do |
| 61 | + context "with default GC implementation on MRI" do |
| 62 | + before do |
| 63 | + @default_config = GC.config({}) |
| 64 | + end |
| 65 | + |
| 66 | + after do |
| 67 | + GC.config(@default_config) |
| 68 | + end |
| 69 | + |
| 70 | + it "includes :rgengc_allow_full_mark option, true by default" do |
| 71 | + GC.config.should include(:rgengc_allow_full_mark) |
| 72 | + GC.config[:rgengc_allow_full_mark].should be_true |
| 73 | + end |
| 74 | + |
| 75 | + it "allows to set :rgengc_allow_full_mark" do |
| 76 | + GC.config(rgengc_allow_full_mark: false).should == @default_config.merge(rgengc_allow_full_mark: false) |
| 77 | + GC.config(rgengc_allow_full_mark: true).should == @default_config |
| 78 | + |
| 79 | + # This key maps truthy and falsey values to true and false. |
| 80 | + GC.config(rgengc_allow_full_mark: nil).should == @default_config.merge(rgengc_allow_full_mark: false) |
| 81 | + GC.config(rgengc_allow_full_mark: 1.23).should == @default_config |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | +end |
0 commit comments