mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 14:13:30 -07:00
add unit tests
This commit is contained in:
parent
c4757daff6
commit
9907af94f3
5 changed files with 149 additions and 10 deletions
|
@ -59,7 +59,7 @@ LocalizationService^ LocalizationService::GetInstance()
|
|||
LocalizationService::LocalizationService()
|
||||
{
|
||||
m_language = ApplicationLanguages::Languages->GetAt(0);
|
||||
m_flowDirection = ResourceContext::GetForCurrentView()->QualifierValues->Lookup(L"LayoutDirection")
|
||||
m_flowDirection = ResourceContext::GetForViewIndependentUse()->QualifierValues->Lookup(L"LayoutDirection")
|
||||
!= L"LTR" ? FlowDirection::RightToLeft : FlowDirection::LeftToRight;
|
||||
|
||||
auto localeName = std::string(m_language->Begin(), m_language->End());
|
||||
|
|
|
@ -504,12 +504,6 @@ bool CurrencyDataLoader::TryParseWebResponses(
|
|||
&& TryParseAllRatiosData(allRatiosJson, allRatiosData);
|
||||
}
|
||||
|
||||
Platform::String ^ te(const UCM::CurrencyStaticData& s)
|
||||
{
|
||||
return ref new Platform::String(s.countryName.c_str());
|
||||
};
|
||||
|
||||
|
||||
bool CurrencyDataLoader::TryParseStaticData(_In_ String^ rawJson, _Inout_ vector<UCM::CurrencyStaticData>& staticData)
|
||||
{
|
||||
JsonArray^ data = nullptr;
|
||||
|
@ -552,11 +546,11 @@ bool CurrencyDataLoader::TryParseStaticData(_In_ String^ rawJson, _Inout_ vector
|
|||
};
|
||||
}
|
||||
|
||||
auto sortCurrencyNames = [](UCM::CurrencyStaticData s) {
|
||||
return ref new Platform::String(s.countryName.c_str());
|
||||
auto sortCountryNames = [](const UCM::CurrencyStaticData & s) {
|
||||
return ref new String(s.countryName.c_str());
|
||||
};
|
||||
|
||||
LocalizationService::GetInstance()->Sort<UCM::CurrencyStaticData>(staticData, sortCurrencyNames);
|
||||
LocalizationService::GetInstance()->Sort<UCM::CurrencyStaticData>(staticData, sortCountryNames);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -244,6 +244,7 @@
|
|||
<ClCompile Include="CurrencyConverterUnitTests.cpp" />
|
||||
<ClCompile Include="DateCalculatorUnitTests.cpp" />
|
||||
<ClCompile Include="HistoryTests.cpp" />
|
||||
<ClCompile Include="LocalizationServiceUnitTests.cpp" />
|
||||
<ClCompile Include="Mocks\CurrencyHttpClient.cpp" />
|
||||
<ClCompile Include="Module.cpp" />
|
||||
<ClCompile Include="MultiWindowUnitTests.cpp" />
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
<ClCompile Include="Mocks\CurrencyHttpClient.cpp">
|
||||
<Filter>Mocks</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="LocalizationServiceUnitTests.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="AsyncHelper.h" />
|
||||
|
|
143
src/CalculatorUnitTests/LocalizationServiceUnitTests.cpp
Normal file
143
src/CalculatorUnitTests/LocalizationServiceUnitTests.cpp
Normal file
|
@ -0,0 +1,143 @@
|
|||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
#include <CppUnitTest.h>
|
||||
|
||||
#include "CalcViewModel/Common/LocalizationService.h"
|
||||
|
||||
using namespace CalculatorApp::Common;
|
||||
using namespace Platform;
|
||||
using namespace std;
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
namespace CalculatorUnitTests
|
||||
{
|
||||
TEST_CLASS(LocalizationServiceUnitTests)
|
||||
{
|
||||
public:
|
||||
|
||||
TEST_METHOD(TestSortStrings)
|
||||
{
|
||||
auto localizationService = LocalizationService::GetInstance();
|
||||
vector<String^> stringsToSort =
|
||||
{
|
||||
L"Zebra",
|
||||
L"Alpha",
|
||||
L"beta1",
|
||||
L"Gamma",
|
||||
L"Beta",
|
||||
L"alpha1",
|
||||
L"États-Unis",
|
||||
L"Epsilon",
|
||||
L"Etude",
|
||||
};
|
||||
|
||||
vector<String^> expectedResult =
|
||||
{
|
||||
L"Alpha",
|
||||
L"alpha1",
|
||||
L"Beta",
|
||||
L"beta1",
|
||||
L"Epsilon",
|
||||
L"États-Unis",
|
||||
L"Etude",
|
||||
L"Gamma",
|
||||
L"Zebra",
|
||||
};
|
||||
VERIFY_ARE_EQUAL(stringsToSort.size(), expectedResult.size());
|
||||
VERIFY_IS_FALSE(equal(stringsToSort.begin(), stringsToSort.end(), expectedResult.begin()));
|
||||
localizationService->Sort(stringsToSort);
|
||||
VERIFY_IS_TRUE(equal(stringsToSort.begin(), stringsToSort.end(), expectedResult.begin()));
|
||||
}
|
||||
|
||||
TEST_METHOD(TestSortEmptyStrings)
|
||||
{
|
||||
//Verify if LocalizationService::Sort doesn't crash when the vector is empty or null
|
||||
auto localizationService = LocalizationService::GetInstance();
|
||||
vector<String^> stringsToSort = {};
|
||||
localizationService->Sort(stringsToSort);
|
||||
stringsToSort = { L"" };
|
||||
localizationService->Sort(stringsToSort);
|
||||
stringsToSort = { L"",L"",L"" };
|
||||
localizationService->Sort(stringsToSort);
|
||||
stringsToSort = { nullptr,L"",L"" };
|
||||
localizationService->Sort(stringsToSort);
|
||||
}
|
||||
|
||||
TEST_METHOD(TestSortGeneric)
|
||||
{
|
||||
vector<String^> stringsToSort =
|
||||
{
|
||||
L"fermentum",
|
||||
L"fringilla",
|
||||
L"Curabitur",
|
||||
L"rhoncus",
|
||||
L"Aenean",
|
||||
L"Fusce",
|
||||
L"sollicitudin",
|
||||
L"empor",
|
||||
L"edapibus",
|
||||
L"édapibas",
|
||||
L"édapîbos",
|
||||
L"édapîbÉs",
|
||||
};
|
||||
|
||||
vector<String^> expectedResult =
|
||||
{
|
||||
L"Aenean",
|
||||
L"Curabitur",
|
||||
L"édapibas",
|
||||
L"édapîbÉs",
|
||||
L"édapîbos",
|
||||
L"edapibus",
|
||||
L"empor",
|
||||
L"fermentum",
|
||||
L"fringilla",
|
||||
L"Fusce",
|
||||
L"rhoncus",
|
||||
L"sollicitudin",
|
||||
};
|
||||
|
||||
|
||||
auto sortFunction = [](String^ s) {
|
||||
return ref new String(L"CAL:") + s + L"TEST";
|
||||
};
|
||||
|
||||
VERIFY_ARE_EQUAL(stringsToSort.size(), expectedResult.size());
|
||||
VERIFY_IS_FALSE(equal(stringsToSort.begin(), stringsToSort.end(), expectedResult.begin()));
|
||||
|
||||
auto localizationService = LocalizationService::GetInstance();
|
||||
localizationService->Sort<String^>(stringsToSort, sortFunction);
|
||||
VERIFY_IS_TRUE(equal(stringsToSort.begin(), stringsToSort.end(), expectedResult.begin()));
|
||||
|
||||
vector<String^> expected2Result =
|
||||
{
|
||||
L"édapibas",
|
||||
L"édapîbÉs",
|
||||
L"édapîbos",
|
||||
L"edapibus",
|
||||
L"Aenean",
|
||||
L"fermentum",
|
||||
L"rhoncus",
|
||||
L"empor",
|
||||
L"sollicitudin",
|
||||
L"fringilla",
|
||||
L"Curabitur",
|
||||
L"Fusce",
|
||||
};
|
||||
|
||||
auto sort2Function = [](String^ s) {
|
||||
return ref new String(s->Begin()+1);
|
||||
};
|
||||
|
||||
VERIFY_ARE_EQUAL(stringsToSort.size(), expected2Result.size());
|
||||
VERIFY_IS_FALSE(equal(stringsToSort.begin(), stringsToSort.end(), expected2Result.begin()));
|
||||
|
||||
localizationService->Sort<String^>(stringsToSort, sort2Function);
|
||||
VERIFY_IS_TRUE(equal(stringsToSort.begin(), stringsToSort.end(), expected2Result.begin()));
|
||||
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue