public void GetParameterFormatter_should_honor_custom_formatters_then_explicit_then_formattable_then_general_then_ToString()
        {
            var config = new ValueFormattingConfiguration()
                         .RegisterFrameworkDefaultGeneralFormatters()
                         .RegisterExplicit(typeof(bool), new FormatAttribute(">{0}<"))
                         .RegisterExplicit(typeof(int), new FormatAttribute("i{0}"))
                         .RegisterExplicit(typeof(MyFormattable2), new FormatAttribute("my-explicit2"))
                         .RegisterGeneral(new MyStructFormatter());

            var parameter = ParameterInfoHelper.GetMethodParameter <object[]>(Step_with_custom_formatters);
            var formatter = GetMetadataProvider(config).GetParameterFormatter(parameter);

            var values = new object[]
            {
                null,                 // null
                5,                    // explicit int
                true,                 // [FormatBoolean] overriding explicit bool
                5.5,                  // general struct
                new MyClass(),        // toString
                new MyFormattable1(), // [Format]
                new MyFormattable2(), // explicit formatter
                new MyFormattable3()  // ISelfFormattable
            };

            Assert.That(formatter(values), Is.EqualTo("#<null> | #i5 | #On | #s5.5 | #my-class | #my-custom-format1 | #my-explicit2 | #my3"));
        }
예제 #2
0
        public void It_should_initialize_object_with_default_values()
        {
            var configuration = new ValueFormattingConfiguration();

            Assert.That(configuration.GeneralFormatters, Is.Empty);
            Assert.That(configuration.ExplicitFormatters.Keys, Is.EquivalentTo(new[] { typeof(string) }));
        }
예제 #3
0
        public void It_should_clear_all_general_formatters()
        {
            var configuration = new ValueFormattingConfiguration()
                                .RegisterGeneral(Mock.Of <IConditionalValueFormatter>())
                                .ClearGeneral();

            Assert.That(configuration.GeneralFormatters, Is.Empty);
        }
예제 #4
0
        public void It_should_register_general_formatter()
        {
            var formatter1    = Mock.Of <IConditionalValueFormatter>();
            var formatter2    = Mock.Of <IConditionalValueFormatter>();
            var configuration = new ValueFormattingConfiguration()
                                .RegisterGeneral(formatter1)
                                .RegisterGeneral(formatter2);

            Assert.That(configuration.GeneralFormatters.ToArray(), Is.EqualTo(new[] { formatter1, formatter2 }));
        }
예제 #5
0
        public void It_should_clear_all_explicit_formatters()
        {
            var configuration             = new ValueFormattingConfiguration();
            var defaultExplicitFormatters = configuration.ExplicitFormatters.ToArray();

            configuration
            .RegisterExplicit(typeof(char), Mock.Of <IValueFormatter>())
            .ClearExplicit();

            Assert.That(configuration.ExplicitFormatters
                        .Select(x => x.GetType())
                        .ToArray(),
                        Is.EquivalentTo(defaultExplicitFormatters
                                        .Select(x => x.GetType())
                                        .ToArray()));
        }
예제 #6
0
        public void It_should_register_explicit_formatter_allowing_to_override_previous_ones()
        {
            var formatter1    = Mock.Of <IValueFormatter>();
            var formatter2    = Mock.Of <IValueFormatter>();
            var configuration = new ValueFormattingConfiguration()
                                .RegisterExplicit(typeof(string), formatter1)
                                .RegisterExplicit(typeof(int), formatter1)
                                .RegisterExplicit(typeof(object), formatter1)
                                .RegisterExplicit(typeof(object), formatter2);

            Assert.That(
                configuration.ExplicitFormatters.ToDictionary(x => x.Key, x => x.Value),
                Is.EqualTo(new Dictionary <Type, IValueFormatter>
            {
                { typeof(string), formatter1 },
                { typeof(int), formatter1 },
                { typeof(object), formatter2 }
            }));
        }
 private static TestMetadataProvider GetMetadataProvider(ValueFormattingConfiguration cfg)
 {
     return(new TestMetadataProvider(cfg));
 }
예제 #8
0
 public TestMetadataProvider(ValueFormattingConfiguration formattingConfiguration) :
     base(new DefaultNameFormatter(), new StepTypeConfiguration(), new DefaultCultureInfoProvider(), formattingConfiguration)
 {
 }
예제 #9
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="nameFormatter"><see cref="INameFormatter"/> object used to format names.</param>
        /// <param name="stepTypeConfiguration"><see cref="StepTypeConfiguration"/> object used in providing step metadata.</param>
        /// <param name="cultureInfoProvider"><see cref="ICultureInfoProvider"/> object used in providing step parameter formatters.</param>
        /// <param name="valueFormattingConfiguration"><see cref="IValueFormattingService"/> object used to format parameters.</param>
        protected CoreMetadataProvider(INameFormatter nameFormatter, StepTypeConfiguration stepTypeConfiguration, ICultureInfoProvider cultureInfoProvider, ValueFormattingConfiguration valueFormattingConfiguration)
        {
            if (stepTypeConfiguration == null)
            {
                throw new ArgumentNullException(nameof(stepTypeConfiguration));
            }
            _valueFormattingService = new ValueFormattingService(valueFormattingConfiguration, cultureInfoProvider);

            NameFormatter       = nameFormatter ?? throw new ArgumentNullException(nameof(nameFormatter));
            CultureInfoProvider = cultureInfoProvider ?? throw new ArgumentNullException(nameof(cultureInfoProvider));
            _nameParser         = new NameParser(nameFormatter);
            _stepTypeProcessor  = new StepTypeProcessor(nameFormatter, stepTypeConfiguration);
        }
 /// <summary>
 /// Applies framework default general formatters.
 /// </summary>
 /// <param name="configuration">Configuration.</param>
 /// <returns><paramref name="configuration"/>.</returns>
 public static ValueFormattingConfiguration RegisterFrameworkDefaultGeneralFormatters(this ValueFormattingConfiguration configuration)
 {
     return(configuration
            .RegisterGeneral(new DictionaryFormatter())
            .RegisterGeneral(new CollectionFormatter()));
 }
예제 #11
0
 public ValueFormattingService(ValueFormattingConfiguration configuration, ICultureInfoProvider cultureInfoProvider)
 {
     _generalFormatters   = configuration.GeneralFormatters.ToArray();
     _formatters          = new ConcurrentDictionary <Type, IValueFormatter>(configuration.ExplicitFormatters);
     _cultureInfoProvider = cultureInfoProvider;
 }