mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-21 05:43:10 -07:00
mock awaitable
This commit is contained in:
parent
bc880f06bc
commit
1114f8b7a6
5 changed files with 39 additions and 18 deletions
|
@ -414,9 +414,8 @@ future<bool> 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;
|
||||
|
|
|
@ -134,15 +134,15 @@ namespace CalculatorApp::ViewModel::DataLoaders
|
|||
m_responseLanguage = responseLanguage;
|
||||
}
|
||||
|
||||
Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const
|
||||
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadata() const
|
||||
{
|
||||
(void)m_responseLanguage; // to be used in production.
|
||||
return ref new Platform::String(MockCurrencyStaticData);
|
||||
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
|
||||
}
|
||||
|
||||
Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const
|
||||
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyRatios() const
|
||||
{
|
||||
(void)m_sourceCurrencyCode; // to be used in production.
|
||||
return ref new Platform::String(MockCurrencyConverterData);
|
||||
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
|
||||
}
|
||||
} // namespace CalculatorApp::ViewModel::DataLoaders
|
||||
|
|
|
@ -2,17 +2,39 @@
|
|||
// Licensed under the MIT License.
|
||||
|
||||
#pragma once
|
||||
#include <cassert>
|
||||
|
||||
namespace CalculatorApp::ViewModel::DataLoaders
|
||||
{
|
||||
template <class T>
|
||||
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<T>(Value);
|
||||
}
|
||||
};
|
||||
|
||||
class CurrencyHttpClient
|
||||
{
|
||||
public:
|
||||
static bool ForceWebFailure;
|
||||
void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage);
|
||||
|
||||
Platform::String ^ GetCurrencyMetadata() const;
|
||||
Platform::String ^ GetCurrencyRatios() const;
|
||||
MockAwaitable<Platform::String ^> GetCurrencyMetadata() const;
|
||||
MockAwaitable<Platform::String ^> GetCurrencyRatios() const;
|
||||
|
||||
private:
|
||||
Platform::String ^ m_sourceCurrencyCode;
|
||||
|
|
|
@ -21,23 +21,23 @@ namespace CalculatorApp::ViewModel::DataLoaders
|
|||
m_responseLanguage = responseLanguage;
|
||||
}
|
||||
|
||||
Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const
|
||||
MockAwaitable<Platform::String ^> 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<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
|
||||
}
|
||||
|
||||
Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const
|
||||
MockAwaitable<Platform::String ^> 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<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
|
||||
}
|
||||
} // namespace CalculatorApp::ViewModel::DataLoaders
|
||||
|
|
|
@ -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" };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue