Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 89e73e1

Browse files
Tweak tests to be more careful about platform, verify code page encodings not present by default.
1 parent 5427b51 commit 89e73e1

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/System.Text.Encoding.CodePages/tests/EncodingCodePages.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,18 @@ private static KeyValuePair<int, string> Map(int codePage, string webName)
453453
}
454454

455455
[Fact]
456+
// Specific asserts which would fail on Unix are conditionally not run.
457+
// [ActiveIssue(846, PlatformID.AnyUnix)]
456458
public static void TestDefaultEncodings()
457459
{
458-
// Check which encodings are available by default.
459-
foreach (var mapping in CrossplatformDefaultEncodings())
460+
ValidateDefaultEncodings();
461+
462+
foreach (object[] mapping in CodePageInfo())
460463
{
461-
Encoding encoding = Encoding.GetEncoding(mapping.Key);
462-
Assert.NotNull(encoding);
463-
Assert.Equal(mapping.Value, encoding.WebName);
464+
Assert.Throws<NotSupportedException>(() => Encoding.GetEncoding((int)mapping[0]));
465+
Assert.Throws<ArgumentException>(() => Encoding.GetEncoding((string)mapping[2]));
464466
}
465-
// Currently the class EncodingInfo isn't present in corefx, but this method should check for the exhaustive list
467+
// Currently the class EncodingInfo isn't present in corefx, so this checks none of the code pages are present.
466468
// When it is, comment out this line and remove the previous foreach/assert.
467469
// Assert.Equal(CrossplatformDefaultEncodings, Encoding.GetEncodings().OrderBy(i => i.CodePage).Select(i => Map(i.CodePage, i.WebName)));
468470

@@ -478,32 +480,42 @@ public static void TestDefaultEncodings()
478480
// Make sure added code pages are identical between the provider and the Encoding class.
479481
foreach (object[] mapping in CodePageInfo())
480482
{
481-
// Get encoding via query string.
482-
Encoding encoding = Encoding.GetEncoding((string)mapping[2]);
483-
Encoding codePageEncoding = CodePagesEncodingProvider.Instance.GetEncoding((string)mapping[2]);
483+
Encoding encoding = Encoding.GetEncoding((int)mapping[0]);
484+
Encoding codePageEncoding = CodePagesEncodingProvider.Instance.GetEncoding((int)mapping[0]);
484485
Assert.Equal(encoding, codePageEncoding);
485486
Assert.Equal(encoding.CodePage, (int)mapping[0]);
486487
Assert.Equal(encoding.WebName, (string)mapping[1]);
488+
// Get encoding via query string.
489+
Assert.Equal(Encoding.GetEncoding((string)mapping[2]), CodePagesEncodingProvider.Instance.GetEncoding((string)mapping[2]));
487490
}
488-
489491
// Adding the code page provider should keep the originals, too.
490-
foreach (var mapping in CrossplatformDefaultEncodings())
491-
{
492-
Encoding encoding = Encoding.GetEncoding(mapping.Key);
493-
Assert.NotNull(encoding);
494-
Assert.Equal(mapping.Value, encoding.WebName);
495-
}
496-
// Currently the class EncodingInfo isn't present in corefx, but this method should check for the exhaustive list
492+
ValidateDefaultEncodings();
493+
// Currently the class EncodingInfo isn't present in corefx, so this checks the complete list
497494
// When it is, comment out this line and remove the previous foreach/assert.
498495
// Assert.Equal(CrossplatformDefaultEncodings().Union(CodePageInfo().Select(i => Map((int)i[0], (string)i[1])).OrderBy(i => i.Key)),
499496
// Encoding.GetEncodings().OrderBy(i => i.CodePage).Select(i => Map(i.CodePage, i.WebName)));
500497

498+
// Default encoding may have changed, should still be something on the combined list.
501499
defaultEncoding = Encoding.GetEncoding(0);
502500
Assert.NotNull(defaultEncoding);
503501
mappedEncoding = Map(defaultEncoding.CodePage, defaultEncoding.WebName);
504502
Assert.Contains(mappedEncoding, CrossplatformDefaultEncodings().Union(CodePageInfo().Select(i => Map((int)i[0], (string)i[1]))));
505503
}
506504

505+
private static void ValidateDefaultEncodings()
506+
{
507+
foreach (var mapping in CrossplatformDefaultEncodings())
508+
{
509+
Encoding encoding = Encoding.GetEncoding(mapping.Key);
510+
Assert.NotNull(encoding);
511+
Assert.Equal(encoding, Encoding.GetEncoding(mapping.Value));
512+
#if !PLATFORM_UNIX
513+
// Currently Unix seems to only have UTF-8 as a default...
514+
Assert.Equal(mapping.Value, encoding.WebName);
515+
#endif
516+
}
517+
}
518+
507519
[Theory]
508520
[MemberData("SpecificCodepageEncodings")]
509521
public static void TestRoundtrippingSpecificCodepageEncoding(string encodingName, byte[] bytes, string expected)

0 commit comments

Comments
 (0)