Skip to content

Instantly share code, notes, and snippets.

@jimweirich
Created August 7, 2010 17:12
Show Gist options
  • Select an option

  • Save jimweirich/512992 to your computer and use it in GitHub Desktop.

Select an option

Save jimweirich/512992 to your computer and use it in GitHub Desktop.
class BlogPost
def initialize(text)
@text = text
end
def word_list
@text.scan(/[a-zA-Z0-9_@-]+/)
end
end
require 'rubygems'
require 'test/unit'
require 'shoulda'
require 'blog_post'
class BlogTest < Test::Unit::TestCase
context "A Blog Post" do
setup { @text = "Some words"}
should("exist") { assert_not_nil post }
context "with a simple word list" do
should("report its word list") do
assert_equal ['Some', 'words'], post.word_list
end
end
context "with a complex word list" do
setup { @text =
"A word list, with: Punctuation. " +
"Why; not? because* [it] (should)."
}
should("report its word list") do
assert_equal(
%w(A word list with Punctuation Why not because it should),
post.word_list)
end
end
context "with a unusual words" do
setup { @text = "a-long_word99" }
should("report its word list") do
assert_equal ['a-long_word99'], post.word_list
end
end
end
private
def post
@post ||= BlogPost.new(@text)
end
end
require 'twitter_trending_words'
class DoesMyBlogPostSuck
def initialize(post)
@post = post
end
def does_this_blog_post_suck?
word_count < 10 ||
word_count > 100 ||
objectionable_word_count >= 3 ||
twitter_word_count == 0
end
private
OBJECTIONABLE_WORDS = ['um', 'like', 'really']
def word_list
@word_list ||= @post.word_list
end
def word_count
@word_count ||= word_list.size
end
def objectionable_word_count
word_list.inject(0) { |c, w| OBJECTIONABLE_WORDS.include?(w) ? c+1 : c }
end
def twitter_word_count
TwitterTrendingWords.list.inject(0) { |c, word|
word_list.include?(word) ? c+1 : c
}
end
end
require 'rubygems'
require 'test/unit'
require 'shoulda'
require 'flexmock/test_unit'
require 'does_my_blog_post_suck'
require 'blog_post'
require 'twitter_trending_words'
class BlogTest < Test::Unit::TestCase
context "A DoesMyBlogPostSuck object" do
setup { flexmock(TwitterTrendingWords).should_receive(:list => ['Ruby', 'Python', 'Clojure']) }
setup { @text =
"This is a blog post that should be ok. " +
"Other than that, it is pretty boring. " +
"Oh, and it should mention the Ruby language."
}
context "with no objectionable words" do
should("be ok") { assert_ok }
end
context "with two objectionable words" do
setup { @text << "this is um ok, really." }
should("be ok") { assert_ok }
end
context "with three objectionable words" do
setup { @text << "this is um like ok, really." }
should("suck") { assert_sucks }
end
context "with fewer than ten words" do
setup { @text = words(9) }
should("suck") { assert_sucks }
end
context "with ten words" do
setup { @text = words(10) }
should("be ok") { assert_ok }
end
context "with 100 words" do
setup { @text = words(100) }
should("be ok") { assert_ok }
end
context "with 101 words" do
setup { @text = words(101) }
should("be too long, so it sucks") { assert_sucks }
end
context "without twitter trending words" do
setup { @text.gsub!(/ruby/i, '') }
should("be irrelevant, so it sucks") { assert_sucks }
end
context "with several defects" do
setup { @text = "um like really" }
should("still suck") { assert_sucks }
end
end
private
def suck_detector
post = BlogPost.new(@text)
@suck_detector ||= DoesMyBlogPostSuck.new(post)
end
def words(n)
(["Ruby"] * n).join(" ")
end
def assert_ok
assert ! suck_detector.does_this_blog_post_suck?
end
def assert_sucks
assert suck_detector.does_this_blog_post_suck?
end
end
#!/usr/bin/env ruby"
require 'rake/clean'
require 'rake/testtask'
task :default => :test
Rake::TestTask.new(:test) do |t|
t.warning = true
t.verbose = false
t.test_files = FileList['*_test.rb']
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment