ahoward (owner)

Revisions

gist: 219322 Download_button fork
public
Public Clone URL: git://gist.github.com/219322.git
Embed All Files: show embed
Ruby #
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require 'fattr'
 
Testing Fattr do
  testing 'that a basic set of methods are defined' do
    o = Class.new{ fattr :a }.new
    %w( a a= a? ).each do |msg|
      assert("respond_to?(#{ msg.inspect })"){ o.respond_to?(msg) }
    end
  end
 
  testing 'that the basic usage works' do
    o = Class.new{ fattr :a }.new
    assert{ o.a==nil }
    assert{ o.a=42 }
    assert{ o.a(42.0) }
    assert{ o.a? }
  end
 
  testing 'that simple defaults work' do
    o = Class.new{ fattr :a => 42 }.new
    assert{ o.a==42 }
  end
 
  testing 'that block defaults work' do
    n = 41
    o = Class.new{ fattr(:a){ n += 1 } }.new
    assert{ o.a==42 }
    assert{ n==42 }
  end
 
  testing 'that > 1 fattrs can be defined at once' do
    o = Class.new{ fattr :a, :b }.new
    %w( a a= a? b b= b? ).each do |msg|
      assert("respond_to?(#{ msg.inspect })"){ o.respond_to?(msg) }
    end
  end
 
  testing 'that > 1 fattrs with defaults can be defined at once' do
    o = Class.new{ fattr :a => 40, :b => 2 }.new
    assert{ o.a+o.b==42 }
  end
 
  testing 'that fattrs can be retrieved from the object' do
    c = Class.new{ fattr :a, :b, :c; self }
    assert{ c.fattrs.sort === %w[a b c] }
  end
 
  testing 'getters as setters' do
    o = Class.new{ fattr :a }.new
    assert{ o.a(42) }
    assert{ o.a==42 }
  end
 
  testing 'module fattrs' do
    m = Module.new{ class << self; fattr :a => 42; end; }
    assert{ m.a==42 }
  end
 
  testing 'module fattr shortcut' do
    m = Module.new{ Fattr :a => 42 }
    assert{ m.a==42 }
  end
 
  testing 'that fattrs support simple class inheritable attributes' do
    a = Class.new{ Fattr :x, :default => 42, :inheritable => true }
    b = Class.new(a)
    c = Class.new(b)
 
    def a.name() 'a' end
    def b.name() 'b' end
    def c.name() 'c' end
 
    assert{ c.x==42 }
    assert{ b.x==42 }
    assert{ a.x==42 }
 
    assert{ b.x=42.0 }
    assert{ b.x==42.0 }
    assert{ a.x==42 }
 
    assert{ a.x='forty-two' }
    assert{ a.x=='forty-two' }
    assert{ b.x==42.0 }
 
    assert{ b.x! }
    assert{ b.x=='forty-two' }
    assert{ b.x='FORTY-TWO' }
 
    assert{ c.x! }
    assert{ c.x=='FORTY-TWO' }
  end
 
  testing 'a list of fattrs can be declared at once' do
    list = %w( a b c )
    c = Class.new{ fattrs list }
    list.each do |attr|
      assert{ c.fattrs.include?(attr.to_s) }
      assert{ c.fattrs.include?(attr.to_sym) }
    end
  end
end
 
 
 
 
 
 
 
BEGIN {
  require 'test/unit'
  STDOUT.sync = true
  $:.unshift 'lib'
  $:.unshift '../lib'
  $:.unshift '.'
 
  def Testing(*args, &block)
    Class.new(Test::Unit::TestCase) do
      def self.slug_for(*args)
        string = args.flatten.compact.join('-')
        words = string.to_s.scan(%r/\w+/)
words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
words.delete_if{|word| word.nil? or word.strip.empty?}
words.join('-').downcase
end
 
@@testing_subclass_count = 0 unless defined?(@@testing_subclass_count)
@@testing_subclass_count += 1
slug = slug_for(*args).gsub(%r/-/,'_')
      name = ['TESTING', '%03d' % @@testing_subclass_count, slug].delete_if{|part| part.empty?}.join('_')
      name = name.upcase!
      const_set(:Name, name)
      def self.name() const_get(:Name) end
 
      def self.testno()
        '%05d' % (@testno ||= 0)
      ensure
        @testno += 1
      end
 
      def self.testing(*args, &block)
        method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
        define_method("test_#{ testno }_#{ slug_for(*args) }", &block)
      end
 
      alias_method '__assert__', 'assert'
 
      def assert(*args, &block)
        if block
          label = "assert(#{ args.join ' ' })"
          result = nil
          assert_nothing_raised{ result = block.call }
          __assert__(result, label)
          result
        else
          result = args.shift
          label = "assert(#{ args.join ' ' })"
          __assert__(result, label)
          result
        end
      end
 
      module_eval &block
      self
    end
  end
}