Example #1
0
        public void StringTemplateValueConverter_Caches_And_Reuses_Converters_And_Behaves_Properly_When_Caches_Are_Cleared()
        {
            StringTemplate.ClearCache(); // Make sure we don't use instances from other tests...

            var values = new ValuesWithMiscConverters {
                AttributedWithStateMutatorConverter = 0
            };
            var otherValues = new OtherValuesWithMiscConverters {
                AttributedWithStateMutatorConverter2 = 0
            };

            string actual1 = StringTemplate.Format("{AttributedWithStateMutatorConverter}", values);
            string actual2 = StringTemplate.Format("{AttributedWithStateMutatorConverter}", values);

            // Clear the cached getters and converters.
            // This test will fail if the getters cache is cleared, but not the converters cache.
            StringTemplate.ClearCache();

            string actual3 = StringTemplate.Format("{AttributedWithStateMutatorConverter}", values);
            string actual4 = StringTemplate.Format("{AttributedWithStateMutatorConverter2}", otherValues);

            // The converter instance should be reused, so its state is preserved and incremented between calls
            Assert.Equal("1", actual1);
            Assert.Equal("2", actual2);
            Assert.Equal("1", actual3);
            Assert.Equal("2", actual4); // tests caching across types and members
        }
Example #2
0
        public void Should_Throw_When_Converter_CanConvert_Is_False()
        {
            var values = new ValuesWithMiscConverters {
                AttributedWithConverterHavingCanConvertAlwaysFalse = 0
            };

            Assert.Throws <NotSupportedException>(() => StringTemplate.Format("{AttributedWithConverterHavingCanConvertAlwaysFalse}", values));
        }
Example #3
0
        public void Should_Throw_When_Attribute_Has_Null_Converter_Type()
        {
            var values = new ValuesWithMiscConverters {
                AttributedWithNullConverterType = 0
            };

            Assert.Throws <InvalidOperationException>(() => StringTemplate.Format("{AttributedWithNullConverterType}", values));
        }
Example #4
0
        public void Generic_Converter_CanConvert_Allows_Derived_Type()
        {
            var values = new ValuesWithMiscConverters {
                StringArrayWithReadOnlyCollectionConverter = StringCollectionValues
            };

            string actual = StringTemplate.Format("{StringArrayWithReadOnlyCollectionConverter}", values);

            Assert.Equal(ExpectedStringCollectionConcatenation, actual);
        }
Example #5
0
        public void Generic_Converter_Allows_Null_Values(object input, bool expectedResult)
        {
            var values = new ValuesWithMiscConverters {
                AttributedWithNullToBoolConverter = input
            };

            string actual = StringTemplate.Format("{AttributedWithNullToBoolConverter}", values);

            Assert.Equal(expectedResult, bool.Parse(actual));
        }
Example #6
0
        public void StringTemplateValueConverter_Caches_And_Reuses_Converters()
        {
            StringTemplate.ClearCache(); // Make sure we don't use instances from other tests...

            var values = new ValuesWithMiscConverters {
                AttributedWithStateMutatorConverter = 0
            };
            var otherValues = new OtherValuesWithMiscConverters {
                AttributedWithStateMutatorConverter2 = 0
            };

            string actual1 = StringTemplate.Format("{AttributedWithStateMutatorConverter}", values);
            string actual2 = StringTemplate.Format("{AttributedWithStateMutatorConverter}", values);
            string actual3 = StringTemplate.Format("{AttributedWithStateMutatorConverter}", values);
            string actual4 = StringTemplate.Format("{AttributedWithStateMutatorConverter2}", otherValues);

            // The converter instance should be reused, so its state is preserved and incremented between calls
            Assert.Equal("1", actual1);
            Assert.Equal("2", actual2);
            Assert.Equal("3", actual3);
            Assert.Equal("4", actual4); // tests caching across types and members
        }