Wiki

Changes between Version 8 and Version 9 of Methods

Show
Ignore:
Timestamp:
12/03/13 09:24:01 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Methods

    v8 v9  
    6666sum takes a variable number of integer args and returns an integer. 
    6767 
     68== Tests, Contracts and Code == 
     69 
     70The above refers to simple method code or method body code. [[BR]] 
     71You can also specify Tests (for the method) and contract requirements on the method. [[BR]] 
     72To do this the method is tagged with named clauses: [[BR]] 
     73  * '''test''' clauses for any tests,  
     74  * '''require''' and '''ensure''' clauses for the pre and post conditions specifying the method contracts and a  
     75  * '''body''' clause for the method code. [[BR]]  
     76 
     77If given, the order of these clauses must be : 
     78  * Test clauses 
     79  * contract clauses (require and/or ensure) 
     80  * body clause 
     81  
    6882== Special Methods == 
    6983These are named methods on a class that have some special responsibility or relationship to the Object Model[[BR]] 
     
    157171   .counter += 1 #method body - not additionally indented 
    158172   .val += i 
    159    return .val      
     173   return .val  
     174 
     175# Heres a full example giving all the clauses 
     176def simpleBump(i as int) as int 
     177   require  # prerequisite contract - in indented clause (1 or many expressions) 
     178      i >0                
     179   ensure   # postcondition contract - in indented clause (1 or many expressions) 
     180      .val > old .val   
     181   test          # unit test clause 
     182       a = Inst() 
     183       assert a.counter == 0 
     184       assert a.val == 0 
     185       a.simpleBump(1) 
     186       assert a.counter ==1 
     187       assert a.val == 1 
     188   test num2         # another unit test clause (named) 
     189       a = Inst() 
     190       a.simpleBump(2) 
     191       assert a.counter == 1 
     192       assert a.val == 2 
     193 
     194   body            #method body - tagged and additionally indented 
     195      .counter += 1  
     196      .val += i 
     197      return .val      
     198      
    160199}}}  
     200 
     201Return to LanguageTopics