Skip to content
Browse files

Merge pull request #3 from EtienneDepaulis/adding_test_suit

Adding test suit
  • Loading branch information...
2 parents 2304368 + 3415e59 commit 7922fc158dbebfd1df99efffb9cf5ce522cb2dc0 @kballenegger committed
Showing with 120 additions and 0 deletions.
  1. +1 −0 .rspec
  2. +5 −0 Gemfile
  3. +15 −0 Gemfile.lock
  4. +7 −0 Rakefile
  5. +3 −0 hue.gemspec
  6. +1 −0 lib/hue.rb
  7. +82 −0 spec/models/hue_spec.rb
  8. +6 −0 spec/spec_helper.rb
View
1 .rspec
@@ -0,0 +1 @@
+--colour
View
5 Gemfile
@@ -7,3 +7,8 @@ gem 'color'
gem 'upnp', path: './upnp'
+group :test do
+ gem 'rspec'
+ gem 'webmock'
+end
+
View
15 Gemfile.lock
@@ -18,7 +18,9 @@ GEM
builder (3.1.4)
color (1.4.1)
cookiejar (0.3.0)
+ crack (0.3.1)
curb (0.8.3)
+ diff-lcs (1.1.3)
em-http-request (1.0.3)
addressable (>= 2.2.3)
cookiejar
@@ -37,6 +39,14 @@ GEM
nokogiri (1.5.5)
nori (1.1.3)
rack (1.4.1)
+ rspec (2.12.0)
+ rspec-core (~> 2.12.0)
+ rspec-expectations (~> 2.12.0)
+ rspec-mocks (~> 2.12.0)
+ rspec-core (2.12.2)
+ rspec-expectations (2.12.1)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.12.1)
savon (1.2.0)
akami (~> 1.2.0)
builder (>= 2.1.2)
@@ -48,6 +58,9 @@ GEM
wasabi (2.5.1)
httpi (~> 1.0)
nokogiri (>= 1.4.0)
+ webmock (1.4.0)
+ addressable (>= 2.2.2)
+ crack (>= 0.1.7)
PLATFORMS
ruby
@@ -56,4 +69,6 @@ DEPENDENCIES
color
curb
nokogiri
+ rspec
upnp!
+ webmock
View
7 Rakefile
@@ -1 +1,8 @@
require 'bundler/gem_tasks'
+#
+# The rspec tasks
+#
+require 'rspec/core'
+require 'rspec/core/rake_task'
+task :default => :spec
+RSpec::Core::RakeTask.new(:spec)
View
3 hue.gemspec
@@ -16,4 +16,7 @@ Gem::Specification.new do |gem|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ['lib']
+
+ gem.add_development_dependency "rspec"
+ gem.add_development_dependency "webmock"
end
View
1 lib/hue.rb
@@ -13,6 +13,7 @@ module Hue
#
class Hue
+
# Hue.discover_ip is a convenience class method that will scan the network
# and attempt to find the Philips base station. It may take ~5s to execute.
#
View
82 spec/models/hue_spec.rb
@@ -0,0 +1,82 @@
+require 'spec_helper'
+
+require 'hue'
+
+describe Hue::Hue do
+ before(:each) do
+ success_json = [{"success"=>{"/lights"=>true}}].to_json
+ stub_request(:any, /^http:\/\/(\d+\.?){4}\/.*/).to_return(:body => success_json)
+
+ Hue::Hue.stub(:discover_ip).and_return("192.168.0.1")
+ end
+
+ it "should call :discover_ip" do
+ Hue::Hue.should_receive(:discover_ip)
+ Hue::Hue.new
+ end
+
+ it "should not call :discover_ip if :ip is provided" do
+ Hue::Hue.should_not_receive(:discover_ip)
+ Hue::Hue.new(ip: "192.168.0.2")
+ end
+
+ it "should call :hexdigest" do
+ Digest::SHA1.should_receive(:hexdigest)
+ Hue::Hue.new
+ end
+
+ it "should not call :hexdigest if :username is provided" do
+ Digest::SHA1.should_not_receive(:hexdigest)
+ Hue::Hue.new(username: "some_random_hex")
+ end
+
+ describe "when requesting the hue API" do
+
+ let(:hue) { Hue::Hue.new }
+
+ it "polls light" do
+ lights_json = {"lights" => {}}.to_json
+ stub_request(:get, /^http:\/\/(\d+\.?){4}\/.*/).to_return(:body => lights_json)
+
+ hue.poll_state
+
+ a_request(:get, /.*\/$/).should have_been_made
+ end
+
+ it "raises an error if :lights params is not present" do
+ expect {
+ hue.poll_state
+ }.to raise_error
+ end
+
+ it "switching on" do
+ hue.on(1)
+
+ a_request(:put, /.*lights\/\d\/state.*/).with(:body => {"on" => true}.to_json).should have_been_made
+ end
+
+ it "switching off" do
+ hue.off(1)
+
+ a_request(:put, /.*lights\/\d\/state.*/).with(:body => {"on" => false}.to_json).should have_been_made
+ end
+
+ it "setting a color" do
+ hue.set_color(1, Color::RGB::Red)
+
+ a_request(:put, /.*lights\/\d\/state.*/).with(:body => {"bri" => 127, "sat" => 255, "hue" => 0}.to_json).should have_been_made
+ end
+
+ it "setting a bright color" do
+ hue.set_bright_color(1, Color::RGB::Red)
+
+ a_request(:put, /.*lights\/\d\/state.*/).with(:body => {"bri" => 255, "sat" => 255, "hue" => 0}.to_json).should have_been_made
+ end
+
+ it "setting a color" do
+ hue.set_color(1, Color::RGB::Red, {"on" => true})
+
+ a_request(:put, /.*lights\/\d\/state.*/).with(:body => {"on" => true, "bri" => 127, "sat" => 255, "hue" => 0}.to_json).should have_been_made
+ end
+ end
+end
View
6 spec/spec_helper.rb
@@ -0,0 +1,6 @@
+require 'curb'
+require 'webmock/rspec'
+
+RSpec.configure do |config|
+ config.include WebMock::API
+end

0 comments on commit 7922fc1

Please sign in to comment.
Something went wrong with that request. Please try again.