Sha256: 24967f464f65d822ec8bc6301459cecabf1a70d6cf85722f2c53f3e17dbd4a61

Contents?: true

Size: 1.09 KB

Versions: 23

Compression:

Stored size: 1.09 KB

Contents

require 'spec_helper'

describe 'String' do
  describe '#gsub' do
    it 'handles recursive gsub' do
      pass_slot_rx = /{(\d+)}/
      recurse_gsub = -> text {
        text.gsub(pass_slot_rx) {
          index = $1.to_i
          if index == 0
            recurse_gsub.call '{1}'
          else
            'value'
          end
        }
      }
      result = recurse_gsub.call '<code>{0}</code>'
      result.should == '<code>value</code>'
    end

    it 'works well with zero-length matches' do
      expect("test".gsub(/^/, '2')).to eq "2test"
      expect("test".gsub(/$/, '2')).to eq "test2"
      expect("test".gsub(/\b/, '2')).to eq "2test2"
    end

    it "doesn't override $~ when it's inspected" do
      'a:b'.gsub(/([a-z]):([a-z])/) do
        $~.inspect
        target, content = $1, $2
        expect([target, content]).to eq(['a', 'b'])
      end
    end
  end

  describe '#sub' do
    it 'works well with zero-length matches' do
      expect("test".sub(/^/, '2')).to eq "2test"
      expect("test".sub(/$/, '2')).to eq "test2"
      expect("test".sub(/\b/, '2')).to eq "2test"
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 spec/opal/core/string/gsub_spec.rb
opal-1.8.2 spec/opal/core/string/gsub_spec.rb
opal-1.8.1 spec/opal/core/string/gsub_spec.rb
opal-1.8.0 spec/opal/core/string/gsub_spec.rb
opal-1.8.0.beta1 spec/opal/core/string/gsub_spec.rb
opal-1.7.4 spec/opal/core/string/gsub_spec.rb
opal-1.8.0.alpha1 spec/opal/core/string/gsub_spec.rb
opal-1.7.3 spec/opal/core/string/gsub_spec.rb
opal-1.7.2 spec/opal/core/string/gsub_spec.rb
opal-1.7.1 spec/opal/core/string/gsub_spec.rb
opal-1.7.0 spec/opal/core/string/gsub_spec.rb
opal-1.7.0.rc1 spec/opal/core/string/gsub_spec.rb
opal-1.6.1 spec/opal/core/string/gsub_spec.rb
opal-1.6.0 spec/opal/core/string/gsub_spec.rb
opal-1.6.0.rc1 spec/opal/core/string/gsub_spec.rb
opal-1.6.0.alpha1 spec/opal/core/string/gsub_spec.rb
opal-1.5.1 spec/opal/core/string/gsub_spec.rb
opal-1.5.0 spec/opal/core/string/gsub_spec.rb
opal-1.5.0.rc1 spec/opal/core/string/gsub_spec.rb
opal-1.4.1 spec/opal/core/string/gsub_spec.rb