From 9842bee45c76c0d06113aac2755029971c9f4ce0 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Sat, 25 Feb 2012 22:01:33 -0600 Subject: [PATCH] first cucumber tests, all pass. NOTE: you need both rspec and cucumber gems (it uses rspec "should" and whatnot) --- Rakefile | 3 +- features/step_definitions/text_mood_steps.rb | 33 ++++++++++++++++++++ features/text_mood.feature | 22 +++++++++++++ features/text_mood_parse.feature | 15 --------- lib/twitter-sentiment/parser/text_mood.rb | 4 +-- 5 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 features/step_definitions/text_mood_steps.rb create mode 100644 features/text_mood.feature delete mode 100644 features/text_mood_parse.feature diff --git a/Rakefile b/Rakefile index ba61e7f..07c3481 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,2 @@ -require 'cucumber/rake/task' \ No newline at end of file +require 'cucumber/rake/task' + diff --git a/features/step_definitions/text_mood_steps.rb b/features/step_definitions/text_mood_steps.rb new file mode 100644 index 0000000..346ca97 --- /dev/null +++ b/features/step_definitions/text_mood_steps.rb @@ -0,0 +1,33 @@ +$:.push File.join(File.dirname(__FILE__),'..','..','lib') +begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end +require 'twitter-sentiment/parser/text_mood' + +Before do + @text_mood = nil +end + +After do +end + +Given /I start an instance initialized with '(.*)'/ do |file| + file = file[1..-1].to_sym if file[0] == ":" # convert to symbol from string if that's what's passed + begin + @text_mood = TwitterSentiment::Parser::TextMood.new(file) + rescue Exception => e + @text_mood = Exception.new + end +end + +When /I ask the score of '(.*)'/ do |word| + @result = @text_mood.score word +end + +Then /the score returned should be '(.*)'/ do |result| + result = result == 'nil' ? nil : result.to_f + + @result.should == result +end + +Then /an exception should be thrown/ do + @text_mood.is_a? Exception +end \ No newline at end of file diff --git a/features/text_mood.feature b/features/text_mood.feature new file mode 100644 index 0000000..4b74d42 --- /dev/null +++ b/features/text_mood.feature @@ -0,0 +1,22 @@ +Feature: Text Mood Parsing + In order to make sure the dictionary bag-of-words with weightings is parsed without mistakes + I am going to pass in a known legal file and request it be parsed and report back a score + + Scenario: Initializing with a symbol + Given I start an instance initialized with ':afinn' + When I ask the score of 'assfucking' + Then the score returned should be '-4' + + Scenario: Initializing with a legit filepath string + Given I start an instance initialized with 'dict/AFINN-111.txt' + When I ask the score of 'assfucking' + Then the score returned should be '-4' + + Scenario: Initialize with a nonexistent filepath string + Given I start an instance initialized with 'dict/thisnameshouldneverexist.diggisthebest.justkidding' + Then an exception should be thrown + + Scenario: Initializing with a legit filepath string + Given I start an instance initialized with 'dict/AFINN-111.txt' + When I ask the score of 'darthvaderinaspeedo' + Then the score returned should be 'nil' \ No newline at end of file diff --git a/features/text_mood_parse.feature b/features/text_mood_parse.feature deleted file mode 100644 index 5befd79..0000000 --- a/features/text_mood_parse.feature +++ /dev/null @@ -1,15 +0,0 @@ -Feature: Text Mood Parsing - In order to make sure the dictionary bag-of-words with weightings is parsed without mistakes - I am going to pass in a known legal file and request it be parsed and report back a score - - Scenario: Initializing with a symbol - Given TextMood is loaded successfully - When I start an instance initialized with ':afinn' - And I ask the score of 'assfucking' - Then the score returned should be '-4' - - Scenario: Initializing with a string - Given TextMood is loaded successfully - When I start an instance initialized with 'dict/AFINN-111.txt' - And I ask the score of 'assfucking' - Then the score returned should be '-4' \ No newline at end of file diff --git a/lib/twitter-sentiment/parser/text_mood.rb b/lib/twitter-sentiment/parser/text_mood.rb index 779c797..357c715 100644 --- a/lib/twitter-sentiment/parser/text_mood.rb +++ b/lib/twitter-sentiment/parser/text_mood.rb @@ -19,14 +19,14 @@ class TextMood def initialize file case file when Symbol - file = @@bags[file] # Convert from symbol to filepath if passed + file = File.join(@@bags_dir, @@bags[file]) # Convert from symbol to filepath if passed when String # Do nuttin', we already have a filepath else raise ArgumentError, "Expected String or Symbol input for file" end @dict = {} - generate_dictionary File.open(File.join(@@bags_dir, file), "r") + generate_dictionary File.open(file, "r") end # Generate Dictionary from file of proper syntax