コード例 #1
0
        public static void TypeConverterStringResources()
        {
            var values = TestData.StringConverter;

            byte[] binaryWriterBuffer;

            using (MemoryStream ms = new MemoryStream())
                using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
                {
                    foreach (var pair in values)
                    {
                        writer.AddResource(pair.Key, TestData.GetStringValue(pair.Value), TestData.GetSerializationTypeName(pair.Value.GetType()));
                    }
                    writer.Generate();
                    binaryWriterBuffer = ms.ToArray();
                }

            using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
                using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
                {
                    IDictionaryEnumerator dictEnum = reader.GetEnumerator();

                    while (dictEnum.MoveNext())
                    {
                        ResourceValueEquals(values[(string)dictEnum.Key], dictEnum.Value);
                    }
                }
        }
コード例 #2
0
        public static void PrimitiveResourcesAsStrings()
        {
            IReadOnlyDictionary <string, object> values = TestData.Primitive;

            byte[] writerBuffer, binaryWriterBuffer;
            using (MemoryStream ms = new MemoryStream())
                using (ResourceWriter writer = new ResourceWriter(ms))
                {
                    foreach (var pair in values)
                    {
                        writer.AddResource(pair.Key, pair.Value);
                    }

                    writer.Generate();
                    writerBuffer = ms.ToArray();
                }

            using (MemoryStream ms = new MemoryStream())
                using (PreserializedResourceWriter writer = new PreserializedResourceWriter(ms))
                {
                    foreach (var pair in values)
                    {
                        writer.AddResource(pair.Key, TestData.GetStringValue(pair.Value), TestData.GetSerializationTypeName(pair.Value.GetType()));
                    }
                    writer.Generate();
                    binaryWriterBuffer = ms.ToArray();
                }

            // PreserializedResourceWriter should write ResourceWriter/ResourceReader format
            Assert.Equal(writerBuffer, binaryWriterBuffer);

            using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
                using (ResourceReader reader = new ResourceReader(ms))
                {
                    IDictionaryEnumerator dictEnum = reader.GetEnumerator();

                    while (dictEnum.MoveNext())
                    {
                        Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value);
                    }
                }

            // DeserializingResourceReader can read ResourceReader format
            using (MemoryStream ms = new MemoryStream(binaryWriterBuffer, false))
                using (DeserializingResourceReader reader = new DeserializingResourceReader(ms))
                {
                    IDictionaryEnumerator dictEnum = reader.GetEnumerator();

                    while (dictEnum.MoveNext())
                    {
                        Assert.Equal(values[(string)dictEnum.Key], dictEnum.Value);
                    }
                }
        }
コード例 #3
0
        public static void ExceptionforNullResourceId()
        {
            using (var writer = new PreserializedResourceWriter(new MemoryStream()))
            {
                Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, "value"));
                Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, new object()));
                Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, new byte[0]));

                using (var stream = new MemoryStream())
                {
                    Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, stream));
                    Assert.Throws <ArgumentNullException>("name", () => writer.AddResource(null, stream, true));
                    Assert.Throws <ArgumentNullException>("name", () => writer.AddActivatorResource(null, "System.DayOfWeek", stream, false));
                }

                Assert.Throws <ArgumentNullException>("name", () => writer.AddBinaryFormattedResource(null, "System.DayOfWeek", new byte[1]));
                Assert.Throws <ArgumentNullException>("name", () => writer.AddTypeConverterResource(null, "System.DayOfWeek", new byte[1]));
                Assert.Throws <ArgumentNullException>("name", () => writer.AddTypeConverterResource(null, "System.DayOfWeek", "Monday"));
            }
        }
コード例 #4
0
        public static void ExceptionForAddAfterGenerate()
        {
            using (var writer = new PreserializedResourceWriter(new MemoryStream()))
            {
                writer.AddResource("duplicate", "value");

                writer.Generate();

                Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", "value"));
                Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", new object()));
                Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", new byte[0]));

                using (var stream = new MemoryStream())
                {
                    Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", stream));
                    Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", stream, true));
                    Assert.Throws <InvalidOperationException>(() => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false));
                }

                Assert.Throws <InvalidOperationException>(() => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek"));
                Assert.Throws <InvalidOperationException>(() => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek"));
                Assert.Throws <InvalidOperationException>(() => writer.AddResource("duplicate", "Monday", "System.DayOfWeek"));
            }
        }
コード例 #5
0
        public static void ExceptionforDuplicateKey()
        {
            using (var writer = new PreserializedResourceWriter(new MemoryStream()))
            {
                writer.AddResource("duplicate", "value");

                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", "value"));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", new object()));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", new byte[0]));

                using (var stream = new MemoryStream())
                {
                    AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", stream));
                    AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", stream, true));
                    AssertExtensions.Throws <ArgumentException>(null, () => writer.AddActivatorResource("duplicate", stream, "System.DayOfWeek", false));
                }

                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicate", new byte[1], "System.DayOfWeek"));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddTypeConverterResource("duplicate", new byte[1], "System.DayOfWeek"));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicate", "Monday", "System.DayOfWeek"));


                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("Duplicate", "value"));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("dUplicate", new object()));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duPlicate", new byte[0]));

                using (var stream = new MemoryStream())
                {
                    AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("dupLicate", stream));
                    AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplIcate", stream, true));
                    AssertExtensions.Throws <ArgumentException>(null, () => writer.AddActivatorResource("dupliCate", stream, "System.DayOfWeek", false));
                }

                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddBinaryFormattedResource("duplicAte", new byte[1], "System.DayOfWeek"));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddTypeConverterResource("duplicaTe", new byte[1], "System.DayOfWeek"));
                AssertExtensions.Throws <ArgumentException>(null, () => writer.AddResource("duplicatE", "Monday", "System.DayOfWeek"));
            }
        }