public void EnumTypeParsing_ValidateCachingOnTypeName_Works()
        {
            EnumTypeAttribute typeAttribute  = EnumTypeAttribute.TypeAttributeOf(typeof(ButtonById));
            EnumTypeAttribute typeAttribute2 = EnumTypeAttribute.TypeAttributeOf(typeof(Fragment.ButtonById));

            Assert.AreEqual(typeAttribute, typeAttribute2);
        }
        public void EnumTypeParsing_TextAll_IsCollection()
        {
            EnumTypeAttribute typeAttribute = EnumTypeAttribute.TypeAttributeOf(typeof(TextAllByClass));

            Assert.AreEqual(typeAttribute.IsCollection, true);
            Assert.AreEqual(typeAttribute.Mechanism, Mechanisms.ByClass);
            Assert.AreEqual(typeAttribute.TagName, HtmlTagName.Text);
        }
        public void EnumTypeParsing_ValidMechnismAndTag1_Success()
        {
            EnumTypeAttribute typeAttribute = EnumTypeAttribute.TypeAttributeOf(typeof(ButtonById));

            Assert.AreEqual(typeAttribute.IsCollection, false);
            Assert.AreEqual(typeAttribute.Mechanism, Mechanisms.ById);
            Assert.AreEqual(typeAttribute.TagName, HtmlTagName.Button);
        }
    public static string EnumType(this Enum value)
    {
        FieldInfo field = value.GetType().GetField(value.ToString());

        EnumTypeAttribute attribute
            = Attribute.GetCustomAttribute(field, typeof(EnumTypeAttribute))
              as EnumTypeAttribute;

        return(attribute == null?value.ToString() : attribute.EnumType);
    }
Example #5
0
        /// <summary>
        ///     Change the property type to an enum type.
        /// </summary>
        /// <param name="typeName"></param>
        public void ChangePropertyType(EnumType enumType)
        {
            Debug.Assert(enumType != null, "typeName parameter value is null");
            if (enumType != null)
            {
                ClearEFObject(_typeAttrForPrimitiveType);
                _typeAttrForPrimitiveType = null;

                EnumTypeAttribute.SetRefName(enumType);
                EnumTypeAttribute.Rebind();
            }
        }
Example #6
0
        public static EnumMemberAttribute EnumMemberAttributeOf(Enum theEnum)
        {
            if (CachedEnumMemberAttributes.ContainsKey(theEnum))
            {
                return(CachedEnumMemberAttributes[theEnum]);
            }

            Type enumType = theEnum.GetType();

            if (!EnumMemberFullNames.ContainsKey(theEnum))
            {
                EnumMemberFullNames.Add(theEnum, string.Format("{0}.{1}", enumType.FullName, theEnum));
            }

            //Get the Type Attribute: IsCollection, Mechanism and HtmlTag
            EnumTypeAttribute   typeAttribute   = EnumTypeAttribute.TypeAttributeOf(enumType);
            EnumMemberAttribute memberAttribute = null;

            //Try to get the EnumMemberAttribute defined in codes, especially the Meaning of theEnum
            var    fi = enumType.GetField(theEnum.ToString());
            var    usageAttributes = GetCustomAttributes(fi, typeof(EnumMemberAttribute), false);
            int    attributesCount = usageAttributes.Count();
            string enumValue       = null;

            if (attributesCount == 0)
            {
                enumValue       = theEnum.DefaultValue();
                memberAttribute = new EnumMemberAttribute(enumValue, false, DescriptionOf(typeAttribute, enumValue));
            }
            else if (attributesCount == 1)
            {
                EnumMemberAttribute attr = (EnumMemberAttribute)usageAttributes[0];
                enumValue = attr.Value ?? theEnum.DefaultValue();
                //if (enumValue != theEnum.DoString())
                //    theEnum.ChangeValue(enumValue);

                memberAttribute = new EnumMemberAttribute(enumValue,
                                                          attr.IsFragment, attr.Description ?? DescriptionOf(typeAttribute, enumValue));
            }

            if (memberAttribute == null)
            {
                throw new Exception("Unexpected situation when memberAttribute is null.");
            }

            string css = CssSelectorOf(typeAttribute, memberAttribute, enumValue);

            memberAttribute = new EnumMemberAttribute(memberAttribute.Value, memberAttribute.IsFragment, memberAttribute.Description, css,
                                                      typeAttribute.Mechanism, typeAttribute.IsCollection, typeAttribute.TagName);
            CachedEnumMemberAttributes.Add(theEnum, memberAttribute);
            return(memberAttribute);
        }
        public void CssComposing_MechanismOfByCustom()
        {
            int    i         = 0;
            string enumValue = " cite";

            Console.WriteLine("When EnumValue = '{0}'", enumValue);
            foreach (var tag in allTags)
            {
                Mechanisms          mechanisms      = Mechanisms.ByCustom;
                EnumTypeAttribute   typeAttribute   = new EnumTypeAttribute(false, mechanisms, tag);
                EnumMemberAttribute memberAttribute = new EnumMemberAttribute(enumValue);
                string css = EnumMemberAttribute.CssSelectorOf(typeAttribute, memberAttribute, enumValue);
                Console.WriteLine(string.Format("{0, 3}) {1, -20} : {2}", i++, typeAttribute, css));
            }
        }
Example #8
0
        public void TestValue4()
        {
            var list = EnumTypeAttribute.GetEnumTypes(SampleEnum1.Value4);

            list.Count.Should().Be(0);

            foreach (var obj in list)
            {
                EnumTypeAttribute.IsDefined(SampleEnum1.Value4, obj).Should().Be.True();
            }

            EnumTypeAttribute.IsDefined(SampleEnum1.Value4, "asd").Should().Be.False();
            EnumTypeAttribute.IsDefined(SampleEnum1.Value4, 123).Should().Be.False();
            EnumTypeAttribute.IsDefined(SampleEnum1.Value4, 234).Should().Be.False();
        }
        public void Composing_WithValueSplitter()
        {
            int    i         = 0;
            string enumValue = "A0=A1";

            Console.WriteLine("When EnumValue = '{0}'", enumValue);
            foreach (var tag in allTags)
            {
                foreach (var mechanismse in allMechanismses)
                {
                    EnumTypeAttribute   typeAttribute   = new EnumTypeAttribute(false, mechanismse, tag);
                    EnumMemberAttribute memberAttribute = new EnumMemberAttribute(enumValue);
                    string css = EnumMemberAttribute.CssSelectorOf(typeAttribute, memberAttribute, enumValue);
                    Console.WriteLine(string.Format("{0, 3}) {1, -20} : {2}", i++, typeAttribute, css));
                }
            }
        }
Example #10
0
        public static string CssSelectorOf(EnumTypeAttribute typeAttribute, EnumMemberAttribute memberAttribute, string memberValue)
        {
            HtmlTagName tagName         = typeAttribute.TagName;
            string      tagLocator      = tagName.Value();
            Mechanisms  mechanism       = typeAttribute.Mechanism;
            string      mechanismFormat = typeAttribute.Mechanism.Value();

            string css;

            switch (mechanism)
            {
            case Mechanisms.ByAttr:
            case Mechanisms.ByAttribute:
            {
                int index = memberValue.IndexOf(EnumValueSplitter);
                if (index == -1) //The Value of the Enum entry shall be treated as the attribute name
                {
                    string withAttr = string.Format("[{0}]", memberValue);
                    css = string.Format(tagLocator, string.Empty, withAttr);
                }
                else
                {
                    string attrString = string.Format(mechanismFormat,
                                                      memberValue.Substring(0, index), memberValue.Substring(index + 1));
                    css = string.Format(tagLocator, string.Empty, attrString);
                }
                break;
            }

            case Mechanisms.ByOrder:
            case Mechanisms.ByOrderLast:
            case Mechanisms.ByChild:
            case Mechanisms.ByChildLast:
            {
                string[] pair = memberValue.Split(EnumValueSplitter);

                string attrString = null;
                switch (pair.Count())
                {
                case 2:
                    attrString = string.Format(mechanismFormat, string.Empty, pair[1]).Trim();
                    css        = string.Format(tagLocator, pair[0], attrString);
                    break;

                case 1:
                    attrString = string.Format(mechanismFormat, string.Empty, memberValue).TrimStart();
                    css        = string.Format(tagLocator, string.Empty, attrString);
                    break;

                default:
                    throw new InvalidEnumArgumentException(mechanismFormat +
                                                           " expects one item or two items seperated by '='.");
                }
                break;
            }

            case Mechanisms.ByText:
            case Mechanisms.ByTag:
            {
                //Notice: this is not preferrable with CSS Locator.
                //The value of the Enum member shall be used afterwards
                css = string.Format(tagLocator, string.Empty, string.Empty);
                break;
            }

            case Mechanisms.ByCustom:
            {
                css = memberValue;
                break;
            }

            case Mechanisms.ByAdjacent:
            case Mechanisms.ByAncestor:
            case Mechanisms.ByParent:
            case Mechanisms.BySibling:
            {
                string[] pair = memberValue.Split(EnumValueSplitter);

                string attrString = null;
                switch (pair.Count())
                {
                case 2:
                    attrString = string.Format(mechanismFormat, pair[0], string.Empty);
                    css        = string.Format(tagLocator, attrString, pair[1]);
                    break;

                case 1:
                    attrString = string.Format(mechanismFormat, memberValue, string.Empty);
                    css        = string.Format(tagLocator, attrString, string.Empty);
                    break;

                default:
                    throw new InvalidEnumArgumentException(mechanismFormat +
                                                           " expects one item or two items seperated by '='.");
                }
                break;
            }

            default:
                //case Mechanisms.ById:
                //case Mechanisms.ByClass:
                //case Mechanisms.ByUrl:
                //case Mechanisms.ByName:
                //case Mechanisms.ByValue:
            {
                string[] pair = memberValue.Split(EnumValueSplitter);
                string   byId = string.Format(mechanismFormat, pair.Last());
                css = string.Format(tagLocator, string.Empty, byId);
                break;
            }
            }
            return(css);
        }
Example #11
0
 public static string DescriptionOf(EnumTypeAttribute typeAttribute, string enumValue)
 {
     return(string.Format("{0}{1} Locator {2}: {3}", typeAttribute.IsCollection ? "All" : "",
                          typeAttribute.TagName, typeAttribute.Mechanism, enumValue));
 }
 public void EnumTypeParsing_InvalidMechnismAndTag_ExceptionThrown()
 {
     Assert.Catch <InvalidEnumArgumentException>(
         () => EnumTypeAttribute.TypeAttributeOf(typeof(InvalidCombination))
         );
 }
        public void EnumExtension_AfterUsingEnumTypeAttribute_MechanismValuesRegistered()
        {
            EnumTypeAttribute typeAttribute = EnumTypeAttribute.TypeAttributeOf(typeof(ButtonById));

            Assert.IsTrue(EnumExtension.EnumValues.ContainsKey(Mechanisms.ByClass));
        }