windows - C++: How to simulate different timezones in unit tests using google test -


i'm working in c++ google test unit testing. in our companies code still have problem, system data types example ctime use example timezone of actual system (we use ctime datatype same way in production code). unit testing, want simulate different time zones because have many problems that.

i invested hours researching how others because can't be, other companies don't have problem :) didn't find solution that. thoughts this: ctime saves time in utc , converts on demand local time. assumption these functions need gettimezoneinformation() functionality declared in timezonapi.h.

winbaseapi _success_(return != time_zone_id_invalid) dword winapi gettimezoneinformation(_out_ lptime_zone_information lptimezoneinformation); 

so solution maybe exchange gettimezoneinformation() functionality customized function returns object can change on demand. don't know how , not know if working or not because it's maybe calling function once when application starts.

does know how deal topic? don't want change behaviours, want able set own timezone without breaking time calculation mechanism. best thing if somehow mock these calls without changing production code that. thing program big, old, and... changing less possible best :)

if i've not found existing post solved problem i'm sorry because either didn't find or didn't can me.

as mentioned in question, mocking way solve problem.

the fastest , easiest way know wrapping methods want test in new class, implements 'strategy' design pattern:

it can switch between real implementation , mocks, once set logic mock, return mock value.

by default should set real implementation.

simple example 'int foo()':

    class strategy{             int foo() = o     }      class strategyimplementation     {     private:             strategy* logic;     public:             void setlogic(strategy* newlogic){logic=newlogic)}             int foo(){return logic->foo()}     }      class mock : public strategy{     public:             setvalue(int value){this.value = value}             int foo(){return value}     private:             int value;         }      class implementation  : public strategy{         int foo(){ here_you_call_the_method_you_want_to_test }     } 

all left, change every call of gettimezoneinformation new strategy, easiest way that, using 'singleton' design pattern.

it doesn't have high risk, , changes need changing calls 'foo()' 'strategyimplementation::getinstance().foo()'


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

Laravel mail error `Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [ #0]` -

C#.NET Oracle.ManagedDataAccess ConfigSchema.xsd -