Skip to content

Commit

Permalink
fix test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven Fuchs committed Oct 31, 2010
1 parent 03f0572 commit a08f6f3
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 37 deletions.
5 changes: 2 additions & 3 deletions lib/i18n/message/cascade.rb
Expand Up @@ -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)
Expand All @@ -30,4 +29,4 @@ def scope
scopes.compact.join('.')
end
end
end
end
8 changes: 5 additions & 3 deletions lib/i18n/message/version.rb
@@ -1,3 +1,5 @@
class I18n::Message
VERSION = "0.0.1"
end
module I18n
class Message
VERSION = "0.0.1"
end
end
6 changes: 3 additions & 3 deletions test/api/base.rb
Expand Up @@ -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
end
41 changes: 35 additions & 6 deletions 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
end
4 changes: 2 additions & 2 deletions test/api/formatted.rb
Expand Up @@ -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
end
2 changes: 0 additions & 2 deletions test/message/all.rb
@@ -1,5 +1,3 @@
# encoding: utf-8

dir = File.dirname(__FILE__)
$LOAD_PATH.unshift(dir)

Expand Down
3 changes: 1 addition & 2 deletions 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
Expand All @@ -23,4 +22,4 @@ def setup
include Api::Cascade
include Api::Variants
include Api::Formatted
end
end
3 changes: 1 addition & 2 deletions test/message/base_test.rb
@@ -1,4 +1,3 @@
# encoding: utf-8
require File.expand_path('../../test_helper', __FILE__)

class I18nMessageBaseTest < Test::Unit::TestCase
Expand All @@ -12,4 +11,4 @@ def setup

include Api::Base
# include Api::NonFormatted
end
end
10 changes: 6 additions & 4 deletions 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
Expand All @@ -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
Expand All @@ -22,4 +25,3 @@ class Message < I18nMessageCascadeTest::Message

include Api::Variants
end

3 changes: 1 addition & 2 deletions 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
Expand All @@ -13,4 +12,4 @@ class Message < I18n::Message

include Api::Base
include Api::Formatted
end
end
3 changes: 1 addition & 2 deletions 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
Expand All @@ -16,4 +15,4 @@ def setup

include Api::Base
include Api::Gettext
end
end
3 changes: 1 addition & 2 deletions 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
Expand All @@ -8,4 +7,4 @@ class Message < I18n::Message

include Api::Base
include Api::Variants
end
end
16 changes: 12 additions & 4 deletions test/test_helper.rb
Expand Up @@ -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)
Expand Down

0 comments on commit a08f6f3

Please sign in to comment.