From a08f6f3569bcdcae8e1e6ee724b9682ee423e6f2 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Sun, 31 Oct 2010 17:46:01 +0100 Subject: [PATCH] fix test setup --- lib/i18n/message/cascade.rb | 5 ++-- lib/i18n/message/version.rb | 8 +++--- test/api/base.rb | 6 ++--- test/api/cascade.rb | 41 ++++++++++++++++++++++++++----- test/api/formatted.rb | 4 +-- test/message/all.rb | 2 -- test/message/all_features_test.rb | 3 +-- test/message/base_test.rb | 3 +-- test/message/cascade_test.rb | 10 +++++--- test/message/formatted_test.rb | 3 +-- test/message/gettext_test.rb | 3 +-- test/message/variants_test.rb | 3 +-- test/test_helper.rb | 16 +++++++++--- 13 files changed, 70 insertions(+), 37 deletions(-) diff --git a/lib/i18n/message/cascade.rb b/lib/i18n/message/cascade.rb index 51a7b6f..a764e08 100644 --- a/lib/i18n/message/cascade.rb +++ b/lib/i18n/message/cascade.rb @@ -19,8 +19,7 @@ def options end def scope - scopes = self.class.cascade_options[:scopes] - scopes = [super] + scopes.map do |scope| + scopes = [super] + Array(self.class.cascade_options[:scopes]).map do |scope| if options[scope] value = options[scope] value = value.class.name unless value.is_a?(String) || value.is_a?(Symbol) @@ -30,4 +29,4 @@ def scope scopes.compact.join('.') end end -end \ No newline at end of file +end diff --git a/lib/i18n/message/version.rb b/lib/i18n/message/version.rb index 62cc88a..55f6b14 100644 --- a/lib/i18n/message/version.rb +++ b/lib/i18n/message/version.rb @@ -1,3 +1,5 @@ -class I18n::Message - VERSION = "0.0.1" -end \ No newline at end of file +module I18n + class Message + VERSION = "0.0.1" + end +end diff --git a/test/api/base.rb b/test/api/base.rb index 183ce88..412b8fb 100644 --- a/test/api/base.rb +++ b/test/api/base.rb @@ -38,14 +38,14 @@ module Base # INTERPOLATION test "subject is a Symbol, translation is a Message, interpolates a variable" do - store_translations(:foo => '{{foo}}') + store_translations(:foo => '%{foo}') assert_equal 'FOO', message(:foo, :foo => 'FOO').to_s end test "subject evaluates to a Symbol, translation is a Message, interpolates a variable" do - store_translations(:foo => '{{foo}}') + store_translations(:foo => '%{foo}') assert_equal 'FOO', message(Proc.new { :foo }, :foo => 'FOO').to_s end end -end \ No newline at end of file +end diff --git a/test/api/cascade.rb b/test/api/cascade.rb index 4db259c..c9f5b22 100644 --- a/test/api/cascade.rb +++ b/test/api/cascade.rb @@ -1,18 +1,47 @@ module Api module Cascade - test "returns translation from attribute scope" do - store_translations(:'models.model.attributes.attribute.message' => 'message') + test "validation messages: returns translation from attribute scope" do + cascade_options :step => 2, :skip_root => false, :scopes => [:model, :attribute] + store_translations(:models => { :model => { :attributes => { :attribute => { :message => 'message' } } } }) assert_equal 'message', message(:message, :model => 'model', :attribute => 'attribute').to_s end - test "returns translation from model scope" do - store_translations(:'models.model.message' => 'message') + test "validation messages: returns translation from model scope" do + cascade_options :step => 2, :skip_root => false, :scopes => [:model, :attribute] + store_translations(:models => { :model => { :message => 'message' } }) assert_equal 'message', message(:message, :model => 'model', :attribute => 'attribute').to_s end - test "returns translation from errors scope" do + test "validation messages: returns translation from errors scope" do + cascade_options :step => 2, :skip_root => false, :scopes => [:model, :attribute] store_translations(:message => 'message') assert_equal 'message', message(:message, :model => 'model', :attribute => 'attribute').to_s end + + test 'view helper: returns the :edit translation from the view path scope admin.sites.menu' do + cascade_options :step => 1, :skip_root => false, :offset => 2 + store_translations(:admin => { :sites => { :menu => { :edit => 'edit' } } }) + assert_equal 'edit', message(:edit, :scope => 'admin.sites.menu').to_s + end + + test 'view helper: returns the :edit translation from the view path scope admin.menu' do + cascade_options :step => 1, :skip_root => false, :offset => 2 + store_translations(:admin => { :menu => { :edit => 'edit' } }) + assert_equal 'edit', message(:edit, :scope => 'admin.sites.menu').to_s + end + + test 'view helper: returns the :edit translation from the view path scope menu' do + cascade_options :step => 1, :skip_root => false, :offset => 2 + store_translations(:menu => { :edit => 'edit' }) + assert_equal 'edit', message(:edit, :scope => 'admin.sites.menu').to_s + end + + # currently not supported by I18n::Cascade because of the way :offset is used + # + # test 'view helper: returns the :edit translation from the root scope' do + # cascade_options :step => 1, :skip_root => false, :offset => 2 + # store_translations(:edit => 'edit') + # assert_equal 'edit', message(:edit, :scope => 'admin.sites.menu').to_s + # end end -end \ No newline at end of file +end diff --git a/test/api/formatted.rb b/test/api/formatted.rb index 886ec57..ce377d9 100644 --- a/test/api/formatted.rb +++ b/test/api/formatted.rb @@ -14,8 +14,8 @@ module Formatted end test "uses a translated format to wrap a message" do - store_translations(:blank => 'blank', :'formats.full' => 'full %{message}') + store_translations(:blank => 'blank', :formats => { :full => 'full %{message}' }) assert_equal "full blank", message(:blank).to_s(:full) end end -end \ No newline at end of file +end diff --git a/test/message/all.rb b/test/message/all.rb index e846143..de050c4 100644 --- a/test/message/all.rb +++ b/test/message/all.rb @@ -1,5 +1,3 @@ -# encoding: utf-8 - dir = File.dirname(__FILE__) $LOAD_PATH.unshift(dir) diff --git a/test/message/all_features_test.rb b/test/message/all_features_test.rb index 3e7c98b..8d165a3 100644 --- a/test/message/all_features_test.rb +++ b/test/message/all_features_test.rb @@ -1,4 +1,3 @@ -# encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../test_helper') class I18nMessageAllFeaturesTest < Test::Unit::TestCase @@ -23,4 +22,4 @@ def setup include Api::Cascade include Api::Variants include Api::Formatted -end \ No newline at end of file +end diff --git a/test/message/base_test.rb b/test/message/base_test.rb index 4bac2c4..73a7bf1 100644 --- a/test/message/base_test.rb +++ b/test/message/base_test.rb @@ -1,4 +1,3 @@ -# encoding: utf-8 require File.expand_path('../../test_helper', __FILE__) class I18nMessageBaseTest < Test::Unit::TestCase @@ -12,4 +11,4 @@ def setup include Api::Base # include Api::NonFormatted -end \ No newline at end of file +end diff --git a/test/message/cascade_test.rb b/test/message/cascade_test.rb index 6eee035..8e55deb 100644 --- a/test/message/cascade_test.rb +++ b/test/message/cascade_test.rb @@ -1,4 +1,3 @@ -# encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../test_helper') class I18nMessageCascadeTest < Test::Unit::TestCase @@ -8,9 +7,13 @@ class Message < I18n::Message def setup I18n.backend = CascadingBackend.new - Message.cascade_options = { :step => 2, :skip_root => false, :scopes => [:model, :attribute] } end - + + def teardown + cascade_options :step => 2, :skip_root => true + super + end + include Api::Base include Api::Cascade end @@ -22,4 +25,3 @@ class Message < I18nMessageCascadeTest::Message include Api::Variants end - diff --git a/test/message/formatted_test.rb b/test/message/formatted_test.rb index 201a7b0..e91d280 100644 --- a/test/message/formatted_test.rb +++ b/test/message/formatted_test.rb @@ -1,4 +1,3 @@ -# encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../test_helper') class I18nMessageFormattedTest < Test::Unit::TestCase @@ -13,4 +12,4 @@ class Message < I18n::Message include Api::Base include Api::Formatted -end \ No newline at end of file +end diff --git a/test/message/gettext_test.rb b/test/message/gettext_test.rb index 4a393ac..99080a5 100644 --- a/test/message/gettext_test.rb +++ b/test/message/gettext_test.rb @@ -1,4 +1,3 @@ -# encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../test_helper') class I18nMessageGettextTest < Test::Unit::TestCase @@ -16,4 +15,4 @@ def setup include Api::Base include Api::Gettext -end \ No newline at end of file +end diff --git a/test/message/variants_test.rb b/test/message/variants_test.rb index e16378a..443beb3 100644 --- a/test/message/variants_test.rb +++ b/test/message/variants_test.rb @@ -1,4 +1,3 @@ -# encoding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../test_helper') class I18nMessageVariantsTest < Test::Unit::TestCase @@ -8,4 +7,4 @@ class Message < I18n::Message include Api::Base include Api::Variants -end \ No newline at end of file +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 6c96411..2c1ea5d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,18 +3,26 @@ require 'rubygems' require 'test/unit' -require 'test_case_declarative' +require 'test_declarative' require 'api' require 'i18n/message' class Test::Unit::TestCase def teardown - I18n.backend = nil + I18n.backend = nil end - + + def message_class + self.class.const_get(:Message) + end + + def cascade_options(options) + message_class.cascade_options = options + end + def message(*args) - self.class.const_get(:Message).new(*args) + message_class.new(*args) end def store_translations(data)