custom_filter_keyword_spec.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe CustomFilterKeyword do
  4. describe '#to_regex' do
  5. context 'when whole_word is true' do
  6. it 'builds a regex with boundaries and the keyword' do
  7. keyword = described_class.new(whole_word: true, keyword: 'test')
  8. expect(keyword.to_regex).to eq(/(?mix:\b#{Regexp.escape(keyword.keyword)}\b)/)
  9. end
  10. it 'builds a regex with starting boundary and the keyword when end with non-word' do
  11. keyword = described_class.new(whole_word: true, keyword: 'test#')
  12. expect(keyword.to_regex).to eq(/(?mix:\btest\#)/)
  13. end
  14. it 'builds a regex with end boundary and the keyword when start with non-word' do
  15. keyword = described_class.new(whole_word: true, keyword: '#test')
  16. expect(keyword.to_regex).to eq(/(?mix:\#test\b)/)
  17. end
  18. end
  19. context 'when whole_word is false' do
  20. it 'builds a regex with the keyword' do
  21. keyword = described_class.new(whole_word: false, keyword: 'test')
  22. expect(keyword.to_regex).to eq(/test/i)
  23. end
  24. end
  25. end
  26. end