From 1114f8b7a6515f8f197c293f5ccd272572cc0cc0 Mon Sep 17 00:00:00 2001 From: Tian Liao Date: Thu, 28 Nov 2024 13:56:24 +0800 Subject: [PATCH] mock awaitable --- .../DataLoaders/CurrencyDataLoader.cpp | 5 ++-- .../DataLoaders/CurrencyHttpClient.cpp | 8 +++--- .../DataLoaders/CurrencyHttpClient.h | 26 +++++++++++++++++-- .../DataLoaders/CurrencyHttpClient.cpp | 8 +++--- .../CurrencyConverterUnitTests.cpp | 10 +++---- 5 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp index 5ff5f581..74bc03af 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp +++ b/src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp @@ -414,9 +414,8 @@ future CurrencyDataLoader::TryLoadDataFromWebAsync() co_return false; } - // TODO: determine if below getters are awaitables in production. - String ^ staticDataResponse = m_client.GetCurrencyMetadata(); - String ^ allRatiosResponse = m_client.GetCurrencyRatios(); + String ^ staticDataResponse = co_await m_client.GetCurrencyMetadata(); + String ^ allRatiosResponse = co_await m_client.GetCurrencyRatios(); if (staticDataResponse == nullptr || allRatiosResponse == nullptr) { co_return false; diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp index 6d2d7301..d073b554 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp @@ -134,15 +134,15 @@ namespace CalculatorApp::ViewModel::DataLoaders m_responseLanguage = responseLanguage; } - Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const + MockAwaitable CurrencyHttpClient::GetCurrencyMetadata() const { (void)m_responseLanguage; // to be used in production. - return ref new Platform::String(MockCurrencyStaticData); + return MockAwaitable{ ref new Platform::String(MockCurrencyStaticData) }; } - Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const + MockAwaitable CurrencyHttpClient::GetCurrencyRatios() const { (void)m_sourceCurrencyCode; // to be used in production. - return ref new Platform::String(MockCurrencyConverterData); + return MockAwaitable{ ref new Platform::String(MockCurrencyConverterData) }; } } // namespace CalculatorApp::ViewModel::DataLoaders diff --git a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h index 79d042e3..3dd8dfd6 100644 --- a/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h +++ b/src/CalcViewModel/DataLoaders/CurrencyHttpClient.h @@ -2,17 +2,39 @@ // Licensed under the MIT License. #pragma once +#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::forward(Value); + } + }; + class CurrencyHttpClient { public: static bool ForceWebFailure; void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage); - Platform::String ^ GetCurrencyMetadata() const; - Platform::String ^ GetCurrencyRatios() const; + MockAwaitable GetCurrencyMetadata() const; + MockAwaitable GetCurrencyRatios() const; private: Platform::String ^ m_sourceCurrencyCode; diff --git a/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp b/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp index 3d62330b..ea4c3d34 100644 --- a/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp +++ b/src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp @@ -21,23 +21,23 @@ namespace CalculatorApp::ViewModel::DataLoaders m_responseLanguage = responseLanguage; } - Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const + MockAwaitable CurrencyHttpClient::GetCurrencyMetadata() 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 ref new Platform::String(MockCurrencyStaticData); + return MockAwaitable{ ref new Platform::String(MockCurrencyStaticData) }; } - Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const + MockAwaitable CurrencyHttpClient::GetCurrencyRatios() 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 ref new Platform::String(MockCurrencyConverterData); + return MockAwaitable{ ref new Platform::String(MockCurrencyConverterData) }; } } // namespace CalculatorApp::ViewModel::DataLoaders diff --git a/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp b/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp index cf94ec50..89fb5e96 100644 --- a/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp +++ b/src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp @@ -158,8 +158,8 @@ namespace CalculatorUnitTests VERIFY_IS_TRUE(DeleteCurrencyCacheFiles()); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata())); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios())); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value)); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios().Value)); } TEST_CLASS(CurrencyConverterLoadTests){ public: TEST_METHOD_INITIALIZE(DeleteCacheFiles){ DeleteCurrencyCacheFiles(); @@ -205,7 +205,7 @@ TEST_METHOD(LoadFromCache_Fail_StaticDataFileDoesNotExist) InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now); VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename)); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios())); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios().Value)); CurrencyDataLoader loader{ L"en-US" }; @@ -223,7 +223,7 @@ TEST_METHOD(LoadFromCache_Fail_AllRatiosDataFileDoesNotExist) DateTime now = Utils::GetUniversalSystemTime(); InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now); - VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata())); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value)); VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename)); CurrencyDataLoader loader{ L"en-US" }; @@ -243,7 +243,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{}.GetCurrencyMetadata())); + VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value)); VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename)); CurrencyDataLoader loader{ L"en-US" };