Skip to content

Commit 1114f8b

Browse files
committed
mock awaitable
1 parent bc880f0 commit 1114f8b

File tree

5 files changed

+39
-18
lines changed

5 files changed

+39
-18
lines changed

src/CalcViewModel/DataLoaders/CurrencyDataLoader.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,8 @@ future<bool> CurrencyDataLoader::TryLoadDataFromWebAsync()
414414
co_return false;
415415
}
416416

417-
// TODO: determine if below getters are awaitables in production.
418-
String ^ staticDataResponse = m_client.GetCurrencyMetadata();
419-
String ^ allRatiosResponse = m_client.GetCurrencyRatios();
417+
String ^ staticDataResponse = co_await m_client.GetCurrencyMetadata();
418+
String ^ allRatiosResponse = co_await m_client.GetCurrencyRatios();
420419
if (staticDataResponse == nullptr || allRatiosResponse == nullptr)
421420
{
422421
co_return false;

src/CalcViewModel/DataLoaders/CurrencyHttpClient.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ namespace CalculatorApp::ViewModel::DataLoaders
134134
m_responseLanguage = responseLanguage;
135135
}
136136

137-
Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const
137+
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadata() const
138138
{
139139
(void)m_responseLanguage; // to be used in production.
140-
return ref new Platform::String(MockCurrencyStaticData);
140+
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
141141
}
142142

143-
Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const
143+
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyRatios() const
144144
{
145145
(void)m_sourceCurrencyCode; // to be used in production.
146-
return ref new Platform::String(MockCurrencyConverterData);
146+
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
147147
}
148148
} // namespace CalculatorApp::ViewModel::DataLoaders

src/CalcViewModel/DataLoaders/CurrencyHttpClient.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,39 @@
22
// Licensed under the MIT License.
33

44
#pragma once
5+
#include <cassert>
56

67
namespace CalculatorApp::ViewModel::DataLoaders
78
{
9+
template <class T>
10+
struct MockAwaitable
11+
{
12+
T Value;
13+
14+
bool await_ready() const noexcept
15+
{
16+
return true;
17+
}
18+
19+
void await_suspend(std::experimental::coroutine_handle<>) const noexcept
20+
{
21+
assert(false && "not implemented.");
22+
}
23+
24+
T&& await_resume() noexcept
25+
{
26+
return std::forward<T>(Value);
27+
}
28+
};
29+
830
class CurrencyHttpClient
931
{
1032
public:
1133
static bool ForceWebFailure;
1234
void Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage);
1335

14-
Platform::String ^ GetCurrencyMetadata() const;
15-
Platform::String ^ GetCurrencyRatios() const;
36+
MockAwaitable<Platform::String ^> GetCurrencyMetadata() const;
37+
MockAwaitable<Platform::String ^> GetCurrencyRatios() const;
1638

1739
private:
1840
Platform::String ^ m_sourceCurrencyCode;

src/CalcViewModelCopyForUT/DataLoaders/CurrencyHttpClient.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ namespace CalculatorApp::ViewModel::DataLoaders
2121
m_responseLanguage = responseLanguage;
2222
}
2323

24-
Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const
24+
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyMetadata() const
2525
{
2626
if (ForceWebFailure)
2727
{
2828
throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata");
2929
}
3030
(void)m_responseLanguage; // to be used in production.
31-
return ref new Platform::String(MockCurrencyStaticData);
31+
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyStaticData) };
3232
}
3333

34-
Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const
34+
MockAwaitable<Platform::String ^> CurrencyHttpClient::GetCurrencyRatios() const
3535
{
3636
if (ForceWebFailure)
3737
{
3838
throw ref new Platform::Exception(E_FAIL, L"Mocked Network Failure: failed to load currency metadata");
3939
}
4040
(void)m_sourceCurrencyCode; // to be used in production.
41-
return ref new Platform::String(MockCurrencyConverterData);
41+
return MockAwaitable<Platform::String ^>{ ref new Platform::String(MockCurrencyConverterData) };
4242
}
4343
} // namespace CalculatorApp::ViewModel::DataLoaders

src/CalculatorUnitTests/CurrencyConverterUnitTests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ namespace CalculatorUnitTests
158158

159159
VERIFY_IS_TRUE(DeleteCurrencyCacheFiles());
160160

161-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
162-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios()));
161+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value));
162+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios().Value));
163163
}
164164

165165
TEST_CLASS(CurrencyConverterLoadTests){ public: TEST_METHOD_INITIALIZE(DeleteCacheFiles){ DeleteCurrencyCacheFiles();
@@ -205,7 +205,7 @@ TEST_METHOD(LoadFromCache_Fail_StaticDataFileDoesNotExist)
205205
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);
206206

207207
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename));
208-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios()));
208+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios().Value));
209209

210210
CurrencyDataLoader loader{ L"en-US" };
211211

@@ -223,7 +223,7 @@ TEST_METHOD(LoadFromCache_Fail_AllRatiosDataFileDoesNotExist)
223223
DateTime now = Utils::GetUniversalSystemTime();
224224
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);
225225

226-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
226+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value));
227227
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));
228228

229229
CurrencyDataLoader loader{ L"en-US" };
@@ -243,7 +243,7 @@ TEST_METHOD(LoadFromCache_Fail_ResponseLanguageChanged)
243243
// Tests always use en-US as response language. Insert a different lang-code to fail the test.
244244
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheLangcodeKey, L"ar-SA");
245245

246-
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
246+
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata().Value));
247247
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));
248248

249249
CurrencyDataLoader loader{ L"en-US" };

0 commit comments

Comments
 (0)