mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-21 05:43:10 -07:00
build succeed
This commit is contained in:
parent
80bbb1ad81
commit
39124bed84
9 changed files with 109 additions and 289 deletions
|
@ -49,6 +49,7 @@ namespace
|
|||
"Rt": 0.25
|
||||
}
|
||||
])";
|
||||
|
||||
constexpr auto MockCurrencyStaticData = LR"(
|
||||
[
|
||||
{
|
||||
|
|
|
@ -356,7 +356,6 @@
|
|||
<ClCompile Include="..\CalcViewModel\Common\TraceLogger.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\Common\Utils.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\DataLoaders\CurrencyDataLoader.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\DataLoaders\CurrencyHttpClient.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\DataLoaders\UnitConverterDataLoader.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\DateCalculatorViewModel.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\GraphingCalculator\EquationViewModel.cpp" />
|
||||
|
@ -378,6 +377,7 @@
|
|||
<ClCompile Include="..\CalcViewModel\Snapshots.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\StandardCalculatorViewModel.cpp" />
|
||||
<ClCompile Include="..\CalcViewModel\UnitConverterViewModel.cpp" />
|
||||
<ClCompile Include="CurrencyHttpClient.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CalcManager\CalcManager.vcxproj">
|
||||
|
|
34
src/CalcViewModelCopyForUT/CurrencyHttpClient.cpp
Normal file
34
src/CalcViewModelCopyForUT/CurrencyHttpClient.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include "pch.h"
|
||||
#include "CalcViewModel/DataLoaders/CurrencyHttpClient.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
constexpr auto MockCurrencyConverterData =
|
||||
LR"([{"An":"USD","Ch":0,"Pc":0,"Rt":1},{"An":"EUR","Ch":0.003803,"Pc":0.4149,"Rt":0.920503,"Yh":0.9667,"Yl":0.86701}])";
|
||||
constexpr auto MockCurrencyStaticData =
|
||||
LR"([{"CountryCode":"USA","CountryName":"United States","CurrencyCode":"USD","CurrencyName":"Dollar","CurrencySymbol":"$"},{"CountryCode":"EUR","CountryName":"Europe","CurrencyCode":"EUR","CurrencyName":"Euro","CurrencySymbol":"€"}])";
|
||||
}
|
||||
|
||||
namespace CalculatorApp::ViewModel::DataLoaders
|
||||
{
|
||||
void CurrencyHttpClient::Initialize(Platform::String ^ sourceCurrencyCode, Platform::String ^ responseLanguage)
|
||||
{
|
||||
m_sourceCurrencyCode = sourceCurrencyCode;
|
||||
m_responseLanguage = responseLanguage;
|
||||
}
|
||||
|
||||
Platform::String ^ CurrencyHttpClient::GetCurrencyMetadata() const
|
||||
{
|
||||
(void)m_responseLanguage; // to be used in production.
|
||||
return ref new Platform::String(MockCurrencyStaticData);
|
||||
}
|
||||
|
||||
Platform::String ^ CurrencyHttpClient::GetCurrencyRatios() const
|
||||
{
|
||||
(void)m_sourceCurrencyCode; // to be used in production.
|
||||
return ref new Platform::String(MockCurrencyConverterData);
|
||||
}
|
||||
} // namespace CalculatorApp::ViewModel::DataLoaders
|
|
@ -207,7 +207,6 @@
|
|||
<ItemGroup>
|
||||
<ClInclude Include="DateUtils.h" />
|
||||
<ClInclude Include="Helpers.h" />
|
||||
<ClInclude Include="Mocks\CurrencyHttpClient.h" />
|
||||
<ClInclude Include="pch.h" />
|
||||
<ClInclude Include="UnitConverterViewModelUnitTests.h" />
|
||||
<ClInclude Include="UnitTestApp.xaml.h">
|
||||
|
@ -243,7 +242,6 @@
|
|||
<ClCompile Include="HistoryTests.cpp" />
|
||||
<ClCompile Include="LocalizationServiceUnitTests.cpp" />
|
||||
<ClCompile Include="LocalizationSettingsUnitTests.cpp" />
|
||||
<ClCompile Include="Mocks\CurrencyHttpClient.cpp" />
|
||||
<ClCompile Include="MultiWindowUnitTests.cpp" />
|
||||
<ClCompile Include="NarratorAnnouncementUnitTests.cpp" />
|
||||
<ClCompile Include="NavCategoryUnitTests.cpp" />
|
||||
|
|
|
@ -24,9 +24,6 @@
|
|||
<ClCompile Include="UnitTestApp.xaml.cpp" />
|
||||
<ClCompile Include="pch.cpp" />
|
||||
<ClCompile Include="UtilsTests.cpp" />
|
||||
<ClCompile Include="Mocks\CurrencyHttpClient.cpp">
|
||||
<Filter>Mocks</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="LocalizationServiceUnitTests.cpp" />
|
||||
<ClCompile Include="RationalTest.cpp" />
|
||||
<ClCompile Include="LocalizationSettingsUnitTests.cpp" />
|
||||
|
@ -38,9 +35,6 @@
|
|||
<ClInclude Include="pch.h" />
|
||||
<ClInclude Include="UnitConverterViewModelUnitTests.h" />
|
||||
<ClInclude Include="UnitTestApp.xaml.h" />
|
||||
<ClInclude Include="Mocks\CurrencyHttpClient.h">
|
||||
<Filter>Mocks</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Xml Include="UnitTestApp.rd.xml" />
|
||||
|
@ -75,8 +69,5 @@
|
|||
<Filter Include="Assets">
|
||||
<UniqueIdentifier>{f2987b0a-9832-46fc-b818-d5347362b3d8}</UniqueIdentifier>
|
||||
</Filter>
|
||||
<Filter Include="Mocks">
|
||||
<UniqueIdentifier>{d3ec8922-022d-4531-8744-f65a872f3841}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -14,61 +14,12 @@ using namespace CalculatorApp::ViewModel;
|
|||
using namespace CalculatorUnitTests;
|
||||
using namespace Concurrency;
|
||||
using namespace Platform;
|
||||
using namespace std;
|
||||
using namespace UnitConversionManager;
|
||||
using namespace Windows::Foundation;
|
||||
using namespace Windows::Storage;
|
||||
using namespace Windows::Web::Http;
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace CalculatorApp::ViewModel
|
||||
{
|
||||
namespace DataLoaders
|
||||
{
|
||||
class MockCurrencyHttpClientWithResult : public CurrencyHttpClient
|
||||
{
|
||||
public:
|
||||
MockCurrencyHttpClientWithResult(String ^ staticResponse, String ^ allRatiosResponse)
|
||||
: m_staticResponse(staticResponse)
|
||||
, m_allRatiosResponse(allRatiosResponse)
|
||||
{
|
||||
}
|
||||
|
||||
IAsyncOperationWithProgress<String ^, HttpProgress> ^ GetCurrencyMetadata() override
|
||||
{
|
||||
return ref new MockAsyncOperationWithProgress(m_staticResponse);
|
||||
}
|
||||
|
||||
IAsyncOperationWithProgress<String ^, HttpProgress> ^ GetCurrencyRatios() override
|
||||
{
|
||||
return ref new MockAsyncOperationWithProgress(m_allRatiosResponse);
|
||||
}
|
||||
|
||||
private:
|
||||
String ^ m_staticResponse;
|
||||
String ^ m_allRatiosResponse;
|
||||
};
|
||||
|
||||
class MockCurrencyHttpClientThrowsException : public CurrencyHttpClient
|
||||
{
|
||||
public:
|
||||
MockCurrencyHttpClientThrowsException()
|
||||
{
|
||||
}
|
||||
|
||||
IAsyncOperationWithProgress<String ^, HttpProgress> ^ GetCurrencyMetadata() override
|
||||
{
|
||||
throw ref new NotImplementedException();
|
||||
}
|
||||
|
||||
IAsyncOperationWithProgress<String ^, HttpProgress> ^ GetCurrencyRatios() override
|
||||
{
|
||||
throw ref new NotImplementedException();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class DataLoadedCallback : public UnitConversionManager::IViewModelCurrencyCallback
|
||||
{
|
||||
public:
|
||||
|
@ -82,10 +33,10 @@ public:
|
|||
m_task_completion_event.set();
|
||||
}
|
||||
|
||||
void CurrencySymbolsCallback(_In_ const wstring& /*fromSymbol*/, _In_ const wstring& /*toSymbol*/) override
|
||||
void CurrencySymbolsCallback(_In_ const std::wstring& /*fromSymbol*/, _In_ const std::wstring& /*toSymbol*/) override
|
||||
{
|
||||
}
|
||||
void CurrencyRatiosCallback(_In_ const wstring& /*ratioEquality*/, _In_ const wstring& /*accRatioEquality*/) override
|
||||
void CurrencyRatiosCallback(_In_ const std::wstring& /*ratioEquality*/, _In_ const std::wstring& /*accRatioEquality*/) override
|
||||
{
|
||||
}
|
||||
void CurrencyTimestampCallback(_In_ const std::wstring& /*timestamp*/, bool /*isWeekOldData*/) override
|
||||
|
@ -103,14 +54,7 @@ namespace CalculatorUnitTests
|
|||
{
|
||||
constexpr auto sc_Language_EN = L"en-US";
|
||||
|
||||
unique_ptr<CurrencyDataLoader> MakeLoaderWithResults(String ^ staticResponse, String ^ allRatiosResponse)
|
||||
{
|
||||
auto client = make_unique<MockCurrencyHttpClientWithResult>(staticResponse, allRatiosResponse);
|
||||
client->SetSourceCurrencyCode(StringReference(DefaultCurrencyCode.data()));
|
||||
return make_unique<CurrencyDataLoader>(move(client));
|
||||
}
|
||||
|
||||
String^ SerializeContent(const vector<String^>& data)
|
||||
String^ SerializeContent(const std::vector<String^>& data)
|
||||
{
|
||||
String^ result = L"";
|
||||
String^ delimiter = CurrencyDataLoaderConstants::CacheDelimiter;
|
||||
|
@ -193,8 +137,8 @@ namespace CalculatorUnitTests
|
|||
|
||||
VERIFY_IS_TRUE(DeleteCurrencyCacheFiles());
|
||||
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient::GetRawStaticDataResponse()));
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient::GetRawAllRatiosDataResponse()));
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios()));
|
||||
}
|
||||
|
||||
TEST_CLASS(CurrencyConverterLoadTests){ public: TEST_METHOD_INITIALIZE(DeleteCacheFiles){ DeleteCurrencyCacheFiles();
|
||||
|
@ -203,7 +147,7 @@ namespace CalculatorUnitTests
|
|||
TEST_METHOD(LoadFromCache_Fail_NoCacheKey)
|
||||
{
|
||||
RemoveFromLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey);
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromCacheAsync().get();
|
||||
|
||||
|
@ -221,7 +165,7 @@ TEST_METHOD(LoadFromCache_Fail_OlderThanADay)
|
|||
dayOld.UniversalTime = now.UniversalTime - CurrencyDataLoaderConstants::DayDuration - 1;
|
||||
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, dayOld);
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromCacheAsync().get();
|
||||
|
||||
|
@ -238,9 +182,9 @@ TEST_METHOD(LoadFromCache_Fail_StaticDataFileDoesNotExist)
|
|||
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);
|
||||
|
||||
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename));
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient::GetRawAllRatiosDataResponse()));
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename, CurrencyHttpClient{}.GetCurrencyRatios()));
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromCacheAsync().get();
|
||||
|
||||
|
@ -256,10 +200,10 @@ TEST_METHOD(LoadFromCache_Fail_AllRatiosDataFileDoesNotExist)
|
|||
DateTime now = Utils::GetUniversalSystemTime();
|
||||
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, now);
|
||||
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient::GetRawStaticDataResponse()));
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
|
||||
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromCacheAsync().get();
|
||||
|
||||
|
@ -276,10 +220,10 @@ 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::GetRawStaticDataResponse()));
|
||||
VERIFY_IS_TRUE(WriteToFileInLocalCacheFolder(CurrencyDataLoaderConstants::StaticDataFilename, CurrencyHttpClient{}.GetCurrencyMetadata()));
|
||||
VERIFY_IS_TRUE(DeleteFileFromLocalCacheFolder(CurrencyDataLoaderConstants::AllRatiosDataFilename));
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromCacheAsync().get();
|
||||
|
||||
|
@ -292,7 +236,7 @@ TEST_METHOD(LoadFromCache_Success)
|
|||
{
|
||||
StandardCacheSetup();
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromCacheAsync().get();
|
||||
|
||||
|
@ -303,7 +247,7 @@ TEST_METHOD(LoadFromCache_Success)
|
|||
|
||||
TEST_METHOD(LoadFromWeb_Fail_ClientIsNullptr)
|
||||
{
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromWebAsync().get();
|
||||
|
||||
|
@ -314,7 +258,7 @@ TEST_METHOD(LoadFromWeb_Fail_ClientIsNullptr)
|
|||
|
||||
TEST_METHOD(LoadFromWeb_Fail_WebException)
|
||||
{
|
||||
CurrencyDataLoader loader(make_unique<MockCurrencyHttpClientThrowsException>(), L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader.TryLoadDataFromWebAsync().get();
|
||||
|
||||
|
@ -325,24 +269,22 @@ TEST_METHOD(LoadFromWeb_Fail_WebException)
|
|||
|
||||
TEST_METHOD(LoadFromWeb_Success)
|
||||
{
|
||||
String ^ staticResponse = CurrencyHttpClient::GetRawStaticDataResponse();
|
||||
String ^ allRatiosResponse = CurrencyHttpClient::GetRawAllRatiosDataResponse();
|
||||
unique_ptr<CurrencyDataLoader> loader = MakeLoaderWithResults(staticResponse, allRatiosResponse);
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
bool didLoad = loader->TryLoadDataFromWebAsync().get();
|
||||
bool didLoad = loader.TryLoadDataFromWebAsync().get();
|
||||
|
||||
VERIFY_IS_TRUE(didLoad);
|
||||
VERIFY_IS_TRUE(loader->LoadFinished());
|
||||
VERIFY_IS_TRUE(loader->LoadedFromWeb());
|
||||
VERIFY_IS_TRUE(loader.LoadFinished());
|
||||
VERIFY_IS_TRUE(loader.LoadedFromWeb());
|
||||
}
|
||||
|
||||
TEST_METHOD(Load_Success_LoadedFromCache)
|
||||
{
|
||||
StandardCacheSetup();
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader.SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader.LoadData();
|
||||
|
@ -362,20 +304,18 @@ TEST_METHOD(Load_Success_LoadedFromWeb)
|
|||
dayOld.UniversalTime = now.UniversalTime - CurrencyDataLoaderConstants::DayDuration - 1;
|
||||
InsertToLocalSettings(CurrencyDataLoaderConstants::CacheTimestampKey, dayOld);
|
||||
|
||||
String ^ staticResponse = CurrencyHttpClient::GetRawStaticDataResponse();
|
||||
String ^ allRatiosResponse = CurrencyHttpClient::GetRawAllRatiosDataResponse();
|
||||
unique_ptr<CurrencyDataLoader> loader = MakeLoaderWithResults(staticResponse, allRatiosResponse);
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader->SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader->LoadData();
|
||||
loader.LoadData();
|
||||
data_loaded_task.wait();
|
||||
|
||||
VERIFY_IS_TRUE(loader->LoadFinished());
|
||||
VERIFY_IS_FALSE(loader->LoadedFromCache());
|
||||
VERIFY_IS_TRUE(loader->LoadedFromWeb());
|
||||
VERIFY_IS_TRUE(loader.LoadFinished());
|
||||
VERIFY_IS_FALSE(loader.LoadedFromCache());
|
||||
VERIFY_IS_TRUE(loader.LoadedFromWeb());
|
||||
}
|
||||
}
|
||||
;
|
||||
|
@ -384,18 +324,18 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
{
|
||||
const UCM::Category CURRENCY_CATEGORY = { NavCategoryStates::Serialize(ViewMode::Currency), L"Currency", false /*supportsNegative*/ };
|
||||
|
||||
const UCM::Unit GetUnit(const vector<UCM::Unit>& unitList, const wstring& target)
|
||||
const UCM::Unit GetUnit(const std::vector<UCM::Unit>& unitList, const std::wstring& target)
|
||||
{
|
||||
return *find_if(begin(unitList), end(unitList), [&target](const UCM::Unit& u) { return u.abbreviation == target; });
|
||||
return *std::find_if(std::begin(unitList), std::end(unitList), [&target](const UCM::Unit& u) { return u.abbreviation == target; });
|
||||
}
|
||||
|
||||
TEST_METHOD(Loaded_LoadOrderedUnits)
|
||||
{
|
||||
StandardCacheSetup();
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader.SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader.LoadData();
|
||||
|
@ -405,27 +345,27 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
VERIFY_IS_TRUE(loader.LoadedFromCache());
|
||||
VERIFY_IS_FALSE(loader.LoadedFromWeb());
|
||||
|
||||
vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
std::vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
VERIFY_ARE_EQUAL(size_t{ 2 }, unitList.size());
|
||||
|
||||
const UCM::Unit usdUnit = GetUnit(unitList, L"USD");
|
||||
const UCM::Unit eurUnit = GetUnit(unitList, L"EUR");
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L"United States - Dollar"), usdUnit.name);
|
||||
VERIFY_ARE_EQUAL(wstring(L"USD"), usdUnit.abbreviation);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"United States - Dollar"), usdUnit.name);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"USD"), usdUnit.abbreviation);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L"Europe - Euro"), eurUnit.name);
|
||||
VERIFY_ARE_EQUAL(wstring(L"EUR"), eurUnit.abbreviation);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"Europe - Euro"), eurUnit.name);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"EUR"), eurUnit.abbreviation);
|
||||
}
|
||||
|
||||
TEST_METHOD(Loaded_LoadOrderedRatios)
|
||||
{
|
||||
StandardCacheSetup();
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader.SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader.LoadData();
|
||||
|
@ -435,13 +375,13 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
VERIFY_IS_TRUE(loader.LoadedFromCache());
|
||||
VERIFY_IS_FALSE(loader.LoadedFromWeb());
|
||||
|
||||
vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
std::vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
VERIFY_ARE_EQUAL(size_t{ 2 }, unitList.size());
|
||||
|
||||
const UCM::Unit usdUnit = GetUnit(unitList, L"USD");
|
||||
const UCM::Unit eurUnit = GetUnit(unitList, L"EUR");
|
||||
|
||||
unordered_map<UCM::Unit, UCM::ConversionData, UCM::UnitHash> ratios = loader.LoadOrderedRatios(usdUnit);
|
||||
std::unordered_map<UCM::Unit, UCM::ConversionData, UCM::UnitHash> ratios = loader.LoadOrderedRatios(usdUnit);
|
||||
VERIFY_ARE_EQUAL(size_t{ 2 }, ratios.size());
|
||||
|
||||
UCM::ConversionData usdRatioData = ratios[usdUnit];
|
||||
|
@ -455,10 +395,10 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
{
|
||||
StandardCacheSetup();
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader.SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader.LoadData();
|
||||
|
@ -468,26 +408,26 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
VERIFY_IS_TRUE(loader.LoadedFromCache());
|
||||
VERIFY_IS_FALSE(loader.LoadedFromWeb());
|
||||
|
||||
vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
std::vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
VERIFY_ARE_EQUAL(size_t{ 2 }, unitList.size());
|
||||
|
||||
const UCM::Unit usdUnit = GetUnit(unitList, L"USD");
|
||||
const UCM::Unit eurUnit = GetUnit(unitList, L"EUR");
|
||||
|
||||
const pair<wstring, wstring> symbols = loader.GetCurrencySymbols(usdUnit, eurUnit);
|
||||
const auto symbols = loader.GetCurrencySymbols(usdUnit, eurUnit);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L"$"), symbols.first);
|
||||
VERIFY_ARE_EQUAL(wstring(L"\x20ac"), symbols.second); // €
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"$"), symbols.first);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"\x20ac"), symbols.second); // €
|
||||
}
|
||||
|
||||
TEST_METHOD(Loaded_GetCurrencySymbols_Invalid)
|
||||
{
|
||||
StandardCacheSetup();
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader.SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader.LoadData();
|
||||
|
@ -501,36 +441,36 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
|
||||
const UCM::Unit fakeUnit2 = { 2, L"fakeUnit2", L"FUD2", false, false, false };
|
||||
|
||||
pair<wstring, wstring> symbols = loader.GetCurrencySymbols(fakeUnit1, fakeUnit2);
|
||||
auto symbols = loader.GetCurrencySymbols(fakeUnit1, fakeUnit2);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L""), wstring(symbols.first.c_str()));
|
||||
VERIFY_ARE_EQUAL(wstring(L""), wstring(symbols.second.c_str()));
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), std::wstring(symbols.first.c_str()));
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), std::wstring(symbols.second.c_str()));
|
||||
|
||||
// Verify that when only one unit is valid, both symbols return as empty string.
|
||||
vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
std::vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
VERIFY_ARE_EQUAL(size_t{ 2 }, unitList.size());
|
||||
|
||||
const UCM::Unit usdUnit = GetUnit(unitList, L"USD");
|
||||
|
||||
symbols = loader.GetCurrencySymbols(fakeUnit1, usdUnit);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L""), symbols.first);
|
||||
VERIFY_ARE_EQUAL(wstring(L""), symbols.second);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), symbols.first);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), symbols.second);
|
||||
|
||||
symbols = loader.GetCurrencySymbols(usdUnit, fakeUnit1);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L""), symbols.first);
|
||||
VERIFY_ARE_EQUAL(wstring(L""), symbols.second);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), symbols.first);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), symbols.second);
|
||||
}
|
||||
|
||||
TEST_METHOD(Loaded_GetCurrencyRatioEquality_Valid)
|
||||
{
|
||||
StandardCacheSetup();
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader.SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader.LoadData();
|
||||
|
@ -540,26 +480,26 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
VERIFY_IS_TRUE(loader.LoadedFromCache());
|
||||
VERIFY_IS_FALSE(loader.LoadedFromWeb());
|
||||
|
||||
vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
std::vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
VERIFY_ARE_EQUAL(size_t{ 2 }, unitList.size());
|
||||
|
||||
const UCM::Unit usdUnit = GetUnit(unitList, L"USD");
|
||||
const UCM::Unit eurUnit = GetUnit(unitList, L"EUR");
|
||||
|
||||
const pair<wstring, wstring> ratio = loader.GetCurrencyRatioEquality(usdUnit, eurUnit);
|
||||
const auto ratio = loader.GetCurrencyRatioEquality(usdUnit, eurUnit);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L"1 USD = 0.9205 EUR"), ratio.first);
|
||||
VERIFY_ARE_EQUAL(wstring(L"1 United States Dollar = 0.9205 Europe Euro"), ratio.second);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"1 USD = 0.9205 EUR"), ratio.first);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L"1 United States Dollar = 0.9205 Europe Euro"), ratio.second);
|
||||
}
|
||||
|
||||
TEST_METHOD(Loaded_GetCurrencyRatioEquality_Invalid)
|
||||
{
|
||||
StandardCacheSetup();
|
||||
|
||||
CurrencyDataLoader loader(nullptr, L"en-US");
|
||||
CurrencyDataLoader loader{ L"en-US" };
|
||||
|
||||
auto data_loaded_event = task_completion_event<void>();
|
||||
loader.SetViewModelCallback(make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
loader.SetViewModelCallback(std::make_shared<DataLoadedCallback>(data_loaded_event));
|
||||
|
||||
auto data_loaded_task = create_task(data_loaded_event);
|
||||
loader.LoadData();
|
||||
|
@ -572,26 +512,26 @@ TEST_CLASS(CurrencyConverterUnitTests)
|
|||
const UCM::Unit fakeUnit1 = { 1, L"fakeUnit1", L"fakeCountry1", L"FUD1", false, false, false };
|
||||
const UCM::Unit fakeUnit2 = { 2, L"fakeUnit2", L"fakeCountry2", L"FUD2", false, false, false };
|
||||
|
||||
pair<wstring, wstring> ratio = loader.GetCurrencyRatioEquality(fakeUnit1, fakeUnit2);
|
||||
auto ratio = loader.GetCurrencyRatioEquality(fakeUnit1, fakeUnit2);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L""), ratio.first);
|
||||
VERIFY_ARE_EQUAL(wstring(L""), ratio.second);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), ratio.first);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), ratio.second);
|
||||
|
||||
// Verify that when only one unit is valid, both symbols return as empty string.
|
||||
vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
std::vector<UCM::Unit> unitList = loader.GetOrderedUnits(CURRENCY_CATEGORY);
|
||||
VERIFY_ARE_EQUAL(size_t{ 2 }, unitList.size());
|
||||
|
||||
const UCM::Unit usdUnit = GetUnit(unitList, L"USD");
|
||||
|
||||
ratio = loader.GetCurrencyRatioEquality(fakeUnit1, usdUnit);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L""), ratio.first);
|
||||
VERIFY_ARE_EQUAL(wstring(L""), ratio.second);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), ratio.first);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), ratio.second);
|
||||
|
||||
ratio = loader.GetCurrencyRatioEquality(usdUnit, fakeUnit1);
|
||||
|
||||
VERIFY_ARE_EQUAL(wstring(L""), ratio.first);
|
||||
VERIFY_ARE_EQUAL(wstring(L""), ratio.second);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), ratio.first);
|
||||
VERIFY_ARE_EQUAL(std::wstring(L""), ratio.second);
|
||||
}
|
||||
|
||||
TEST_METHOD(Test_RoundCurrencyRatio)
|
||||
|
|
|
@ -1,55 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include "pch.h"
|
||||
#include "CurrencyHttpClient.h"
|
||||
|
||||
#include "CalcViewModel/Common/NetworkManager.h"
|
||||
|
||||
using namespace CalculatorApp::ViewModel::DataLoaders;
|
||||
using namespace Platform;
|
||||
using namespace Windows::Foundation;
|
||||
using namespace Windows::System::UserProfile;
|
||||
using namespace Windows::Web::Http;
|
||||
|
||||
// Generic responses so unit tests will pass.
|
||||
static constexpr auto STATIC_DATA_RESPONSE =
|
||||
LR"([{"CountryCode":"USA","CountryName":"United States","CurrencyCode":"USD","CurrencyName":"Dollar","CurrencySymbol":"$"},{"CountryCode":"EUR","CountryName":"Europe","CurrencyCode":"EUR","CurrencyName":"Euro","CurrencySymbol":"€"}])";
|
||||
static constexpr auto ALL_RATIOS_RESPONSE =
|
||||
LR"([{"An":"USD","Ch":0,"Pc":0,"Rt":1},{"An":"EUR","Ch":0.003803,"Pc":0.4149,"Rt":0.920503,"Yh":0.9667,"Yl":0.86701}])";
|
||||
|
||||
CurrencyHttpClient::CurrencyHttpClient()
|
||||
{
|
||||
}
|
||||
|
||||
String ^ CurrencyHttpClient::GetRawStaticDataResponse()
|
||||
{
|
||||
return StringReference(STATIC_DATA_RESPONSE);
|
||||
}
|
||||
|
||||
String ^ CurrencyHttpClient::GetRawAllRatiosDataResponse()
|
||||
{
|
||||
return StringReference(ALL_RATIOS_RESPONSE);
|
||||
}
|
||||
|
||||
IAsyncOperationWithProgress<String ^, HttpProgress> ^ CurrencyHttpClient::GetCurrencyMetadata()
|
||||
{
|
||||
return ref new MockAsyncOperationWithProgress(StringReference(STATIC_DATA_RESPONSE));
|
||||
}
|
||||
|
||||
IAsyncOperationWithProgress<String ^, HttpProgress> ^ CurrencyHttpClient::GetCurrencyRatios()
|
||||
{
|
||||
return ref new MockAsyncOperationWithProgress(StringReference(ALL_RATIOS_RESPONSE));
|
||||
}
|
||||
|
||||
MockAsyncOperationWithProgress::MockAsyncOperationWithProgress(String ^ result)
|
||||
: m_result(result)
|
||||
{
|
||||
}
|
||||
|
||||
HResult MockAsyncOperationWithProgress::ErrorCode::get()
|
||||
{
|
||||
HResult okayResult;
|
||||
okayResult.Value = S_OK;
|
||||
return okayResult;
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace CalculatorApp::ViewModel
|
||||
{
|
||||
namespace DataLoaders
|
||||
{
|
||||
class CurrencyHttpClient
|
||||
{
|
||||
public:
|
||||
CurrencyHttpClient();
|
||||
|
||||
static Platform::String ^ GetRawStaticDataResponse();
|
||||
static Platform::String ^ GetRawAllRatiosDataResponse();
|
||||
|
||||
// ICurrencyHttpClient
|
||||
void SetSourceCurrencyCode(Platform::String ^ sourceCurrencyCode) override
|
||||
{
|
||||
}
|
||||
void SetResponseLanguage(Platform::String ^ responseLanguage) override
|
||||
{
|
||||
}
|
||||
|
||||
virtual Windows::Foundation::IAsyncOperationWithProgress<Platform::String ^, Windows::Web::Http::HttpProgress> ^ GetCurrencyMetadata() override;
|
||||
virtual Windows::Foundation::IAsyncOperationWithProgress<Platform::String ^, Windows::Web::Http::HttpProgress> ^ GetCurrencyRatios() override;
|
||||
// ICurrencyHttpClient
|
||||
};
|
||||
|
||||
public
|
||||
ref class MockAsyncOperationWithProgress sealed
|
||||
: public Windows::Foundation::IAsyncOperationWithProgress<Platform::String ^, Windows::Web::Http::HttpProgress>
|
||||
{
|
||||
public:
|
||||
MockAsyncOperationWithProgress(Platform::String ^ result);
|
||||
|
||||
// IAsyncInfo
|
||||
virtual property Windows::Foundation::HResult ErrorCode
|
||||
{
|
||||
Windows::Foundation::HResult get();
|
||||
}
|
||||
|
||||
virtual property unsigned int Id
|
||||
{
|
||||
unsigned int get()
|
||||
{
|
||||
return 128u;
|
||||
}
|
||||
}
|
||||
|
||||
virtual property Windows::Foundation::AsyncStatus Status
|
||||
{
|
||||
Windows::Foundation::AsyncStatus get()
|
||||
{
|
||||
return Windows::Foundation::AsyncStatus::Completed;
|
||||
}
|
||||
}
|
||||
|
||||
virtual void Cancel()
|
||||
{
|
||||
}
|
||||
virtual void Close()
|
||||
{
|
||||
}
|
||||
// IAsyncInfo
|
||||
|
||||
// IAsyncOperationWithProgress
|
||||
virtual property Windows::Foundation::AsyncOperationProgressHandler<Platform::String^, Windows::Web::Http::HttpProgress>^ Progress
|
||||
{
|
||||
Windows::Foundation::AsyncOperationProgressHandler<Platform::String^, Windows::Web::Http::HttpProgress>^ get() { return nullptr; }
|
||||
void set(Windows::Foundation::AsyncOperationProgressHandler<Platform::String^, Windows::Web::Http::HttpProgress>^ handler) {}
|
||||
}
|
||||
|
||||
virtual property Windows::Foundation::AsyncOperationWithProgressCompletedHandler<Platform::String^, Windows::Web::Http::HttpProgress>^ Completed
|
||||
{
|
||||
Windows::Foundation::AsyncOperationWithProgressCompletedHandler<Platform::String^, Windows::Web::Http::HttpProgress>^ get() { return nullptr; }
|
||||
void set(Windows::Foundation::AsyncOperationWithProgressCompletedHandler<Platform::String^, Windows::Web::Http::HttpProgress>^ handler) {}
|
||||
}
|
||||
|
||||
virtual Platform::String^ GetResults() { return m_result; }
|
||||
// IAsyncOperationWithProgress
|
||||
|
||||
private:
|
||||
Platform::String^ m_result;
|
||||
};
|
||||
}
|
||||
}
|
|
@ -62,7 +62,6 @@
|
|||
#include "CalcViewModel/Common/CalculatorButtonUser.h"
|
||||
#include "CalcViewModel/Common/NetworkManager.h"
|
||||
|
||||
#include "Mocks/CurrencyHttpClient.h"
|
||||
#include "Helpers.h"
|
||||
|
||||
#include "UnitTestApp.xaml.h"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue