How to use Convert method of Atata.ObjectConverter class

Best Atata code snippet using Atata.ObjectConverter.Convert

ObjectCreator.cs

Source:ObjectCreator.cs Github

copy

Full Screen

...5namespace Atata6{7 public class ObjectCreator : IObjectCreator8 {9 private readonly IObjectConverter _objectConverter;10 private readonly IObjectMapper _objectMapper;11 public ObjectCreator(IObjectConverter objectConverter, IObjectMapper objectMapper)12 {13 _objectConverter = objectConverter;14 _objectMapper = objectMapper;15 }16 /// <inheritdoc/>17 public object Create(Type type, Dictionary<string, object> valuesMap)18 {19 return Create(type, valuesMap, new Dictionary<string, string>());20 }21 /// <inheritdoc/>22 public object Create(Type type, Dictionary<string, object> valuesMap, Dictionary<string, string> alternativeParameterNamesMap)23 {24 type.CheckNotNull(nameof(type));25 valuesMap.CheckNotNull(nameof(valuesMap));26 alternativeParameterNamesMap.CheckNotNull(nameof(alternativeParameterNamesMap));27 if (!valuesMap.Any())28 return ActivatorEx.CreateInstance(type);29 string[] parameterNamesWithAlternatives = valuesMap.Keys30 .Concat(GetAlternativeParameterNames(valuesMap.Keys, alternativeParameterNamesMap))31 .ToArray();32 ConstructorInfo constructor = FindMostAppropriateConstructor(type, parameterNamesWithAlternatives);33 var workingValuesMap = new Dictionary<string, object>(valuesMap);34 object instance = CreateInstanceViaConstructorAndRemoveUsedValues(35 constructor,36 workingValuesMap,37 alternativeParameterNamesMap);38 _objectMapper.Map(workingValuesMap, instance);39 return instance;40 }41 private static ConstructorInfo FindMostAppropriateConstructor(Type type, IEnumerable<string> parameterNames)42 {43 return type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)44 .Where(constructor =>45 {46 var parameters = constructor.GetParameters();47 return parameters.Length == 048 || parameters.All(parameter =>49 parameterNames.Contains(parameter.Name, StringComparer.OrdinalIgnoreCase)50 || parameter.IsOptional51 || parameter.GetCustomAttributes(true).Any(attr => attr is ParamArrayAttribute));52 })53 .OrderByDescending(x => x.GetParameters().Length)54 .FirstOrDefault()55 ?? throw new MissingMethodException(56 $"No appropriate constructor found for {type.FullName} type.");57 }58 private static IEnumerable<string> GetAlternativeParameterNames(59 IEnumerable<string> parameterNames,60 Dictionary<string, string> alternativeParameterNamesMap)61 {62 foreach (string parameterName in parameterNames)63 {64 if (TryGetAlternativeParameterName(alternativeParameterNamesMap, parameterName, out string alternativeParameterName))65 yield return alternativeParameterName;66 }67 }68 private static bool TryGetAlternativeParameterName(69 Dictionary<string, string> alternativeParameterNamesMap,70 string parameterName,71 out string alternativeParameterName)72 {73 KeyValuePair<string, string> alternativePair = alternativeParameterNamesMap.FirstOrDefault(x => x.Key.Equals(parameterName, StringComparison.OrdinalIgnoreCase));74 if (alternativePair.Key != null)75 {76 alternativeParameterName = alternativePair.Value;77 return true;78 }79 else80 {81 alternativeParameterName = null;82 return false;83 }84 }85 private object CreateInstanceViaConstructorAndRemoveUsedValues(86 ConstructorInfo constructor,87 Dictionary<string, object> valuesMap,88 Dictionary<string, string> alternativeParameterNamesMap)89 {90 object[] arguments = constructor.GetParameters()91 .Select(parameter =>92 {93 KeyValuePair<string, object> valuePair = RetrievePairByName(valuesMap, alternativeParameterNamesMap, parameter);94 valuesMap.Remove(valuePair.Key);95 return _objectConverter.Convert(valuePair.Value, parameter.ParameterType);96 })97 .ToArray();98 return constructor.Invoke(arguments);99 }100 private static KeyValuePair<string, object> RetrievePairByName(101 Dictionary<string, object> valuesMap,102 Dictionary<string, string> alternativeParameterNamesMap,103 ParameterInfo parameter)104 {105 KeyValuePair<string, object> valuePair = valuesMap.FirstOrDefault(pair =>106 {107 if (pair.Key.Equals(parameter.Name, StringComparison.OrdinalIgnoreCase))108 return true;109 else if (TryGetAlternativeParameterName(alternativeParameterNamesMap, pair.Key, out string alternativeParameterName))...

Full Screen

Full Screen

ObjectConverterTests.cs

Source:ObjectConverterTests.cs Github

copy

Full Screen

...6using NUnit.Framework;7namespace Atata.Tests8{9 [TestFixture]10 public class ObjectConverterTests11 {12 private ObjectConverter _sut;13 [SetUp]14 public void SetUp()15 {16 _sut = new ObjectConverter();17 }18 [Test]19 public void Convert_NullToString()20 {21 _sut.Convert(null, typeof(string))22 .Should().BeNull();23 }24 [Test]25 public void Convert_NullToArrayOfInt()26 {27 _sut.Convert(null, typeof(int[]))28 .Should().BeNull();29 }30 [Test]31 public void Convert_NullToInt_Throws()32 {33 Assert.Throws<ConversionException>(() =>34 _sut.Convert(null, typeof(int)));35 }36 [Test]37 public void Convert_StringToDecimal()38 {39 TestConvert<decimal>("5.555")40 .Should().Be(5.555m);41 }42 [Test]43 public void Convert_DecimalToString()44 {45 TestConvert<string>(5.555m)46 .Should().Be("5.555");47 }48 [Test]49 public void Convert_StringToBool()50 {51 TestConvert<bool>("true")52 .Should().Be(true);53 }54 [Test]55 public void Convert_StringToNullableBool()56 {57 TestConvert<bool?>("true")58 .Should().Be(true);59 }60 [Test]61 public void Convert_BoolToString()62 {63 TestConvert<string>(true)64 .Should().Be("True");65 }66 [Test]67 public void Convert_StringToEnum()68 {69 TestConvert<TermCase>(nameof(TermCase.Kebab))70 .Should().Be(TermCase.Kebab);71 }72 [TestCase("findByIdAttribute", ExpectedResult = typeof(FindByIdAttribute))]73 [TestCase("ordinaryPage", ExpectedResult = typeof(OrdinaryPage))]74 [TestCase("inputPage", ExpectedResult = typeof(InputPage))]75 public Type Convert_StringToType(string value)76 {77 return TestConvert<Type>(value);78 }79 [Test]80 public void Convert_StringToArrayOfString()81 {82 TestConvert<string[]>("abc")83 .Should().Equal(new[] { "abc" });84 }85 [Test]86 public void Convert_ListOfStringToArrayOfString()87 {88 TestConvert<string[]>(new List<string> { "abc", "def" })89 .Should().Equal("abc", "def");90 }91 [Test]92 public void Convert_ReadOnlyCollectionOfStringToArrayOfString()93 {94 TestConvert<string[]>(new ReadOnlyCollection<string>(new[] { "abc", "def" }))95 .Should().Equal("abc", "def");96 }97 [Test]98 public void Convert_ReadOnlyCollectionOfStringToIEnumerableOfString()99 {100 TestConvert<IEnumerable<string>>(new ReadOnlyCollection<string>(new[] { "abc", "def" }))101 .Should().Equal("abc", "def");102 }103 [Test]104 public void Convert_ReadOnlyCollectionOfObjectToIEnumerableOfString()105 {106 TestConvert<IEnumerable<string>>(new ReadOnlyCollection<object>(new[] { "abc", "def" }))107 .Should().Equal("abc", "def");108 }109 [Test]110 public void Convert_ReadOnlyCollectionOfObjectToReadOnlyCollectionOfString()111 {112 TestConvert<ReadOnlyCollection<string>>(new ReadOnlyCollection<object>(new[] { "abc", "def" }))113 .Should().Equal("abc", "def");114 }115 [Test]116 public void Convert_ReadOnlyCollectionOfObjectToQueueOfString()117 {118 TestConvert<Queue<string>>(new ReadOnlyCollection<object>(new[] { "abc", "def" }))119 .Should().Equal("abc", "def");120 }121 [Test]122 public void Convert_ReadOnlyCollectionOfIntToReadOnlyCollectionOfString()123 {124 TestConvert<ReadOnlyCollection<string>>(new ReadOnlyCollection<int>(new[] { 3, 2, 1 }))125 .Should().Equal("3", "2", "1");126 }127 [Test]128 public void Convert_ReadOnlyCollectionOfStringToReadOnlyCollectionOfInt()129 {130 TestConvert<ReadOnlyCollection<int>>(new ReadOnlyCollection<string>(new[] { "3", "2", "1" }))131 .Should().Equal(3, 2, 1);132 }133 [Test]134 public void Convert_ReadOnlyCollectionOfIntToListOfInt()135 {136 TestConvert<List<int>>(new ReadOnlyCollection<int>(new[] { 3, 2, 1 }))137 .Should().Equal(3, 2, 1);138 }139 [Test]140 public void Convert_ListOfIntToArrayOfString()141 {142 TestConvert<string[]>(new List<int> { 3, 2, 1 })143 .Should().Equal("3", "2", "1");144 }145 [Test]146 public void Convert_IEnumerableOfIntToArrayOfString()147 {148 TestConvert<string[]>(Enumerable.Range(1, 3))149 .Should().Equal("1", "2", "3");150 }151 private TDestination TestConvert<TDestination>(object sourceValue)152 {153 object result = _sut.Convert(sourceValue, typeof(TDestination));154 return result.Should().BeAssignableTo<TDestination>().Subject;155 }156 }157}...

Full Screen

Full Screen

ObjectMapper.cs

Source:ObjectMapper.cs Github

copy

Full Screen

...4namespace Atata5{6 public class ObjectMapper : IObjectMapper7 {8 private readonly IObjectConverter _objectConverter;9 public ObjectMapper(IObjectConverter objectConverter)10 {11 _objectConverter = objectConverter;12 }13 public void Map(Dictionary<string, object> propertiesMap, object destination)14 {15 Type destinationType = destination.GetType();16 foreach (var item in propertiesMap)17 {18 Map(item.Key, item.Value, destination, destinationType);19 }20 }21 public void Map(string propertyName, object propertyValue, object destination)22 {23 Map(propertyName, propertyValue, destination, destination.GetType());24 }25 private void Map(string propertyName, object propertyValue, object destination, Type destinationType)26 {27 PropertyInfo property = destinationType.GetProperty(28 propertyName,29 BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase);30 if (property == null)31 throw new MappingException(32 BuildMappingExceptionMessage(destinationType, propertyName, "Property is not found."));33 if (!property.CanWrite)34 throw new MappingException(35 BuildMappingExceptionMessage(destinationType, property.Name, "Property cannot be set."));36 Type propertyValueType = propertyValue?.GetType();37 Type propertyType = property.PropertyType;38 Type underlyingPropertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;39 try40 {41 object valueToSet = _objectConverter.Convert(propertyValue, underlyingPropertyType);42 property.SetValue(destination, valueToSet, null);43 }44 catch (Exception exception)45 {46 string additionalMessage = propertyValue == null47 ? $"Property null value cannot be converted to {propertyType} type."48 : $"Property \"{propertyValue}\" value of {propertyValueType} type cannot be converted to {propertyType} type.";49 throw new MappingException(50 BuildMappingExceptionMessage(destinationType, property.Name, additionalMessage),51 exception);52 }53 }54 private static string BuildMappingExceptionMessage(Type type, string propertyName, string additionalMessage)55 {...

Full Screen

Full Screen

Convert

Using AI Code Generation

copy

Full Screen

1using Atata;2using NUnit.Framework;3{4 {5 [FindById("id")]6 public CheckBox<_> CheckBox { get; set; }7 }8 {9 public void Test()10 {11 Go.To<SamplePage>()12 .CheckBox.Set(ObjectConverter.Convert("true", typeof(bool)));13 }14 }15}16using Atata;17using NUnit.Framework;18{19 {20 [FindById("id")]21 public CheckBox<_> CheckBox { get; set; }22 }23 {24 public object Convert(object value, Type targetType)25 {26 return (bool)value;27 }28 }29 {30 public void Test()31 {32 Go.To<SamplePage>()33 .CheckBox.Set(ObjectConverter.Convert("true", typeof(bool), new CustomConverter()));34 }35 }36}37using Atata;38using NUnit.Framework;39{40 {41 [FindById("id")]42 public CheckBox<_> CheckBox { get; set; }43 }44 [CustomConverter(typeof(CustomConverter))]45 {46 public CustomConverterAttribute(Type converterType)47 : base(converterType)48 {49 }50 }51 {52 public object Convert(object value, Type targetType)53 {54 return (bool)value;55 }56 }57 {58 public void Test()59 {60 Go.To<SamplePage>()61 .CheckBox.Set(ObjectConverter.Convert("true", typeof(bool), new CustomConverterAttribute(typeof(CustomConverter))));62 }63 }64}65using Atata;66using NUnit.Framework;67{

Full Screen

Full Screen

Convert

Using AI Code Generation

copy

Full Screen

1public void TestMethod1()2{3 string str = "123";4 int i = Atata.ObjectConverter.Convert<int>(str);5 Assert.AreEqual(123, i);6}7public void TestMethod2()8{9 string str = "123";10 int i = Atata.ObjectConverter.Convert(str, typeof(int));11 Assert.AreEqual(123, i);12}13public void TestMethod3()14{15 string str = "123";16 int i = Atata.ObjectConverter.Convert(str, typeof(int), null);17 Assert.AreEqual(123, i);18}19public void TestMethod4()20{21 string str = "123";22 int i = Atata.ObjectConverter.Convert(str, typeof(int), null, null);23 Assert.AreEqual(123, i);24}25public void TestMethod5()26{27 string str = "123";28 int i = Atata.ObjectConverter.Convert(str, typeof(int), null, null, null);29 Assert.AreEqual(123, i);30}31public void TestMethod6()32{33 string str = "123";34 int i = Atata.ObjectConverter.Convert(str, typeof(int), null, null, null, null);35 Assert.AreEqual(123, i);36}37public void TestMethod7()38{39 string str = "123";40 int i = Atata.ObjectConverter.Convert(str, typeof(int), null, null, null, null, null);41 Assert.AreEqual(123, i);42}43public void TestMethod8()44{45 string str = "123";46 int i = Atata.ObjectConverter.Convert(str, typeof(int), null, null, null, null, null, null);47 Assert.AreEqual(123, i);48}

Full Screen

Full Screen

Convert

Using AI Code Generation

copy

Full Screen

1var value = Atata.ObjectConverter.Convert("1", typeof(int));2var value = Atata.ObjectConverter.Convert<int>("1");3var value = Atata.ObjectConverter.Convert<int>("1", 2);4var value = Atata.ObjectConverter.Convert("1", typeof(int), 2);5var value = Atata.ObjectConverter.Convert("1", typeof(int), () => 2);6var value = Atata.ObjectConverter.Convert("1", typeof(int), () => (int?)2);7var value = Atata.ObjectConverter.Convert("1", typeof(int), () => (object)2);8var value = Atata.ObjectConverter.Convert("1", typeof(int), () => (object?)2);9var value = Atata.ObjectConverter.Convert("1", typeof(int), () => (object?)null);

Full Screen

Full Screen

Convert

Using AI Code Generation

copy

Full Screen

1decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");2Console.WriteLine(decimalValue);3decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");4Console.WriteLine(decimalValue);5decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");6Console.WriteLine(decimalValue);7decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");8Console.WriteLine(decimalValue);9decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");10Console.WriteLine(decimalValue);11decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");12Console.WriteLine(decimalValue);13decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");14Console.WriteLine(decimalValue);15decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");16Console.WriteLine(decimalValue);17decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");18Console.WriteLine(decimalValue);19decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");20Console.WriteLine(decimalValue);21decimal decimalValue = ObjectConverter.Convert<decimal>("123.45");22Console.WriteLine(decimalValue);

Full Screen

Full Screen

Convert

Using AI Code Generation

copy

Full Screen

1using Atata;2using NUnit.Framework;3{4 {5 public void TestMethod()6 {7 var testString = "Test text";8 var convertedValue = ObjectConverter.Convert(testString, typeof(int));9 Assert.That(convertedValue, Is.EqualTo(0));10 }11 }12}13using Atata;14using NUnit.Framework;15{16 {17 public void TestMethod()18 {19 var testString = "Test text";20 var convertedValue = ObjectConverter.Convert(testString, typeof(int));21 Assert.That(convertedValue, Is.EqualTo(0));22 }23 }24}25using Atata;26using NUnit.Framework;27{28 {29 public void TestMethod()30 {31 var testString = "Test text";32 var convertedValue = ObjectConverter.Convert(testString, typeof(int));33 Assert.That(convertedValue, Is.EqualTo(0));34 }35 }36}37using Atata;38using NUnit.Framework;39{40 {41 public void TestMethod()42 {43 var testString = "Test text";44 var convertedValue = ObjectConverter.Convert(testString, typeof(int));45 Assert.That(convertedValue, Is.EqualTo(0));46 }47 }48}49using Atata;50using NUnit.Framework;51{52 {53 public void TestMethod()54 {55 var testString = "Test text";56 var convertedValue = ObjectConverter.Convert(testString, typeof(int));57 Assert.That(convertedValue, Is.EqualTo(0));58 }59 }60}61using Atata;62using NUnit.Framework;63{

Full Screen

Full Screen

Convert

Using AI Code Generation

copy

Full Screen

1public void TestMethod1()2{3 var page = Go.To<PageObject>();4 page.StringField.Set("test");5 page.IntField.Set("1");6 page.DoubleField.Set("1.1");7 page.EnumField.Set("Value1");8 page.BoolField.Set("true");9 page.StringListField.Set("test1, test2");10 page.IntListField.Set("1, 2");11 page.DoubleListField.Set("1.1, 2.2");12 page.EnumListField.Set("Value1, Value2");13 page.BoolListField.Set("true, false");14 page.ClickSave();15}16public void TestMethod1()17{18 var page = Go.To<PageObject>();19 page.StringField.Set("test");20 page.IntField.Set("1");21 page.DoubleField.Set("1.1");22 page.EnumField.Set("Value1");23 page.BoolField.Set("true");24 page.StringListField.Set("test1, test2");25 page.IntListField.Set("1, 2");26 page.DoubleListField.Set("1.1, 2.2");27 page.EnumListField.Set("Value1, Value2");28 page.BoolListField.Set("true, false");29 page.ClickSave();30}31public void TestMethod1()32{33 var page = Go.To<PageObject>();34 page.StringField.Set("test");35 page.IntField.Set("1");36 page.DoubleField.Set("1.1");37 page.EnumField.Set("Value1");38 page.BoolField.Set("true");39 page.StringListField.Set("test1, test2");40 page.IntListField.Set("1, 2");41 page.DoubleListField.Set("1.1, 2.2");42 page.EnumListField.Set("Value1, Value2");43 page.BoolListField.Set("true, false");44 page.ClickSave();45}46public void TestMethod1()47{48 var page = Go.To<PageObject>();49 page.StringField.Set("test");50 page.IntField.Set("1");

Full Screen

Full Screen

Convert

Using AI Code Generation

copy

Full Screen

1using System;2using Atata;3{4 {5 static void Main(string[] args)6 {7 var obj = new object();8 string str = ObjectConverter.Convert<string>(obj);9 Console.WriteLine(str);10 }11 }12}13using System;14using Atata;15{16 {17 static void Main(string[] args)18 {19 var obj = new object();20 string str = obj.Convert<string>();21 Console.WriteLine(str);22 }23 }24}25using System;26using Atata;27{28 {29 static void Main(string[] args)30 {31 var obj = new object();32 string str = obj.ConvertTo<string>();33 Console.WriteLine(str);34 }35 }36}37using System;38using Atata;39{40 {41 static void Main(string[] args)42 {43 var obj = new object();44 string str = obj.As<string>();45 Console.WriteLine(str);46 }47 }48}49using System;50using Atata;51{52 {53 static void Main(string[] args)54 {55 var obj = new object();56 string str = obj.To<string>();57 Console.WriteLine(str);58 }59 }60}61using System;62using Atata;63{64 {65 static void Main(string[] args)66 {67 var obj = new object();68 string str = obj.AsType<string>();69 Console.WriteLine(str);70 }71 }72}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Atata automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful