From f2bb440f3193b4b9f6e4b9f7f0f611ddd04ea45b Mon Sep 17 00:00:00 2001 From: Manfred Stienstra Date: Mon, 14 Mar 2011 18:02:05 +0100 Subject: [PATCH] Confim behaviour for garbled input. --- Rakefile | 4 +++- spec/examples/garbled_1.txt | 1 + spec/regression/garbled_encoding_spec.rb | 23 +++++++++++++++++++++++ spec/start.rb | 14 ++++++++++---- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 spec/examples/garbled_1.txt create mode 100644 spec/regression/garbled_encoding_spec.rb diff --git a/Rakefile b/Rakefile index 579eb0b..0d569cb 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/spec/examples/garbled_1.txt b/spec/examples/garbled_1.txt new file mode 100644 index 0000000..0a4e54d --- /dev/null +++ b/spec/examples/garbled_1.txt @@ -0,0 +1 @@ +Le botier tanche permet de le mettre en œuvre dans les conditions les plus extrmes. \ No newline at end of file diff --git a/spec/regression/garbled_encoding_spec.rb b/spec/regression/garbled_encoding_spec.rb new file mode 100644 index 0000000..6d324e9 --- /dev/null +++ b/spec/regression/garbled_encoding_spec.rb @@ -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 \ No newline at end of file diff --git a/spec/start.rb b/spec/start.rb index 4548584..a5a5d03 100644 --- a/spec/start.rb +++ b/spec/start.rb @@ -8,7 +8,7 @@ $:.unshift(File.expand_path('../../lib', __FILE__)) -require 'ensure' +require 'ensure/encoding' module EncodingTestHelpers EXAMPLES = { @@ -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