Skip to content

Commit

Permalink
Refactor tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Suschlik committed Jan 3, 2012
1 parent 68d6a42 commit 6c61511
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 87 deletions.
34 changes: 34 additions & 0 deletions test/helper.rb
Expand Up @@ -4,3 +4,37 @@
require 'minitest/libnotify'

require 'libnotify'

module LibnotifyAssertions
def assert_timeout(expected, value, message)
assert_value_set :timeout, expected, value, message
end

def assert_icon_path(expected, value, message)
assert_value_set :icon_path, expected, value, message
end

def assert_value_set(attribute, expected, value, message)
libnotify.send("#{attribute}=", value)
got = libnotify.send(attribute)
case expected
when Regexp
assert_match expected, got, message
else
assert_equal expected, got, message
end
end
end

class LibnotifyTestCase < MiniTest::Spec
include LibnotifyAssertions

class << self
alias :test :it
alias :context :describe
end

def libnotify(options={}, &block)
@libnotify ||= Libnotify::API.new(options, &block)
end
end
155 changes: 68 additions & 87 deletions test/test_libnotify.rb
@@ -1,106 +1,87 @@
require 'helper'

class TestLibnotify < MiniTest::Unit::TestCase
def test_respond_to
assert_respond_to Libnotify, :new
assert_respond_to Libnotify, :show
end

def test_version
assert defined?(Libnotify::VERSION), "version is defined"
assert Libnotify::VERSION
end
end

class TestLibnotifyAPI < MiniTest::Unit::TestCase
def test_without_options_and_block
assert_equal " ", libnotify.summary
assert_equal " ", libnotify.body
assert_equal :normal, libnotify.urgency
assert_nil libnotify.timeout
assert_nil libnotify.icon_path
assert libnotify.append
assert !libnotify.transient
end

def test_with_options_and_block
libnotify(:summary => "hello", :body => "body", :invalid_option => 23) do |n|
n.body = "overwritten"
n.icon_path = "/path/to/icon"
n.append = false
n.transient = true
class TestLibnotify < LibnotifyTestCase
context Libnotify do
test "responds to new" do
assert_respond_to Libnotify, :new
end

assert_equal "hello", libnotify.summary
assert_equal "overwritten", libnotify.body
assert_equal "/path/to/icon", libnotify.icon_path
assert !libnotify.append
assert libnotify.transient
end

def test_timeout_setter
assert_timeout 2500, 2.5, "with float"
assert_timeout 100, 100, "with fixnum ms"
assert_timeout 101, 101, "with fixnum ms"
assert_timeout 1000, 1, "with fixnum seconds"
assert_timeout 99000, 99, "with fixnum seconds"
assert_timeout nil, nil, "with nil"
assert_timeout nil, false, "with false"
assert_timeout 2, :"2 s", "with to_s.to_i"
end

def test_icon_path_setter
Libnotify::API.icon_dirs << File.expand_path("../..", __FILE__)
assert_icon_path "/some/path/image.jpg", "/some/path/image.jpg", "with absolute path"
assert_icon_path "some-invalid-path.jpg", "some-invalid-path.jpg", "with non-existant relative path"
assert_icon_path %r{^/.*/libnotify.png}, "libnotify.png", "with relative path"
assert_icon_path %r{^/.*/libnotify.png}, :"libnotify", "with symbol"
end
test "responds to show" do
assert_respond_to Libnotify, :show
end

def test_update
libnotify(:summary => "hello", :body => "world").show!
libnotify.update(:summary => "hell") do |n|
n.body = "yeah"
test "has version" do
assert defined?(Libnotify::VERSION), "version is defined"
assert Libnotify::VERSION
end
assert_equal "hell", libnotify.summary
assert_equal "yeah", libnotify.body
libnotify.close
end

def test_integration
libnotify = Libnotify::API.new(:timeout => 0.5, :icon_path => :"emblem-favorite", :append => true)
context Libnotify::API do
test "instance default values" do
assert_equal " ", libnotify.summary
assert_equal " ", libnotify.body
assert_equal :normal, libnotify.urgency
assert_nil libnotify.timeout
assert_nil libnotify.icon_path
assert libnotify.append
assert !libnotify.transient
end

[ :low, :normal, :critical ].each do |urgency|
libnotify.summary = "#{RUBY_VERSION} at #{RUBY_PLATFORM}"
libnotify.body = [ urgency, defined?(RUBY_DESCRIPTION) ? RUBY_DESCRIPTION : '?' ].join(" ")
libnotify.urgency = urgency
libnotify.show!
test "instance with options and block" do
libnotify(:summary => "hello", :body => "body", :invalid_option => 23) do |n|
n.body = "overwritten"
n.icon_path = "/path/to/icon"
n.append = false
n.transient = true
end

assert_equal "hello", libnotify.summary
assert_equal "overwritten", libnotify.body
assert_equal "/path/to/icon", libnotify.icon_path
assert !libnotify.append
assert libnotify.transient
end
end

private
test "timeout=" do
assert_timeout 2500, 2.5, "with float"
assert_timeout 100, 100, "with fixnum ms"
assert_timeout 101, 101, "with fixnum ms"
assert_timeout 1000, 1, "with fixnum seconds"
assert_timeout 99000, 99, "with fixnum seconds"
assert_timeout nil, nil, "with nil"
assert_timeout nil, false, "with false"
assert_timeout 2, :"2 s", "with to_s.to_i"
end

def libnotify(options={}, &block)
@libnotify ||= Libnotify::API.new(options, &block)
end
test "icon_path=" do
Libnotify::API.icon_dirs << File.expand_path("../..", __FILE__)
assert_icon_path "/some/path/image.jpg", "/some/path/image.jpg", "with absolute path"
assert_icon_path "some-invalid-path.jpg", "some-invalid-path.jpg", "with non-existant relative path"
assert_icon_path %r{^/.*/libnotify.png}, "libnotify.png", "with relative path"
assert_icon_path %r{^/.*/libnotify.png}, :"libnotify", "with symbol"
end

def assert_timeout(expected, value, message)
assert_value_set :timeout, expected, value, message
test "update" do
libnotify(:summary => "hello", :body => "world").show!
libnotify.update(:summary => "hell") do |n|
n.body = "yeah"
end
assert_equal "hell", libnotify.summary
assert_equal "yeah", libnotify.body
libnotify.close
end
end

def assert_icon_path(expected, value, message)
assert_value_set :icon_path, expected, value, message
end
context "integration" do
test "works" do
libnotify = Libnotify::API.new(:timeout => 0.5, :icon_path => :"emblem-favorite", :append => true)

def assert_value_set(attribute, expected, value, message)
libnotify.send("#{attribute}=", value)
got = libnotify.send(attribute)
case expected
when Regexp
assert_match expected, got, message
else
assert_equal expected, got, message
[ :low, :normal, :critical ].each do |urgency|
libnotify.summary = "#{RUBY_VERSION} at #{RUBY_PLATFORM}"
libnotify.body = [ urgency, defined?(RUBY_DESCRIPTION) ? RUBY_DESCRIPTION : '?' ].join(" ")
libnotify.urgency = urgency
libnotify.show!
end
end
end

end

0 comments on commit 6c61511

Please sign in to comment.