diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-04-20 23:50:22 +0000 |
commit | 9dc93a4519d9d5d7be48ff274127136236a3adb3 (patch) | |
tree | 70467ae3692a0e35e5ea56bcb803eb512a10bedb /spec/rubocop/cop/style | |
parent | 4b0f34b6d759d6299322b3a54453e930c6121ff0 (diff) | |
download | gitlab-ce-9dc93a4519d9d5d7be48ff274127136236a3adb3.tar.gz |
Add latest changes from gitlab-org/gitlab@13-11-stable-eev13.11.0-rc43
Diffstat (limited to 'spec/rubocop/cop/style')
-rw-r--r-- | spec/rubocop/cop/style/regexp_literal_mixed_preserve_spec.rb | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/spec/rubocop/cop/style/regexp_literal_mixed_preserve_spec.rb b/spec/rubocop/cop/style/regexp_literal_mixed_preserve_spec.rb new file mode 100644 index 00000000000..384a834a512 --- /dev/null +++ b/spec/rubocop/cop/style/regexp_literal_mixed_preserve_spec.rb @@ -0,0 +1,131 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' + +require_relative '../../../../rubocop/cop/style/regexp_literal_mixed_preserve' + +# This spec contains only relevant examples. +# +# See also https://github.com/rubocop/rubocop/pull/9688 +RSpec.describe RuboCop::Cop::Style::RegexpLiteralMixedPreserve, :config do + let(:config) do + supported_styles = { 'SupportedStyles' => %w[slashes percent_r mixed mixed_preserve] } + RuboCop::Config.new('Style/PercentLiteralDelimiters' => + percent_literal_delimiters_config, + 'Style/RegexpLiteralMixedPreserve' => + cop_config.merge(supported_styles)) + end + + let(:percent_literal_delimiters_config) { { 'PreferredDelimiters' => { '%r' => '{}' } } } + + context 'when EnforcedStyle is set to mixed_preserve' do + let(:cop_config) { { 'EnforcedStyle' => 'mixed_preserve' } } + + describe 'a single-line `//` regex without slashes' do + it 'is accepted' do + expect_no_offenses('foo = /a/') + end + end + + describe 'a single-line `//` regex with slashes' do + it 'registers an offense and corrects' do + expect_offense(<<~'RUBY') + foo = /home\// + ^^^^^^^^ Use `%r` around regular expression. + RUBY + + expect_correction(<<~'RUBY') + foo = %r{home/} + RUBY + end + + describe 'when configured to allow inner slashes' do + before do + cop_config['AllowInnerSlashes'] = true + end + + it 'is accepted' do + expect_no_offenses('foo = /home\\//') + end + end + end + + describe 'a multi-line `//` regex without slashes' do + it 'is accepted' do + expect_no_offenses(<<~'RUBY') + foo = / + foo + bar + /x + RUBY + end + end + + describe 'a multi-line `//` regex with slashes' do + it 'registers an offense and corrects' do + expect_offense(<<~'RUBY') + foo = / + ^ Use `%r` around regular expression. + https?:\/\/ + example\.com + /x + RUBY + + expect_correction(<<~'RUBY') + foo = %r{ + https?:// + example\.com + }x + RUBY + end + end + + describe 'a single-line %r regex without slashes' do + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{a} + RUBY + end + end + + describe 'a single-line %r regex with slashes' do + it 'is accepted' do + expect_no_offenses('foo = %r{home/}') + end + + describe 'when configured to allow inner slashes' do + before do + cop_config['AllowInnerSlashes'] = true + end + + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{home/} + RUBY + end + end + end + + describe 'a multi-line %r regex without slashes' do + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{ + foo + bar + }x + RUBY + end + end + + describe 'a multi-line %r regex with slashes' do + it 'is accepted' do + expect_no_offenses(<<~RUBY) + foo = %r{ + https?:// + example\.com + }x + RUBY + end + end + end +end |