ndd-rspec-rails 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -2
- data/README.md +1 -1
- data/bin/yard-server +9 -0
- data/lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb +100 -0
- data/lib/ndd/rspec/rails/matchers/controller.rb +52 -0
- data/lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb +2 -27
- data/lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb +13 -47
- data/lib/ndd/rspec/rails/matchers/model/have_a_translated_model.rb +2 -27
- data/lib/ndd/rspec/rails/matchers/model.rb +94 -0
- data/lib/ndd/rspec/rails/matchers/translation_matcher.rb +57 -0
- data/lib/ndd/rspec/rails/matchers.rb +13 -0
- data/lib/ndd/rspec/rails/version.rb +1 -1
- data/lib/ndd/rspec/rails.rb +10 -0
- metadata +6 -3
- data/lib/ndd/rspec/rails/matchers/model/translation_matcher.rb +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb94dd2da1098be2a2f3d48c83368ff5f1602926
|
4
|
+
data.tar.gz: df64d93cc0963f6e481a63825354cfd3560d83a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d662d5a3ebb2700794504f5fc15b8c24fee69e0a6e3ad929926df404e595affa908b8129ffaf4be74352a048714187d09274d599da7b8365898f3e09cb1f8c7
|
7
|
+
data.tar.gz: 405873989113b8c9ea54ded246968ff8433359a3252c8066c1c9ed926d38169e239457c00e81ede37e1736723df75797fc3aaaa34e0788d570db897fd33318c8
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
|
-
# NDD RSpec Rails
|
1
|
+
# NDD RSpec Rails changelog
|
2
|
+
|
3
|
+
## Version 0.2.0
|
4
|
+
|
5
|
+
- add `have_a_translated_flash` matcher
|
2
6
|
|
3
7
|
## Version 0.1.0
|
4
8
|
|
5
|
-
- initial commit
|
9
|
+
- initial commit with matchers:
|
10
|
+
- `have_a_translated_attribute`
|
11
|
+
- `have_a_translated_error`
|
12
|
+
- `have_a_translated_model`
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# NDD RSpec Rails
|
2
2
|
|
3
3
|
[](http://travis-ci.org/ddidier/ndd-rspec-rails)
|
4
4
|
[](https://gemnasium.com/ddidier/ndd-rspec-rails)
|
data/bin/yard-server
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'ndd/rspec/rails/matchers/translation_matcher'
|
2
|
+
|
3
|
+
module Ndd
|
4
|
+
module RSpec
|
5
|
+
module Rails
|
6
|
+
module Matchers
|
7
|
+
module Controller
|
8
|
+
|
9
|
+
# Implements {#have_a_translated_flash}.
|
10
|
+
class HaveATranslatedFlash < TranslationMatcher
|
11
|
+
|
12
|
+
# @param message [String|Symbol] the message to test.
|
13
|
+
def initialize(message)
|
14
|
+
super()
|
15
|
+
@message = message
|
16
|
+
end
|
17
|
+
|
18
|
+
# Set the action of the message to test.
|
19
|
+
# @param action [String|Symbol] the action associated to the message to test.
|
20
|
+
# @return self
|
21
|
+
def on_action(action)
|
22
|
+
@action = action
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
# @param controller [Object] the controller being tested.
|
27
|
+
# @return [Boolean] true if the message has an associated translation, false otherwise.
|
28
|
+
def matches?(controller)
|
29
|
+
@controller = controller
|
30
|
+
@failed_locales = []
|
31
|
+
@tested_locales.each do |tested_locale|
|
32
|
+
@failed_locales << tested_locale unless translated_in?(tested_locale)
|
33
|
+
end
|
34
|
+
@failed_locales.empty?
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [String] a description of this matcher.
|
38
|
+
def description
|
39
|
+
description = "have a translated flash message for '#{@message}'"
|
40
|
+
description << " on '#{@action}'" if @action.present?
|
41
|
+
description << " in #{locales_as_string(@tested_locales)}"
|
42
|
+
description
|
43
|
+
end
|
44
|
+
|
45
|
+
# @return [String] details about the failure of this matcher.
|
46
|
+
def failure_message
|
47
|
+
message = "expected '#{subject_as_string}' to have a translated flash message for '#{@message}'\n"
|
48
|
+
message << "but none of the following keys was found:\n"
|
49
|
+
message << "#{translation_keys.map { |l| " - #{l}" }.join("\n")}\n"
|
50
|
+
message << "for the locales: #{locales_as_string(@failed_locales)}"
|
51
|
+
message
|
52
|
+
end
|
53
|
+
|
54
|
+
# -------------------- private
|
55
|
+
private
|
56
|
+
|
57
|
+
# @return [String] a human readable string of the subject of the error.
|
58
|
+
def subject_as_string
|
59
|
+
@action.present? ? "#{@controller.class}##{@action}" : @controller.class.to_s
|
60
|
+
end
|
61
|
+
|
62
|
+
def translation_keys
|
63
|
+
@action.present? ? translation_keys_with_action : translation_keys_without_action
|
64
|
+
end
|
65
|
+
|
66
|
+
def translation_keys_with_action
|
67
|
+
controller_key = @controller.class.name.underscore
|
68
|
+
message_key = @message.to_s
|
69
|
+
action_key = @action.to_s
|
70
|
+
%W[
|
71
|
+
actioncontroller.#{controller_key}.#{action_key}.flash.#{message_key}
|
72
|
+
actioncontroller.#{controller_key}.flash.#{message_key}
|
73
|
+
actioncontroller.#{action_key}.flash.#{message_key}
|
74
|
+
actioncontroller.flash.#{message_key}
|
75
|
+
]
|
76
|
+
end
|
77
|
+
|
78
|
+
def translation_keys_without_action
|
79
|
+
controller_key = @controller.class.name.underscore
|
80
|
+
message_key = @message.to_s
|
81
|
+
%W[
|
82
|
+
actioncontroller.#{controller_key}.flash.#{message_key}
|
83
|
+
actioncontroller.flash.#{message_key}
|
84
|
+
]
|
85
|
+
end
|
86
|
+
|
87
|
+
def translated_in?(tested_locale)
|
88
|
+
translation_keys.each do |translation_key|
|
89
|
+
return true if translated?(tested_locale, translation_key)
|
90
|
+
end
|
91
|
+
false
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require_relative 'controller/have_a_translated_flash'
|
2
|
+
|
3
|
+
module Ndd
|
4
|
+
module RSpec
|
5
|
+
module Rails
|
6
|
+
module Matchers
|
7
|
+
# RSpec matchers for Rails controllers.
|
8
|
+
module Controller
|
9
|
+
|
10
|
+
# Ensure that a controller has a translated flash message for the given key.
|
11
|
+
#
|
12
|
+
# More precisely, ensure that (with all parameters being snake case) one of
|
13
|
+
# I18n.t(locale, "actioncontroller.#{controller_key}.#{action_key}.flash.#{message_key}")
|
14
|
+
# I18n.t(locale, "actioncontroller.#{controller_key}.flash.#{message_key}")
|
15
|
+
# I18n.t(locale, "actioncontroller.#{action_key}.flash.#{message_key}")
|
16
|
+
# I18n.t(locale, "actioncontroller.flash.#{message_key}")
|
17
|
+
# returns a value for the default locale (i.e. +I18n.default_locale+)
|
18
|
+
# or all the available locales (i.e. +I18n.available_locales+).
|
19
|
+
#
|
20
|
+
# @example
|
21
|
+
#
|
22
|
+
# RSpec.describe MyController, type: :controller do
|
23
|
+
#
|
24
|
+
# # both are equivalent
|
25
|
+
# it { is_expected.to have_a_translated_flash(:out_of_range) }
|
26
|
+
# it { is_expected.to have_a_translated_flash(:out_of_range).in_available_locales }
|
27
|
+
#
|
28
|
+
# it { is_expected.to have_a_translated_flash(:out_of_range).in_default_locale }
|
29
|
+
#
|
30
|
+
# # both are equivalent
|
31
|
+
# it { is_expected.to have_a_translated_flash(:out_of_range).on_action(:index) }
|
32
|
+
# it { is_expected.to have_a_translated_flash(:out_of_range).on_action(:index).in_available_locales }
|
33
|
+
#
|
34
|
+
# it { is_expected.to have_a_translated_flash(:out_of_range).on_action(:index).in_default_locale }
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# @param message [String|Symbol] the message to test.
|
38
|
+
# @return [Ndd::RSpec::Rails::Matchers::Controller::HaveATranslatedFlash]
|
39
|
+
#
|
40
|
+
def have_a_translated_flash(message) # rubocop:disable Style/PredicateName
|
41
|
+
HaveATranslatedFlash.new(message)
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
RSpec.configure do |config|
|
51
|
+
config.include Ndd::RSpec::Rails::Matchers::Controller, type: :controller
|
52
|
+
end
|
@@ -1,37 +1,12 @@
|
|
1
|
-
require '
|
2
|
-
require 'i18n'
|
3
|
-
require 'ndd/rspec/rails/matchers/model/translation_matcher'
|
1
|
+
require 'ndd/rspec/rails/matchers/translation_matcher'
|
4
2
|
|
5
3
|
module Ndd
|
6
4
|
module RSpec
|
7
5
|
module Rails
|
8
6
|
module Matchers
|
9
|
-
module Model
|
7
|
+
module Model
|
10
8
|
|
11
|
-
# Ensure that an attribute has an associated translation.
|
12
|
-
#
|
13
|
-
# More precisely, ensure that
|
14
|
-
# I18n.t(locale, "activerecord.attributes.{snake_case_class_name}.{snake_case_attribute_name}")
|
15
|
-
# returns a value for the default locale (i.e. +I18n.default_locale+)
|
16
|
-
# or all the available locales (i.e. +I18n.available_locales+).
|
17
|
-
#
|
18
|
-
# For example:
|
19
|
-
#
|
20
|
-
# RSpec.describe MyModel, type: :model do
|
21
|
-
# # both are equivalent
|
22
|
-
# it { is_expected.to have_a_translated_attribute(:comment) }
|
23
|
-
# it { is_expected.to have_a_translated_attribute(:comment).in_available_locales }
|
24
|
-
#
|
25
|
-
# it { is_expected.to have_a_translated_attribute(:comment).in_default_locale }
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
def have_a_translated_attribute(attribute) # rubocop:disable Style/PredicateName
|
29
|
-
HaveATranslatedAttribute.new(attribute)
|
30
|
-
end
|
31
|
-
|
32
|
-
# ------------------------------------------------------------------------------------------------------------
|
33
9
|
# Implements {#have_a_translated_attribute}.
|
34
|
-
#
|
35
10
|
class HaveATranslatedAttribute < TranslationMatcher
|
36
11
|
|
37
12
|
# @param attribute [String|Symbol] the attribute to test.
|
@@ -1,47 +1,12 @@
|
|
1
|
-
require '
|
2
|
-
require 'i18n'
|
3
|
-
require 'ndd/rspec/rails/matchers/model/translation_matcher'
|
1
|
+
require 'ndd/rspec/rails/matchers/translation_matcher'
|
4
2
|
|
5
3
|
module Ndd
|
6
4
|
module RSpec
|
7
5
|
module Rails
|
8
6
|
module Matchers
|
9
|
-
module Model
|
7
|
+
module Model
|
10
8
|
|
11
|
-
# Ensure that an error on a model or an attribute has an associated translation.
|
12
|
-
#
|
13
|
-
# More precisely, ensure that (with all parameters being snake case) one of
|
14
|
-
# I18n.t(locale, "activerecord.errors.models.{class_name}.attributes.{attribute_name}.{error_key}")
|
15
|
-
# I18n.t(locale, "activerecord.errors.models.{class_name}.{error_key}")
|
16
|
-
# I18n.t(locale, "activerecord.errors.messages.{error_key}")
|
17
|
-
# I18n.t(locale, "errors.attributes.{attribute_name}.{error_key}")
|
18
|
-
# I18n.t(locale, "errors.messages.{error_key}")
|
19
|
-
# returns a value for the default locale (i.e. +I18n.default_locale+)
|
20
|
-
# or all the available locales (i.e. +I18n.available_locales+).
|
21
|
-
#
|
22
|
-
# For example:
|
23
|
-
#
|
24
|
-
# RSpec.describe MyModel, type: :model do
|
25
|
-
# # both are equivalent
|
26
|
-
# it { is_expected.to have_a_translated_error(:duplicate) }
|
27
|
-
# it { is_expected.to have_a_translated_error(:duplicate).in_available_locales }
|
28
|
-
#
|
29
|
-
# it { is_expected.to have_a_translated_error(:duplicate).in_default_locale }
|
30
|
-
#
|
31
|
-
# # both are equivalent
|
32
|
-
# it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments) }
|
33
|
-
# it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_available_locales }
|
34
|
-
#
|
35
|
-
# it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_default_locale }
|
36
|
-
# end
|
37
|
-
#
|
38
|
-
def have_a_translated_error(message) # rubocop:disable Style/PredicateName
|
39
|
-
HaveATranslatedError.new(message)
|
40
|
-
end
|
41
|
-
|
42
|
-
# ------------------------------------------------------------------------------------------------------------
|
43
9
|
# Implements {#have_a_translated_error}.
|
44
|
-
#
|
45
10
|
class HaveATranslatedError < TranslationMatcher
|
46
11
|
|
47
12
|
# @param error [String|Symbol] the error to test.
|
@@ -51,6 +16,7 @@ module Ndd
|
|
51
16
|
end
|
52
17
|
|
53
18
|
# Set the attribute of the error to test.
|
19
|
+
# @param attribute [String|Symbol] the attribute associated to the error to test.
|
54
20
|
# @return self
|
55
21
|
def on_attribute(attribute)
|
56
22
|
@attribute = attribute
|
@@ -99,24 +65,24 @@ module Ndd
|
|
99
65
|
|
100
66
|
def translation_keys_with_attribute
|
101
67
|
model_key = @model.class.name.underscore
|
102
|
-
|
68
|
+
error_key = @error.to_s
|
103
69
|
attribute_key = @attribute.to_s
|
104
70
|
%W[
|
105
|
-
activerecord.errors.models.#{model_key}.attributes.#{attribute_key}.#{
|
106
|
-
activerecord.errors.models.#{model_key}.#{
|
107
|
-
activerecord.errors.messages.#{
|
108
|
-
errors.attributes.#{attribute_key}.#{
|
109
|
-
errors.messages.#{
|
71
|
+
activerecord.errors.models.#{model_key}.attributes.#{attribute_key}.#{error_key}
|
72
|
+
activerecord.errors.models.#{model_key}.#{error_key}
|
73
|
+
activerecord.errors.messages.#{error_key}
|
74
|
+
errors.attributes.#{attribute_key}.#{error_key}
|
75
|
+
errors.messages.#{error_key}
|
110
76
|
]
|
111
77
|
end
|
112
78
|
|
113
79
|
def translation_keys_without_attribute
|
114
80
|
model_key = @model.class.name.underscore
|
115
|
-
|
81
|
+
error_key = @error.to_s
|
116
82
|
%W[
|
117
|
-
activerecord.errors.models.#{model_key}.#{
|
118
|
-
activerecord.errors.messages.#{
|
119
|
-
errors.messages.#{
|
83
|
+
activerecord.errors.models.#{model_key}.#{error_key}
|
84
|
+
activerecord.errors.messages.#{error_key}
|
85
|
+
errors.messages.#{error_key}
|
120
86
|
]
|
121
87
|
end
|
122
88
|
|
@@ -1,37 +1,12 @@
|
|
1
|
-
require '
|
2
|
-
require 'i18n'
|
3
|
-
require 'ndd/rspec/rails/matchers/model/translation_matcher'
|
1
|
+
require 'ndd/rspec/rails/matchers/translation_matcher'
|
4
2
|
|
5
3
|
module Ndd
|
6
4
|
module RSpec
|
7
5
|
module Rails
|
8
6
|
module Matchers
|
9
|
-
module Model
|
7
|
+
module Model
|
10
8
|
|
11
|
-
# Ensure that a model has an associated translation.
|
12
|
-
#
|
13
|
-
# More precisely, ensure that
|
14
|
-
# I18n.t(locale, "activerecord.models.{snake_case_class_name}")
|
15
|
-
# returns a value for the default locale (i.e. +I18n.default_locale+)
|
16
|
-
# or all the available locales (i.e. +I18n.available_locales+).
|
17
|
-
#
|
18
|
-
# For example:
|
19
|
-
#
|
20
|
-
# RSpec.describe MyModel, type: :model do
|
21
|
-
# # both are equivalent
|
22
|
-
# it { is_expected.to have_a_translated_model }
|
23
|
-
# it { is_expected.to have_a_translated_model.in_available_locales }
|
24
|
-
#
|
25
|
-
# it { is_expected.to have_a_translated_model.in_default_locale }
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
def have_a_translated_model # rubocop:disable Style/PredicateName
|
29
|
-
HaveATranslatedModel.new
|
30
|
-
end
|
31
|
-
|
32
|
-
# ------------------------------------------------------------------------------------------------------------
|
33
9
|
# Implements {#have_a_translated_model}.
|
34
|
-
#
|
35
10
|
class HaveATranslatedModel < TranslationMatcher
|
36
11
|
|
37
12
|
# @param model [Object] the model being tested.
|
@@ -2,6 +2,100 @@ require_relative 'model/have_a_translated_attribute'
|
|
2
2
|
require_relative 'model/have_a_translated_error'
|
3
3
|
require_relative 'model/have_a_translated_model'
|
4
4
|
|
5
|
+
module Ndd
|
6
|
+
module RSpec
|
7
|
+
module Rails
|
8
|
+
module Matchers
|
9
|
+
# RSpec matchers for Rails models.
|
10
|
+
module Model
|
11
|
+
|
12
|
+
# Ensure that an attribute has an associated translation.
|
13
|
+
#
|
14
|
+
# More precisely, ensure that
|
15
|
+
# I18n.t(locale, "activerecord.attributes.{snake_case_class_name}.{snake_case_attribute_name}")
|
16
|
+
# returns a value for the default locale (i.e. +I18n.default_locale+)
|
17
|
+
# or all the available locales (i.e. +I18n.available_locales+).
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
#
|
21
|
+
# RSpec.describe MyModel, type: :model do
|
22
|
+
# # both are equivalent
|
23
|
+
# it { is_expected.to have_a_translated_attribute(:comment) }
|
24
|
+
# it { is_expected.to have_a_translated_attribute(:comment).in_available_locales }
|
25
|
+
#
|
26
|
+
# it { is_expected.to have_a_translated_attribute(:comment).in_default_locale }
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# @param attribute [String|Symbol] the attribute to test.
|
30
|
+
# @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedAttribute]
|
31
|
+
#
|
32
|
+
def have_a_translated_attribute(attribute) # rubocop:disable Style/PredicateName
|
33
|
+
HaveATranslatedAttribute.new(attribute)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Ensure that an error on a model or an attribute has an associated translation.
|
37
|
+
#
|
38
|
+
# More precisely, ensure that (with all parameters being snake case) one of
|
39
|
+
# I18n.t(locale, "activerecord.errors.models.{class_name}.attributes.{attribute_name}.{error_key}")
|
40
|
+
# I18n.t(locale, "activerecord.errors.models.{class_name}.{error_key}")
|
41
|
+
# I18n.t(locale, "activerecord.errors.messages.{error_key}")
|
42
|
+
# I18n.t(locale, "errors.attributes.{attribute_name}.{error_key}")
|
43
|
+
# I18n.t(locale, "errors.messages.{error_key}")
|
44
|
+
# returns a value for the default locale (i.e. +I18n.default_locale+)
|
45
|
+
# or all the available locales (i.e. +I18n.available_locales+).
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
#
|
49
|
+
# RSpec.describe MyModel, type: :model do
|
50
|
+
# # both are equivalent
|
51
|
+
# it { is_expected.to have_a_translated_error(:duplicate) }
|
52
|
+
# it { is_expected.to have_a_translated_error(:duplicate).in_available_locales }
|
53
|
+
#
|
54
|
+
# it { is_expected.to have_a_translated_error(:duplicate).in_default_locale }
|
55
|
+
#
|
56
|
+
# # both are equivalent
|
57
|
+
# it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments) }
|
58
|
+
# it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_available_locales }
|
59
|
+
#
|
60
|
+
# it { is_expected.to have_a_translated_error(:duplicate).on_attribute(:comments).in_default_locale }
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# @param error [String|Symbol] the error to test.
|
64
|
+
# @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedError]
|
65
|
+
#
|
66
|
+
def have_a_translated_error(error) # rubocop:disable Style/PredicateName
|
67
|
+
HaveATranslatedError.new(error)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Ensure that a model has an associated translation.
|
71
|
+
#
|
72
|
+
# More precisely, ensure that
|
73
|
+
# I18n.t(locale, "activerecord.models.{snake_case_class_name}")
|
74
|
+
# returns a value for the default locale (i.e. +I18n.default_locale+)
|
75
|
+
# or all the available locales (i.e. +I18n.available_locales+).
|
76
|
+
#
|
77
|
+
# @example
|
78
|
+
#
|
79
|
+
# RSpec.describe MyModel, type: :model do
|
80
|
+
# # both are equivalent
|
81
|
+
# it { is_expected.to have_a_translated_model }
|
82
|
+
# it { is_expected.to have_a_translated_model.in_available_locales }
|
83
|
+
#
|
84
|
+
# it { is_expected.to have_a_translated_model.in_default_locale }
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# @return [Ndd::RSpec::Rails::Matchers::Model::HaveATranslatedModel]
|
88
|
+
#
|
89
|
+
def have_a_translated_model # rubocop:disable Style/PredicateName
|
90
|
+
HaveATranslatedModel.new
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
5
99
|
RSpec.configure do |config|
|
6
100
|
config.include Ndd::RSpec::Rails::Matchers::Model, type: :model
|
7
101
|
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
require 'i18n'
|
3
|
+
|
4
|
+
module Ndd
|
5
|
+
module RSpec
|
6
|
+
module Rails
|
7
|
+
module Matchers
|
8
|
+
|
9
|
+
# Base class for matchers dealing with translation.
|
10
|
+
class TranslationMatcher
|
11
|
+
|
12
|
+
# Set the locales to test to +I18n.available_locales+.
|
13
|
+
def initialize
|
14
|
+
@tested_locales = I18n.available_locales
|
15
|
+
end
|
16
|
+
|
17
|
+
# Set the locales to test to all the available locales (i.e. +I18n.available_locales+).
|
18
|
+
# @return self
|
19
|
+
def in_available_locales
|
20
|
+
@tested_locales = I18n.available_locales
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set the locales to test to the default locale (i.e. +I18n.default_locale+) only.
|
25
|
+
# @return self
|
26
|
+
def in_default_locale
|
27
|
+
@tested_locales = [I18n.default_locale]
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
# -------------------------------------------------------------------------------------------- private -----
|
32
|
+
private
|
33
|
+
|
34
|
+
# Convert an array of locales to a human readable list, i.e. ':en, :fr, :jp'.
|
35
|
+
# @param locales [Array<String|Symbol>] the locales to convert.
|
36
|
+
# @return [String] the converted locales.
|
37
|
+
def locales_as_string(locales)
|
38
|
+
locales.map { |locale| ":#{locale}" }.join(', ')
|
39
|
+
end
|
40
|
+
|
41
|
+
# Check that a translation exists for the given key in the given locale.
|
42
|
+
# @param locale [Symbol] the locale of the translation to lookup.
|
43
|
+
# @param key [String] the key of the translation to lookup.
|
44
|
+
# @return [Boolean] true if a translation exists for the given key in the given locale, false otherwise.
|
45
|
+
def translated?(locale, key)
|
46
|
+
I18n.with_locale(locale) { I18n.t(key, raise: true) }
|
47
|
+
return true
|
48
|
+
rescue I18n::MissingTranslationData
|
49
|
+
return false
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/lib/ndd/rspec/rails.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ndd-rspec-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David DIDIER
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -205,13 +205,16 @@ files:
|
|
205
205
|
- bin/console
|
206
206
|
- bin/rubocop
|
207
207
|
- bin/setup
|
208
|
+
- bin/yard-server
|
208
209
|
- lib/ndd/rspec/rails.rb
|
209
210
|
- lib/ndd/rspec/rails/matchers.rb
|
211
|
+
- lib/ndd/rspec/rails/matchers/controller.rb
|
212
|
+
- lib/ndd/rspec/rails/matchers/controller/have_a_translated_flash.rb
|
210
213
|
- lib/ndd/rspec/rails/matchers/model.rb
|
211
214
|
- lib/ndd/rspec/rails/matchers/model/have_a_translated_attribute.rb
|
212
215
|
- lib/ndd/rspec/rails/matchers/model/have_a_translated_error.rb
|
213
216
|
- lib/ndd/rspec/rails/matchers/model/have_a_translated_model.rb
|
214
|
-
- lib/ndd/rspec/rails/matchers/
|
217
|
+
- lib/ndd/rspec/rails/matchers/translation_matcher.rb
|
215
218
|
- lib/ndd/rspec/rails/version.rb
|
216
219
|
homepage: http://github.com/ddidier/ndd-rspec-rails
|
217
220
|
licenses:
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/string/inflections'
|
2
|
-
require 'i18n'
|
3
|
-
|
4
|
-
module Ndd
|
5
|
-
module RSpec
|
6
|
-
module Rails
|
7
|
-
module Matchers
|
8
|
-
module Model #:nodoc:
|
9
|
-
|
10
|
-
# Base class for matchers dealing with translation.
|
11
|
-
class TranslationMatcher
|
12
|
-
|
13
|
-
# Set the locales to test to +I18n.available_locales+.
|
14
|
-
def initialize
|
15
|
-
@tested_locales = I18n.available_locales
|
16
|
-
end
|
17
|
-
|
18
|
-
# Set the locales to test to all the available locales (i.e. +I18n.available_locales+).
|
19
|
-
# @return self
|
20
|
-
def in_available_locales
|
21
|
-
@tested_locales = I18n.available_locales
|
22
|
-
self
|
23
|
-
end
|
24
|
-
|
25
|
-
# Set the locales to test to the default locale (i.e. +I18n.default_locale+) only.
|
26
|
-
# @return self
|
27
|
-
def in_default_locale
|
28
|
-
@tested_locales = [I18n.default_locale]
|
29
|
-
self
|
30
|
-
end
|
31
|
-
|
32
|
-
# -------------------------------------------------------------------------------------------- private -----
|
33
|
-
private
|
34
|
-
|
35
|
-
# Convert an array of locales to a human readable list, i.e. ':en, :fr, :jp'.
|
36
|
-
# @param locales [Array<String|Symbol>] the locales to convert.
|
37
|
-
# @return [String] the converted locales.
|
38
|
-
def locales_as_string(locales)
|
39
|
-
locales.map { |locale| ":#{locale}" }.join(', ')
|
40
|
-
end
|
41
|
-
|
42
|
-
# Check that a translation exists for the given key in the given locale.
|
43
|
-
# @param locale [Symbol] the locale of the translation to lookup.
|
44
|
-
# @param key [String] the key of the translation to lookup.
|
45
|
-
# @return [Boolean] true if a translation exists for the given key in the given locale, false otherwise.
|
46
|
-
def translated?(locale, key)
|
47
|
-
I18n.with_locale(locale) { I18n.t(key, raise: true) }
|
48
|
-
return true
|
49
|
-
rescue I18n::MissingTranslationData
|
50
|
-
return false
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|