<commit_before>require File.expand_path('../../../spec_helper', __FILE__)

ruby_version_is "2.5" do
  describe "Hash#transform_keys" do
    before :each do
      @hash = { a: 1, b: 2, c: 3 }
    end

    it "returns new hash" do
      ret = @hash.transform_keys(&:succ)
      ret.should_not equal(@hash)
      ret.should be_an_instance_of(Hash)
    end

    it "sets the result as transformed keys with the given block" do
      @hash.transform_keys(&:succ).should ==  { b: 1, c: 2, d: 3 }
    end

    it "keeps last pair if new keys conflict" do
      @hash.transform_keys { |_| :a }.should == { a: 3 }
    end

    it "makes both hashes to share values" do
      value = [1, 2, 3]
      new_hash = { a: value }.transform_keys(&:upcase)
      new_hash[:A].should equal(value)
    end

    context "when no block is given" do
      it "returns a sized Enumerator" do
        enumerator = @hash.transform_keys
        enumerator.should be_an_instance_of(Enumerator)
        enumerator.size.should == @hash.size
        enumerator.each(&:succ).should == { b: 1, c: 2, d: 3 }
      end
    end

    it "returns a Hash instance, even on subclasses" do
      klass = Class.new(Hash)
      h = klass.new
      h[:foo] = 42
      r = h.transform_keys{|v| :"x#{v}"}
      r.keys.should == [:xfoo]
      r.class.should == Hash
    end
  end

  describe "Hash#transform_keys!" do
    before :each do
      @hash = { a: 1, b: 2, c: 3, d: 4 }
      @initial_pairs = @hash.dup
    end

    it "returns self" do
      @hash.transform_keys!(&:succ).should equal(@hash)
    end

    it "updates self as transformed values with the given block" do
      @hash.transform_keys!(&:to_s)
      @hash.should == { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 }
    end

    it "prevents conflicts between new keys and old ones" do
      @hash.transform_keys!(&:succ)
      @hash.should == { b: 1, c: 2, d: 3, e: 4 }
    end

    it "partially modifies the contents if we broke from the block" do
      @hash.transform_keys! do |v|
        break if v == :c
        v.succ
      end
      @hash.should == { b: 1, c: 2 }
    end
<commit_msg>Version transform_keys spec as it is not yet clear if it will be backported<commit_after>require File.expand_path('../../../spec_helper', __FILE__)

ruby_version_is "2.5" do
  describe "Hash#transform_keys" do
    before :each do
      @hash = { a: 1, b: 2, c: 3 }
    end

    it "returns new hash" do
      ret = @hash.transform_keys(&:succ)
      ret.should_not equal(@hash)
      ret.should be_an_instance_of(Hash)
    end

    it "sets the result as transformed keys with the given block" do
      @hash.transform_keys(&:succ).should ==  { b: 1, c: 2, d: 3 }
    end

    it "keeps last pair if new keys conflict" do
      @hash.transform_keys { |_| :a }.should == { a: 3 }
    end

    it "makes both hashes to share values" do
      value = [1, 2, 3]
      new_hash = { a: value }.transform_keys(&:upcase)
      new_hash[:A].should equal(value)
    end

    context "when no block is given" do
      it "returns a sized Enumerator" do
        enumerator = @hash.transform_keys
        enumerator.should be_an_instance_of(Enumerator)
        enumerator.size.should == @hash.size
        enumerator.each(&:succ).should == { b: 1, c: 2, d: 3 }
      end
    end

    it "returns a Hash instance, even on subclasses" do
      klass = Class.new(Hash)
      h = klass.new
      h[:foo] = 42
      r = h.transform_keys{|v| :"x#{v}"}
      r.keys.should == [:xfoo]
      r.class.should == Hash
    end
  end

  describe "Hash#transform_keys!" do
    before :each do
      @hash = { a: 1, b: 2, c: 3, d: 4 }
      @initial_pairs = @hash.dup
    end

    it "returns self" do
      @hash.transform_keys!(&:succ).should equal(@hash)
    end

    it "updates self as transformed values with the given block" do
      @hash.transform_keys!(&:to_s)
      @hash.should == { 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 }
    end

    # https://bugs.ruby-lang.org/issues/14380
    ruby_version_is ""..."2.6" do
      it "does not prevent conflicts between new keys and old ones" do
        @hash.transform_keys!(&:succ)
        @hash.should == { e: 1 }
      end
    end

    ruby_version_is "2.6" do
      it "prevents conflicts between new keys and old ones" do
        @hash.transform_keys!(&:succ)
        @hash.should == { b: 1, c: 2, d: 3, e: 4 }
      end
    end

    ruby_version_is ""..."2.6" do
      it "partially modifies the contents if we broke from the block" do
        @hash.transform_keys! do |v|
          break if v == :c
          v.succ
        end
        @hash.should == { c: 1, d: 4 }
      end
    end

    ruby_version_is "2.6" do
      it "returns the processed keys if we broke from the block" do
        @hash.transform_keys! do |v|
          break if v == :c
          v.succ
        end
        @hash.should == { b: 1, c: 2 }
      end
    end

{'fraction_non_alphanumeric': 0.12910509031198686, 'fraction_numerical': 0.01600985221674877, 'mean_word_length': 2.162232316677482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5086458', 'n_tokens_mistral': 1777, 'n_tokens_neox': 1677, 'n_words': 540}