Changeset 56d62f for src


Ignore:
Timestamp:
Jan 16, 2011, 6:34:28 PM (15 years ago)
Author:
Frederik Heber <heber@…>
Children:
fe056c
Parents:
192c04
Message:

Placed various Observer stubs into own module.

  • Library version is now 4:0:1, API version is still 1.0.7.
  • the stubs are also much easier to read when declared additionally in a header file.
Location:
src
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/Patterns/unittests/Makefile.am

    r192c04 r56d62f  
    101101ObserverUnitTest_SOURCES = $(top_srcdir)/src/unittests/UnitTestMain.cpp \
    102102        ObserverUnitTest.cpp \
    103         ObserverUnitTest.hpp
     103        ObserverUnitTest.hpp \
     104        stubs/ObserverStub.cpp \
     105        stubs/ObserverStub.hpp
    104106nodist_ObserverUnitTest_SOURCES = \
    105107        ../Observer.hpp \
  • src/Patterns/unittests/ObserverUnitTest.cpp

    r192c04 r56d62f  
    2323#include <set>
    2424
    25 #include "Observer.hpp"
    26 #include "ObservedIterator.hpp"
    2725#include "Assert.hpp"
    2826
    2927#include <iostream>
     28
     29#include "stubs/ObserverStub.hpp"
    3030
    3131#include "ObserverUnitTest.hpp"
     
    3939
    4040/******************* Test stubs ************************/
    41 
    42 class UpdateCountObserver : public Observer {
    43 public:
    44   UpdateCountObserver() :
    45     Observer("UpdateCountObserver"),
    46     updates(0)
    47   {};
    48   void update(Observable *publisher){
    49     updates++;
    50   }
    51   void subjectKilled(Observable *publisher) {
    52   }
    53   int updates;
    54 };
    55 
    56 class SimpleObservable : public Observable {
    57 public:
    58   SimpleObservable() :
    59     Observable("SimpleObservable")
    60   {}
    61 
    62   void changeMethod() {
    63     OBSERVE;
    64     int i = 0;
    65     i++;
    66   }
    67 };
    68 
    69 class CallObservable : public Observable {
    70 public:
    71   CallObservable() :
    72     Observable("CallObservable")
    73   {}
    74 
    75   void changeMethod1() {
    76     OBSERVE;
    77     int i = 0;
    78     i++;
    79   }
    80 
    81   void changeMethod2() {
    82     OBSERVE;
    83     int i = 0;
    84     i++;
    85     changeMethod1();
    86   }
    87 };
    88 
    89 class BlockObservable : public Observable {
    90 public:
    91   BlockObservable() :
    92     Observable("BlockObservable")
    93   {}
    94 
    95   void changeMethod1(){
    96     OBSERVE;
    97     // test if we report correctly as blocked
    98     CPPUNIT_ASSERT(isBlocked());
    99   }
    100 
    101   void changeMethod2(){
    102     OBSERVE;
    103     internalMethod1();
    104     internalMethod2();
    105   }
    106 
    107   void internalMethod1(){
    108     // we did not block, but our caller did...
    109     // see if this is found
    110     CPPUNIT_ASSERT(isBlocked());
    111   }
    112 
    113   void internalMethod2(){
    114     OBSERVE;
    115     // Both this method and the caller do block
    116     // Does the reporting still work as expected?
    117     CPPUNIT_ASSERT(isBlocked());
    118   }
    119 
    120   void noChangeMethod(){
    121     // No Block introduced here
    122     // reported correctely?
    123     CPPUNIT_ASSERT(!isBlocked());
    124   }
    125 };
    126 
    127 class SuperObservable : public Observable {
    128 public:
    129   SuperObservable():
    130     Observable("SuperObservable")
    131   {
    132     subObservable = new SimpleObservable();
    133     subObservable->signOn(this);
    134   }
    135   ~SuperObservable(){
    136     delete subObservable;
    137   }
    138   void changeMethod() {
    139     OBSERVE;
    140     int i = 0;
    141     i++;
    142     subObservable->changeMethod();
    143   }
    144   SimpleObservable *subObservable;
    145 };
    146 
    147 class NotificationObservable : public Observable {
    148 public:
    149   NotificationObservable() :
    150     Observable("NotificationObservable"),
    151     notification1(new Notification(this)),
    152     notification2(new Notification(this))
    153   {}
    154 
    155   ~NotificationObservable(){
    156     delete notification1;
    157     delete notification2;
    158   }
    159 
    160   void operation1(){
    161     OBSERVE;
    162     NOTIFY(notification1);
    163   }
    164 
    165   void operation2(){
    166     OBSERVE;
    167     NOTIFY(notification2);
    168   }
    169 
    170   Notification_ptr notification1;
    171   Notification_ptr notification2;
    172 };
    173 
    174 class NotificationObserver : public Observer {
    175 public:
    176   NotificationObserver(Notification_ptr notification) :
    177     Observer("NotificationObserver"),
    178     requestedNotification(notification),
    179     wasNotified(false)
    180   {}
    181 
    182   void update(Observable*){}
    183   void subjectKilled(Observable*){}
    184   void recieveNotification(Observable *publisher, Notification_ptr notification){
    185     ASSERT(requestedNotification==notification,"Notification received that was not requested");
    186     wasNotified = true;
    187   }
    188 
    189   Notification_ptr requestedNotification;
    190 
    191   bool wasNotified;
    192 };
    193 
    194 class ObservableSet : public Observable {
    195 public:
    196   typedef std::set<SimpleObservable*> set;
    197   typedef ObservedIterator<set> iterator;
    198   typedef set::const_iterator const_iterator;
    199 
    200   ObservableSet(int _num) :
    201     Observable("ObservableCollection"),
    202     num(_num)
    203   {
    204     for(int i=0; i<num; ++i){
    205       SimpleObservable *content = new SimpleObservable();
    206       content->signOn(this);
    207       theSet.insert(content);
    208     }
    209   }
    210 
    211   ~ObservableSet(){
    212     set::iterator iter;
    213     for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
    214       delete (*iter);
    215     }
    216   }
    217 
    218   iterator begin(){
    219     return iterator(theSet.begin(),this);
    220   }
    221 
    222   iterator end(){
    223     return iterator(theSet.end(),this);
    224   }
    225 
    226   const int num;
    227 
    228 private:
    229   set theSet;
    230 };
    231 
    232 class ObservableMap : public Observable {
    233 public:
    234   typedef std::map<int,SimpleObservable*> set;
    235   typedef ObservedIterator<set> iterator;
    236   typedef set::const_iterator const_iterator;
    237 
    238   ObservableMap(int _num) :
    239     Observable("ObservableCollection"),
    240     num(_num)
    241   {
    242     for(int i=0; i<num; ++i){
    243       SimpleObservable *content = new SimpleObservable();
    244       content->signOn(this);
    245       theSet.insert(std::make_pair(i,content));
    246     }
    247   }
    248 
    249   ~ObservableMap(){
    250     set::iterator iter;
    251     for(iter=theSet.begin(); iter!=theSet.end(); ++iter ){
    252       delete iter->second;
    253     }
    254   }
    255 
    256   iterator begin(){
    257     return iterator(theSet.begin(),this);
    258   }
    259 
    260   iterator end(){
    261     return iterator(theSet.end(),this);
    262   }
    263 
    264   const int num;
    265 
    266 private:
    267   set theSet;
    268 };
    26941
    27042
  • src/unittests/Makefile.am

    r192c04 r56d62f  
    3939  $(top_srcdir)/src/Patterns/unittests/stubs/ManipulableCloneStub.cpp \
    4040  $(top_srcdir)/src/Patterns/unittests/stubs/ManipulablePrototypeFactoryStub.cpp \
     41  $(top_srcdir)/src/Patterns/unittests/stubs/ObserverStub.cpp \
    4142  $(top_srcdir)/src/Patterns/unittests/stubs/PrototypeFactoryStub.cpp \
    4243  InfoUnitTest.cpp \
     
    6667  $(top_srcdir)/src/Patterns/unittests/stubs/ManipulablePrototypeFactoryStub.def \
    6768  $(top_srcdir)/src/Patterns/unittests/stubs/ManipulablePrototypeFactoryStub.undef \
     69  $(top_srcdir)/src/Patterns/unittests/stubs/ObserverStub.hpp \
    6870  $(top_srcdir)/src/Patterns/unittests/stubs/PrototypeFactoryStub.def \
    6971  $(top_srcdir)/src/Patterns/unittests/stubs/PrototypeFactoryStub.undef \
Note: See TracChangeset for help on using the changeset viewer.