Esempio n. 1
0
        public void GetEncodings_FromProvider_DoesNotContainDisallowedEncodings(string encodingName, int codePage)
        {
            Mock <EncodingProvider> mockProvider = new Mock <EncodingProvider>(MockBehavior.Strict);

            mockProvider.Setup(o => o.GetEncodings()).Returns(
                new[] { new EncodingInfo(mockProvider.Object, codePage, encodingName, "UTF-7") });

            ThreadStaticEncodingProvider.WithEncodingProvider(mockProvider.Object, () =>
            {
                foreach (EncodingInfo encodingInfo in Encoding.GetEncodings())
                {
                    Assert.NotEqual(encodingName, encodingInfo.Name, StringComparer.OrdinalIgnoreCase);
                    Assert.NotEqual(codePage, encodingInfo.CodePage);
                }
            });
        }
Esempio n. 2
0
        public void GetEncoding_FromProvider_ByEncodingName_WithDisallowedEncoding_Throws(string encodingName, int codePage)
        {
            Mock <Encoding> mockEncoding = new Mock <Encoding>();

            mockEncoding.Setup(o => o.CodePage).Returns(codePage);

            Mock <EncodingProvider> mockProvider = new Mock <EncodingProvider>();

            mockProvider.Setup(o => o.GetEncoding(encodingName)).Returns(mockEncoding.Object);
            mockProvider.Setup(o => o.GetEncoding(encodingName, It.IsAny <EncoderFallback>(), It.IsAny <DecoderFallback>())).Returns(mockEncoding.Object);

            ThreadStaticEncodingProvider.WithEncodingProvider(mockProvider.Object, () =>
            {
                Assert.Throws <NotSupportedException>(() => Encoding.GetEncoding(encodingName));
                Assert.Throws <NotSupportedException>(() => Encoding.GetEncoding(encodingName, EncoderFallback.ReplacementFallback, DecoderFallback.ReplacementFallback));
            });
        }
Esempio n. 3
0
#pragma warning disable xUnit1026 // Theory methods should use all of their parameters
        public void GetEncoding_FromProvider_ByCodePage_WithDisallowedEncoding_Throws(string encodingName, int codePage)
#pragma warning restore xUnit1026 // Theory methods should use all of their parameters
        {
            Mock <Encoding> mockEncoding = new Mock <Encoding>();

            mockEncoding.Setup(o => o.CodePage).Returns(codePage);

            Mock <EncodingProvider> mockProvider = new Mock <EncodingProvider>();

            mockProvider.Setup(o => o.GetEncoding(codePage)).Returns(mockEncoding.Object);
            mockProvider.Setup(o => o.GetEncoding(codePage, It.IsAny <EncoderFallback>(), It.IsAny <DecoderFallback>())).Returns(mockEncoding.Object);

            ThreadStaticEncodingProvider.WithEncodingProvider(mockProvider.Object, () =>
            {
                Assert.Throws <NotSupportedException>(() => Encoding.GetEncoding(codePage));
                Assert.Throws <NotSupportedException>(() => Encoding.GetEncoding(codePage, EncoderFallback.ReplacementFallback, DecoderFallback.ReplacementFallback));
            });
        }