Skip to content

Commit

Permalink
Bug 1650698 - Convert test_collation.js to gtest. r=jfkthame
Browse files Browse the repository at this point in the history
test_collation.js uses nsICollation and nsICollationFactory from script, but
I would like to make it non-scriptable. Actually, it is only used from
xpcshell tests.

Differential Revision: https://phabricator.services.mozilla.com/D82310
  • Loading branch information
makotokato committed Jul 6, 2020
1 parent 4cb7f58 commit 7472376
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 81 deletions.
114 changes: 114 additions & 0 deletions intl/locale/tests/gtest/TestCollation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,117 @@ TEST(Collation, AllocateRowSortKey)

ASSERT_TRUE(result < 0);
}

class CollationComparator final {
public:
explicit CollationComparator(nsICollation* aCollation)
: mCollation(aCollation) {}

bool Equals(const nsString& a, const nsString& b) const {
int32_t result = 0;
mCollation->CompareString(nsICollation::kCollationStrengthDefault, a, b,
&result);
return result == 0;
}

bool LessThan(const nsString& a, const nsString& b) const {
int32_t result = 0;
mCollation->CompareString(nsICollation::kCollationStrengthDefault, a, b,
&result);
return result < 0;
}

private:
nsCOMPtr<nsICollation> mCollation;
};

TEST(Collation, CompareString)
{
nsTArray<nsString> input;
input.AppendElement(u"Argentina"_ns);
input.AppendElement(u"Oerlikon"_ns);
input.AppendElement(u"Offenbach"_ns);
input.AppendElement(u"Sverige"_ns);
input.AppendElement(u"Vaticano"_ns);
input.AppendElement(u"Zimbabwe"_ns);
input.AppendElement(u"la France"_ns);
input.AppendElement(u"\u00a1viva Espa\u00f1a!"_ns);
input.AppendElement(u"\u00d6sterreich"_ns);
input.AppendElement(u"\u4e2d\u56fd"_ns);
input.AppendElement(u"\u65e5\u672c"_ns);
input.AppendElement(u"\ud55c\uad6d"_ns);

nsCOMPtr<nsICollationFactory> colFactory =
do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
ASSERT_TRUE(colFactory);

// Locale en-US; default options.
nsCOMPtr<nsICollation> collation;
colFactory->CreateCollationForLocale("en-US"_ns, getter_AddRefs(collation));
ASSERT_TRUE(collation);

{
CollationComparator comparator(collation);
input.Sort(comparator);

ASSERT_TRUE(input[0].Equals(u"\u00a1viva Espa\u00f1a!"_ns));
ASSERT_TRUE(input[1].Equals(u"Argentina"_ns));
ASSERT_TRUE(input[2].Equals(u"la France"_ns));
ASSERT_TRUE(input[3].Equals(u"Oerlikon"_ns));
ASSERT_TRUE(input[4].Equals(u"Offenbach"_ns));
ASSERT_TRUE(input[5].Equals(u"\u00d6sterreich"_ns));
ASSERT_TRUE(input[6].Equals(u"Sverige"_ns));
ASSERT_TRUE(input[7].Equals(u"Vaticano"_ns));
ASSERT_TRUE(input[8].Equals(u"Zimbabwe"_ns));
ASSERT_TRUE(input[9].Equals(u"\ud55c\uad6d"_ns));
ASSERT_TRUE(input[10].Equals(u"\u4e2d\u56fd"_ns));
ASSERT_TRUE(input[11].Equals(u"\u65e5\u672c"_ns));
}

// Locale sv-SE; default options.
// Swedish treats "Ö" as a separate character, which sorts after "Z".
colFactory->CreateCollationForLocale("sv-SE"_ns, getter_AddRefs(collation));
ASSERT_TRUE(collation);

{
CollationComparator comparator(collation);
input.Sort(comparator);

ASSERT_TRUE(input[0].Equals(u"\u00a1viva Espa\u00f1a!"_ns));
ASSERT_TRUE(input[1].Equals(u"Argentina"_ns));
ASSERT_TRUE(input[2].Equals(u"la France"_ns));
ASSERT_TRUE(input[3].Equals(u"Oerlikon"_ns));
ASSERT_TRUE(input[4].Equals(u"Offenbach"_ns));
ASSERT_TRUE(input[5].Equals(u"Sverige"_ns));
ASSERT_TRUE(input[6].Equals(u"Vaticano"_ns));
ASSERT_TRUE(input[7].Equals(u"Zimbabwe"_ns));
ASSERT_TRUE(input[8].Equals(u"\u00d6sterreich"_ns));
ASSERT_TRUE(input[9].Equals(u"\ud55c\uad6d"_ns));
ASSERT_TRUE(input[10].Equals(u"\u4e2d\u56fd"_ns));
ASSERT_TRUE(input[11].Equals(u"\u65e5\u672c"_ns));
}

// Locale de-DE; default options.
// In German standard sorting, umlauted characters are treated as variants
// of their base characters: ä ≅ a, ö ≅ o, ü ≅ u.
colFactory->CreateCollationForLocale("de-DE"_ns, getter_AddRefs(collation));
ASSERT_TRUE(collation);

{
CollationComparator comparator(collation);
input.Sort(comparator);

ASSERT_TRUE(input[0].Equals(u"\u00a1viva Espa\u00f1a!"_ns));
ASSERT_TRUE(input[1].Equals(u"Argentina"_ns));
ASSERT_TRUE(input[2].Equals(u"la France"_ns));
ASSERT_TRUE(input[3].Equals(u"Oerlikon"_ns));
ASSERT_TRUE(input[4].Equals(u"Offenbach"_ns));
ASSERT_TRUE(input[5].Equals(u"\u00d6sterreich"_ns));
ASSERT_TRUE(input[6].Equals(u"Sverige"_ns));
ASSERT_TRUE(input[7].Equals(u"Vaticano"_ns));
ASSERT_TRUE(input[8].Equals(u"Zimbabwe"_ns));
ASSERT_TRUE(input[9].Equals(u"\ud55c\uad6d"_ns));
ASSERT_TRUE(input[10].Equals(u"\u4e2d\u56fd"_ns));
ASSERT_TRUE(input[11].Equals(u"\u65e5\u672c"_ns));
}
}
78 changes: 0 additions & 78 deletions intl/locale/tests/unit/test_collation.js

This file was deleted.

3 changes: 0 additions & 3 deletions intl/locale/tests/unit/xpcshell.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ support-files =
[test_bug22310.js]
skip-if = toolkit != "windows" && toolkit != "cocoa"

[test_collation.js]
skip-if = toolkit == "android" # ICU might be turned off

[test_bug1086527.js]
[test_intl_on_workers.js]
skip-if = toolkit == "android" # bug 1309447
Expand Down

0 comments on commit 7472376

Please sign in to comment.