Permalink
Browse files

Initial commit of work of Test::Unit to RSpec

  • Loading branch information...
David Czarnecki
David Czarnecki committed Apr 5, 2012
1 parent b289d99 commit 4308c8b7784ef139eba48f91e2f7c3b88c1d00e3
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format nested
View
@@ -1,23 +1,23 @@
-require 'bundler/gem_tasks'
-require 'rake'
-require 'rake/testtask'
-
-Rake::TestTask.new(:test) do |test|
- test.libs << 'lib' << 'test'
- test.pattern = 'test/**/test_*.rb'
- test.verbose = true
- # test.warning = true
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rspec_opts = ['--backtrace']
+ # spec.ruby_opts = ['-w']
end
-REDIS_DIR = File.expand_path(File.join("..", "test"), __FILE__)
+REDIS_DIR = File.expand_path(File.join("..", "spec"), __FILE__)
REDIS_CNF = File.join(REDIS_DIR, "test.conf")
REDIS_PID = File.join(REDIS_DIR, "db", "redis.pid")
REDIS_LOCATION = ENV['REDIS_LOCATION']
task :default => :run
desc "Run tests and manage server start/stop"
-task :run => [:start, :test, :stop]
+task :run => [:start, :spec, :stop]
desc "Start the Redis server"
task :start do
@@ -46,6 +46,6 @@ end
task :test_rubies do
Rake::Task['start'].execute
- system "rvm 1.8.7@leaderboard_gem,1.9.2@leaderboard_gem,1.9.3@leaderboard_gem do rake test"
+ system "rvm 1.8.7@leaderboard_gem,1.9.2@leaderboard_gem,1.9.3@leaderboard_gem do rake spec"
Rake::Task['stop'].execute
end
View
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
if '1.8.7'.eql?(RUBY_VERSION)
s.add_development_dependency('SystemTimer')
end
+ s.add_development_dependency('rspec')
end
File renamed without changes.
View
@@ -0,0 +1,67 @@
+require 'spec_helper'
+
+describe Leaderboard do
+ it 'should be initialized with defaults' do
+ @leaderboard.leaderboard_name.should == 'name'
+ @leaderboard.page_size.should == Leaderboard::DEFAULT_PAGE_SIZE
+ end
+
+ it 'should be able to disconnect its connection to Redis' do
+ @leaderboard.disconnect.should be_nil
+ end
+
+ it 'should automatically reconnect to Redis after a disconnect' do
+ @leaderboard.total_members.should be(0)
+ rank_members_in_leaderboard(5)
+ @leaderboard.total_members.should be(5)
+ @leaderboard.disconnect.should be_nil
+ @leaderboard.total_members.should be(5)
+ end
+
+ it 'should set the page size to the default page size if passed an invalid value' do
+ some_leaderboard = Leaderboard.new('name', {:page_size => 0})
+
+ some_leaderboard.page_size.should be(Leaderboard::DEFAULT_PAGE_SIZE)
+ some_leaderboard.disconnect
+ end
+
+ it 'should allow you to delete a leaderboard' do
+ rank_members_in_leaderboard
+
+ @redis_connection.exists('name').should be_true
+ @leaderboard.delete_leaderboard
+ @redis_connection.exists('name').should be_false
+ end
+
+ it 'should allow you to pass in an existing redis connection in the initializer' do
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_OPTIONS, {:redis_connection => @redis_connection})
+ rank_members_in_leaderboard
+
+ @redis_connection.info["connected_clients"].to_i.should be(1)
+ end
+
+ it 'should allow you to rank a member and see that reflected in total members' do
+ @leaderboard.rank_member('member', 1)
+
+ @leaderboard.total_members.should be(1)
+ end
+
+ it 'should return the correct number of members in a given score range' do
+ rank_members_in_leaderboard(5)
+
+ @leaderboard.total_members_in_score_range(2, 4).should be(3)
+ end
+
+ it 'should return the correct rank when calling rank_for' do
+ rank_members_in_leaderboard(5)
+
+ @leaderboard.rank_for('member_4').should be(2)
+ @leaderboard.rank_for('member_4', true).should be(1)
+ end
+
+ it 'should return the correct score when calling score_for' do
+ rank_members_in_leaderboard(5)
+
+ @leaderboard.score_for('member_4').should == 4
+ end
+end
View
@@ -0,0 +1,23 @@
+require 'rspec'
+require 'leaderboard'
+
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+RSpec.configure do |config|
+ def rank_members_in_leaderboard(members_to_add = 5)
+ 1.upto(members_to_add) do |index|
+ @leaderboard.rank_member("member_#{index}", index)
+ end
+ end
+
+ config.before(:each) do
+ @redis_connection = Redis.new(:host => "127.0.0.1")
+ @leaderboard = Leaderboard.new('name', Leaderboard::DEFAULT_LEADERBOARD_REQUEST_OPTIONS, :host => "127.0.0.1")
+ end
+
+ config.after(:each) do
+ @redis_connection.flushdb
+ @leaderboard.disconnect
+ @redis_connection.client.disconnect
+ end
+end
@@ -1,4 +1,4 @@
-dir ./test/db
+dir ./spec/db
pidfile ./redis.pid
port 6379
timeout 300
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,7 @@
+require 'spec_helper'
+
+describe 'Leaderboard::VERSION' do
+ it 'should be the correct version' do
+ Leaderboard::VERSION.should == '2.0.5'
+ end
+end
View
@@ -1,19 +0,0 @@
-require 'rubygems'
-require 'test/unit'
-
-require 'leaderboard'
-
-class LeaderboardTest < Test::Unit::TestCase
-
- def test_version
- assert_equal '2.0.5', Leaderboard::VERSION
- end
-
- private
-
- def rank_members_in_leaderboard(members_to_add = 5)
- 1.upto(members_to_add) do |index|
- @leaderboard.rank_member("member_#{index}", index)
- end
- end
-end

0 comments on commit 4308c8b

Please sign in to comment.