diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp index d53c525f8..448f64723 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp @@ -127,22 +127,25 @@ namespace namespace CalculatorApp::ViewModel::DataLoaders { - bool CurrencyHttpClient::ForceWebFailure = false; void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage) { m_sourceCurrencyCode = sourceCurrencyCode; m_responseLanguage = responseLanguage; } - MockAwaitable CurrencyHttpClient::GetCurrencyMetadataAsync() const + std::future CurrencyHttpClient::GetCurrencyMetadataAsync() const { (void)m_responseLanguage; // to be used in production. - return MockAwaitable{ ref new Platform::String(MockCurrencyStaticData) }; + std::promise mockedTask; + mockedTask.set_value(ref new Platform::String(MockCurrencyStaticData)); + return mockedTask.get_future(); } - MockAwaitable CurrencyHttpClient::GetCurrencyRatiosAsync() const + std::future CurrencyHttpClient::GetCurrencyRatiosAsync() const { (void)m_sourceCurrencyCode; // to be used in production. - return MockAwaitable{ ref new Platform::String(MockCurrencyConverterData) }; + std::promise mockedTask; + mockedTask.set_value(ref new Platform::String(MockCurrencyConverterData)); + return mockedTask.get_future(); } } // namespace CalculatorApp::ViewModel::DataLoaders diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h index 354dc6855..7ed6cf2eb 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h @@ -3,38 +3,20 @@ #pragma once #include +#include namespace CalculatorApp::ViewModel::DataLoaders { - template - struct MockAwaitable - { - T Value; - - bool await_ready() const noexcept - { - return true; - } - - void await_suspend(std::experimental::coroutine_handle<>) const noexcept - { - assert(false && "not implemented."); - } - - T await_resume() noexcept - { - return std::move(Value); - } - }; - class CurrencyHttpClient { public: +#ifdef VIEWMODEL_FOR_UT static bool ForceWebFailure; +#endif void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage); - MockAwaitable GetCurrencyMetadataAsync() const; - MockAwaitable GetCurrencyRatiosAsync() const; + std::future GetCurrencyMetadataAsync() const; + std::future GetCurrencyRatiosAsync() const; private: Platform::String ^ m_sourceCurrencyCode; diff --git a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj index 842c12791..1abc596f8 100644 --- a/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj +++ b/src/CalcViewModelCopyForUT/CalcViewModelCopyForUT.vcxproj @@ -136,6 +136,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console @@ -156,6 +157,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console @@ -176,6 +178,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console @@ -196,6 +199,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console @@ -216,6 +220,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console @@ -236,6 +241,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console @@ -256,6 +262,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console @@ -276,6 +283,7 @@ /bigobj /await /std:c++17 /utf-8 %(AdditionalOptions) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) Console diff --git a/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp b/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp index c3be0e5d5..c2b84e0fc 100644 --- a/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp +++ b/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp @@ -21,23 +21,28 @@ namespace CalculatorApp::ViewModel::DataLoaders m_responseLanguage = responseLanguage; } - MockAwaitable CurrencyHttpClient::GetCurrencyMetadataAsync() const + std::future CurrencyHttpClient::GetCurrencyMetadataAsync() const { if (ForceWebFailure) { throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata"); } (void)m_responseLanguage; // to be used in production. - return MockAwaitable{ ref new Platform::String(MockCurrencyStaticData) }; + std::promise mockedTask; + mockedTask.set_value(ref new Platform::String(MockCurrencyStaticData)); + return mockedTask.get_future(); } - MockAwaitable CurrencyHttpClient::GetCurrencyRatiosAsync() const + std::future CurrencyHttpClient::GetCurrencyRatiosAsync() const { if (ForceWebFailure) { throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata"); } (void)m_sourceCurrencyCode; // to be used in production. - return MockAwaitable{ ref new Platform::String(MockCurrencyConverterData) }; + + std::promise mockedTask; + mockedTask.set_value(ref new Platform::String(MockCurrencyConverterData)); + return mockedTask.get_future(); } } // namespace CalculatorApp::ViewModel::DataLoaders diff --git a/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj b/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj index 39b49d58a..f90a21a6d 100644 --- a/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj +++ b/src/CalculatorUnitTests/CalculatorUnitTests.vcxproj @@ -135,6 +135,7 @@ $(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) @@ -145,6 +146,7 @@ Level4 true Guard + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) @@ -154,6 +156,7 @@ $(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) @@ -164,6 +167,7 @@ Level4 true Guard + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) @@ -173,6 +177,7 @@ $(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) @@ -183,6 +188,7 @@ Level4 true Guard + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) @@ -192,6 +198,7 @@ $(SolutionDir);$(SolutionDir)CalcManager;$(SolutionDir)CalcViewModel;%(AdditionalIncludeDirectories) Level4 true + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) @@ -202,6 +209,7 @@ Level4 true Guard + VIEWMODEL_FOR_UT;%(PreprocessorDefinitions) diff --git a/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp b/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp index cd9f73670..78c0c2fc2 100644 --- a/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp +++ b/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp @@ -160,8 +160,8 @@ namespace CalculatorUnitTests VERIFY_IS_TRUE(DeleteCurrencyCacheFiles()); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().Value)); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().Value)); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().get())); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().get())); } TEST_CLASS(CurrencyConverterLoadTests){ public: TEST_METHOD_INITIALIZE(DeleteCacheFiles){ DeleteCurrencyCacheFiles(); @@ -207,7 +207,7 @@ TEST_METHOD(LoadFromCache_Fail_StaticDataFileDoesNotExist) InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now); VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename)); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().Value)); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatiosAsync().get())); CurrencyDataLoader loader{ L"en-US" }; @@ -225,7 +225,7 @@ TEST_METHOD(LoadFromCache_Fail_AllRatiosDataFileDoesNotExist) DateTime now = Utils::GetUniversalSystemTime(); InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().Value)); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().get())); VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename)); CurrencyDataLoader loader{ L"en-US" }; @@ -245,7 +245,7 @@ TEST_METHOD(LoadFromCache_Fail_ResponseLanguageChanged) // Tests always use en-US as response language. Insert a different lang-code to fail the test. InsertToLocalSettings(CurrencyDataLoaderConstants::CacheLangcodeKey, L"ar-SA"); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().Value)); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadataAsync().get())); VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename)); CurrencyDataLoader loader{ L"en-US" };