Changeset 288

Show
Ignore:
Timestamp:
09/30/08 15:00:29 (7 weeks ago)
Author:
ged
Message:
  • Updated build system
  • Finished converting statistic tests to specs.
Location:
trunk
Files:
1 removed
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/Rakefile

    r287 r288  
    270270### Task: cruise (Cruisecontrol task) 
    271271desc "Cruisecontrol build" 
    272 task :cruise => [:clean, :spec, :package] do |task| 
     272task :cruise => [:clean, 'spec:quiet', :package] do |task| 
    273273    raise "Artifacts dir not set." if ARTIFACTS_DIR.to_s.empty? 
    274274    artifact_dir = ARTIFACTS_DIR.cleanpath 
  • trunk/Rakefile.local

    • Property svn:keywords set to Date Rev Author URL Id
  • trunk/lib/fm/composedobject.rb

    r287 r288  
    8787                define_method( devel ) { self.statistic.developmental } 
    8888                define_method( "#{devel}=" ) {|arg| 
    89                     check_type( arg, FaerieMUD::DevelopmentalObject ) 
    9089                    self.statistic.developmental = arg 
    9190                } 
     
    121120            # just use Hydrogen. 
    122121            element = args.delete( :statistic ) || 1 
    123             @statistic = FaerieMUD::Statistic.new( 
    124                 self.class.statistic_names[0], 
    125                 self.class.statistic_names[1], 
    126                 element ) 
     122            devel, linear = self.class.statistic_names 
     123            @statistic = FaerieMUD::Statistic.new( element, devel, linear ) 
    127124 
    128125            super 
  • trunk/lib/fm/periodicobject/element.rb

    r287 r288  
    228228    ### atomic number or element name +elem+. 
    229229    def self::fetch( elem=1 ) 
     230        elem ||= 1 
    230231        FaerieMUD::Logger[ self ].debug "Fetching element %p" % [ elem ] 
    231232 
    232233        # Allow elem to be an Element object for implementations of element= 
    233234        # mutators. 
    234         return elem if elem.is_a?( self ) 
     235        if elem.is_a?( self ) 
     236            FaerieMUD::Logger[ self ].debug "self-referential fetch" 
     237            return elem 
     238        end 
    235239 
    236240        # Attempt to convert names or chemical symbols to atomic numbers. 
     
    240244                raise ArgumentError, "No such element '#{name}'" 
    241245        end 
     246 
     247        FaerieMUD::Logger[ self ].debug "...<%s(%s):%d>" % [ NAMES[elem], SYMBOLS[elem], elem ] 
    242248 
    243249        super( elem, 
  • trunk/lib/fm/statistic.rb

    r287 r288  
    6767    DEFAULT_LINEAR_NAME = "linear" 
    6868 
     69 
    6970    ############################################################# 
    7071    ### I N S T A N C E   M E T H O D S 
     
    7273 
    7374    ### Create a new Statistic object with the specified +devel_name+, 
    74     ### +linear_name+ and +element+. The +devel_name+ and +linear_name+ arguments 
    75     ### are the name of the developmental and linear aspects of the statistic, 
    76     ### respectively, and must be Strings or objects which respond to #to_s. The 
    77     ### +element+ argument is the primary aspect of the statistic, and can be 
    78     ### any valid argument to PeriodicObject's constructor. 
    79     def initialize( element=:H, devel_name=DEFAULT_DEVEL_NAME, linear_name=DEFAULT_LINEAR_NAME, options={} ) 
    80         @devel_name = devel_name || DEFAULT_DEVEL_NAME 
    81         @linear_name = linear_name || DEFAULT_LINEAR_NAME 
    82         @developmental = FaerieMUD::DevelopmentalObject::new 
     75    ### +linear_name+ and +element+. The +element+ argument is the primary 
     76    ### aspect of the statistic, and can be 
     77    ### any valid argument to PeriodicObject's constructor. The +devel_name+ and 
     78    ### +linear_name+ arguments are the name of the developmental and linear 
     79    ### aspects of the statistic, 
     80    ### respectively, and must be Strings or objects which respond to #to_s.  
     81    def initialize( element=:Li, devel_name=DEFAULT_DEVEL_NAME, linear_name=DEFAULT_LINEAR_NAME, options={} ) 
     82        @developmental     = FaerieMUD::DevelopmentalObject.new( element ) 
     83        @devel_name        = devel_name ? devel_name.to_s : DEFAULT_DEVEL_NAME 
     84        @linear_name       = linear_name ? linear_name.to_s : DEFAULT_LINEAR_NAME 
    8385        @linear_adjustment = 0 
    8486 
     
    118120    end 
    119121 
     122 
     123    ### Return a copy of the receiver with its linear aspect decremented by +value+. 
     124    def -( value ) 
     125        copy = self.dup 
     126        copy.linear -= value 
     127        return copy 
     128    end 
     129     
     130     
     131    ### Subtract +value+ from the linear aspect of the statistic. 
     132    ### :TODO: Decrement the developmental aspect once the linear is zero. 
     133    def decrement( value ) 
     134        self.linear -= value 
     135    end 
     136     
     137     
     138    ### Return a copy of the receiver with its linear aspect incremented by +value+. 
     139    def +( value ) 
     140        copy = self.dup 
     141        copy.linear += value 
     142        return copy 
     143    end 
     144     
     145 
     146    ### Add +value+ to the linear aspect of the statistic, with bounds-checking. 
     147    def increment( value ) 
     148        self.linear += value 
     149        return self 
     150    end 
     151     
    120152 
    121153    ### Return the linear aspect of the statistic (an Integer) 
     
    162194    def to_s 
    163195        "<%s/%s: %d/%d>" % [ 
    164             self.devel_name.capitalize, 
    165             self.linear_name.capitalize, 
     196            self.devel_name.to_s.capitalize, 
     197            self.linear_name.to_s.capitalize, 
    166198            self.developmental.to_i, 
    167199            self.linear, 
  • trunk/spec/fm/periodicobject/element_spec.rb

    r287 r288  
    7676    it "raises an appropriate exception for other invalid values" do 
    7777        lambda { 
    78             FaerieMUD::PeriodicObject::Element.fetch( nil ) 
     78            FaerieMUD::PeriodicObject::Element.fetch( Object ) 
    7979        }.should raise_error( ArgumentError, /no such element/i ) 
    8080    end 
  • trunk/spec/fm/statistic_spec.rb

    r287 r288  
    2323 
    2424describe FaerieMUD::Statistic do 
     25    include FaerieMUD::SpecHelpers, 
     26            FaerieMUD::TestConstants 
    2527 
     28    before( :all ) do 
     29        setup_logging( :crit ) 
     30    end 
     31     
     32    after( :all ) do 
     33        reset_logging() 
     34    end 
     35 
     36     
    2637    it "can be instantiated with no arguments" do 
    2738        stat = FaerieMUD::Statistic.new 
     39        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :Lithium ) 
    2840        stat.devel_name.should == 'developmental' 
     41        stat.linear_name.should == 'linear' 
    2942    end 
    3043 
     44    it "can be instantiated with an element name" do 
     45        stat = FaerieMUD::Statistic.new( 'Magnesium' ) 
     46        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :Mg ) 
     47        stat.devel_name.should == 'developmental' 
     48        stat.linear_name.should == 'linear' 
     49    end 
     50     
     51    it "can be instantiated with an atomic number" do 
     52        stat = FaerieMUD::Statistic.new( 28 ) 
     53        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :Ni ) 
     54        stat.devel_name.should == 'developmental' 
     55        stat.linear_name.should == 'linear' 
     56    end 
     57     
     58 
     59    it "can be instantiated with an element symbol" do 
     60        stat = FaerieMUD::Statistic.new( :Ac ) 
     61        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :Actinium ) 
     62        stat.devel_name.should == 'developmental' 
     63        stat.linear_name.should == 'linear' 
     64    end 
     65     
     66 
    3167    it "can be instantiated with a development name as a String" do 
    32         stat = FaerieMUD::Statistic.new( 'test1' ) 
     68        stat = FaerieMUD::Statistic.new( :n, 'test1' ) 
     69        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :N ) 
    3370        stat.devel_name.should == 'test1' 
     71        stat.linear_name.should == 'linear' 
    3472    end 
    3573     
    3674    it "can be instantiated with a development name as a Symbol" do 
    37         stat = FaerieMUD::Statistic.new( :test1 ) 
     75        stat = FaerieMUD::Statistic.new( :N, :test1 ) 
     76        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :N ) 
    3877        stat.devel_name.should == 'test1' 
     78        stat.linear_name.should == 'linear' 
    3979    end 
    4080 
    4181    it "can be instantiated with a development name and a linear name as a String" do 
    42         FaerieMUD::Statistic.new( 'test1', 'test2' ) 
     82        stat = FaerieMUD::Statistic.new( :Nitrogen, 'test1', 'test2' ) 
     83        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :N ) 
     84        stat.devel_name.should == 'test1' 
     85        stat.linear_name.should == 'test2' 
    4386    end 
    4487     
    4588    it "can be instantiated with a development name and a linear name as a Symbol" do 
    46         FaerieMUD::Statistic.new( 'test1', :test2 ) 
     89        stat = FaerieMUD::Statistic.new( 17, 'test1', :test2 ) 
     90        stat.developmental.element.should == FaerieMUD::PeriodicObject::Element.fetch( :Chlorine ) 
     91        stat.devel_name.should == 'test1' 
     92        stat.linear_name.should == 'test2' 
    4793    end 
    4894 
    49     it "can be instantiated with both names and an element name" do 
    50         FaerieMUD::Statistic.new( 'test1', 'test2', 'Nitrogen' ) 
    51     end 
    5295     
    53     it "can be instantiated with both names and an element symbol" do 
    54         FaerieMUD::Statistic.new( 'test1', 'test2', 'Nitrogen' ) 
    55     end 
    56      
    57     it "can be instantiated with both names and a numeric element" 
    58  
    59     it "provides a reasonable default for an unspecified development name" 
    60     it "provides a reasonable default for an unspecified linear name" 
    61     it "provides a reasonable default for an unspecified linear name" 
    62      
     96    describe "instance" do 
     97        before( :each ) do 
     98            @stat = FaerieMUD::Statistic.new( :Se, 'vitality', 'energy' ) 
     99        end 
    63100 
    64101 
    65     # Argument sets to test the constructor with, along with what the result 
    66     # should be. 
    67     ArgSets = { 
    68         # Valid args: { :devel_name, :linear_name, :element } 
    69         []                              => FaerieMUD::Statistic, 
    70         ["test1"]                       => FaerieMUD::Statistic, 
    71         [:test1]                        => FaerieMUD::Statistic, 
    72         [nil, "test2"]                  => FaerieMUD::Statistic, 
    73         [nil, :test2]                   => FaerieMUD::Statistic, 
    74         ["test1", "test2", nil]         => FaerieMUD::Statistic, 
    75         ["test1", "test2"]              => FaerieMUD::Statistic, 
    76         [:test1, :test2]                => FaerieMUD::Statistic, 
    77         ["test1", "test2", :Yb]         => FaerieMUD::Statistic, 
    78         [nil, nil, :Yb]                 => FaerieMUD::Statistic, 
    79         ["test1", "test2", :Lithium]    => FaerieMUD::Statistic, 
    80         ["test1", "test2", "Mg"]        => FaerieMUD::Statistic, 
    81         ["test1", "test2", :boron]      => FaerieMUD::Statistic, 
    82         ["test1", "test2", "es"]        => FaerieMUD::Statistic, 
    83         ["test1", "test2", 71 ]         => FaerieMUD::Statistic, 
    84  
    85         # Invalid arg lists 
    86         ["test1", "test2", :foo]        => ArgumentError, 
    87         ["test1", "test2", false]       => TypeError, 
     102        it "doesn't allow its linear value to fall below 0" do 
     103            @stat.linear = -10 
     104            @stat.linear.should == 0 
     105        end 
    88106         
    89     } 
    90  
    91     Interface = [ 
    92         :devel_name, 
    93         :devel_name=, 
    94         :linear_name, 
    95         :linear_name=, 
    96         :element, 
    97         :element=, 
    98         :developmental, 
    99         :linear_adjustment, 
    100         :linear, 
    101         :linear=, 
    102         :average, 
    103         :to_s, 
    104     ]        
    105  
    106     # The arguments to be used for the testing statistic 
    107     TestArgSet = {:devel_name => "test1", :linear_name => "test2", :element => 22} 
    108  
    109  
    110     ################################################################# 
    111     ### E X A M P L E S 
    112     ################################################################# 
    113  
    114     ### Instance test 
    115     def test_00_Instance 
    116         rval = nil 
    117         printTestHeader "Statistic: Instantiation" 
    118  
    119         assert_instance_of Class, FaerieMUD::Statistic 
    120  
    121         # Test various valid argument configurations 
    122         ArgSets.each {|arglist, result| 
    123             args = {} 
    124             args[:devel_name] = arglist[0] unless arglist[0].nil? 
    125             args[:linear_name] = arglist[1] unless arglist[1].nil? 
    126             args[:element] = arglist[2] unless arglist[2].nil? 
    127  
    128             description = "for args = %s, result = %s" % 
    129                 [ args.inspect, result.inspect ] 
    130                  
    131             if result < Exception 
    132                 assert_raises( result, description ) { 
    133                     FaerieMUD::Statistic::new( args ) 
    134                 } 
    135             else 
    136                 elem = FaerieMUD::PeriodicObject::Element::fetch( args[:element] || 1 ) 
    137                 assert_nothing_raised( description ) { 
    138                     rval = FaerieMUD::Statistic::new( args ) 
    139                 } 
    140                 assert_instance_of FaerieMUD::Statistic, rval 
    141                  
    142             end 
    143         } 
    144  
    145         self.class.addSetupBlock { 
    146             @stat = FaerieMUD::Statistic::new( TestArgSet ) 
    147         } 
    148         self.class.addTeardownBlock { 
    149             @stat = nil 
    150         } 
    151     end 
    152  
    153  
    154     ### Test the statistic's interface 
    155     def test_10_interface 
    156         rval = nil 
    157         printTestHeader "Statistic: Interface" 
    158  
    159         Interface.each do |meth| 
    160             assert_respond_to @stat, meth 
    161             if /=/ =~ meth.to_s 
    162                 assert_raises( ArgumentError, "#{meth}" ) { @stat.send(meth) } 
    163             else 
    164                 assert_nothing_raised( "#{meth}" ) { rval = @stat.send(meth) } 
    165             end 
     107         
     108        it "doesn't allow its linear value to exceed its developmental aspect" do 
     109            @stat.linear = 60 
     110            @stat.linear.should == 34 
    166111        end 
    167     end 
    168  
    169  
    170     ### Test the developmental part 
    171     def test_20_developmental 
    172         rval = nil 
    173         printTestHeader "Statistic: Developmental Aspect" 
    174  
    175         assert_nothing_raised { rval = @stat.developmental } 
    176         assert_instance_of FaerieMUD::DevelopmentalObject, rval 
    177     end 
    178  
    179  
    180     ### Test linear part 
    181     def test_25_linear 
    182         rval = nil 
    183         printTestHeader "Statistic: Linear Aspect" 
    184  
    185         # The linear value should be the same as the initial developmental value 
    186         assert_nothing_raised { rval = @stat.linear } 
    187         assert_equal TestArgSet[:element], rval, "initial linear value" 
    188  
    189         # Subtract increasing values, making sure the offset stuff is working 
    190         # correctly. 
    191         [ 0, 1, 4, 12 ].inject {|total, val| 
    192             assert_nothing_raised { 
    193                 @stat.linear -= val 
    194                 rval = @stat.linear 
    195             } 
    196             assert_equal TestArgSet[:element] - total - val, rval, 
    197                 "linear value after subtracting #{val} (#{total} total)" 
    198             total + val 
    199         } 
    200  
    201         # Try subtracting something that would make it negative. It should be 
    202         # zero. 
    203         assert_nothing_raised { 
    204             @stat.linear -= TestArgSet[:element] * 2 
    205             rval = @stat.linear 
    206         } 
    207         assert_equal 0, rval, 
    208             "linear value after subtracting #{TestArgSet[:element] * 2}" 
    209  
    210  
    211         # Try adding a number that would make it exceed the developmental 
    212         # value. It should max out at what it started at. 
    213         assert_nothing_raised { 
    214             @stat.linear += TestArgSet[:element] * 2 
    215             rval = @stat.linear 
    216         } 
    217         assert_equal TestArgSet[:element], rval, 
    218             "linear value after incrementing out-of-bounds" 
    219     end  
    220  
    221  
    222     ### Test duplication 
    223     def test_30_Duplication 
    224         rval = nil 
    225  
    226         assert_nothing_raised { rval = @stat.clone } 
    227         assert_instance_of FaerieMUD::Statistic, rval 
    228         assert_not_same @stat, rval 
    229         assert_same @stat.developmental.element, 
    230             rval.developmental.element, 
    231             "Elements of the statistic should be identical" 
     112         
     113         
     114        it "allows its linear value to be decremented" do 
     115            @stat.decrement( 40 ) 
     116            @stat.linear.should == 0 
     117        end 
     118         
     119        it "allows its linear value to be incremented" do 
     120            @stat.linear = 0 
     121            @stat.increment( 20 ) 
     122            @stat.linear.should == 20 
     123        end 
     124         
    232125    end 
    233126