예제 #1
0
        public void CanConvert()
        {
            var result = EnumListOptionHelper.ConvertToOptions(typeof(TestEnum))?.ToList();

            using (new AssertionScope())
            {
                result.Should().NotBeNullOrEmpty();
                result.Should().HaveCount(5);

                result[0].Text.Should().Be("None");
                result[0].Value.Should().Be("None");

                result[1].Text.Should().Be("Item one");
                result[1].Value.Should().Be("ItemOne");

                result[2].Text.Should().Be("Item 2");
                result[2].Value.Should().Be("ItemTwo");

                result[3].Text.Should().Be("Item three and four");
                result[3].Value.Should().Be("ItemThreeAndFour");

                result[4].Text.Should().Be("88");
                result[4].Value.Should().Be("ItemEightyEight");
            }
        }
        public static void AddToMetadata(DisplayMetadata modelMetaData, Type optionSourceType)
        {
            if (optionSourceType == null)
            {
                throw new ArgumentNullException(nameof(optionSourceType));
            }

            ICollection <ListOption> options = null;

            if (optionSourceType.IsEnum)
            {
                options = EnumListOptionHelper.ConvertToOptions(optionSourceType);
            }
            else
            {
                if (optionSourceType.GetConstructor(Type.EmptyTypes) == null)
                {
                    var msg = "OptionSource type does not have a public parameterless constructor.";
                    throw new InvalidOperationException(msg);
                }

                var source = Activator.CreateInstance(optionSourceType);

                if (source is IListOptionApiSource apiSource)
                {
                    ValidateApiSource(optionSourceType, apiSource);

                    modelMetaData.AdditionalValues.Add("OptionsApi", apiSource.Path);
                    modelMetaData.AdditionalValues.Add("OptionName", TextFormatter.Camelize(apiSource.NameField));
                    modelMetaData.AdditionalValues.Add("OptionValue", TextFormatter.Camelize(apiSource.ValueField));
                }
                else if (source is IListOptionSource listOptionSource)
                {
                    options = listOptionSource.Create();
                }
                else
                {
                    var msg = "OptionSource type is not a valid type. Valid types are ";
                    throw new InvalidOperationException(msg);
                }
            }

            if (options != null)
            {
                modelMetaData.AdditionalValues.Add("OptionName", "text");
                modelMetaData.AdditionalValues.Add("OptionValue", "value");
                modelMetaData.AdditionalValues.Add("Options", options);
            }
        }