Changeset 1725

Show
Ignore:
Timestamp:
03/14/07 21:48:44
Author:
pvanhoof
Message:

Design by contract assertions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r1723 r1725  
    33        * tny_folder_store_remove now specifies and its implementation will 
    44        unsubscribe all the observers of the folder being removed. 
     5        * Design by contract assertions 
    56 
    67        * This was a major API change 
  • trunk/libtinymail/tny-account.c

    r1718 r1725  
    2020#include <config.h> 
    2121 
     22#ifdef DBC 
     23#include <string.h> 
     24#endif 
     25 
    2226#include <tny-account.h> 
    23  
    2427 
    2528/** 
     
    4447tny_account_matches_url_string (TnyAccount *self, const gchar *url_string) 
    4548{ 
    46 #ifdef DEBUG 
    47         if (!TNY_ACCOUNT_GET_IFACE (self)->matches_url_string_func) 
    48                 g_critical ("You must implement tny_account_matches_url_string\n"); 
    49 #endif 
    50  
    51         return TNY_ACCOUNT_GET_IFACE (self)->matches_url_string_func (self, url_string); 
     49        gboolean retval; 
     50 
     51#ifdef DBC /* require */ 
     52        g_assert (TNY_IS_ACCOUNT (self)); 
     53        g_assert (url_string); 
     54        g_assert (strlen (url_string) > 0); 
     55        g_assert (TNY_ACCOUNT_GET_IFACE (self)->matches_url_string_func != NULL); 
     56#endif 
     57 
     58        retval = TNY_ACCOUNT_GET_IFACE (self)->matches_url_string_func (self, url_string); 
     59 
     60#ifdef DBC /* ensure*/ 
     61#endif 
     62 
     63        return retval; 
    5264} 
    5365 
     
    6274tny_account_cancel (TnyAccount *self) 
    6375{ 
    64 #ifdef DEBUG 
    65         if (!TNY_ACCOUNT_GET_IFACE (self)->cancel_func) 
    66                g_critical ("You must implement tny_account_cancel\n"); 
     76#ifdef DBC /* require */ 
     77        g_assert (TNY_IS_ACCOUNT (self)); 
     78        g_assert (TNY_ACCOUNT_GET_IFACE (self)->cancel_func != NULL); 
    6779#endif 
    6880 
    6981        TNY_ACCOUNT_GET_IFACE (self)->cancel_func (self); 
     82 
     83#ifdef BDC /* ensure */ 
     84#endif 
     85 
    7086        return; 
    7187} 
     
    89105tny_account_get_account_type (TnyAccount *self) 
    90106{ 
    91 #ifdef DEBUG 
    92         if (!TNY_ACCOUNT_GET_IFACE (self)->get_account_type_func) 
    93                 g_critical ("You must implement tny_account_get_account_type\n"); 
    94 #endif 
    95  
    96         return TNY_ACCOUNT_GET_IFACE (self)->get_account_type_func (self); 
     107TnyAccountType retval; 
     108 
     109#ifdef DBC /* require */ 
     110        g_assert (TNY_IS_ACCOUNT (self)); 
     111        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_account_type_func != NULL); 
     112#endif 
     113 
     114        retval = TNY_ACCOUNT_GET_IFACE (self)->get_account_type_func (self); 
     115 
     116#ifdef DBC /* ensure */ 
     117#endif 
     118 
     119        return retval; 
    97120} 
    98121 
     
    109132tny_account_is_connected (TnyAccount *self) 
    110133{ 
    111 #ifdef DEBUG 
    112         if (!TNY_ACCOUNT_GET_IFACE (self)->is_connected_func) 
    113                 g_critical ("You must implement tny_account_is_connected\n"); 
    114 #endif 
    115  
    116         return TNY_ACCOUNT_GET_IFACE (self)->is_connected_func (self); 
     134gboolean retval; 
     135 
     136#ifdef DBC /* require */ 
     137        g_assert (TNY_IS_ACCOUNT (self)); 
     138        g_assert (TNY_ACCOUNT_GET_IFACE (self)->is_connected_func != NULL); 
     139#endif 
     140 
     141        retval = TNY_ACCOUNT_GET_IFACE (self)->is_connected_func (self); 
     142 
     143#ifdef DBC /* ensure */ 
     144#endif 
     145 
     146        return retval; 
    117147} 
    118148 
     
    133163tny_account_get_id (TnyAccount *self) 
    134164{ 
    135 #ifdef DEBUG 
    136         if (!TNY_ACCOUNT_GET_IFACE (self)->get_id_func) 
    137                 g_critical ("You must implement tny_account_get_id\n"); 
    138 #endif 
    139  
    140         return TNY_ACCOUNT_GET_IFACE (self)->get_id_func (self); 
     165        const gchar *retval; 
     166 
     167#ifdef DBC /* require */ 
     168        g_assert (TNY_IS_ACCOUNT (self)); 
     169        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_id_func != NULL); 
     170#endif 
     171 
     172        retval = TNY_ACCOUNT_GET_IFACE (self)->get_id_func (self); 
     173 
     174#ifdef DBC /* ensure */ 
     175        g_assert (retval); 
     176        g_assert (strlen (retval) > 0); 
     177#endif 
     178 
     179        return retval; 
    141180} 
    142181 
     
    152191tny_account_set_name (TnyAccount *self, const gchar *name) 
    153192{ 
    154 #ifdef DEBUG 
    155         if (!TNY_ACCOUNT_GET_IFACE (self)->set_name_func) 
    156                 g_critical ("You must implement tny_account_set_name\n"); 
     193#ifdef DBC /* require */ 
     194        g_assert (TNY_IS_ACCOUNT (self)); 
     195        g_assert (name); 
     196        g_assert (strlen (name) > 0); 
     197        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_name_func != NULL); 
    157198#endif 
    158199 
    159200        TNY_ACCOUNT_GET_IFACE (self)->set_name_func (self, name); 
     201 
     202#ifdef DBC /* require */ 
     203        g_assert (!strcmp (tny_account_get_name (self), name)); 
     204#endif 
     205 
    160206        return; 
    161207} 
     
    175221tny_account_set_mech (TnyAccount *self, const gchar *mech) 
    176222{ 
    177 #ifdef DEBUG 
    178         if (!TNY_ACCOUNT_GET_IFACE (self)->set_mech_func) 
    179                 g_critical ("You must implement tny_account_set_mech\n"); 
     223#ifdef DBC /* require */ 
     224        g_assert (TNY_IS_ACCOUNT (self)); 
     225        g_assert (mech); 
     226        g_assert (strlen (mech) > 0); 
     227        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_mech_func != NULL); 
    180228#endif 
    181229 
    182230        TNY_ACCOUNT_GET_IFACE (self)->set_mech_func (self, mech); 
     231 
     232#ifdef DBC /* require */ 
     233        g_assert (!strcmp (tny_account_get_mech (self), mech)); 
     234#endif 
     235 
    183236        return; 
    184237} 
     
    197250tny_account_set_id (TnyAccount *self, const gchar *id) 
    198251{ 
    199 #ifdef DEBUG 
    200         if (!TNY_ACCOUNT_GET_IFACE (self)->set_id_func) 
    201                 g_critical ("You must implement tny_account_set_id\n"); 
     252#ifdef DBC /* require */ 
     253        g_assert (TNY_IS_ACCOUNT (self)); 
     254        g_assert (id); 
     255        g_assert (strlen (id) > 0); 
     256        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_id_func != NULL); 
    202257#endif 
    203258 
    204259        TNY_ACCOUNT_GET_IFACE (self)->set_id_func (self, id); 
     260 
     261 
     262#ifdef DBC /* require */ 
     263        g_assert (!strcmp (tny_account_get_id (self), id)); 
     264#endif 
     265 
    205266        return; 
    206267} 
     
    253314tny_account_set_forget_pass_func (TnyAccount *self, TnyForgetPassFunc forget_pass_func) 
    254315{ 
    255 #ifdef DEBUG 
    256         if (!TNY_ACCOUNT_GET_IFACE (self)->set_forget_pass_func_func) 
    257                g_critical ("You must implement tny_account_set_forget_pass_func\n"); 
     316#ifdef DBC /* require */ 
     317        g_assert (TNY_IS_ACCOUNT (self)); 
     318        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_forget_pass_func_func != NULL); 
    258319#endif 
    259320 
    260321        TNY_ACCOUNT_GET_IFACE (self)->set_forget_pass_func_func (self, forget_pass_func); 
     322 
     323#ifdef DBC /* ensure */ 
     324        g_assert (tny_account_get_forget_pass_func (self) == forget_pass_func); 
     325#endif 
     326 
    261327        return; 
    262328} 
     
    272338tny_account_get_forget_pass_func (TnyAccount *self) 
    273339{ 
    274 #ifdef DEBUG 
    275         if (!TNY_ACCOUNT_GET_IFACE (self)->get_forget_pass_func_func) 
    276                 g_critical ("You must implement tny_account_get_forget_pass_func\n"); 
    277 #endif 
    278  
    279         return TNY_ACCOUNT_GET_IFACE (self)->get_forget_pass_func_func (self); 
     340        TnyForgetPassFunc retval; 
     341 
     342#ifdef DBC /* require */ 
     343        g_assert (TNY_IS_ACCOUNT (self)); 
     344        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_forget_pass_func_func != NULL); 
     345#endif 
     346 
     347        retval = TNY_ACCOUNT_GET_IFACE (self)->get_forget_pass_func_func (self); 
     348 
     349#ifdef DBC /* ensure */ 
     350#endif 
     351 
     352        return retval; 
    280353} 
    281354 
     
    297370tny_account_set_url_string (TnyAccount *self, const gchar *url_string) 
    298371{ 
    299 #ifdef DEBUG 
    300         if (!TNY_ACCOUNT_GET_IFACE (self)->set_url_string_func) 
    301                 g_critical ("You must implement tny_account_set_url_string\n"); 
     372#ifdef DBC /* require */ 
     373        g_assert (TNY_IS_ACCOUNT (self)); 
     374        g_assert (url_string); 
     375        g_assert (strlen (url_string) > 0); 
     376        g_assert (strstr (url_string, "://") != NULL); 
     377        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_url_string_func != NULL); 
    302378#endif 
    303379 
    304380        TNY_ACCOUNT_GET_IFACE (self)->set_url_string_func (self, url_string); 
     381 
     382#ifdef DBC /* ensure */ 
     383        /* TNY TODO: It's possible that tny_account_get_url_string strips the 
     384         * password. It would be interesting to have a contract check that  
     385         * deals with this. */ 
     386#endif 
     387 
    305388        return; 
    306389} 
     
    319402tny_account_set_proto (TnyAccount *self, const gchar *proto) 
    320403{ 
    321 #ifdef DEBUG 
    322         if (!TNY_ACCOUNT_GET_IFACE (self)->set_proto_func) 
    323                 g_critical ("You must implement tny_account_set_proto\n"); 
     404#ifdef DBC /* require */ 
     405        g_assert (TNY_IS_ACCOUNT (self)); 
     406        g_assert (proto); 
     407        g_assert (strlen (proto) > 0); 
     408        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_proto_func != NULL); 
    324409#endif 
    325410 
    326411        TNY_ACCOUNT_GET_IFACE (self)->set_proto_func (self, proto); 
     412 
     413#ifdef DBC /* ensure */ 
     414        g_assert (!strcmp (tny_account_get_proto (self), proto)); 
     415#endif 
     416 
    327417        return; 
    328418} 
     
    341431tny_account_set_user (TnyAccount *self, const gchar *user) 
    342432{ 
    343 #ifdef DEBUG 
    344         if (!TNY_ACCOUNT_GET_IFACE (self)->set_user_func) 
    345                 g_critical ("You must implement tny_account_set_user\n"); 
     433#ifdef DBC /* require */ 
     434        g_assert (TNY_IS_ACCOUNT (self)); 
     435        g_assert (user); 
     436        g_assert (strlen (user) > 0); 
     437        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_user_func != NULL); 
    346438#endif 
    347439 
    348440        TNY_ACCOUNT_GET_IFACE (self)->set_user_func (self, user); 
     441 
     442#ifdef DBC /* ensure */ 
     443        g_assert (!strcmp (tny_account_get_user (self), user)); 
     444#endif 
     445 
    349446        return; 
    350447} 
     
    363460tny_account_set_hostname (TnyAccount *self, const gchar *host) 
    364461{ 
    365 #ifdef DEBUG 
    366         if (!TNY_ACCOUNT_GET_IFACE (self)->set_hostname_func) 
    367                 g_critical ("You must implement tny_account_set_hostname\n"); 
     462#ifdef DBC /* require */ 
     463        g_assert (TNY_IS_ACCOUNT (self)); 
     464        g_assert (host); 
     465        g_assert (strlen (host) > 0); 
     466        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_hostname_func != NULL); 
    368467#endif 
    369468 
    370469        TNY_ACCOUNT_GET_IFACE (self)->set_hostname_func (self, host); 
     470 
     471#ifdef DBC /* ensure */ 
     472        g_assert (!strcmp (tny_account_get_hostname (self), host)); 
     473#endif 
     474 
    371475        return; 
    372476} 
     
    386490tny_account_set_port (TnyAccount *self, guint port) 
    387491{ 
    388 #ifdef DEBUG 
    389         if (!TNY_ACCOUNT_GET_IFACE (self)->set_port_func) 
    390                 g_critical ("You must implement tny_account_set_port\n"); 
     492#ifdef DBC /* require */ 
     493        g_assert (TNY_IS_ACCOUNT (self)); 
     494        g_assert (port <= 65536); 
     495        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_port_func != NULL); 
    391496#endif 
    392497 
    393498        TNY_ACCOUNT_GET_IFACE (self)->set_port_func (self, port); 
     499 
     500#ifdef DBC /* ensure */ 
     501        g_assert (tny_account_get_port (self) == port); 
     502#endif 
     503 
    394504        return; 
    395505} 
     
    457567tny_account_set_pass_func (TnyAccount *self, TnyGetPassFunc get_pass_func) 
    458568{ 
    459 #ifdef DEBUG 
    460         if (!TNY_ACCOUNT_GET_IFACE (self)->set_pass_func_func) 
    461                g_critical ("You must implement tny_account_set_pass_func\n"); 
     569#ifdef DBC /* require */ 
     570        g_assert (TNY_IS_ACCOUNT (self)); 
     571        g_assert (TNY_ACCOUNT_GET_IFACE (self)->set_pass_func_func != NULL); 
    462572#endif 
    463573 
    464574        TNY_ACCOUNT_GET_IFACE (self)->set_pass_func_func (self, get_pass_func); 
     575 
     576#ifdef DBC /* ensure */ 
     577        g_assert (tny_account_get_pass_func (self) == get_pass_func); 
     578#endif 
     579 
    465580        return; 
    466581} 
     
    478593tny_account_get_proto (TnyAccount *self) 
    479594{ 
    480 #ifdef DEBUG 
    481         if (!TNY_ACCOUNT_GET_IFACE (self)->get_proto_func) 
    482                 g_critical ("You must implement tny_account_get_proto\n"); 
    483 #endif 
    484  
    485         return TNY_ACCOUNT_GET_IFACE (self)->get_proto_func (self); 
     595        const gchar *retval; 
     596 
     597#ifdef DBC /* require */ 
     598        g_assert (TNY_IS_ACCOUNT (self)); 
     599        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_proto_func != NULL); 
     600#endif 
     601 
     602        retval = TNY_ACCOUNT_GET_IFACE (self)->get_proto_func (self); 
     603 
     604#ifdef DBC /* ensure */ 
     605#endif 
     606 
     607        return retval; 
    486608} 
    487609 
     
    504626tny_account_get_url_string (TnyAccount *self) 
    505627{ 
    506 #ifdef DEBUG 
    507         if (!TNY_ACCOUNT_GET_IFACE (self)->get_url_string_func) 
    508                 g_critical ("You must implement tny_account_get_url_string\n"); 
    509 #endif 
    510  
    511         return TNY_ACCOUNT_GET_IFACE (self)->get_url_string_func (self); 
     628        gchar *retval; 
     629 
     630#ifdef DBC /* require */ 
     631        g_assert (TNY_IS_ACCOUNT (self)); 
     632        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_url_string_func != NULL); 
     633#endif 
     634 
     635        retval = TNY_ACCOUNT_GET_IFACE (self)->get_url_string_func (self); 
     636 
     637#ifdef DBC /* ensure */ 
     638        if (retval) 
     639                g_assert (strstr (retval, "://") != NULL); 
     640#endif 
     641 
     642        return retval; 
     643 
    512644} 
    513645 
     
    516648 * @self: a #TnyAccount object 
    517649 *  
    518  * Get the user or login of @self. The returned value should not be freed. 
     650 * Get the user or login of @self. The returned value should not be freed. The 
     651 * returned value van be NULL in case of no user. 
    519652 *  
    520653 * Return value: the user as a read-only string 
     
    524657tny_account_get_user (TnyAccount *self) 
    525658{ 
    526 #ifdef DEBUG 
    527         if (!TNY_ACCOUNT_GET_IFACE (self)->get_user_func) 
    528                 g_critical ("You must implement tny_account_get_user\n"); 
    529 #endif 
    530  
    531         return TNY_ACCOUNT_GET_IFACE (self)->get_user_func (self); 
     659        const gchar *retval; 
     660 
     661#ifdef DBC /* require */ 
     662        g_assert (TNY_IS_ACCOUNT (self)); 
     663        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_user_func != NULL); 
     664#endif 
     665 
     666        retval = TNY_ACCOUNT_GET_IFACE (self)->get_user_func (self); 
     667 
     668#ifdef DBC /* ensure */ 
     669#endif 
     670 
     671        return retval; 
    532672} 
    533673 
     
    537677 *  
    538678 * Get the human readable name of @self. The returned value should not  
    539  * be freed. 
     679 * be freed. The returned value van be NULL in case of no human reabable name. 
    540680 *  
    541681 * Return value: the human readable name as a read-only string 
     
    545685tny_account_get_name (TnyAccount *self) 
    546686{ 
    547 #ifdef DEBUG 
    548         if (!TNY_ACCOUNT_GET_IFACE (self)->get_name_func) 
    549                 g_critical ("You must implement tny_account_get_name\n"); 
    550 #endif 
    551  
    552         return TNY_ACCOUNT_GET_IFACE (self)->get_name_func (self); 
     687        const gchar *retval; 
     688 
     689#ifdef DBC /* require */ 
     690        g_assert (TNY_IS_ACCOUNT (self)); 
     691        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_name_func != NULL); 
     692#endif 
     693 
     694        retval = TNY_ACCOUNT_GET_IFACE (self)->get_name_func (self); 
     695 
     696#ifdef DBC /* ensure */ 
     697#endif 
     698 
     699        return retval; 
    553700} 
    554701 
     
    558705 * @self: a #TnyAccount object 
    559706 *  
    560  * Get the authentication mechanism for this account. Default is "PLAIN". 
     707 * Get the authentication mechanism for this account. Default is "PLAIN". The 
     708 * returned value can be NULL, in which case a undefined default is used. 
    561709 *  
    562710 * Return value: the authentication mechanism as a read-only string 
     
    566714tny_account_get_mech (TnyAccount *self) 
    567715{ 
    568 #ifdef DEBUG 
    569         if (!TNY_ACCOUNT_GET_IFACE (self)->get_mech_func) 
    570                 g_critical ("You must implement tny_account_get_mech\n"); 
    571 #endif 
    572  
    573         return TNY_ACCOUNT_GET_IFACE (self)->get_mech_func (self); 
     716        const gchar *retval; 
     717 
     718#ifdef DBC /* require */ 
     719        g_assert (TNY_IS_ACCOUNT (self)); 
     720        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_mech_func != NULL); 
     721#endif 
     722 
     723        retval = TNY_ACCOUNT_GET_IFACE (self)->get_mech_func (self); 
     724 
     725#ifdef DBC /* ensure */ 
     726#endif 
     727 
     728        return retval; 
    574729} 
    575730 
     
    579734 * @self: a #TnyAccount object 
    580735 *  
    581  * Get the hostname of @self. The returned value should not be freed. 
     736 * Get the hostname of @self. The returned value should not be freed. The  
     737 * returned value can be NULL, in which case no hostname is set (for example  
     738 * for a local account). 
    582739 *  
    583740 * Return value: the hostname as a read-only string 
     
    587744tny_account_get_hostname (TnyAccount *self) 
    588745{ 
    589 #ifdef DEBUG 
    590         if (!TNY_ACCOUNT_GET_IFACE (self)->get_hostname_func) 
    591                 g_critical ("You must implement tny_account_get_hostname\n"); 
    592 #endif 
    593  
    594         return TNY_ACCOUNT_GET_IFACE (self)->get_hostname_func (self); 
     746        const gchar *retval; 
     747 
     748#ifdef DBC /* require */ 
     749        g_assert (TNY_IS_ACCOUNT (self)); 
     750        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_hostname_func != NULL); 
     751#endif 
     752 
     753        retval = TNY_ACCOUNT_GET_IFACE (self)->get_hostname_func (self); 
     754 
     755#ifdef DBC /* ensure */ 
     756#endif 
     757 
     758        return retval; 
    595759} 
    596760 
     
    607771tny_account_get_port (TnyAccount *self) 
    608772{ 
    609 #ifdef DEBUG 
    610         if (!TNY_ACCOUNT_GET_IFACE (self)->get_port_func) 
    611                 g_critical ("You must implement tny_account_get_port\n"); 
    612 #endif 
    613  
    614         return TNY_ACCOUNT_GET_IFACE (self)->get_port_func (self); 
     773        guint retval; 
     774 
     775#ifdef DBC /* require */ 
     776        g_assert (TNY_IS_ACCOUNT (self)); 
     777        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_port_func != NULL); 
     778#endif 
     779 
     780        retval = TNY_ACCOUNT_GET_IFACE (self)->get_port_func (self); 
     781 
     782#ifdef DBC /* ensure */ 
     783        g_assert (retval <= 65536); 
     784#endif 
     785 
     786        return retval; 
    615787} 
    616788/** 
     
    624796tny_account_get_pass_func (TnyAccount *self) 
    625797{ 
    626 #ifdef DEBUG 
    627         if (!TNY_ACCOUNT_GET_IFACE (self)->get_pass_func_func) 
    628                 g_critical ("You must implement tny_account_get_pass_func\n"); 
    629 #endif 
    630  
    631         return TNY_ACCOUNT_GET_IFACE (self)->get_pass_func_func (self); 
     798        TnyGetPassFunc retval; 
     799 
     800#ifdef DBC /* require */ 
     801        g_assert (TNY_IS_ACCOUNT (self)); 
     802        g_assert (TNY_ACCOUNT_GET_IFACE (self)->get_port_func != NULL); 
     803#endif 
     804 
     805        retval = TNY_ACCOUNT_GET_IFACE (self)->get_pass_func_func (self); 
     806 
     807#ifdef DBC /* ensure */ 
     808#endif 
     809 
     810        return retval; 
    632811} 
    633812