Skip to content

Instantly share code, notes, and snippets.

@bbrowning
Created September 26, 2012 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbrowning/d8dc0a4b071104fd0463 to your computer and use it in GitHub Desktop.
Save bbrowning/d8dc0a4b071104fd0463 to your computer and use it in GitHub Desktop.
require 'fileutils'
require 'rspec'
describe 'regex_gsub' do
before(:each) do
@content = "Rails::Initializer.run do |config|\nend"
end
it 'should match against a string' do
check_content(@content)
end
it 'should match when read from a file' do
file_name = 'temp_gsub_file'
File.open(file_name, 'wb') { |file| file.write(@content) }
content_from_file = File.read(file_name)
FileUtils.rm_f(file_name)
check_content(content_from_file)
end
def check_content(content)
data = "config.gem 'torquebox', :version => '2.2.0'"
sentinel = 'Rails::Initializer.run do |config|'
regexp = /(#{Regexp.escape(sentinel)})/mi
replaced_content = content.gsub(regexp) do |match|
"#{match}\n " << data
end
replaced_content.should include('torquebox')
end
end
bbrowning@bbrowning-mbp:~/tmp$ jruby -v
jruby 1.7.0.RC1 (1.9.3p203) 2012-09-25 df7874f on Java HotSpot(TM) 64-Bit Server VM 1.6.0_35-b10-428-10M3811 [darwin-x86_64]
bbrowning@bbrowning-mbp:~/tmp$ jruby --1.8 -S rspec gsub_file_spec.rb
..
Finished in 0.041 seconds
2 examples, 0 failures
bbrowning@bbrowning-mbp:~/tmp$ jruby --1.9 -S rspec gsub_file_spec.rb
.F
Failures:
1) regex_gsub should match when read from a file
Failure/Error: replaced_content.should include('torquebox')
expected "Rails::Initializer.run do |config|\nend" to include "torquebox"
Diff:
@@ -1,2 +1,3 @@
-torquebox
+Rails::Initializer.run do |config|
+end
# ./gsub_file_spec.rb:29:in `check_content'
# ./gsub_file_spec.rb:19:in `(root)'
Finished in 0.081 seconds
2 examples, 1 failure
Failed examples:
rspec ./gsub_file_spec.rb:14 # regex_gsub should match when read from a file
bbrowning@bbrowning-mbp:~/tmp$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment