# encoding: utf-8 require 'spec_helper' describe String do let(:path) { '/book/12/blahahaha/test/chapter-3/stuff4/12' } let(:regex_with_names) do / \/(?\w+) # matches category type \/ # path separator (?\d+) # matches book ID numbers \/ # path separator .* # irrelevant \/ # path separator chapter-(?\d+) # matches chapter ID numbers \/ # path separator stuff(?\d+) # matches stuff ID numbers /x end let(:grouped_substitutions) do { 'category' => 'new-category', 'book-id' => 'new-book-id', 'chapter-id' => 'new-chapter-id', 'stuff-id' => '-new-stuff-id' } end describe '#scan_in_groups' do it 'returns regexp matches in named groups' do path.scan_in_groups( regex_with_names ).should == { 'category' => 'book', 'book-id' => '12', 'chapter-id' => '3', 'stuff-id' => '4' } end context 'when there are no matches' do it 'returns an empty hash' do 'test'.scan_in_groups( regex_with_names ).should == {} end end context 'when the regexp does not contain named captures' do it 'raises ArgumentError' do expect { 'test'.scan_in_groups( /./ ) }.to raise_error ArgumentError end end end describe '#sub_in_groups' do it 'substitutes the named matches' do path.sub_in_groups( regex_with_names, grouped_substitutions ).should == '/new-category/new-book-id/blahahaha/test/chapter-new-chapter-id/stuff-new-stuff-id/12' end context 'when using invalid group names' do it 'raises IndexError' do grouped_substitutions['blah'] = 'blah2' expect do path.sub_in_groups!( regex_with_names, grouped_substitutions ) end.to raise_error IndexError end end end describe '#sub_in_groups!' do it 'substitutes the named matches in place' do path.sub_in_groups!( regex_with_names, grouped_substitutions ) path.should == '/new-category/new-book-id/blahahaha/test/chapter-new-chapter-id/stuff-new-stuff-id/12' end context 'when using invalid group names' do it 'raises IndexError' do grouped_substitutions['blah'] = 'blah2' expect do path.sub_in_groups!( regex_with_names, grouped_substitutions ) end.to raise_error IndexError end end end describe '#rdiff' do it 'should return the common parts between self and another string' do str = <<-END This is the first test. Not really sure what else to put here... END str2 = <<-END This is the second test. Not really sure what else to put here... Boo-Yah! END str.rdiff( str2 ).should == " This is the test.\n" + ' Not really sure what else to put here' end end describe '#diff_ratio' do context 'when the strings are identical' do it 'returns 0.0' do ''.diff_ratio( '' ).should == 0 'test'.diff_ratio( 'test' ).should == 0 'test this'.diff_ratio( 'test this' ).should == 0 end end context 'when the strings completely different' do it 'returns 1.0' do ''.diff_ratio( 'toast' ).should == 1 'test'.diff_ratio( 'toast' ).should == 1 'test this'.diff_ratio( 'toast that' ).should == 1 end end context 'when the strings share less than half of their words' do it 'returns < 0.5' do 'test this here now'.diff_ratio( 'test that here now' ).should > 0.0 'test this here now'.diff_ratio( 'test that here now' ).should < 0.5 end end context 'when the strings share more than half of their words' do it 'returns > 0.5' do 'test this here now'.diff_ratio( 'test that here later' ).should > 0.0 'test this here now'.diff_ratio( 'test that here later' ).should > 0.5 end end end describe '#words' do context 'when strict is set to true' do it 'does not include boundaries' do 'blah.bloo