1
0

extractor_spec.rb 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Extractor do
  4. describe 'extract_mentions_or_lists_with_indices' do
  5. it 'returns an empty array if the given string does not have at signs' do
  6. text = 'a string without at signs'
  7. extracted = described_class.extract_mentions_or_lists_with_indices(text)
  8. expect(extracted).to eq []
  9. end
  10. it 'does not extract mentions which ends with particular characters' do
  11. text = '@screen_name@'
  12. extracted = described_class.extract_mentions_or_lists_with_indices(text)
  13. expect(extracted).to eq []
  14. end
  15. it 'returns mentions as an array' do
  16. text = '@screen_name'
  17. extracted = described_class.extract_mentions_or_lists_with_indices(text)
  18. expect(extracted).to eq [
  19. { screen_name: 'screen_name', indices: [0, 12] },
  20. ]
  21. end
  22. it 'yields mentions if a block is given' do
  23. text = '@screen_name'
  24. described_class.extract_mentions_or_lists_with_indices(text) do |screen_name, start_position, end_position|
  25. expect(screen_name).to eq 'screen_name'
  26. expect(start_position).to eq 0
  27. expect(end_position).to eq 12
  28. end
  29. end
  30. end
  31. describe 'extract_hashtags_with_indices' do
  32. it 'returns an empty array if it does not have #' do
  33. text = 'a string without hash sign'
  34. extracted = described_class.extract_hashtags_with_indices(text)
  35. expect(extracted).to eq []
  36. end
  37. it 'does not exclude normal hash text before ://' do
  38. text = '#hashtag://'
  39. extracted = described_class.extract_hashtags_with_indices(text)
  40. expect(extracted).to eq [{ hashtag: 'hashtag', indices: [0, 8] }]
  41. end
  42. it 'excludes http://' do
  43. text = '#hashtaghttp://'
  44. extracted = described_class.extract_hashtags_with_indices(text)
  45. expect(extracted).to eq [{ hashtag: 'hashtag', indices: [0, 8] }]
  46. end
  47. it 'excludes https://' do
  48. text = '#hashtaghttps://'
  49. extracted = described_class.extract_hashtags_with_indices(text)
  50. expect(extracted).to eq [{ hashtag: 'hashtag', indices: [0, 8] }]
  51. end
  52. it 'yields hashtags if a block is given' do
  53. text = '#hashtag'
  54. described_class.extract_hashtags_with_indices(text) do |hashtag, start_position, end_position|
  55. expect(hashtag).to eq 'hashtag'
  56. expect(start_position).to eq 0
  57. expect(end_position).to eq 8
  58. end
  59. end
  60. end
  61. describe 'extract_entities_with_indices' do
  62. it 'returns empty array when cashtag present' do
  63. text = '$cashtag'
  64. extracted = described_class.extract_entities_with_indices(text)
  65. expect(extracted).to eq []
  66. end
  67. end
  68. end