about summary refs log tree commit diff
path: root/spec
diff options
context:
space:
mode:
authorAkihiko Odaki (@fn_aki@pawoo.net) <akihiko.odaki.4i@stu.hosei.ac.jp>2017-06-04 21:57:02 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-06-04 14:57:02 +0200
commit9475fbae78db2aad6b4d4d5ad29a7e724dc7b4bd (patch)
treed048059eec084b0f10245aaa48f2b3845b334d4a /spec
parent00e61d6807acad2a09dfceb1b9441848ffbcbbfd (diff)
Spec Extractor (#3540)
Diffstat (limited to 'spec')
-rw-r--r--spec/lib/extractor_spec.rb79
1 files changed, 79 insertions, 0 deletions
diff --git a/spec/lib/extractor_spec.rb b/spec/lib/extractor_spec.rb
new file mode 100644
index 000000000..dba4bd0bb
--- /dev/null
+++ b/spec/lib/extractor_spec.rb
@@ -0,0 +1,79 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe Extractor do
+  describe 'extract_mentions_or_lists_with_indices' do
+    it 'returns an empty array if the given string does not have at signs' do
+      text = 'a string without at signs'
+      extracted = Extractor.extract_mentions_or_lists_with_indices(text)
+      expect(extracted).to eq []
+    end
+
+    it 'does not extract mentions which ends with particular characters' do
+      text = '@screen_name@'
+      extracted = Extractor.extract_mentions_or_lists_with_indices(text)
+      expect(extracted).to eq []
+    end
+
+    it 'returns mentions as an array' do
+      text = '@screen_name'
+      extracted = Extractor.extract_mentions_or_lists_with_indices(text)
+      expect(extracted).to eq [
+        { screen_name: 'screen_name', indices: [ 0, 12 ] }
+      ]
+    end
+
+    it 'yields mentions if a block is given' do
+      text = '@screen_name'
+      Extractor.extract_mentions_or_lists_with_indices(text) do |screen_name, start_position, end_position|
+        expect(screen_name).to eq 'screen_name'
+        expect(start_position).to eq 0
+        expect(end_position).to eq 12
+      end
+    end
+  end
+
+  describe 'extract_hashtags_with_indices' do
+    it 'returns an empty array if it does not have #' do
+      text = 'a string without hash sign'
+      extracted = Extractor.extract_hashtags_with_indices(text)
+      expect(extracted).to eq []
+    end
+
+    it 'does not exclude normal hash text before ://' do
+      text = '#hashtag://'
+      extracted = Extractor.extract_hashtags_with_indices(text)
+      expect(extracted).to eq [ { hashtag: 'hashtag', indices: [ 0, 8 ] } ]
+    end
+
+    it 'excludes http://' do
+      text = '#hashtaghttp://'
+      extracted = Extractor.extract_hashtags_with_indices(text)
+      expect(extracted).to eq [ { hashtag: 'hashtag', indices: [ 0, 8 ] } ]
+    end
+
+    it 'excludes https://' do
+      text = '#hashtaghttps://'
+      extracted = Extractor.extract_hashtags_with_indices(text)
+      expect(extracted).to eq [ { hashtag: 'hashtag', indices: [ 0, 8 ] } ]
+    end
+
+    it 'yields hashtags if a block is given' do
+      text = '#hashtag'
+      Extractor.extract_hashtags_with_indices(text) do |hashtag, start_position, end_position|
+        expect(hashtag).to eq 'hashtag'
+        expect(start_position).to eq 0
+        expect(end_position).to eq 8
+      end
+    end
+  end
+
+  describe 'extract_cashtags_with_indices' do
+    it 'returns []' do
+      text = '$cashtag'
+      extracted = Extractor.extract_cashtags_with_indices(text)
+      expect(extracted).to eq []
+    end
+  end
+end