Ejemplo n.º 1
0
        public void TestICredential_Attributes()
        {
            try
            {
                var cred = (new NetworkCredential(uName, pwd, domain)).ToICredential();
                cred.TargetName = "TestSystem_Attributes";
                cred.Attributes = new Dictionary <string, Object>();

                var sample = new SampleAttribute()
                {
                    role = "regular", created = DateTime.UtcNow
                };
                cred.Attributes.Add("sampleAttribute", sample);

                Assert.IsTrue(cred.SaveCredential(), "SaveCredential on ICredential failed");
                var cred1 = CredentialManager.GetICredential(cred.TargetName);
                Assert.IsNotNull(cred, "GetICredential failed");
                Assert.IsTrue(cred1.UserName == cred.UserName && cred1.CredentialBlob == cred.CredentialBlob && cred1.Attributes?.Count == cred.Attributes?.Count, "Saved and retrieved data doesn't match");
                //Assert.IsTrue(cred.Attributes.All(a=>a.Value == cred1.Attributes[a.Key]), "Saved and retrieved data doesn't match");
                Assert.IsTrue(((SampleAttribute)cred1.Attributes["sampleAttribute"]).role == sample.role, "Saved and retrieved data doesn't match");
            }
            catch (Exception e)
            {
                Assert.Fail("Unexpected exception of type {0} caught: {1}",
                            e.GetType(), e.Message);
                return;
            }
        }
        public void Sample_Collection_SetValues()
        {
            var subject = new SampleAttribute(1, 2, 3);

            Assert.That(subject.Value, Is.Null);
            Assert.That(subject.Values, Is.EqualTo(new[] { 1, 2, 3 }));
        }
Ejemplo n.º 3
0
        public void Dynamic_Class_Must_Be_Created_With_Specified_Method_With_Its_Parameter_Names()
        {
            var method1Name       = "Method1";
            var method1ReturnType = typeof(string);
            var method1Params     = new Dictionary <Type, string>
            {
                { typeof(int), "number" },
                { typeof(string), "message" },
            };
            var attributeType      = typeof(SampleAttribute);
            var ctorParamsMapping  = SampleAttribute.GetCtorParamValueMapping();
            var propsValuesMapping = SampleAttribute.GetPropertyValueMaaping();
            var setAttributeParam  = new Dictionary <Type, Tuple <IDictionary <Type, object>, IDictionary <string, object> > >
            {
                { attributeType, new Tuple <IDictionary <Type, object>, IDictionary <string, object> >(ctorParamsMapping, propsValuesMapping) },
            };


            var builder = DynamicTypeBuilderFactory.CreateClassBuilder("Dynamic.TestClass", new Dictionary <string, Type>());

            SetMethod(builder, method1Name, method1ReturnType, method1Params, setAttributeParam);

            var classType = builder.Build();

            AssertOnHavingMethodWithParamNames(
                classType: classType,
                methodName: method1Name,
                returnType: method1ReturnType,
                @params: method1Params,
                propValuesMapping: new Dictionary <Type, IDictionary <string, object> >
            {
                { attributeType, propsValuesMapping },
            });
        }
        public void Sample_Scalar_SetValue()
        {
            string scalar = "value";

            var subject = new SampleAttribute(scalar);

            Assert.That(subject.Value, Is.EqualTo(scalar));
            Assert.That(subject.Values, Is.Null);
        }
Ejemplo n.º 5
0
    private SampleContext CreateSampleContext(MethodInfo fixtureMethod, SampleAttribute sample)
    {
        var items = fixtureMethod.GetParameters().Select(parameter => new SampleContext.Item(parameter.Name ?? parameter.ToString())).ToArray();

        for (var index = 0; index < Math.Min(sample.Data.Length, items.Length); ++index)
        {
            items[index].Value = sample.Data[index];
        }
        return(new SampleContext(sample.Description, items));
    }
Ejemplo n.º 6
0
        [Ignore] // TODO: Known issue - we don't copy attributes yet
        public void ProxyPropertyReflectionReturnsAttributes()
        {
            IInstanceInterceptor interceptor = new InterfaceInterceptor();
            HasSomeProperties    target      = new HasSomeProperties();
            IInterceptingProxy   proxy       = interceptor.CreateProxy(typeof(IHaveSomeProperties), target);

            PropertyInfo    nameProp = proxy.GetType().GetProperty("Name");
            SampleAttribute attr     = (SampleAttribute)Attribute.GetCustomAttribute(nameProp, typeof(SampleAttribute));

            Assert.AreEqual("A name", attr.Name);
        }
Ejemplo n.º 7
0
            public void PrintAtt(MemberInfo member)
            {
                SampleAttribute a = Attribute.GetCustomAttribute(member, typeof(SampleAttribute)) as SampleAttribute;

                if (a == null)
                {
                    Console.WriteLine("Att not found.");
                }
                else
                {
                    Console.WriteLine(a.Msg);
                    Console.WriteLine(a.More);
                }
            }
Ejemplo n.º 8
0
        private void frmSample25_Load(object sender, System.EventArgs e)
        {
            grid.Redim(1, 3);

            grid.Columns[0].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize;
            grid.Columns[1].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize;
            grid.Columns[2].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize | SourceGrid.AutoSizeMode.EnableStretch;

            grid.FixedRows = 1;

            grid[0, 0] = new SourceGrid.Cells.ColumnHeader("Date");
            grid[0, 1] = new SourceGrid.Cells.ColumnHeader("Type");
            grid[0, 2] = new SourceGrid.Cells.ColumnHeader("Description");

            grid.AutoStretchColumnsToFitWidth = true;
            grid.SelectionMode = SourceGrid.GridSelectionMode.Row;

            AddLog("Log", "Application Started");


            //Create Data Cells
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            Type[] assemblyTypes = assembly.GetTypes();
            for (int i = 0; i < assemblyTypes.Length; i++)
            {
                object[] attributes = assemblyTypes[i].GetCustomAttributes(typeof(SampleAttribute), true);
                if (attributes != null && attributes.Length > 0)
                {
                    SampleAttribute sampleAttribute = (SampleAttribute)attributes[0];

                    AddLog("Log Menu", "Add menu for " + sampleAttribute.Description + " " + assemblyTypes[i].Name);

                    if (assemblyTypes[i] != this.GetType())
                    {
                        MenuItem menu = new MenuForm(this, assemblyTypes[i], sampleAttribute.Description + " " + sampleAttribute.SampleNumber.ToString());
                        mnWindow.MenuItems.Add(menu);
                    }
                }
            }

            for (int i = 0; i < 200; i++)
            {
                AddLog("Log", "Application test " + i.ToString());
            }

            grid.Columns.AutoSize(true);
            grid.Columns.StretchToFit();
        }
        public void Dynamic_Interface_Must_Be_Generated_With_Defined_Attribute()
        {
            var interfaceFullName = "Dynamic.GeneratedInterface";
            var attributeType     = typeof(SampleAttribute);
            var attributeCtorParamValueMapping = SampleAttribute.GetCtorParamValueMapping();
            var attributePropertyValueMapping  = SampleAttribute.GetPropertyValueMaaping();

            var interfaceBuilder = DynamicTypeBuilderFactory.CreateInterfaceBuilder(interfaceFullName);

            interfaceBuilder.SetAttribute(attributeType, attributeCtorParamValueMapping, attributePropertyValueMapping);

            var generatedType = interfaceBuilder.Build();

            AssertOnHavingAttributeOnType(
                interfaceType: generatedType,
                attributeType: attributeType,
                attributeCtorParamValueMapping: attributeCtorParamValueMapping,
                attributePropertyValueMapping: attributePropertyValueMapping);
        }
        public void Dynamic_Dto_Type_Must_Have_The_Specified_Attribute_On_The_Defined_Property()
        {
            var propertyName  = "SomeProperty";
            var attributeType = typeof(SampleAttribute);
            var ctorParametersValuesMapping = SampleAttribute.GetCtorParamValueMapping();
            var propertiesValuesMapping     = SampleAttribute.GetPropertyValueMaaping();

            var typeBuilder = DynamicTypeBuilderFactory.CreateDtoBuilder(propertyName);

            typeBuilder
            .SetProperty(propertyName, typeof(string))
            .SetAttribute(
                attributeType,
                ctorParametersValuesMapping,
                propertiesValuesMapping);

            var generatedType = typeBuilder.Build();

            AssertOnHavingPropertyWithFollowingAttribute(generatedType, propertyName, typeof(SampleAttribute), propertiesValuesMapping);
        }
Ejemplo n.º 11
0
        public void AttributeWrapper(Type type, int index)
        {
            SampleAttribute target = (SampleAttribute)type.GetCustomAttributes(typeof(SampleAttribute), true)[index];
            IAttributeInfo  info   = GenericCollectionUtils.ToArray(GetType(type).GetAttributeInfos(Reflector.Wrap(typeof(SampleAttribute)), true))[index];

            WrapperAssert.AreEquivalent(target, info, false);

            SampleAttribute resolvedAttrib = (SampleAttribute)info.Resolve(true);

            Assert.AreEqual(target.param, resolvedAttrib.param);
            Assert.AreEqual(target.Field, resolvedAttrib.Field);
            Assert.AreEqual(target.Property, resolvedAttrib.Property);

            try
            {
                WrapperAssert.AreEquivalent(typeof(SampleAttribute).GetConstructors()[0], info.Constructor, false);
            }
            catch (NotSupportedException)
            {
                // This is also acceptable behavior.
            }
            Dictionary <IFieldInfo, object> fieldValues = new Dictionary <IFieldInfo, object>();

            foreach (KeyValuePair <IFieldInfo, ConstantValue> entry in info.InitializedFieldValues)
            {
                fieldValues.Add(entry.Key, entry.Value.Resolve(true));
            }

            Dictionary <IPropertyInfo, object> propertyValues = new Dictionary <IPropertyInfo, object>();

            foreach (KeyValuePair <IPropertyInfo, ConstantValue> entry in info.InitializedPropertyValues)
            {
                propertyValues.Add(entry.Key, entry.Value.Resolve(true));
            }

            if (target.param == typeof(int))
            {
                try
                {
                    object[] values = GenericCollectionUtils.ConvertAllToArray <ConstantValue, object>(info.InitializedArgumentValues,
                                                                                                       delegate(ConstantValue constantValue) { return(constantValue.Resolve(true)); });

                    Assert.AreElementsEqual(new object[] { typeof(int) }, values);
                }
                catch (NotSupportedException)
                {
                    // This is also acceptable behavior.
                }

                if (fieldValues.Count != 0)
                {
                    Assert.AreEqual(1, fieldValues.Count, "The implementation may return values for uninitialized fields, but there is only one such field.");
                    Assert.AreEqual(0, fieldValues[GetField(typeof(SampleAttribute).GetField("Field"))]);
                }

                if (propertyValues.Count != 0)
                {
                    Assert.AreEqual(1, propertyValues.Count, "The implementation may return values uninitialized properties, but there is only one such field.");
                    Assert.AreEqual(null, propertyValues[GetProperty(typeof(SampleAttribute).GetProperty("Property"))]);
                }
            }
            else
            {
                try
                {
                    object[] values = GenericCollectionUtils.ConvertAllToArray <ConstantValue, object>(info.InitializedArgumentValues,
                                                                                                       delegate(ConstantValue constantValue) { return(constantValue.Resolve(true)); });

                    Assert.AreElementsEqual(new object[] { typeof(string[]) }, values);
                }
                catch (NotSupportedException)
                {
                    // This is also acceptable behavior.
                }


                Assert.AreElementsEqual(new KeyValuePair <IFieldInfo, object>[] {
                    new KeyValuePair <IFieldInfo, object>(GetField(typeof(SampleAttribute).GetField("Field")), 2)
                }, fieldValues);

                Assert.AreElementsEqual(new KeyValuePair <IPropertyInfo, object>[] {
                    new KeyValuePair <IPropertyInfo, object>(GetProperty(typeof(SampleAttribute).GetProperty("Property")), "foo")
                }, propertyValues);
            }
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Create a SampleBrowserItem and inserts into the right location.
        /// </summary>
        /// <param name="Samples">Collections of Samples.</param>
        /// <param name="type">Type of Sample.</param>
        /// <param name="attribute">Attributes for the Sample.</param>
        internal static void AddSample(IList<SampleBrowserItem> Samples, Type type, SampleAttribute attribute)
        {
            string name = attribute.Name;
            // Find any existing item
            SampleBrowserItem item = null;
            if (Samples != null)
            {
                foreach (SampleBrowserItem other in Samples)
                {
                    if (string.Compare(other.OriginalName, name, StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        item = other;
                        break;
                    }
                }
            }
            // Escape any name sorting
            string escapedName = name;
            if (name[0] == '(')
            {
                int index = name.IndexOf(')');
                if (index > 0)
                {
                    escapedName = name.Substring(index + 1, name.Length - index - 1);
                }
            }

            item = new SampleBrowserItem { Name = escapedName, OriginalName = name, SampleType = type, SampleLevel = attribute.DifficultyLevel };

            // Insert the item into the list in sorted order (linearly
            // because no item should have very many children)
            int sortedIndex = 0;
            foreach (SampleBrowserItem other in Samples)
            {
                if (string.Compare(other.OriginalName, name, StringComparison.OrdinalIgnoreCase) > 0)
                {
                    break;
                }
                else
                {
                    sortedIndex++;
                }
            }
            Samples.Insert(sortedIndex, item);
        }
Ejemplo n.º 13
0
 private IEnumerable <SampleContext> RetrieveSampleFromData(MethodInfo fixtureMethod, SampleAttribute sample)
 => new[] { CreateSampleContext(fixtureMethod, sample) };
Ejemplo n.º 14
0
 private IEnumerable <SampleContext> RetrieveSamples(MethodInfo fixtureMethod, SampleAttribute sample)
 => sample.Source is null?RetrieveSampleFromData(fixtureMethod, sample) : RetrieveSampleFromSource(fixtureMethod, sample.Source);