Permalink
Browse files

Zonebie can cough up a random timezone too

  • Loading branch information...
1 parent 69c2914 commit 0abf8c31ce6b069eebaa6f6915e7b18f61c36133 @alindeman alindeman committed Mar 5, 2012
Showing with 38 additions and 14 deletions.
  1. +15 −11 README.md
  2. +7 −3 lib/zonebie.rb
  3. +16 −0 spec/lib/zonebie_spec.rb
View
26 README.md
@@ -13,20 +13,16 @@ Tests must run green against:
* JRuby 1.6 (1.8 and 1.9 mode)
* Rubinius 1.2 and 2.0 (1.8 and 1.9 mode)
-A gem that adds timezone support; currently supported is:
-
* `activesupport` >= 2.3 (compatible with Rails 2.3, 3.0, 3.1, 3.2)
-OR
-
-* **Unimplemented** `tzinfo`
-
## Installation
If using Bundler (recommended), add to Gemfile:
gem 'zonebie'
+## Usage with Rails & ActiveSupport
+
### Test::Unit & Minitest
Add to `test/test_helper.rb`:
@@ -38,19 +34,27 @@ Add to `test/test_helper.rb`:
Add to `spec/spec_helper.rb` after `require "rspec"` or `require
"rspec/rails"`:
- require "zonebie/rspec"
+ Zonebie.set_random_timezone
### Cucumber
Add a file `features/support/zonebie.rb` with the following contents:
Zonebie.set_random_timezone
-## Usage
+## Other Usage
+
+If you simply need a random timezone for some other part of your tests,
+Zonebie can help as well.
+
+ # e.g. with Rails
+ zone = ActiveSupport::TimeZone[Zonebie.random_timezone]
+ puts zone.now
+
+## Reproducing Bugs
-When `Zonebie.set_random_timezone` is called (if using RSpec, this call is
-automatically setup for you), Zonebie assigns a timezone and prints a message
-to STDOUT:
+When `Zonebie.set_random_timezone` is called, Zonebie assigns a timezone and
+prints a message to STDOUT:
[Zonebie] Setting timezone to "Eastern Time (US & Canada)"
View
10 lib/zonebie.rb
@@ -33,11 +33,15 @@ def add_backend(backend)
end
def set_random_timezone
- zones = backend.zones
- zone = zones[rand(zones.length)]
+ zone = random_timezone
$stdout.puts("[Zonebie] Setting timezone to \"#{zone}\"") unless quiet
- @backend.zone = zones[rand(zones.length)]
+ @backend.zone = zone
+ end
+
+ def random_timezone
+ zones = backend.zones
+ zones[rand(zones.length)]
end
end
end
View
16 spec/lib/zonebie_spec.rb
@@ -62,4 +62,20 @@
end
end
end
+
+ describe "#random_timezone" do
+ let(:backend) {
+ stub_everything(:name => :my_awesome_backend,
+ :zones => ["Eastern Time (US & Canada)"])
+ }
+
+ before do
+ Zonebie.add_backend(backend)
+ Zonebie.backend = :my_awesome_backend
+ end
+
+ it "returns a random timezone" do
+ Zonebie.random_timezone.should == "Eastern Time (US & Canada)"
+ end
+ end
end

0 comments on commit 0abf8c3

Please sign in to comment.