|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::I18nLocaleTexts, :config do |
| 4 | + it 'registers an offense when using `validates` with text messages' do |
| 5 | + expect_offense(<<~RUBY) |
| 6 | + validates :email, presence: { message: "must be present" }, |
| 7 | + ^^^^^^^^^^^^^^^^^ Move locale texts to the locale files in the `config/locales` directory. |
| 8 | + format: { with: /@/, message: "not an email" }, |
| 9 | + ^^^^^^^^^^^^^^ Move locale texts to the locale files in the `config/locales` directory. |
| 10 | + length: { maximum: 64 } |
| 11 | + RUBY |
| 12 | + end |
| 13 | + |
| 14 | + it 'does not register an offense when using `validates` with localized messages' do |
| 15 | + expect_no_offenses(<<~RUBY) |
| 16 | + validates :email, presence: { message: :email_missing }, |
| 17 | + format: { with: /@/, message: I18n.t('email_format') }, |
| 18 | + length: { maximum: 64 } |
| 19 | + RUBY |
| 20 | + end |
| 21 | + |
| 22 | + it 'registers an offense when using `redirect_to` with text flash messages' do |
| 23 | + expect_offense(<<~RUBY) |
| 24 | + redirect_to root_path, notice: "Post created!" |
| 25 | + ^^^^^^^^^^^^^^^ Move locale texts to the locale files in the `config/locales` directory. |
| 26 | + RUBY |
| 27 | + end |
| 28 | + |
| 29 | + it 'does not register an offense when using `redirect_to` with localized flash messages' do |
| 30 | + expect_no_offenses(<<~RUBY) |
| 31 | + redirect_to root_path, notice: t(".success") |
| 32 | + RUBY |
| 33 | + end |
| 34 | + |
| 35 | + it 'registers an offense when assigning to `flash` text messages' do |
| 36 | + expect_offense(<<~RUBY) |
| 37 | + flash[:notice] = "Post created!" |
| 38 | + ^^^^^^^^^^^^^^^ Move locale texts to the locale files in the `config/locales` directory. |
| 39 | + RUBY |
| 40 | + end |
| 41 | + |
| 42 | + it 'does not register an offense when assigning to `flash` localized messages' do |
| 43 | + expect_no_offenses(<<~RUBY) |
| 44 | + flash[:notice] = t(".success") |
| 45 | + RUBY |
| 46 | + end |
| 47 | + |
| 48 | + it 'registers an offense when using `mail` with text subject' do |
| 49 | + expect_offense(<<~RUBY) |
| 50 | + mail(to: user.email, subject: "Welcome to My Awesome Site") |
| 51 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Move locale texts to the locale files in the `config/locales` directory. |
| 52 | + RUBY |
| 53 | + end |
| 54 | + |
| 55 | + it 'does not register an offense when using `mail` with localized subject' do |
| 56 | + expect_no_offenses(<<~RUBY) |
| 57 | + mail(to: user.email) |
| 58 | + mail(to: user.email, subject: t("mailers.users.welcome")) |
| 59 | + RUBY |
| 60 | + end |
| 61 | +end |
0 commit comments