public
Description: A SSB OSX application, which at some point will be able to create a new application which wraps a specific web application, (Think Campfire, Twitter etc) and allows the user to use Ruby to create event handlers to be able support things like Growl or whatever you would like.
Clone URL: git://github.com/alloy/webapp-app.git
Search Repo:
webapp-app / test / test_case.rb
100644 134 lines (125 sloc) 4.936 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
class OSX::NSObject
  class << self
    # An array of defined ib_outlets.
    def defined_ib_outlets
      @defined_ib_outlets ||= []
    end
    
    # Override ib_outlets so we can store which ones need to be defined.
    def ib_outlets(*outlets)
      defined_ib_outlets.concat(outlets.flatten)
    end
    alias_method :ib_outlet, :ib_outlets
  end
end
 
module Rucola
  module TestCase
    # Defines the controller that will be tested.
    #
    # class ApplicationController < OSX::NSObject
    # ib_outlet :window
    # ib_outlets :tableView, :searchField
    # ib_outlets :textField
    # end
    #
    # class TestFoo < Test::Unit::TestCase
    # tests ApplicationController
    #
    # def after_setup
    # ib_outlets :window => mock("Main Window"),
    # :tableView => OSX::NSTableView.alloc.init,
    # :searchField => OSX::NSSearchField.alloc.init
    #
    # window.stubs(:title => 'Main Window')
    # tableView.addTableColumn OSX::NSTableColumn.alloc.init
    # searchField.stringValue = "foo"
    # end
    #
    # def test_something
    # p controller # => #<ApplicationController:0xdfa1ce class='ApplicationController' id=0x1e8d0e0>
    # p window.title # => "Main Window"
    # p tableView.tableColumns # => #<NSCFArray [#<OSX::NSTableColumn:0xdf9d0a class='NSTableColumn' id=0x1e90d00>]>
    # p searchField # => #<OSX::NSSearchField:0xdfa43a class='NSSearchField' id=0x1e84cb0>
    #
    # # Note that we haven't set the textField ib_outlet to anything else in the after_setup method,
    # # so it will be a stub which responds to everything by returning nil.
    # p textField # => #<Mock:textField>
    # end
    # end
    def tests(class_to_be_tested)
      @class_to_be_tested = class_to_be_tested
      include Rucola::TestCase::InstanceMethods
    end
    
    module InstanceMethods
      # Sets up the ib_outlets to all be stubs which respond to everything with nil.
      #
      # In your test use #after_setup to do any custom setup.
      def setup
        class_to_be_tested.defined_ib_outlets.each do |outlet|
          ib_outlet(outlet, stub_everything(outlet.to_s))
        end
        after_setup if respond_to? :after_setup
      end
      
      # Sets the ib_outlets and instance to be tested to nil at the end of the test.
      #
      # In your test use #after_teardown to do any custom teardown.
      def teardown
        class_to_be_tested.defined_ib_outlets.each do |outlet|
          instance_to_be_tested.instance_variable_set("@#{outlet}", nil)
        end
        @instance_to_be_tested = nil
        after_teardown if respond_to? :after_teardown
      end
      
      # Returns the class that's to be tested.
      def class_to_be_tested
        self.class.instance_variable_get(:@class_to_be_tested)
      end
      
      # An instance of the class that's to be tested.
      def instance_to_be_tested
        @instance_to_be_tested ||= class_to_be_tested.alloc.init
      end
      alias_method :controller, :instance_to_be_tested
      
      # Lets you get an instance variable from the instance.
      #
      # obj.instance_variable_set(:@some_attr, 'foo')
      # assigns(:some_attr) # => 'foo'
      #
      # You can also set an instance variable in the instance.
      #
      # obj.assigns(:some_attr, 'bar')
      # obj.instance_variable_get(:@some_attr) # => 'bar'
      def assigns(name, obj = nil)
        obj.nil? ? instance_to_be_tested.instance_variable_get("@#{name}") : instance_to_be_tested.instance_variable_set("@#{name}", obj)
      end
      
      # Defines instance variables in the instance which represent the ib_outlets.
      # It basically just sets the instance variables, but also creates shorcut accessors to get at them from your tests.
      #
      # def after_setup
      # ib_outlet :textField, OSX::NSTextField.alloc.init
      # p textField # => #<OSX::NSTextField:0xdfa3f4 class='NSTextField' id=0x1e842b0>
      # end
      #
      # Note that not every class can be instantiated in a test.
      # So you can also supply something like a mock.
      def ib_outlet(name, obj)
        unless respond_to? name
          self.class.class_eval do
            define_method(name) { assigns(name) }
            private name
          end
        end
        assigns(name, obj)
      end
      
      # Shortcut method to defined multiple ib_outlets by supplying a hash.
      #
      # def after_setup
      # ib_outlets :window => mock("Main Window"),
      # :tableView => OSX::NSTableView.alloc.init,
      # :searchField => OSX::NSSearchField.alloc.init
      # end
      def ib_outlets(outlets)
        outlets.each {|k,v| ib_outlet(k, v) }
      end
    end
  end
end
Test::Unit::TestCase.send(:extend, Rucola::TestCase)