Skip to content

Commit

Permalink
mock awaitable
Browse files Browse the repository at this point in the history
  • Loading branch information
tian-lt committed Nov 28, 2024
1 parent bc880f0 commit 1114f8b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
5 changes: 2 additions & 3 deletions src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 24 additions & 2 deletions src/CalcViewModel/DataLoaders/CurrencyHttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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" };

Expand All @@ -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" };
Expand All @@ -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" };
Expand Down

0 comments on commit 1114f8b

Please sign in to comment.