Example #1
0
        public static SmartType OverrideSmartType(SmartType smartType, string memberName, object value)
        {
            if (value == null || memberName == null)
            {
                return(smartType);
            }

            if (memberName != "m_targetBuilding")
            {
                return(smartType);
            }

            switch (value)
            {
            case Vehicle vehicle:
                var vehicleCopy = vehicle;
                if (vehicle.Info?.m_vehicleAI?.GetTargetID(0, ref vehicleCopy).NetNode > 0)
                {
                    return(SmartType.NetNode);
                }

                break;

            case CitizenInstance citizenInstance:
                var citizenInstanceCopy = citizenInstance;
                if (citizenInstance.Info?.m_citizenAI?.GetTargetID(0, ref citizenInstanceCopy).NetNode > 0)
                {
                    return(SmartType.NetNode);
                }

                break;
            }

            return(smartType);
        }
 private void Load(SmartType type, IEnumerable <SmartGenericJsonData> data)
 {
     foreach (var d in data)
     {
         Add(type, d);
     }
 }
Example #3
0
        private static void AssertNullCheckForEveryPossibleArgumentOf(IConstraintsViolations violations,
                                                                      IConstructorWrapper constructor,
                                                                      FallbackTypeGenerator fallbackTypeGenerator)
        {
            for (int i = 0; i < constructor.GetParametersCount(); ++i)
            {
                var parameters = constructor.GenerateAnyParameterValues(Any.Instance);
                if (SmartType.ForTypeOf(parameters[i]).CanBeAssignedNullValue())
                {
                    parameters[i] = null;

                    try
                    {
                        fallbackTypeGenerator.GenerateInstance(parameters);
                        violations.Add("Not guarded against nulls: " + constructor + ", Not guarded parameter: " +
                                       constructor.GetDescriptionForParameter(i));
                    }
                    catch (TargetInvocationException exception)
                    {
                        if (exception.InnerException.GetType() == typeof(ArgumentNullException))
                        {
                            //do nothing, this is the expected case
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
        }
        private string GetTitleForType(SmartType type)
        {
            switch (type)
            {
            case SmartType.SmartEvent:
                return("Pick event");

            case SmartType.SmartAction:
                return("Pick action");

            case SmartType.SmartTarget:
                return("Pick action target");

            case SmartType.SmartCondition:
                return("Pick condition");

            case SmartType.SmartConditionSource:
                return("Pick condition source");

            case SmartType.SmartSource:
                return("Pick action source");

            default:
                return("Pick");
            }
        }
Example #5
0
    public object?Dummy(GenerationRequest request, Type type)
    {
        var smartType = SmartType.For(type);

        if (type.IsPrimitive)
        {
            return(_unconstrainedChain.Resolve(this, request, type));
        }

        if (type == typeof(string))
        {
            return(_unconstrainedChain.Resolve(this, request, type));
        }

        var emptyCollectionInstantiation = new EmptyCollectionInstantiation();

        if (smartType.IsImplementationOfOpenGeneric(typeof(IEnumerable <>)))
        {
            return(emptyCollectionInstantiation.CreateCollectionPassedAsGenericType(type));
        }

        if (smartType.IsOpenGeneric(typeof(IEnumerable <>)))
        {
            return(emptyCollectionInstantiation.EmptyEnumerableOf(type.GetCollectionItemType()));
        }

        if (type.IsAbstract)
        {
            return(default);
        public void ShouldReportWhetherTypeHasAtMostGivenConstructorCount(
            int maxCount, bool expectedResult)
        {
            var type = SmartType.For(typeof(string));

            XAssert.Equal(expectedResult, type.HasPublicConstructorCountOfAtMost(maxCount));
        }
Example #7
0
 public int NameToId(SmartType type, string id)
 {
     if (!smart_name_to_id.ContainsKey(type))
     {
         return(-1);
     }
     return(smart_name_to_id[type][id]);
 }
Example #8
0
        public static void NoStaticFields(Type type)
        {
            var staticFields = new List <IFieldWrapper>(SmartType.For(type).GetAllStaticFields());

            staticFields.Should()
            .BeEmpty("SmartType " + type + " should not contain static fields, but: " + Environment.NewLine +
                     StringFrom(staticFields));
        }
Example #9
0
 private GenericFakeChainFactory <T> CreateGenericFakeChainFactory <T>()
 {
     return(new GenericFakeChainFactory <T>(CreateSpecialCasesOfResolutions <T>(), new FallbackTypeGenerator <T>(new FallbackTypeGenerator(
                                                                                                                     new IFallbackGeneratedObjectCustomization[]
     {
         new FillPropertiesCustomization(),
         new FillFieldsCustomization()
     }, SmartType.For(typeof(T))))));
 }
        public bool Contains(SmartType type, int id)
        {
            if (!_smartIdData.ContainsKey(type))
            {
                return(false);
            }

            return(_smartIdData[type].ContainsKey(id));
        }
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var properties = SmartType.For(_type).GetAllPublicInstanceProperties();

            foreach (var item in properties.Where(item => item.HasPublicSetter()))
            {
                violations.Add(item.ShouldNotBeMutableButIs());
            }
        }
Example #12
0
        public bool SmartExist(SmartType type, string id)
        {
            if (!smart_id_data.ContainsKey(type))
            {
                return(false);
            }

            return(smart_name_to_id.ContainsKey(type) && smart_name_to_id[type].ContainsKey(id));
        }
        public SmartGenericJsonData GetDataByName(SmartType type, string name)
        {
            if (!_smartNameData[type].ContainsKey(name))
            {
                throw new NullReferenceException();
            }

            return(_smartNameData[type][name]);
        }
        public SmartGenericJsonData GetRawData(SmartType type, int id)
        {
            if (!_smartIdData[type].ContainsKey(id))
            {
                throw new NullReferenceException();
            }

            return(_smartIdData[type][id]);
        }
        public bool Contains(SmartType type, string id)
        {
            if (!_smartNameData.ContainsKey(type))
            {
                return(false);
            }

            return(_smartNameData[type].ContainsKey(id));
        }
Example #16
0
        public void CheckAndRecord(ConstraintsViolations violations)
        {
            var constructors          = SmartType.For(_type).GetAllPublicConstructors();
            var fallbackTypeGenerator = new FallbackTypeGenerator(_type);

            foreach (var constructor in constructors)
            {
                AssertNullCheckForEveryPossibleArgumentOf(violations, constructor, fallbackTypeGenerator);
            }
        }
        public static AndConstraint <TypeAssertions> NotHaveStaticFields(this TypeAssertions assertions)
        {
            Type type         = assertions.Subject;
            var  staticFields = new List <IAmField>(SmartType.For(type).GetAllStaticFields());

            staticFields.Should()
            .BeEmpty("SmartType " + type + " should not contain static fields, but: " + Environment.NewLine +
                     ReflectionElementsList.Format(staticFields));
            return(new AndConstraint <TypeAssertions>(assertions));
        }
Example #18
0
        private void CheckImmutability(ConstraintsViolations violations, Type type)
        {
            var fields        = SmartType.For(type).GetAllInstanceFields().ToList();
            var fieldWrappers = fields
                                .Where(item => item.IsNotDeveloperDefinedReadOnlyField());

            foreach (var item in fieldWrappers)
            {
                violations.Add(item.ShouldNotBeMutableButIs());
            }
        }
        public static AndConstraint <TypeAssertions> HaveEventWithShortName(this TypeAssertions assertions,
                                                                            string eventName)
        {
            var allEvents = SmartType.For(assertions.Subject).GetAllEvents();

            allEvents.Should().Match(
                events => events.Any(ev => ev.HasName(eventName)),
                "Type " + assertions.Subject + " should define expected event " + eventName);

            return(new AndConstraint <TypeAssertions>(assertions));
        }
Example #20
0
        public int?Get(SmartType type, Func <SmartGenericJsonData, bool> predicate)
        {
            SmartSelectViewModel model = new(type, predicate, smartDataManager, conditionDataManager);

            if (windowManager.ShowDialog(model))
            {
                return(model.SelectedItem.Id);
            }

            return(null);
        }
Example #21
0
 public static void AssertEqualityOperatorIsDefinedFor(Type type)
 {
     try
     {
         SmartType.For(type).EqualityOperator();
     }
     catch (Exception e)
     {
         throw new InvalidOperationException("Expected no exception when retrieving equality operator, but got " + e);
     }
 }
Example #22
0
        public static void HasNullProtectedConstructors <T>()
        {
            var type = SmartType.For(typeof(T));

            if (!type.HasConstructorWithParameters())
            {
                var constraints = new List <IConstraint> {
                    new ConstructorsMustBeNullProtected(type.ToClrType())
                };
                TypeAdheresTo(constraints);
            }
        }
        public async System.Threading.Tasks.Task <int?> Get(SmartType type, Func <SmartGenericJsonData, bool> predicate)
        {
            var title = GetTitleForType(type);
            SmartSelectViewModel model = new(title, type, predicate, smartDataManager, conditionDataManager);

            if (await windowManager.ShowDialog(model))
            {
                return(model.SelectedItem?.Id);
            }

            return(null);
        }
Example #24
0
        public static void NoStaticFields(Assembly assembly)
        {
            var staticFields = new List <IFieldWrapper>();

            foreach (var type in assembly.GetTypes())
            {
                staticFields.AddRange(SmartType.For(type).GetAllStaticFields());
            }

            staticFields.Should()
            .BeEmpty("assembly " + assembly + " should not contain static fields, but: " + Environment.NewLine +
                     StringFrom(staticFields));
        }
Example #25
0
 public ToolWindow(String file, String title, SmartType type)
 {
     stringFilter  = new StringFilter();
     saiTypeFilter = new SAITypeFilter();
     InitializeComponent();
     this.Text = title;
     this.textBox.Placeholder = "Search " + title;
     this.HideOnClose         = true;
     this.type = type;
     label1.Filters.Add(stringFilter);
     label1.Filters.Add(saiTypeFilter);
     LoadSmartFromFile(file);
 }
Example #26
0
        public static void HasUniqueConstants <T>()
        {
            string errors    = "";
            var    constants = SmartType.For(typeof(T)).GetAllConstants();

            foreach (var constant in constants)
            {
                foreach (var otherConstant in constants)
                {
                    constant.AssertNotDuplicateOf(otherConstant);
                }
            }
        }
Example #27
0
        public T SubstituteOf <T>() where T : class
        {
            var type = typeof(T);
            var sub  = Substitute.For <T>();

            var methods = SmartType.For(type).GetAllPublicInstanceMethodsWithReturnValue();

            foreach (var method in methods)
            {
                method.InvokeWithAnyArgsOn(sub, Instance).ReturnsForAnyArgs(method.GenerateAnyReturnValue(type1 => Instance(type1)));
            }
            return(sub);
        }
Example #28
0
        public static void NoNonPublicEvents(Assembly assembly)
        {
            var nonPublicEvents = new List <IEventWrapper>();

            foreach (var type in assembly.GetTypes())
            {
                nonPublicEvents.AddRange(SmartType.For(type).GetAllNonPublicEventsWithoutExplicitlyImplemented());
            }

            nonPublicEvents.Should()
            .BeEmpty("assembly " + assembly + " should not contain non-public events, but: " + Environment.NewLine +
                     StringFrom(nonPublicEvents));
        }
        private void Add(SmartType type, SmartGenericJsonData data)
        {
            if (type == SmartType.SmartSource)
            {
                ActualAdd(SmartType.SmartTarget, data);
            }
            else if (type == SmartType.SmartTarget && !data.IsOnlyTarget)
            {
                ActualAdd(SmartType.SmartSource, data);
            }

            ActualAdd(type, data);
        }
Example #30
0
 public ToolWindow(String file, String title, SmartType type)
 {
     stringFilter = new StringFilter();
     saiTypeFilter = new SAITypeFilter();
     InitializeComponent();
     this.Text = title;
     this.textBox.Placeholder = "Search " + title;
     this.HideOnClose = true;
     this.type = type;
     label1.Filters.Add(stringFilter);
     label1.Filters.Add(saiTypeFilter);
     LoadSmartFromFile(file);
 }
        public static AndConstraint <TypeAssertions> HaveUniqueConstants(this TypeAssertions assertions)
        {
            var constants = SmartType.For(assertions.Subject).GetAllConstants();

            foreach (var constant in constants)
            {
                foreach (var otherConstant in constants)
                {
                    constant.AssertNotDuplicateOf(otherConstant);
                }
            }

            return(new AndConstraint <TypeAssertions>(assertions));
        }
Example #32
0
        public void Add(SmartType type, SmartGenericJSONData data)
        {
            if (!smart_id_data.ContainsKey(type))
            {
                smart_id_data[type] = new Dictionary<int, SmartGenericJSONData>();
                smart_name_to_id[type] = new Dictionary<string,int>();
            }

            if (!smart_id_data[type].ContainsKey(data.id))
            {
                smart_id_data[type].Add(data.id, data);
                smart_name_to_id[type].Add(data.name, data.id);
                if (type == SmartType.SMART_EVENT)
                    events_name_types.Add(data.name, data.valid_types);
            }
        }
Example #33
0
 public int NameToId(SmartType type, string id)
 {
     if (!smart_name_to_id.ContainsKey(type))
         return -1;
     return smart_name_to_id[type][id];
 }
Example #34
0
        public bool SmartExist(SmartType type, string id)
        {
            if (!smart_id_data.ContainsKey(type))
                return false;

            return (smart_name_to_id.ContainsKey(type) && smart_name_to_id[type].ContainsKey(id));
        }