Changeset 276

Show
Ignore:
Timestamp:
07/12/07 12:01:34 (17 months ago)
Author:
ged
Message:

source:trunk/Rakefile

  • Moved the default spec task into the toplevel namespace.

source:trunk/spec/hookable.tests.rb
source:trunk/spec/hookable_spec.rb

  • Converted Hookable unit test suite to RSpec specification

source:trunk/spec/gameobject_spec.rb

source:trunk/tests/entity.tests.rb
source:trunk/tests/event.tests.rb
source:trunk/tests/hookable.tests.rb
source:trunk/tests/developmentalobject.tests.rb
source:trunk/tests/commandparser.tests.rb
source:trunk/tests/character.tests.rb
source:trunk/tests/composedobject.tests.rb
source:trunk/tests/gameobject.tests.rb
source:trunk/tests/area.tests.rb
source:trunk/tests/animatedobject.tests.rb

  • Deleting unit tests that have already been converted to RSpec specifications

source:trunk/lib/fm/mixins.rb

  • Fixed spelling mistake
Location:
trunk
Files:
10 removed
3 modified
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/Rakefile

    r270 r276  
    4242                LIB_FILES 
    4343 
    44 task :default  => 'spec:normal' 
     44task :default  => :spec 
    4545 
    4646########################### 
     
    4949 
    5050# Task: rdoc 
    51 Rake::RDocTask.new { |rdoc| 
     51Rake::RDocTask.new do |rdoc| 
    5252    rdoc.rdoc_dir = 'doc' 
    5353    rdoc.title    = "FaerieMUD" 
     
    5555    rdoc.rdoc_files.include TEXT_FILES 
    5656    rdoc.rdoc_files.include LIB_FILES 
    57 } 
     57end 
    5858 
    5959 
     
    6262########################### 
    6363 
     64desc "Run specs outputting to STDOUT" 
     65Spec::Rake::SpecTask.new( :spec ) do |task| 
     66    task.ruby_opts << '-rubygems' 
     67    task.libs << 'lib' 
     68    task.spec_files = SPEC_FILES 
     69    task.spec_opts = ['-f s', '-r fm', '-c'] 
     70end 
     71 
    6472### Task: spec 
    6573namespace :spec do 
    66     desc "Run specs outputting to STDOUT" 
    67     Spec::Rake::SpecTask.new( :normal ) do |task| 
    68         task.ruby_opts << '-rubygems' 
    69         task.libs << 'lib' 
    70         task.spec_files = SPEC_FILES 
    71         task.spec_opts = ['-f s', '-r fm', '-c'] 
    72     end 
    73  
    7474    desc "Generate plaintext output" 
    7575    Spec::Rake::SpecTask.new( :text ) do |task| 
  • trunk/lib/fm/mixins.rb

    r270 r276  
    445445                name = symbol.id2name 
    446446 
    447                 # If the caller hasn't speicifed a method, just pop the last one 
     447                # If the caller hasn't specified a method, just pop the last one 
    448448                # off the stack. If the caller specified a Proc, subtract it 
    449449                # from the stack after making sure it's there for the return 
  • trunk/spec/gameobject_spec.rb

    r275 r276  
    9696 
    9797 
     98class RevClass < FaerieMUD::GameObject 
     99    SVNRev = 'Rev: 270' 
     100end 
     101 
    98102describe FaerieMUD::GameObject do 
    99     before( :all ) do 
    100         @class = Class.new( FaerieMUD::GameObject ) do 
    101             SVNRev = 'Rev: 270 ' 
    102         end 
    103     end 
    104      
    105      
    106103    it "can extract a numeric version from its subversion rev number" do 
    107         @class.rev.should == 270 
     104        RevClass.rev.should == 270 
    108105    end 
    109106end 
  • trunk/spec/hookable_spec.rb

    r270 r276  
    11#!/usr/bin/ruby -w 
    22# 
    3 # Unit test for the FaerieMUD::Hookable mixin. 
     3# Specification for the FaerieMUD::GameObject class 
    44# $Id$ 
    55# 
    6 # Copyright (c) 2004 The FaerieMUD Consortium. Most rights reserved. 
    7 #  
    8 # This work is licensed under the Creative Commons Attribution-ShareAlike 
    9 # License. To view a copy of this license, visit 
    10 # http://creativecommons.org/licenses/by-sa/1.0/ or send a letter to Creative 
    11 # Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. 
    12 # 
     6# Copyright (c) 2003-2007 The FaerieMUD Consortium. 
    137#  
    148 
     
    1812    $LOAD_PATH.unshift "#{basedir}/lib" unless 
    1913        $LOAD_PATH.include?( "#{basedir}/lib" ) 
    20     $LOAD_PATH.unshift "#{basedir}/tests/lib" unless 
    21         $LOAD_PATH.include?( "#{basedir}/tests/lib" ) 
    22  
    23     require 'fmtestcase' 
     14    $LOAD_PATH.unshift "#{basedir}/spec/lib" unless 
     15        $LOAD_PATH.include?( "#{basedir}/spec/lib" ) 
    2416end 
    2517 
     18require 'spec/runner' 
    2619require 'fm/mixins' 
    2720 
    28 ### Test case for the FaerieMUD::Hookable mixin. 
    29 class HookableTestCase < FaerieMUD::TestCase 
     21# Hooked class 
     22class HookableObject < FaerieMUD::GameObject 
     23    include FaerieMUD::Hookable 
    3024 
    31     WarkMessage = "Wark! Wark! Some tasty %s greens!" 
    32  
    33     TestedHooks = { 
    34         :tripling       => Proc::new {|arg| arg * 3}, 
    35         :reversing      => Proc::new {|arg| arg.reverse}, 
    36         :uppercasing    => Proc::new {|arg| arg.upcase}, 
    37         :novowels       => :extractVowels, 
    38     } 
     25    # By default, just return the first argument unmodified 
     26    def test( msg ) 
     27        return msg 
     28    end 
     29end 
    3930 
    4031 
    41     ### Setup method (aliased for older versions of Test::Unit) 
    42     def setup  
     32describe "An instance of a class which has included FaerieMUD::Hookable" do 
    4333 
    44         # Hooked class 
    45         @hclass = Class::new( Object ) { 
    46             include FaerieMUD::Hookable 
    47             def wark( thing ) 
    48                 msg = WarkMessage % thing 
    49                 debug_msg( msg + "\n" ) 
    50                 return msg 
    51             end 
    52         } 
    53  
    54         # Derived hooked class 
    55         @dhclass = Class::new( @hclass ) { 
    56             include FaerieMUD::Hookable 
    57         } 
    58  
    59         super 
    60     end 
    61     alias_method :set_up, :setup 
    62  
    63  
    64     ### Teardown method (aliased for older version of Test::Unit) 
    65     def teardown  
    66         @hclass = nil 
    67         @dhclass = nil 
    68  
    69         super 
    70     end 
    71     alias_method :tear_down, :teardown 
    72  
    73  
    74     ### Hook method to test method hooks 
    75     def extractVowels( arg ) 
    76         arg.gsub( /[aeiou]+/, '' ) 
     34    before( :each ) do 
     35        @hookableobj = HookableObject.new 
    7736    end 
    7837 
    7938 
    80  
    81  
    82     ################################################################# 
    83     ### E X A M P L E S 
    84     ################################################################# 
    85  
    86     ### Test inclusion 
    87     def test_00_included 
    88         hobj, dhobj = nil, nil 
    89  
    90         assert_nothing_raised { hobj = @hclass::new } 
    91         assert_nothing_raised { dhobj = @dhclass::new } 
    92  
    93         [ :hooks, :hook, :unhook ].each {|sym| 
    94             assert_respond_to hobj, sym, "Hooked class instance" 
    95             assert_respond_to dhobj, sym, "Hooked class derivative instance" 
    96         } 
    97  
    98         self.class::addSetupBlock { 
    99             @hobj, @dhobj = @hclass::new, @dhclass::new 
    100         } 
    101         self.class::addTeardownBlock { 
    102             @hobj = nil 
    103             @dhobj = nil 
    104         } 
     39    it "allows a public method to be intercepted via a hook" do 
     40        @hookableobj.test( 'thing' ).should == 'thing' 
     41        @hookableobj.hook( :test ) {|arg| arg.reverse } 
     42        @hookableobj.test( 'thing' ).should == 'gniht' 
    10543    end 
    10644 
    10745 
    108     ### Test methods with no hooks 
    109     def test_01_emptyhooks 
    110         { 
    111             @hobj => "Hooked class instance", 
    112             @dhobj => "Hooked class derivative instance" 
    113         }.each {|obj, objtype| 
    114             rval = nil 
    115             assert_nothing_raised { rval = obj.hooks } 
    116             assert_equal Hash::new, rval, 
    117                 "#{objtype} #hooks method did not return a Hash." 
    118         } 
     46    it "s hooks don't interfere with any other instances of the same class" do 
     47        @hookableobj2 = HookableObject.new 
     48        @hookableobj.hook( :test ) {    } 
    11949    end 
     50     
     51    it "knows about any hooks which are applied to it" do 
     52        # Starts out with no hooks... 
     53        @hookableobj.hooks.should be_a_kind_of( Hash ) 
     54        @hookableobj.hooks.should be_empty() 
    12055 
     56        # Hook the #test methodq 
     57        @hookableobj.hook( :test ) {|arg| arg.reverse } 
    12158 
    122     ### Unhooked method call 
    123     def test_02_prehook 
    124         printTestHeader "Unhooked method call" 
    125         rval = nil 
    126         greensType = "Tantal" 
     59        # ...then it should have hooks. The original method should be the first thing  
     60        # in the list, then a Proc for each hook 
     61        @hookableobj.hooks.should have(1).keys 
     62        @hookableobj.hooks[ :test ].should be_an_instance_of( Array ) 
     63        @hookableobj.hooks[ :test ].should have(2).members 
     64        @hookableobj.hooks[ :test ].first.should be_an_instance_of( Method ) 
     65        @hookableobj.hooks[ :test ].last.should be_an_instance_of( Proc ) 
     66    end 
     67     
     68     
     69    it "allows the last-applied hook to be removed from a method" do 
     70        hookproc = Proc.new {|arg| :noop } 
     71        @hookableobj.hook( :test, &hookproc )  
     72        @hookableobj.unhook( :test ).should == hookproc 
     73    end 
     74     
     75     
     76    it "allows a chain of hooks to be applied to a single method, which are called " + 
     77       "in reverse of the order they are applied" do 
     78         
     79        @hookableobj.hook( :test ) {|arg| arg.reverse + '1' } 
     80        @hookableobj.hook( :test ) {|arg| arg.upcase + '2' } 
     81        @hookableobj.hook( :test ) {|arg| arg.gsub(/[aeiou]+/i, '') + '3' } 
     82         
     83        @hookableobj.test( 'thingerything' ).should == '23GNHTYRGNHT1' 
     84    end 
     85     
     86     
     87    it "allows a hook to be removed from an arbitrary point in a chain via reference" do 
     88        @hookableobj.hook( :test ) {|arg| arg.reverse + '1' } 
     89        upcasehook = @hookableobj.hook( :test ) {|arg| arg.upcase + '2' } 
     90        @hookableobj.hook( :test ) {|arg| arg.gsub(/[aeiou]+/i, '') + '3' } 
     91         
     92        @hookableobj.unhook( :test, upcasehook ) 
    12793 
    128         assert_nothing_raised { rval = @hobj.wark(greensType) } 
    129         assert_equal WarkMessage % greensType, rval 
     94        @hookableobj.test( 'thingerything' ).should == '3gnhtyrgnht1' 
    13095    end 
     96     
     97     
     98    it "allows all hooks to be removed from a method" do 
     99        @hookableobj.hook( :test ) {|arg| arg.reverse + '1' } 
     100        @hookableobj.hook( :test ) {|arg| arg.upcase + '2' } 
     101        @hookableobj.hook( :test ) {|arg| arg.gsub(/[aeiou]+/i, '') + '3' } 
     102 
     103        @hookableobj.unhook_all( :test ) 
    131104         
     105        @hookableobj.test( 'thingerything' ).should == 'thingerything' 
     106    end 
     107     
     108     
     109    it "sets up an alias for the unhooked version of a hooked method" do 
     110        @hookableobj.hook( :test ) {|arg| arg.reverse + '1' } 
     111        @hookableobj.hook( :test ) {|arg| arg.upcase + '2' } 
     112        @hookableobj.hook( :test ) {|arg| arg.gsub(/[aeiou]+/i, '') + '3' } 
    132113 
    133     ### Test hooks 
    134     def test_03_hooks 
    135         printTestHeader "Hooked method calls" 
    136         hook, msg = nil, nil 
    137         greensType = "Tantal" 
    138         procs = [] 
    139         count = 0 
    140  
    141         # For each hook in TestedHooks, add the proc as a hook and test the object 
    142         TestedHooks.sort {|a,b| a[0].to_s <=> b[0].to_s}.each {|name,proc| 
    143             count += 1 
    144             proc = method( proc ).to_proc if proc.is_a?( Symbol ) 
    145             procs.push( proc ) 
    146  
    147             assert_nothing_raised { 
    148                 hook = @hobj.hook( :wark, &proc ) 
    149             } 
    150             assert_instance_of Proc, hook 
    151             assert_equal proc.to_proc, hook 
    152              
    153             assert_nothing_raised { msg = @hobj.wark(greensType) } 
    154             debug_msg "Wark, after #{count} hook/s: #{msg.inspect}" 
    155             expectedMsg = greensType 
    156             procs.each {|proc| expectedMsg.replace( proc.call(expectedMsg) )} 
    157             assert_equal WarkMessage % expectedMsg, msg 
    158         } 
    159  
    160         # Add setup to to hook @hobj for each successive test 
    161         self.class::addSetupBlock { 
    162             @hooks = [] 
    163             TestedHooks.sort {|a,b| a[0].to_s <=> b[0].to_s}.each {|name,proc| 
    164                 proc = method( proc ).to_proc if proc.is_a?( Symbol ) 
    165                 @hooks.push( proc ) 
    166                 @hobj.hook( :wark, &proc ) 
    167             } 
    168         } 
    169         self.class::addTeardownBlock { 
    170             @hooks = nil 
    171         } 
    172     end 
    173  
    174     ### Test second instance of the same class as the hooked object 
    175     def test_04_second_instance 
    176         printTestHeader "Testing second instance of the hooked object's class" 
    177         msg = nil 
    178  
    179         assert_nothing_raised { 
    180             msg = @hclass::new.wark( "Gysal" ) 
    181         } 
    182         debug_msg msg.inspect 
    183         assert_equal WarkMessage % "Gysal", msg 
    184     end 
    185  
    186  
    187     ### Test the instance of the derivative class 
    188     def test_05_derivative_instance 
    189         printTestHeader "Testing the derivative class instance" 
    190         msg = nil 
    191  
    192         assert_nothing_raised { 
    193             msg = @dhobj.wark( "Pahsana" ) 
    194         } 
    195         debug_msg msg.inspect 
    196         assert_equal WarkMessage % "Pahsana", msg 
    197     end 
    198  
    199     ### Test to make sure the unhooked method can still be called through the 
    200     ### __unhooked_ alias. 
    201     def test_06_unhooked_variant 
    202         debug_msg "Trying @hobj's unhooked alias" 
    203         msg = nil 
    204  
    205         assert_nothing_raised { 
    206             msg = @hobj.__unhooked_wark( "Reagan" ) 
    207         } 
    208         debug_msg msg.inspect 
    209         assert_equal WarkMessage % "Reagan", msg 
    210     end 
    211  
    212     ### Test removal of last hook. 
    213     def test_07_unhook_onearg 
    214         printTestHeader "Testing unhook( symbol )" 
    215         hook, msg = nil, nil 
    216         greensType = "Curiel" 
    217  
    218         # Last hook 
    219         debug_msg "Removing last hook added:" 
    220         assert_nothing_raised { 
    221             hook = @hobj.unhook( :wark ) 
    222         } 
    223         assert_equal @hooks.last, hook 
    224         @hooks.pop 
    225  
    226         debug_msg "Hooks: %s" % @hobj.hooks.inspect 
    227  
    228         debug_msg "First wark after unhook: " 
    229         assert_nothing_raised { 
    230             msg = @hobj.wark( greensType ) 
    231         } 
    232         debug_msg msg.inspect 
    233         expectedMsg = greensType 
    234         @hooks.each {|proc| expectedMsg.replace( proc.call(expectedMsg) )} 
    235         assert_equal WarkMessage % expectedMsg, msg 
    236     end 
    237  
    238  
    239     ### Test removal of a specific hook, out of order. 
    240     def test_08_unhook_twoargs 
    241         printTestHeader "Testing unhook( symbol, hook )" 
    242         hook, msg = nil, nil 
    243         greensType = "Curiel" 
    244  
    245         # First hook (out of order) 
    246         debug_msg "Removing first hook (out of order):" 
    247         assert_nothing_raised { 
    248             hook = @hobj.unhook( :wark, @hooks.first ) 
    249         } 
    250         assert_equal @hooks.first, hook 
    251         @hooks.shift 
    252  
    253         debug_msg "Hooks: %s" % @hobj.hooks.inspect 
    254  
    255         assert_nothing_raised { 
    256             msg = @hobj.wark( greensType ) 
    257         } 
    258         debug_msg msg.inspect 
    259         expectedMsg = greensType 
    260         @hooks.each {|proc| expectedMsg.replace( proc.call(expectedMsg) )} 
    261         assert_equal WarkMessage % expectedMsg, msg 
    262     end 
    263  
    264  
    265     ### Test removing all hooks 
    266     def test_09_unhook_all 
    267         printTestHeader "Testing behaviour after all hooks are unhooked" 
    268         hook, msg = nil, nil 
    269         greensType = "Curiel" 
    270  
    271         # Remaining hook 
    272         debug_msg "Removing all hooks:" 
    273         assert_nothing_raised { 
    274             @hobj.unhook_all( :wark ) 
    275         } 
    276  
    277         debug_msg "Hooks: %s" % @hobj.hooks.inspect 
    278  
    279         debug_msg "Wark after unhook all: " 
    280         assert_nothing_raised { 
    281             msg = @hobj.wark( greensType ) 
    282         } 
    283         debug_msg msg.inspect 
    284         assert_equal WarkMessage % greensType, msg 
    285  
    286         # Test to be sure #wark is unhooked 
    287         debug_msg "Should be completely unhooked now:" 
    288         assert_equal @hobj.method(:__unhooked_wark), @hobj.method( :wark ) 
     114        @hookableobj.__unhooked_test( 'thingerything' ).should == 'thingerything' 
    289115    end 
    290116