Guest User

Untitled

a guest
Apr 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. require 'fattr'
  2.  
  3. Testing Fattr do
  4. testing 'that a basic set of methods are defined' do
  5. o = Class.new{ fattr :a }.new
  6. %w( a a= a? ).each do |msg|
  7. assert("respond_to?(#{ msg.inspect })"){ o.respond_to?(msg) }
  8. end
  9. end
  10.  
  11. testing 'that the basic usage works' do
  12. o = Class.new{ fattr :a }.new
  13. assert{ o.a==nil }
  14. assert{ o.a=42 }
  15. assert{ o.a(42.0) }
  16. assert{ o.a? }
  17. end
  18.  
  19. testing 'that simple defaults work' do
  20. o = Class.new{ fattr :a => 42 }.new
  21. assert{ o.a==42 }
  22. end
  23.  
  24. testing 'that block defaults work' do
  25. n = 41
  26. o = Class.new{ fattr(:a){ n += 1 } }.new
  27. assert{ o.a==42 }
  28. assert{ n==42 }
  29. end
  30.  
  31. testing 'that > 1 fattrs can be defined at once' do
  32. o = Class.new{ fattr :a, :b }.new
  33. %w( a a= a? b b= b? ).each do |msg|
  34. assert("respond_to?(#{ msg.inspect })"){ o.respond_to?(msg) }
  35. end
  36. end
  37.  
  38. testing 'that > 1 fattrs with defaults can be defined at once' do
  39. o = Class.new{ fattr :a => 40, :b => 2 }.new
  40. assert{ o.a+o.b==42 }
  41. end
  42.  
  43. testing 'that fattrs can be retrieved from the object' do
  44. c = Class.new{ fattr :a, :b, :c; self }
  45. assert{ c.fattrs.sort === %w[a b c] }
  46. end
  47.  
  48. testing 'getters as setters' do
  49. o = Class.new{ fattr :a }.new
  50. assert{ o.a(42) }
  51. assert{ o.a==42 }
  52. end
  53.  
  54. testing 'module fattrs' do
  55. m = Module.new{ class << self; fattr :a => 42; end; }
  56. assert{ m.a==42 }
  57. end
  58.  
  59. testing 'module fattr shortcut' do
  60. m = Module.new{ Fattr :a => 42 }
  61. assert{ m.a==42 }
  62. end
  63.  
  64. testing 'that fattrs support simple class inheritable attributes' do
  65. a = Class.new{ Fattr :x, :default => 42, :inheritable => true }
  66. b = Class.new(a)
  67. c = Class.new(b)
  68.  
  69. def a.name() 'a' end
  70. def b.name() 'b' end
  71. def c.name() 'c' end
  72.  
  73. assert{ c.x==42 }
  74. assert{ b.x==42 }
  75. assert{ a.x==42 }
  76.  
  77. assert{ b.x=42.0 }
  78. assert{ b.x==42.0 }
  79. assert{ a.x==42 }
  80.  
  81. assert{ a.x='forty-two' }
  82. assert{ a.x=='forty-two' }
  83. assert{ b.x==42.0 }
  84.  
  85. assert{ b.x! }
  86. assert{ b.x=='forty-two' }
  87. assert{ b.x='FORTY-TWO' }
  88.  
  89. assert{ c.x! }
  90. assert{ c.x=='FORTY-TWO' }
  91. end
  92.  
  93. testing 'a list of fattrs can be declared at once' do
  94. list = %w( a b c )
  95. c = Class.new{ fattrs list }
  96. list.each do |attr|
  97. assert{ c.fattrs.include?(attr.to_s) }
  98. assert{ c.fattrs.include?(attr.to_sym) }
  99. end
  100. end
  101. end
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. BEGIN {
  110. require 'test/unit'
  111. STDOUT.sync = true
  112. $:.unshift 'lib'
  113. $:.unshift '../lib'
  114. $:.unshift '.'
  115.  
  116. def Testing(*args, &block)
  117. Class.new(Test::Unit::TestCase) do
  118. def self.slug_for(*args)
  119. string = args.flatten.compact.join('-')
  120. words = string.to_s.scan(%r/\w+/)
  121. words.map!{|word| word.gsub %r/[^0-9a-zA-Z_-]/, ''}
  122. words.delete_if{|word| word.nil? or word.strip.empty?}
  123. words.join('-').downcase
  124. end
  125.  
  126. @@testing_subclass_count = 0 unless defined?(@@testing_subclass_count)
  127. @@testing_subclass_count += 1
  128. slug = slug_for(*args).gsub(%r/-/,'_')
  129. name = ['TESTING', '%03d' % @@testing_subclass_count, slug].delete_if{|part| part.empty?}.join('_')
  130. name = name.upcase!
  131. const_set(:Name, name)
  132. def self.name() const_get(:Name) end
  133.  
  134. def self.testno()
  135. '%05d' % (@testno ||= 0)
  136. ensure
  137. @testno += 1
  138. end
  139.  
  140. def self.testing(*args, &block)
  141. method = ["test", testno, slug_for(*args)].delete_if{|part| part.empty?}.join('_')
  142. define_method("test_#{ testno }_#{ slug_for(*args) }", &block)
  143. end
  144.  
  145. alias_method '__assert__', 'assert'
  146.  
  147. def assert(*args, &block)
  148. if block
  149. label = "assert(#{ args.join ' ' })"
  150. result = nil
  151. assert_nothing_raised{ result = block.call }
  152. __assert__(result, label)
  153. result
  154. else
  155. result = args.shift
  156. label = "assert(#{ args.join ' ' })"
  157. __assert__(result, label)
  158. result
  159. end
  160. end
  161.  
  162. module_eval &block
  163. self
  164. end
  165. end
  166. }
Add Comment
Please, Sign In to add comment