perennial 0.2.2.2 → 1.0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,129 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class DispatchableTest < Test::Unit::TestCase
4
-
5
- class ExampleDispatcher
6
- include Perennial::Dispatchable
7
- end
8
-
9
- class ExampleHandlerA
10
-
11
- attr_accessor :messages
12
-
13
- def initialize
14
- @messages = []
15
- end
16
-
17
- def handle(name, opts)
18
- @messages << [name, opts]
19
- end
20
-
21
- end
22
-
23
- class ExampleHandlerB < ExampleHandlerA; end
24
-
25
- context 'marking a class as dispatchable' do
26
-
27
- setup do
28
- @dispatcher = ExampleDispatcher.new
29
- end
30
-
31
- should 'define a dispatch method' do
32
- assert @dispatcher.respond_to?(:dispatch)
33
- end
34
-
35
- should 'require atleast a name for dispatch' do
36
- assert_equal -2, @dispatcher.method(:dispatch).arity
37
- end
38
-
39
- end
40
-
41
- context 'when registering handlers' do
42
-
43
- setup do
44
- @dispatcher = test_class_for(Perennial::Dispatchable)
45
- end
46
-
47
- should 'append a handler using register_handler' do
48
- assert_equal [], @dispatcher.handlers
49
- @dispatcher.register_handler(handler = ExampleHandlerA.new)
50
- assert_equal [handler], @dispatcher.handlers
51
- end
52
-
53
- should 'batch assign handlers on handlers= using register_handler' do
54
- handlers = [ExampleHandlerA.new, ExampleHandlerB.new]
55
- assert_equal [], @dispatcher.handlers
56
- @dispatcher.handlers = handlers
57
- assert_equal handlers, @dispatcher.handlers
58
- end
59
-
60
- should 'return all handlers via the handlers class method' do
61
- handlers = [ExampleHandlerA.new, ExampleHandlerB.new]
62
- @dispatcher.handlers = handlers
63
- assert_equal handlers, @dispatcher.handlers
64
- end
65
-
66
- should 'make handlers available to myself and all subclasses' do
67
- # Set A
68
- dispatcher_a = class_via(@dispatcher)
69
- dispatcher_a.register_handler(handler_a = ExampleHandlerA.new)
70
- # Set B
71
- dispatcher_b = class_via(dispatcher_a)
72
- dispatcher_b.register_handler(handler_b = ExampleHandlerA.new)
73
- # Set C
74
- dispatcher_c = class_via(dispatcher_b)
75
- dispatcher_c.register_handler(handler_c = ExampleHandlerA.new)
76
- # Set D
77
- dispatcher_d = class_via(dispatcher_a)
78
- dispatcher_d.register_handler(handler_d = ExampleHandlerB.new)
79
- # Actual Assertions
80
- assert_equal [], @dispatcher.handlers
81
- assert_equal [handler_a], dispatcher_a.handlers
82
- assert_equal [handler_a, handler_b], dispatcher_b.handlers
83
- assert_equal [handler_a, handler_b, handler_c], dispatcher_c.handlers
84
- assert_equal [handler_a, handler_d], dispatcher_d.handlers
85
- end
86
-
87
- end
88
-
89
- context 'dispatching events' do
90
-
91
- setup do
92
- @dispatcher = class_via(ExampleDispatcher).new
93
- @handler = ExampleHandlerA.new
94
- @dispatcher.class.register_handler @handler
95
- end
96
-
97
- should 'attempt to call handle_[event_name] on itself' do
98
- mock(@dispatcher).respond_to?(:handle_sample_event) { true }
99
- mock(@dispatcher).handle_sample_event(:awesome => true, :sauce => 2)
100
- @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
101
- end
102
-
103
- should 'attempt to call handle_[event_name] on each handler' do
104
- mock(@handler).respond_to?(:handle_sample_event) { true }
105
- mock(@handler).handle_sample_event(:awesome => true, :sauce => 2)
106
- @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
107
- end
108
-
109
- should 'call handle on each handler if handle_[event_name] isn\'t defined' do
110
- mock(@handler).respond_to?(:handle_sample_event) { false }
111
- mock(@handler).handle(:sample_event, :awesome => true, :sauce => 2)
112
- @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
113
- end
114
-
115
- should 'let you halt handler processing if you raise HaltHandlerProcessing' do
116
- handler_two = ExampleHandlerB.new
117
- @dispatcher.class.register_handler handler_two
118
- mock(@handler).handle(:sample_event, :awesome => true, :sauce => 2) do
119
- raise Perennial::HaltHandlerProcessing
120
- end
121
- dont_allow(handler_two).handle(:sample_event, :awesome => true, :sauce => 2)
122
- @dispatcher.dispatch :sample_event, :awesome => true, :sauce => 2
123
- end
124
-
125
- should 'log exceptions when encountered and not crash'
126
-
127
- end
128
-
129
- end
@@ -1,61 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class HookableTest < Test::Unit::TestCase
4
-
5
- context 'Hookable Classes' do
6
-
7
- setup do
8
- @hookable_class = test_class_for(Perennial::Hookable)
9
- end
10
-
11
- should 'let you append hooks via append_hook' do
12
- assert_equal [], @hookable_class.hooks_for(:awesome)
13
- @hookable_class.append_hook(:awesome) { puts "Hello!" }
14
- assert_equal 1, @hookable_class.hooks_for(:awesome).size
15
- end
16
-
17
- should 'only append hooks if they aren\'t blank' do
18
- @hookable_class.append_hook(:awesome)
19
- assert_equal [], @hookable_class.hooks_for(:awesome)
20
- end
21
-
22
- should 'let you get an array of hooks' do
23
- @hookable_class.append_hook(:awesome) { puts "A" }
24
- @hookable_class.append_hook(:awesome) { puts "B" }
25
- assert_equal 2, @hookable_class.hooks_for(:awesome).size
26
- end
27
-
28
- should 'let you invoke hooks' do
29
- items = []
30
- @hookable_class.append_hook(:awesome) { items << :a }
31
- @hookable_class.append_hook(:awesome) { items << :b }
32
- @hookable_class.append_hook(:awesome) { items << :c }
33
- @hookable_class.invoke_hooks!(:awesome)
34
- assert_equal [:a, :b, :c], items
35
- end
36
-
37
- should 'call them in the order they are appended' do
38
- items = []
39
- @hookable_class.append_hook(:awesome) { items << :a }
40
- @hookable_class.append_hook(:awesome) { items << :b }
41
- @hookable_class.append_hook(:awesome) { items << :c }
42
- @hookable_class.invoke_hooks!(:awesome)
43
- [:a, :b, :c].each_with_index do |value, index|
44
- assert_equal value, items[index]
45
- end
46
- end
47
-
48
- should 'let you define hook accessors' do
49
- assert_equal [], @hookable_class.hooks_for(:awesome)
50
- assert !@hookable_class.respond_to?(:awesome)
51
- assert !@hookable_class.respond_to?(:sauce)
52
- @hookable_class.define_hook :awesome, :sauce
53
- assert @hookable_class.respond_to?(:awesome)
54
- assert @hookable_class.respond_to?(:sauce)
55
- @hookable_class.awesome { puts "A" }
56
- assert_equal 1, @hookable_class.hooks_for(:awesome).size
57
- end
58
-
59
- end
60
-
61
- end
data/test/loader_test.rb DELETED
@@ -1 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
@@ -1,38 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class LoggableTest < Test::Unit::TestCase
4
-
5
- class ExampleLoggable
6
- include Perennial::Loggable
7
- end
8
-
9
- context "Defining a class as loggable" do
10
-
11
- setup do
12
- @example = ExampleLoggable.new
13
- end
14
-
15
- should 'define a logger instance method' do
16
- assert @example.respond_to?(:logger)
17
- end
18
-
19
- should 'define a logger class method' do
20
- assert ExampleLoggable.respond_to?(:logger)
21
- end
22
-
23
- should 'not define a logger= instance method' do
24
- assert !@example.respond_to?(:logger=)
25
- end
26
-
27
- should 'not define a logger= class method' do
28
- assert !ExampleLoggable.respond_to?(:logger=)
29
- end
30
-
31
- should 'define logger to be an instance of Perennial::Logger' do
32
- assert_equal Perennial::Logger, ExampleLoggable.logger
33
- assert_equal Perennial::Logger, @example.logger
34
- end
35
-
36
- end
37
-
38
- end
data/test/logger_test.rb DELETED
@@ -1,57 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class LoggerTest < Test::Unit::TestCase
4
- context 'logger tests' do
5
-
6
- setup do
7
- @root_path = Perennial::Settings.root / "log"
8
- Perennial::Logger.log_name = "example.log"
9
- FileUtils.mkdir_p @root_path
10
- end
11
-
12
- context 'setting up a logger' do
13
-
14
- setup { Perennial::Logger.setup! }
15
-
16
- should 'create the log file file after writing' do
17
- Perennial::Logger.fatal "Blergh."
18
- assert File.exist?(@root_path / "example.log")
19
- end
20
-
21
- Perennial::Logger::LEVELS.each_key do |level_name|
22
- should "define a method for the #{level_name} log level" do
23
- assert Perennial::Logger.respond_to?(level_name)
24
- assert Perennial::Logger.logger.respond_to?(level_name)
25
- assert_equal 1, Perennial::Logger.logger.method(level_name).arity
26
- end
27
- end
28
-
29
- should 'have a log exception method' do
30
- assert Perennial::Logger.respond_to?(:log_exception)
31
- assert Perennial::Logger.logger.respond_to?(:log_exception)
32
- end
33
-
34
- end
35
-
36
- context 'writing to the log' do
37
-
38
- Perennial::Logger::LEVELS.each_key do |level_name|
39
- should "let you write to the #{level_name} log level" do
40
- Perennial::Logger.verbose = false
41
- Perennial::Logger.level = level_name
42
- assert_nothing_raised do
43
- Perennial::Logger.logger.send(level_name, "An Example Message No. 1")
44
- end
45
- end
46
- end
47
-
48
- end
49
-
50
- teardown do
51
- log_path = @root_path / "example.log"
52
- FileUtils.rm_rf(log_path) if File.exist?(log_path)
53
- end
54
-
55
- end
56
-
57
- end
@@ -1,99 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "test_helper")
2
-
3
- class SettingsTest < Test::Unit::TestCase
4
-
5
- context 'default settings' do
6
-
7
- setup do
8
- Perennial::Settings.setup!
9
- end
10
-
11
- should "default the application root to the parent folder of perennial" do
12
- assert_equal __FILE__.to_pathname.dirname.join("..").expand_path,
13
- Perennial::Settings.root.to_pathname
14
- Perennial::Settings.root = "/awesome/sauce"
15
- assert_equal "/awesome/sauce", Perennial::Settings.root
16
- end
17
-
18
- should "default daemonized to false" do
19
- assert !Perennial::Settings.daemon?
20
- Perennial::Settings.daemon = true
21
- assert Perennial::Settings.daemon?
22
- Perennial::Settings.daemon = false
23
- assert !Perennial::Settings.daemon?
24
- end
25
-
26
- should "default the log level to :info" do
27
- assert_equal :info, Perennial::Settings.log_level
28
- Perennial::Settings.log_level = :debug
29
- assert_equal :debug, Perennial::Settings.log_level
30
- end
31
-
32
- should "default verbose to false" do
33
- assert !Perennial::Settings.verbose?
34
- Perennial::Settings.verbose = true
35
- assert Perennial::Settings.verbose?
36
- Perennial::Settings.verbose = false
37
- assert !Perennial::Settings.verbose?
38
- end
39
-
40
- end
41
-
42
- context 'loading settings' do
43
-
44
- setup do
45
- config_folder = Perennial::Settings.root / "config"
46
- @default_settings = {
47
- "default" => {
48
- "introduction" => true,
49
- "description" => "Ninjas are Totally Awesome",
50
- "channel" => "#offrails",
51
- "users" => ["Sutto", "njero", "zapnap"]
52
- }
53
- }
54
- FileUtils.mkdir_p(config_folder)
55
- File.open(config_folder / "settings.yml", "w+") do |file|
56
- file.write(@default_settings.to_yaml)
57
- end
58
- Perennial::Settings.setup!
59
- end
60
-
61
- should 'load settings from the file' do
62
- assert Perennial::Settings.setup?
63
- assert_equal @default_settings["default"].symbolize_keys, Perennial::Settings.to_hash
64
- end
65
-
66
- should 'define readers for the settings' do
67
- instance = Perennial::Settings.new
68
- @default_settings["default"].each_pair do |key, value|
69
- assert Perennial::Settings.respond_to?(key.to_sym)
70
- assert_equal value, Perennial::Settings.send(key)
71
- assert instance.respond_to?(key.to_sym)
72
- assert_equal value, instance.send(key)
73
- end
74
- end
75
-
76
- should 'let you access settings via hash-style accessors' do
77
- @default_settings["default"].each_pair do |key, value|
78
- assert_equal value, Perennial::Settings[key]
79
- Perennial::Settings[key] = "a-new-value from #{value.inspect}"
80
- assert_equal "a-new-value from #{value.inspect}", Perennial::Settings[key]
81
- end
82
- end
83
-
84
- should 'define writers for the settings' do
85
- instance = Perennial::Settings.new
86
- @default_settings["default"].each_pair do |key, value|
87
- setter = :"#{key}="
88
- assert Perennial::Settings.respond_to?(setter)
89
- Perennial::Settings.send(setter, "value #{value.inspect} on class")
90
- assert_equal "value #{value.inspect} on class", Perennial::Settings.send(key)
91
- assert instance.respond_to?(setter)
92
- instance.send(setter, "value #{value.inspect} on instance")
93
- assert_equal "value #{value.inspect} on instance", instance.send(key)
94
- end
95
- end
96
-
97
- end
98
-
99
- end
data/test/test_helper.rb DELETED
@@ -1,38 +0,0 @@
1
- require 'rubygems'
2
-
3
- # Testing dependencies
4
- require 'test/unit'
5
- require 'shoulda'
6
- require 'rr'
7
- # RedGreen doesn't seem to be needed under 1.9
8
- require 'redgreen' if RUBY_VERSION < "1.9"
9
-
10
- require 'pathname'
11
- root_directory = Pathname.new(__FILE__).dirname.join("..").expand_path
12
- require root_directory.join("lib", "perennial")
13
- require root_directory.join("vendor", "fakefs", "lib", "fakefs")
14
-
15
- class Test::Unit::TestCase
16
- include RR::Adapters::TestUnit
17
-
18
- protected
19
-
20
- # Short hand for creating a class with
21
- # a given class_eval block.
22
- def class_via(*args, &blk)
23
- klass = Class.new(*args)
24
- klass.class_eval(&blk) unless blk.blank?
25
- return klass
26
- end
27
-
28
- # Short hand for creating a test class
29
- # for a set of mixins - give it the modules
30
- # and it will include them all.
31
- def test_class_for(*mods, &blk)
32
- klass = Class.new
33
- klass.class_eval { include(*mods) }
34
- klass.class_eval(&blk) unless blk.blank?
35
- return klass
36
- end
37
-
38
- end
@@ -1,20 +0,0 @@
1
- Copyright (c) 2009 Chris Wanstrath
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,37 +0,0 @@
1
- FakeFS
2
- ======
3
-
4
- Mocha is great. But when your library is all about manipulating the
5
- filesystem, you really want to test the behavior and not the implementation.
6
-
7
- If you're mocking and stubbing every call to FileUtils or File, you're
8
- tightly coupling your tests with the implementation.
9
-
10
- def test_creates_directory
11
- FileUtils.expects(:mkdir).with("directory").once
12
- Library.add "directory"
13
- end
14
-
15
- The above test will break if we decide to use `mkdir_p` in our code. Refactoring
16
- code shouldn't necessitate refactoring tests.
17
-
18
- With FakeFS:
19
-
20
- def test_creates_directory
21
- Library.add "directory"
22
- assert File.directory?("directory")
23
- end
24
-
25
- Woot.
26
-
27
- How is this different than MockFS?
28
- ----------------------------------
29
-
30
- FakeFS provides a test suite and works with symlinks. It's also strictly a
31
- test-time dependency: your actual library does not need to use or know about
32
- FakeFS.
33
-
34
- Authors
35
- -------
36
-
37
- Chris Wanstrath [chris@ozmm.org]
@@ -1,3 +0,0 @@
1
- task :default do
2
- exec "ruby test/fakefs_test.rb"
3
- end