wordini 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/wordini.rb +9 -2
- data/spec/wordini_spec.rb +65 -32
- metadata +1 -1
data/lib/wordini.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Wordini
|
2
2
|
|
3
3
|
def self.obfuscate(word_string, words_to_remove)
|
4
|
-
|
4
|
+
word_string.split.map{ |word|
|
5
5
|
if words_to_remove.include? word
|
6
6
|
word = "*" * word.split('').count
|
7
7
|
else
|
@@ -10,5 +10,12 @@ class Wordini
|
|
10
10
|
}.join(' ')
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
def self.report(word_string, search_terms)
|
14
|
+
word_set = {}
|
15
|
+
search_terms.each { |word|
|
16
|
+
count = word_string.split.count(word)
|
17
|
+
word_set[word] = count
|
18
|
+
}
|
19
|
+
word_set
|
20
|
+
end
|
14
21
|
end
|
data/spec/wordini_spec.rb
CHANGED
@@ -1,50 +1,83 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Wordini do
|
4
|
-
describe "#
|
4
|
+
describe "#obfuscate" do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
describe "when testing a one word string" do
|
7
|
+
it 'matches the word count' do
|
8
|
+
Wordini.obfuscate("doughnut", ["doughnut"]).split.count.should == 1
|
9
|
+
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
11
|
+
it 'matches the word length' do
|
12
|
+
Wordini.obfuscate("doughnut", ["doughnut"]).length.should == 8
|
13
|
+
end
|
15
14
|
|
16
|
-
it "takes an string or array as the second argument" do
|
17
|
-
Wordini.obfuscate(@word_string, @words_to_remove)
|
18
|
-
@words_to_remove.should be_an_instance_of(Array)
|
19
15
|
end
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
17
|
+
describe "when testing a two word string" do
|
18
|
+
it 'matches the word count' do
|
19
|
+
Wordini.obfuscate("red velvet", ["doughnut"]).split.count.should == 2
|
20
|
+
end
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
it 'matches the word length including spaces' do
|
23
|
+
Wordini.obfuscate("red velvet", ["doughnut"]).length.should == 10
|
24
|
+
end
|
29
25
|
end
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
describe "maintains the integrity of the string" do
|
28
|
+
it 'matches the word count' do
|
29
|
+
Wordini.obfuscate("a b c d e", ["doughnut"]).split.count.should == 5
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'matches the word length including spaces' do
|
33
|
+
Wordini.obfuscate("a b c d e", ["doughnut"]).length.should == 9
|
34
|
+
end
|
35
|
+
|
36
|
+
it "replaces the chosen words with * maintaining correct spacing" do
|
37
|
+
new_string = Wordini.obfuscate("I ate five doughnuts today", ["doughnuts", "ate"])
|
38
|
+
new_string.gsub("*", "").length.should == 14
|
39
|
+
end
|
40
|
+
|
41
|
+
it "contains replaced words with equivalent * count for word_char" do
|
42
|
+
new_string = Wordini.obfuscate("I ate five doughnuts today", ["doughnuts", "ate"])
|
43
|
+
new_string.count('*').should == 12
|
44
|
+
end
|
34
45
|
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#report" do
|
49
|
+
describe "when searching for one word" do
|
50
|
+
it 'returns a hash' do
|
51
|
+
Wordini.report("I ate five doughnuts today", ["five"]).should be_an_instance_of(Hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'returns a key that matches the search term' do
|
55
|
+
word_hash = Wordini.report("I ate five doughnuts today", ["five"])
|
56
|
+
word_hash.keys[0].should == "five"
|
57
|
+
end
|
35
58
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
remaining_char.count.should == 14
|
59
|
+
it 'returns a value that matches the frequency of the word' do
|
60
|
+
word_hash = Wordini.report("I ate five doughnuts today", ["five"])
|
61
|
+
word_hash.values[0].should == 1
|
62
|
+
end
|
41
63
|
end
|
42
64
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
65
|
+
describe "when searching for multiple words" do
|
66
|
+
it 'returns a key that matches the search term' do
|
67
|
+
word_hash = Wordini.report("I ate five doughnuts today", ["five", "doughnuts", "three"])
|
68
|
+
puts word_hash
|
69
|
+
word_hash.keys.should == ["five", "doughnuts", "three"]
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'returns a value that matches the frequency of the word' do
|
73
|
+
word_hash = Wordini.report("I ate five doughnuts today five times", ["five", "doughnuts", "three"])
|
74
|
+
word_hash.values.should == [2, 1, 0]
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'returns a hash with correct key => value pairs' do
|
78
|
+
word_hash = Wordini.report("I ate five doughnuts today five times", ["five", "doughnuts", "three"])
|
79
|
+
word_hash.should == {"five" => 2, "doughnuts" => 1, "three" => 0}
|
80
|
+
end
|
48
81
|
end
|
49
82
|
end
|
50
83
|
end
|