private Dictionary <CompetitionReferee, Dictionary <GameValueType, int?> > ScaleResult(Dictionary <CompetitionReferee, Dictionary <GameValueType, int?> > original) { var result = new Dictionary <CompetitionReferee, Dictionary <GameValueType, int?> >(); foreach (var c in original) { result.Add(c.Key, new Dictionary <GameValueType, int?>()); foreach (var gv in c.Value) { var param = ParamSettings.First(f => f.GameValueType == gv.Key); if (!param.Enabled) { continue; } if ((param.OrigMaxValue == param.MaxValue && param.OrigMinValue == param.MinValue) || !gv.Value.HasValue) { result[c.Key].Add(gv.Key, gv.Value); } else { var procent = (double)(gv.Value - param.OrigMinValue) / (double)(param.OrigMaxValue - param.OrigMinValue); var val = param.MinValue + (param.MaxValue - param.MinValue) * procent; result[c.Key].Add(gv.Key, (int)val); } } } return(result); }
public void WhenGivingParameterName_ThenOtherParameterNamesStayTheSame() { Spec.SpecifyMethod() .WithFunctionSignature <int, int, int>(nameof(CustomizedParameterShouldModel.Sum), ParamSettings.WithName("first")); var parameterInfo = GetAdapter().GetType().GetMethod(nameof(CustomizedParameterShouldModel.Sum)).GetParameters()[1]; parameterInfo.Name.ShouldBe("y"); }
public void GivenDefaultParameterNotAtEnd_WhenSpecIsValidated_ThenErrorIsReturned() { Spec.SpecifyMethod() .WithFunctionSignature <int, int, int>("Sum", param1: ParamSettings.WithName("x").WithDefault(3)) .SpecifyLinq(x => Spec.Linq.Arg <int>(1) + Spec.Linq.Arg <int>(2)); var validation = Spec.Validate().Errors.ShouldHaveSingleItem(); validation.ShouldContain("default"); }
public void WhenGivingParameterDefault_ThenAdapterParameterHasDefault() { Spec.SpecifyMethod() .WithFunctionSignature <int, int, int>(nameof(CustomizedParameterShouldModel.Sum), param2: ParamSettings.WithName("second").WithDefault(5)); dynamic adapter = GetAdapter(new CustomizedParameterShouldModel()); int result = adapter.Sum(3); result.ShouldBe(8); }
public void WhenParameterAttributeIsHidden_ThenAdapterParameterDoesNotHaveHiddenAttribute() { var p = ParamSettings.WithName("x"); var pAtts = p.Attributes; Spec.SpecifyMethod() .WithFunctionSignature <int, int, int>(nameof(CustomizedParameterShouldModel.Sum), p); IAdapter <CustomizedParameterShouldModel> adapter = null; var attributeTester = new AttributeTester <ParameterInfo>( () => { }, () => pAtts, () => adapter = Spec.Finish().Create(null), () => adapter.GetType().GetMethod(nameof(CustomizedParameterShouldModel.Sum)).GetParameters()[0], pi => pi.GetCustomAttributes()); attributeTester.TestHidingAttribute(); }
/// <summary> /// Gets a container for the given settings. Null-safe. /// </summary> /// <typeparam name="T">Type of parameter</typeparam> /// <param name="settings">Settings</param> /// <returns>Container</returns> internal static ParamSettingsContainer OfType <T>(this ParamSettings settings) => new ParamSettingsContainer() { Type = typeof(T), Settings = settings };
public void WhenLinqParamIsSpecifiedByName_ThenLinqReceivesCorrectParam() { var spec = Specification.New <CustomizedMethodShouldModel>(); spec.SpecifyMethod() .WithFunctionSignature <string, string>(nameof(CustomizedMethodShouldModel.Run), ParamSettings.WithName("x")) .SpecifyLinq(a => spec.Linq.Arg <string>("x")); dynamic adapter = spec.Finish().Create(); string result = adapter.Run("test"); result.ShouldBe("test"); }
public void WhenParamIsSpecifiedByName_ThenDelegateReceivesCorrectParam() { string backingField = null; var spec = Specification.New <CustomizedMethodShouldModel>(); spec .SpecifyMethod() .WithFunctionSignature <string, string>(nameof(CustomizedMethodShouldModel.Run), ParamSettings.WithName("x")) .SpecifyDelegate(Param.Arg <string>("x"), a => { backingField = a; return("blocked"); }); dynamic adapter = spec.Finish().Create(); string result = adapter.Run("test"); result.ShouldBe("blocked"); backingField.ShouldBe("test"); }