summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo k <mo@mokhan.ca>2012-05-21 10:18:22 -0600
committermo k <mo@mokhan.ca>2012-05-21 10:18:22 -0600
commit3224af1304c68e8a5033bb668eeed626c3553419 (patch)
tree4558a4b42f33f6559c37bcc9cd1b7c875edcb16b
parentc3e2548a64202ec926a9d10c0604a1c1bced0f5c (diff)
add RockScore specs.
-rw-r--r--app/services/rock_score.rb9
-rw-r--r--app/services/search_engine.rb2
-rw-r--r--config/application.rb2
-rw-r--r--features/search.feature6
-rw-r--r--features/step_definitions/search_steps.rb8
-rw-r--r--spec/foo_spec.rb2
-rw-r--r--spec/services/rock_score_spec.rb25
7 files changed, 52 insertions, 2 deletions
diff --git a/app/services/rock_score.rb b/app/services/rock_score.rb
new file mode 100644
index 0000000..060e9b4
--- /dev/null
+++ b/app/services/rock_score.rb
@@ -0,0 +1,9 @@
+class RockScore
+ NoScore = Class.new
+ def self.for_term(term)
+ positive = SearchEngine.count_results("#{term} rocks").to_f
+ negative = SearchEngine.count_results("#{term} sucks").to_f
+ score = 10 * (positive / (positive + negative))
+ score.nan? ? NoScore : score
+ end
+end
diff --git a/app/services/search_engine.rb b/app/services/search_engine.rb
new file mode 100644
index 0000000..8dcdc2c
--- /dev/null
+++ b/app/services/search_engine.rb
@@ -0,0 +1,2 @@
+class SearchEngine
+end
diff --git a/config/application.rb b/config/application.rb
index 1946cbb..8e2ee72 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -16,7 +16,7 @@ module Rockssucks
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
- # config.autoload_paths += %W(#{config.root}/extras)
+ config.autoload_paths += %W(#{config.root}/app/services)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
diff --git a/features/search.feature b/features/search.feature
new file mode 100644
index 0000000..ec443a4
--- /dev/null
+++ b/features/search.feature
@@ -0,0 +1,6 @@
+Feature: Users can learn how good something is
+
+ Scenario: User compares two terms
+ When I search for Microsoft
+ And I search for Apple
+ Then apple should have a higher score than microsoft
diff --git a/features/step_definitions/search_steps.rb b/features/step_definitions/search_steps.rb
new file mode 100644
index 0000000..d42b3e0
--- /dev/null
+++ b/features/step_definitions/search_steps.rb
@@ -0,0 +1,8 @@
+When /^I search for (.*)$/ do |term|
+ @scores ||= {}
+ @scores[term] = RockScore.for_term(term)
+end
+
+Then /^apple should have a higher score than microsoft$/ do
+ pending
+end
diff --git a/spec/foo_spec.rb b/spec/foo_spec.rb
index ca0457a..a53d6f1 100644
--- a/spec/foo_spec.rb
+++ b/spec/foo_spec.rb
@@ -1,5 +1,5 @@
describe 'foo' do
it 'foo' do
- 2.should == 1
+ 1.should == 1
end
end
diff --git a/spec/services/rock_score_spec.rb b/spec/services/rock_score_spec.rb
new file mode 100644
index 0000000..b83a6f2
--- /dev/null
+++ b/spec/services/rock_score_spec.rb
@@ -0,0 +1,25 @@
+require_relative '../../app/services/rock_score'
+require_relative '../../app/services/search_engine'
+
+describe RockScore do
+ it "returns 0 for unpopular terms" do
+ SearchEngine.stub(:count_results).with("apple rocks") { 0 }
+ SearchEngine.stub(:count_results).with("apple sucks") { 1 }
+ RockScore.for_term("apple").should == 0.0
+ end
+ it "returns 10 for popular terms" do
+ SearchEngine.stub(:count_results).with("apple rocks") { 1 }
+ SearchEngine.stub(:count_results).with("apple sucks") { 0 }
+ RockScore.for_term("apple").should == 10.0
+ end
+ it "returns mediocore results for mediocre terms" do
+ SearchEngine.stub(:count_results).with("apple rocks") { 9 }
+ SearchEngine.stub(:count_results).with("apple sucks") { 11 }
+ RockScore.for_term("apple").should == 4.5
+ end
+ it "does not divide by zero" do
+ SearchEngine.stub(:count_results).with("apple rocks") { 0 }
+ SearchEngine.stub(:count_results).with("apple sucks") { 0 }
+ RockScore.for_term("apple").should == RockScore::NoScore
+ end
+end