Skip to content

Commit

Permalink
Confim behaviour for garbled input.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Mar 14, 2011
1 parent ae6d990 commit f2bb440
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ task :default => :spec
desc 'Run all specs'
task :spec do
ruby = File.join(*Config::CONFIG.values_at('bindir', 'ruby_install_name'))
sh "#{ruby} spec/**/*_spec.rb"
FileList[File.expand_path('../spec/**/*_spec.rb', __FILE__)].each do |spec|
sh "#{ruby} #{spec}"
end
end

namespace :documentation do
Expand Down
1 change: 1 addition & 0 deletions spec/examples/garbled_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Le botier tanche permet de le mettre en �uvre dans les conditions les plus extrmes.
23 changes: 23 additions & 0 deletions spec/regression/garbled_encoding_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# encoding: utf-8

require File.expand_path('../../start', __FILE__)

describe "Ensure::Encoding, concerning cleanup of garbled input" do
it "should be allowed to throw errors when not forced to drop invalid characters" do
Dir.glob(File.expand_path('../../examples', __FILE__) + '/garbled*').each do |filename|
text = read_example(filename)
lambda {
Ensure::Encoding.force_encoding(text, 'UTF-8')
}.should.raise(Encoding::UndefinedConversionError)
end
end

it "should not throw errors when forced to drop invalid characters" do
Dir.glob(File.expand_path('../../examples', __FILE__) + '/garbled*').each do |filename|
text = read_example(filename)
lambda {
Ensure::Encoding.force_encoding(text, 'UTF-8', :invalid_characters => :drop)
}.should.not.raise
end
end
end
14 changes: 10 additions & 4 deletions spec/start.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

$:.unshift(File.expand_path('../../lib', __FILE__))

require 'ensure'
require 'ensure/encoding'

module EncodingTestHelpers
EXAMPLES = {
Expand All @@ -23,11 +23,17 @@ def examples
EXAMPLES.keys
end

def example(name, options={})
filename, contents = name.gsub(/-/, '_').downcase, ''
File.open(File.expand_path("../examples/#{filename}.txt", __FILE__), 'r:binary') do |file|
def read_example(filename)
contents = ''
File.open(filename, 'r:binary') do |file|
contents << file.read
end
contents
end

def example(name, options={})
filename, contents = name.gsub(/-/, '_').downcase, ''
contents = read_example(File.expand_path("../examples/#{filename}.txt", __FILE__))
contents.force_encoding(options[:mark_as] || Encoding.find(name))
return contents, EXAMPLES[name]
end
Expand Down

0 comments on commit f2bb440

Please sign in to comment.