Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

client tests

  • Loading branch information...
commit f32248aa41e304dd3446c91707c41e5768635f90 1 parent 28c9f81
@txus authored
Showing with 147 additions and 39 deletions.
  1. +26 −9 lib/domodoro/client.rb
  2. +121 −30 test/domodoro/client_test.rb
View
35 lib/domodoro/client.rb
@@ -1,10 +1,13 @@
+require 'notify'
+
module Domodoro
class Client
class << self
def start(host, port)
- EM.run do
+ Config.load
+ EM.run do
EM.connect(host, port) do |c|
c.extend EM::P::LineText2
def c.receive_line(line)
@@ -13,27 +16,41 @@ def c.receive_line(line)
Client.work
when /stop/
Client.break
- end
end
end
end
-
end
+
end
def work
- puts "WORK!!!!!"
- fork do
- system('afplay alarm.mp3 && afplay alarm.mp3')
+ EM.defer do
+ if Config.visual
+ Notify.notify "Domodoro", "Time to work!"
+ end
+ if Config.sound
+ system("afplay #{path_to('start.wav')}")
+ end
end
end
def break
- puts "BREAK!!!!!"
- fork do
- system('afplay alarm.mp3 && afplay alarm.mp3')
+ EM.defer do
+ if Config.visual
+ Notify.notify "Domodoro", "Take a 5 min. break."
+ end
+ if Config.sound
+ system("afplay #{path_to('stop.wav')} && afplay #{path_to('stop.mp3')}")
+ end
end
end
+ private
+
+ def path_to(asset)
+ File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'assets', asset))
+ end
+
+ end
end
end
View
151 test/domodoro/client_test.rb
@@ -2,68 +2,159 @@
module ExampleServer; end
-module Received
- attr_accessor :work, :break
- extend self
-end
-
module Domodoro
describe Client do
include EM::MiniTest::Spec
- before do
- Received.break = false
- Received.work = false
- end
-
- it 'calls .work when receives :start', :timeout => 0.2 do
+ it 'calls .work when receives :start', :timeout => 0.3 do
EM.start_server '127.0.0.1', 12345, ExampleServer do |conn|
conn.send_data ":start\n"
end
- Client.start '127.0.0.1', 12345
+ Client.expects(:work)
- def Client.work
- Received.work = true
- end
+ Client.start '127.0.0.1', 12345
- EM.add_timer(0.1) do
- assert Received.work
+ EM.add_timer(0.2) do
+ mocha_verify
done!
end
wait!
end
- it 'calls .break when receives :stop', :timeout => 0.2 do
- EM.run do
+ it 'calls .break when receives :stop', :timeout => 0.3 do
EM.start_server '127.0.0.1', 12345, ExampleServer do |conn|
conn.send_data ":stop\n"
end
- Client.start '127.0.0.1', 12345
+ Client.expects(:break)
- def Client.break
- Received.break = true
- end
+ Client.start '127.0.0.1', 12345
- EM.add_timer(0.1) do
- assert Received.break
+ EM.add_timer(0.2) do
+ mocha_verify
done!
end
wait!
- end
end
describe '.work' do
- it 'plays a sound if sound is activated'
- it 'displays a growl if growl is activated'
+ describe 'if sound is activated' do
+ it 'plays a sound', :timeout => 0.2 do
+ Config.stubs(:sound).returns true
+ Client.expects(:system)
+ Client.work
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+ end
+
+ describe 'otherwise' do
+ it 'does not play a sound', :timeout => 0.2 do
+ Config.stubs(:sound).returns false
+ Client.expects(:system).never
+ Client.work
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+ end
+
+ describe 'if visual is activated' do
+ it 'displays a visual notification', :timeout => 0.2 do
+ Config.stubs(:visual).returns true
+ Notify.expects(:notify)
+ Client.work
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+ end
+
+ describe 'otherwise' do
+ it 'does not display any visual notification', :timeout => 0.2 do
+ Config.stubs(:visual).returns false
+ Notify.expects(:notify).never
+ Client.work
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+ end
end
describe '.break' do
- it 'plays a sound if sound is activated'
- it 'displays a growl if growl is activated'
+ describe 'if sound is activated' do
+ it 'plays a sound', :timeout => 0.2 do
+ Config.stubs(:sound).returns true
+ Client.expects(:system)
+ Client.break
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+
+ end
+
+ describe 'otherwise' do
+ it 'does not play a sound', :timeout => 0.2 do
+ Config.stubs(:sound).returns false
+ Client.expects(:system).never
+ Client.break
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+ end
+
+ describe 'if visual is activated' do
+ it 'displays a visual notification', :timeout => 0.2 do
+ Config.stubs(:visual).returns true
+ Notify.expects(:notify)
+ Client.break
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+ end
+
+ describe 'otherwise' do
+ it 'does not display any visual notification', :timeout => 0.2 do
+ Config.stubs(:visual?).returns false
+ Notify.expects(:notify).never
+ Client.break
+
+ EM.add_timer(0.1) do
+ mocha_verify
+ done!
+ end
+ wait!
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.