public void Build_Should_ReturnTheExpectedMethodDescriptionObject_Given_ADynamicComponentForAThenMethodWithoutParameters() { MethodDescriptionBuilderTestDynamicComponent component = UnitTestUtility.CreateComponent <MethodDescriptionBuilderTestDynamicComponent>(); MethodInfo methodInfo = component.GetType().GetMethod("ThenMethod"); string parametersIndex = null; MethodDescription expectedMethodDescription = new MethodDescription(); expectedMethodDescription.ComponentObject = component; expectedMethodDescription.Method = methodInfo; expectedMethodDescription.StepType = typeof(ThenBaseAttribute); expectedMethodDescription.Text = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(ThenBaseAttribute), true)[0]).GetStepScenarioText(); expectedMethodDescription.ExecutionOrder = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(ThenBaseAttribute), true)[0]).GetExecutionOrder(); ArrayStorageUtilities arrayStorageUtilities = new ArrayStorageUtilities(); FieldInfo arrayStorage = arrayStorageUtilities.GetArrayStorageFieldInfoByType(component, typeof(string)); arrayStorage.SetValue(component, new string[0]); MethodParameters methodParameters = new MethodParameters(); methodParameters.Parameters = new MethodParameter[0]; expectedMethodDescription.Parameters = methodParameters; expectedMethodDescription.ParametersIndex = parametersIndex; MethodParametersLoader methodParametersLoader = Substitute.For <MethodParametersLoader>(); methodParametersLoader.LoadMethodParameters(component, methodInfo, string.Empty, parametersIndex).Returns(expectedMethodDescription.Parameters); BaseMethodDescription baseMethodDescription = new BaseMethodDescription(); baseMethodDescription.ComponentObject = component; baseMethodDescription.Method = methodInfo; baseMethodDescription.StepType = typeof(ThenBaseAttribute); baseMethodDescription.Text = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(ThenBaseAttribute), true)[0]).GetStepScenarioText(); baseMethodDescription.ExecutionOrder = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(ThenBaseAttribute), true)[0]).GetExecutionOrder(); MethodDescriptionBuilder methodDescriptionBuilder = new MethodDescriptionBuilder(); MethodDescription builderResult = methodDescriptionBuilder.Build(methodParametersLoader, baseMethodDescription, parametersIndex); Assert.IsTrue(expectedMethodDescription.Equals(builderResult), "The method MethodDescriptionBuilder.Build does not return the expected object"); }
public void ResetArrayStorage_Should_ResetTheParametersValuesStorageArray_Given_ADynamicComponentAndTheFieldInfoObjectOfTheParametersValuesStorageArray() { ArrayStorageUtilities arrayStorageUtilities = new ArrayStorageUtilities(); ArrayStorageUtilitiesTestFirstDynamicComponent dynamicBDDComponent = UnitTestUtility.CreateComponent <ArrayStorageUtilitiesTestFirstDynamicComponent>(); FieldInfo stringArrayStorage = arrayStorageUtilities.GetArrayStorageFieldInfoByType(dynamicBDDComponent, typeof(string)); string[] stringArray = new string[1] { "FirstElement" }; stringArrayStorage.SetValue(dynamicBDDComponent, stringArray); Array previousStringArray = stringArrayStorage.GetValue(dynamicBDDComponent) as Array; Assert.AreEqual(1, previousStringArray.Length, "The method ResetArrayStorage doesn't reset the array storage properly"); arrayStorageUtilities.ResetArrayStorage(stringArrayStorage, dynamicBDDComponent); Array currentValue = stringArrayStorage.GetValue(dynamicBDDComponent) as Array; Assert.AreEqual(0, currentValue.Length, "The method ResetArrayStorage doesn't reset the array storage properly"); }
public void CheckForNotMatchingPVS_Should_ReturnTheExpectedListOfUnityTestBDDErrorObjects_Given_TheParametersIndexesAndADynamicComponentWithTheCorrespondingParametersValuesStorages() { Component component = UnitTestUtility.CreateComponent <ChosenMethodsCheckerTestFirstDynamicComponent>(); Component[] components = new Component[1] { component }; ArrayStorageUtilities arrayStorageUtilities = new ArrayStorageUtilities(); FieldInfo stringPVS = arrayStorageUtilities.GetArrayStorageFieldInfoByType(component, typeof(string)); Array array = new string[1]; stringPVS.SetValue(component, array); string[] chosenMethods = new string[1] { "ChosenMethodsCheckerTestFirstDynamicComponent.GivenMethod" }; string[] parametersIndexes = new string[1] { ";System.String,ChosenMethodsCheckerTestFirstDynamicComponent.GivenMethod.stringParamWrongName.,stringPVS.Array.data[0];" }; ChosenMethodsChecker checkForErrors = new ChosenMethodsChecker(); List <UnityTestBDDError> result = checkForErrors.CheckForNotMatchingPVS <GivenBaseAttribute>(chosenMethods, parametersIndexes, components); Assert.AreEqual(0, result.Count, "The method CheckForNotMatchingPVS doesn't check properly"); }
/// <summary> /// Builds the parameters location. /// </summary> /// <param name="fullMethodsDescriptionsList">The full methods descriptions list.</param> internal void BuildParametersLocation(List <FullMethodDescription> fullMethodsDescriptionsList) { foreach (FullMethodDescription fullMethodDescription in fullMethodsDescriptionsList) { fullMethodDescription.ParametersIndex = string.Empty; } ArrayStorageUtilities arrayStorageUtilities = new ArrayStorageUtilities(); foreach (FullMethodDescription fullMethodDescription in fullMethodsDescriptionsList) { if (fullMethodDescription != null) { string parametersIndex = string.Empty; foreach (MethodParameter methodParameter in fullMethodDescription.Parameters.Parameters) { FieldInfo arrayStorageFieldInfo = arrayStorageUtilities.GetArrayStorageFieldInfoByType(fullMethodDescription.ComponentObject, methodParameter.ParameterInfoObject.ParameterType); Array array = arrayStorageFieldInfo.GetValue(fullMethodDescription.ComponentObject) as Array; int oldArrayLength = array.Length; int newArrayLength = oldArrayLength + 1; Array newArray = Array.CreateInstance(methodParameter.ParameterInfoObject.ParameterType, newArrayLength); if (array.Length > 0) { array.CopyTo(newArray, 0); } int index = newArray.Length - 1; newArray.SetValue(methodParameter.Value, index); arrayStorageFieldInfo.SetValue(fullMethodDescription.ComponentObject, newArray); parametersIndex += ";" + this.BuildParameterIndex(fullMethodDescription, methodParameter, index, arrayStorageFieldInfo); } this.AddParametersIndex(fullMethodDescription, parametersIndex); } } }
public void GetArrayStorageFieldInfoByType_Should_ReturnTheCorrespondingParametersValuesStorageFieldInfoObjectForAllThePlannedSuppoertedTypes() { ArrayStorageUtilities arrayStorageUtilities = new ArrayStorageUtilities(); ArrayStorageUtilitiesTestSecondDynamicComponent dynamicBDDComponent = UnitTestUtility.CreateComponent <ArrayStorageUtilitiesTestSecondDynamicComponent>(); Type[] typesToTest = new Type[43] { typeof(bool), typeof(byte), typeof(sbyte), typeof(char), typeof(decimal), typeof(double), typeof(float), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(short), typeof(ushort), typeof(string), typeof(Boolean), typeof(Byte), typeof(SByte), typeof(Char), typeof(Decimal), typeof(Double), typeof(Int16), typeof(Int32), typeof(Int64), typeof(UInt16), typeof(UInt32), typeof(UInt64), typeof(String), typeof(Vector2), typeof(Vector3), typeof(Vector4), typeof(Rect), typeof(Quaternion), typeof(Matrix4x4), typeof(Color), typeof(Color32), typeof(LayerMask), typeof(AnimationCurve), typeof(Gradient), typeof(RectOffset), typeof(GUIStyle), typeof(GameObject), typeof(Transform), typeof(Component) }; for (int index = 0; index < typesToTest.Length; index++) { Assert.IsNotNull(arrayStorageUtilities.GetArrayStorageFieldInfoByType(dynamicBDDComponent, typesToTest[index]), "The GetArrayStorageFieldInfo can't locate the ParametersValuesStorage array of type " + typesToTest[index].Name); Type fieldType = arrayStorageUtilities.GetArrayStorageFieldInfoByType(dynamicBDDComponent, typesToTest[index]).FieldType.GetElementType(); Assert.That(typesToTest[index].Equals(fieldType), "The GetArrayStorageFieldInfo returns the wrong ParametersValuesStorage array of type " + typesToTest[index].Name); } }
public void Build_Should_ReturnTheExpectedMethodDescriptionObject_Given_ADynamicComponentForAGivenMethodWithAStringParameterWithItsParameterIndex() { MethodDescriptionBuilderTestDynamicComponent component = UnitTestUtility.CreateComponent <MethodDescriptionBuilderTestDynamicComponent>(); MethodInfo methodInfo = component.GetType().GetMethod("GivenMethod"); string parametersIndex = ";string,DynamicBDDForTest.GivenMethod.stringParam.,StringsArrayStorage.Array.data[0];"; MethodDescription expectedMethodDescription = new MethodDescription(); expectedMethodDescription.ComponentObject = component; expectedMethodDescription.Method = methodInfo; expectedMethodDescription.StepType = typeof(GivenBaseAttribute); expectedMethodDescription.Text = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(GivenBaseAttribute), true)[0]).GetStepScenarioText(); expectedMethodDescription.ExecutionOrder = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(GivenBaseAttribute), true)[0]).GetExecutionOrder(); ArrayStorageUtilities arrayStorageUtilities = new ArrayStorageUtilities(); FieldInfo arrayStorage = arrayStorageUtilities.GetArrayStorageFieldInfoByType(component, typeof(string)); arrayStorage.SetValue(component, new string[1] { "Parameter Value" }); MethodParameter expectedParameter = new MethodParameter(); expectedParameter.ParameterInfoObject = methodInfo.GetParameters()[0]; ParameterLocation parameterLocation = new ParameterLocation(); parameterLocation.ParameterClassLocation = new ParameterLocation.ClassLocation(); parameterLocation.ParameterClassLocation.ComponentObject = component; parameterLocation.ParameterClassLocation.ComponentType = component.GetType(); parameterLocation.ParameterArrayLocation = new ParameterLocation.ArrayLocation(); parameterLocation.ParameterArrayLocation.ArrayFieldInfo = component.GetType().GetField("StringsArrayStorage"); parameterLocation.ParameterArrayLocation.ArrayName = "StringsArrayStorage"; parameterLocation.ParameterArrayLocation.ArrayIndex = 0; expectedParameter.ParameterLocation = parameterLocation; expectedParameter.Value = "Parameter Value"; MethodParameters methodParameters = new MethodParameters(); methodParameters.Parameters = new MethodParameter[1] { expectedParameter }; expectedMethodDescription.Parameters = methodParameters; expectedMethodDescription.ParametersIndex = parametersIndex; MethodParametersLoader methodParametersLoader = Substitute.For <MethodParametersLoader>(); methodParametersLoader.LoadMethodParameters(component, methodInfo, string.Empty, parametersIndex).Returns(expectedMethodDescription.Parameters); BaseMethodDescription baseMethodDescription = new BaseMethodDescription(); baseMethodDescription.ComponentObject = component; baseMethodDescription.Method = methodInfo; baseMethodDescription.StepType = typeof(GivenBaseAttribute); baseMethodDescription.Text = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(GivenBaseAttribute), true)[0]).GetStepScenarioText(); baseMethodDescription.ExecutionOrder = ((IGivenWhenThenDeclaration)methodInfo.GetCustomAttributes(typeof(GivenBaseAttribute), true)[0]).GetExecutionOrder(); MethodDescriptionBuilder methodDescriptionBuilder = new MethodDescriptionBuilder(); MethodDescription builderResult = methodDescriptionBuilder.Build(methodParametersLoader, baseMethodDescription, parametersIndex); Assert.IsTrue(expectedMethodDescription.Equals(builderResult), "The method MethodDescriptionBuilder.Build does not return the expected object"); }
public void LoadMethodParameters_Should_ReturnTheExpectedMethodParametersObject_Given_ADynamicComponentAndAMethodWithOneStringParameterAndOneIntParameterWithTheirParameterIndexes() { MethodParametersLoaderTestDynamicComponent component = UnitTestUtility.CreateComponent <MethodParametersLoaderTestDynamicComponent>(); ArrayStorageUtilities arrayStorageUtilities = new ArrayStorageUtilities(); FieldInfo stringArrayStorage = arrayStorageUtilities.GetArrayStorageFieldInfoByType(component, typeof(string)); stringArrayStorage.SetValue(component, new string[1] { "Parameter Value" }); FieldInfo intArrayStorage = arrayStorageUtilities.GetArrayStorageFieldInfoByType(component, typeof(int)); intArrayStorage.SetValue(component, new int[1] { 102 }); MethodInfo method = component.GetType().GetMethod("WhenMethod"); ParameterInfo[] parametersInfo = method.GetParameters(); MethodParameter expectedParameter1 = new MethodParameter(); expectedParameter1.ParameterInfoObject = parametersInfo[0]; ParameterLocation parameterLocation1 = new ParameterLocation(); parameterLocation1.ParameterClassLocation = new ParameterLocation.ClassLocation(); parameterLocation1.ParameterClassLocation.ComponentObject = component; parameterLocation1.ParameterClassLocation.ComponentType = component.GetType(); parameterLocation1.ParameterArrayLocation = new ParameterLocation.ArrayLocation(); parameterLocation1.ParameterArrayLocation.ArrayFieldInfo = arrayStorageUtilities.GetArrayStorageFieldInfoByType(component, typeof(string)); parameterLocation1.ParameterArrayLocation.ArrayName = "stringPVS"; parameterLocation1.ParameterArrayLocation.ArrayIndex = 0; expectedParameter1.ParameterLocation = parameterLocation1; expectedParameter1.Value = "Parameter Value"; MethodParameter expectedParameter2 = new MethodParameter(); expectedParameter2.ParameterInfoObject = parametersInfo[1]; ParameterLocation parameterLocation2 = new ParameterLocation(); parameterLocation2.ParameterClassLocation = new ParameterLocation.ClassLocation(); parameterLocation2.ParameterClassLocation.ComponentObject = component; parameterLocation2.ParameterClassLocation.ComponentType = component.GetType(); parameterLocation2.ParameterArrayLocation = new ParameterLocation.ArrayLocation(); parameterLocation2.ParameterArrayLocation.ArrayFieldInfo = arrayStorageUtilities.GetArrayStorageFieldInfoByType(component, typeof(int)); parameterLocation2.ParameterArrayLocation.ArrayName = "intPVS"; parameterLocation2.ParameterArrayLocation.ArrayIndex = 0; expectedParameter2.ParameterLocation = parameterLocation2; expectedParameter2.Value = 102; MethodParameters expectedMethodParameters = new MethodParameters(); expectedMethodParameters.Parameters = new MethodParameter[2] { expectedParameter1, expectedParameter2 }; string parametersIndex = ";System.String,MethodParametersLoaderTestDynamicComponent.WhenMethod.whenStringParam.,stringPVS.Array.data[0];System.Int32,MethodParametersLoaderTestDynamicComponent.WhenMethod.whenIntParam.,intPVS.Array.data[0];"; MethodParametersLoader methodParametersLoader = new MethodParametersLoader(); MethodParameters methodParameters = methodParametersLoader.LoadMethodParameters(component, method, string.Empty, parametersIndex); Assert.That(expectedMethodParameters.Equals(methodParameters), "The method MethodParametersLoader.LoadMethodParameters doesn't return the right object."); }