Add CustomFilterKeyword#to_regex
method (#28893)
This commit is contained in:
parent
a69506a434
commit
6b6586f5d0
3 changed files with 52 additions and 10 deletions
35
spec/models/custom_filter_keyword_spec.rb
Normal file
35
spec/models/custom_filter_keyword_spec.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe CustomFilterKeyword do
|
||||
describe '#to_regex' do
|
||||
context 'when whole_word is true' do
|
||||
it 'builds a regex with boundaries and the keyword' do
|
||||
keyword = described_class.new(whole_word: true, keyword: 'test')
|
||||
|
||||
expect(keyword.to_regex).to eq(/(?mix:\b#{Regexp.escape(keyword.keyword)}\b)/)
|
||||
end
|
||||
|
||||
it 'builds a regex with starting boundary and the keyword when end with non-word' do
|
||||
keyword = described_class.new(whole_word: true, keyword: 'test#')
|
||||
|
||||
expect(keyword.to_regex).to eq(/(?mix:\btest\#)/)
|
||||
end
|
||||
|
||||
it 'builds a regex with end boundary and the keyword when start with non-word' do
|
||||
keyword = described_class.new(whole_word: true, keyword: '#test')
|
||||
|
||||
expect(keyword.to_regex).to eq(/(?mix:\#test\b)/)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when whole_word is false' do
|
||||
it 'builds a regex with the keyword' do
|
||||
keyword = described_class.new(whole_word: false, keyword: 'test')
|
||||
|
||||
expect(keyword.to_regex).to eq(/test/i)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue