How to use GetEnumTerms method of Atata.TermResolver class

Best Atata code snippet using Atata.TermResolver.GetEnumTerms

TermResolver.cs

Source:TermResolver.cs Github

copy

Full Screen

...165166 if (value is string stringValue)167 return new[] { FormatStringValue(stringValue, termOptions) };168 else if (value is Enum enumValue)169 return GetEnumTerms(enumValue, termOptions);170 else if (s_typeTermConverters.TryGetValue(value.GetType(), out TermConverter termConverter) && termConverter.ToStringConverter != null)171 return new[] { termConverter.ToStringConverter(value, termOptions) };172 else173 return new[] { FormatValue(value, termOptions.Format, termOptions.Culture) };174 }175176 private static string FormatStringValue(string value, TermOptions termOptions)177 {178 string valueToFormat = termOptions.GetCaseOrNull()?.ApplyTo(value) ?? value;179180 return FormatValue(valueToFormat, termOptions.Format, termOptions.Culture);181 }182183 private static string FormatValue(object value, string format, CultureInfo culture)184 {185 if (IsComplexStringFormat(format))186 return string.Format(culture, format, value);187 else if (value is IFormattable formattableValue)188 return formattableValue.ToString(format, culture);189 else190 return value?.ToString();191 }192193 private static bool IsComplexStringFormat(string format)194 {195 return format != null && format.Contains("{0");196 }197198 private static string RetrieveValueFromString(string value, string format)199 {200 return IsComplexStringFormat(format) ? RetrieveValuePart(value, format) : value;201 }202203 private static string RetrieveSpecificFormatFromStringFormat(string format)204 {205 if (string.IsNullOrEmpty(format))206 {207 return null;208 }209 else if (IsComplexStringFormat(format))210 {211 int startIndex = format.IndexOf("{0", StringComparison.Ordinal);212 int endIndex = format.IndexOf('}', startIndex + 2);213214 return endIndex - startIndex == 2215 ? null216 : format.Substring(startIndex + 3, endIndex - startIndex - 3);217 }218 else219 {220 return format;221 }222 }223224 private static string RetrieveValuePart(string value, string format)225 {226 if (string.IsNullOrEmpty(format))227 return value;228229 string[] formatParts = format.Split(new[] { "{0" }, 2, StringSplitOptions.None);230231 if (formatParts.Length != 2)232 {233 throw new ArgumentException(234 $"Incorrect \"{format}\" {nameof(format)} for \"{value}\" string. Format should match regular expression: \".*{{0}}.*\".",235 nameof(format));236 }237238 formatParts[1] = formatParts[1].Substring(formatParts[1].IndexOf('}') + 1);239240 string formatStart = formatParts[0];241 string formatEnd = formatParts[1];242243 if (!value.StartsWith(formatStart, StringComparison.Ordinal))244 {245 throw new ArgumentException(246 $"\"{value}\" value doesn't match the \"{format}\" {nameof(format)}. Should start with \"{formatStart}\".",247 nameof(value));248 }249250 if (!value.EndsWith(formatEnd, StringComparison.Ordinal))251 {252 throw new ArgumentException(253 $"\"{value}\" value doesn't match the \"{format}\" {nameof(format)}. Should end with \"{formatEnd}\".",254 nameof(value));255 }256257 return value.Substring(formatStart.Length, value.Length - formatStart.Length - formatEnd.Length);258 }259260 public static string CreateXPathCondition(object value, TermOptions termOptions = null, string operand = ".")261 {262 string[] terms = GetTerms(value, termOptions);263 TermMatch match = GetMatch(value, termOptions);264 return match.CreateXPathCondition(terms, operand);265 }266267 public static T FromString<T>(string value, TermOptions termOptions = null)268 {269 object result = FromString(value, typeof(T), termOptions);270 return (T)result;271 }272273 public static object FromString(string value, Type destinationType, TermOptions termOptions = null)274 {275 object result = value is null276 ? null277 : RetrieveValueFromString(value, destinationType, termOptions ?? new TermOptions());278279 if (result == null && !destinationType.IsClassOrNullable())280 {281 throw new ArgumentException(282 "Failed to find value of type '{0}' corresponding to '{1}'.".FormatWith(destinationType.FullName, value),283 nameof(value));284 }285 else286 {287 return result;288 }289 }290291 private static object RetrieveValueFromString(string value, Type destinationType, TermOptions termOptions)292 {293 Type underlyingType = Nullable.GetUnderlyingType(destinationType) ?? destinationType;294295 if (underlyingType.IsEnum)296 {297 return StringToEnum(value, underlyingType, termOptions);298 }299 else if (!string.IsNullOrEmpty(value))300 {301 return s_typeTermConverters.TryGetValue(underlyingType, out TermConverter termConverter)302 ? termConverter.FromStringConverter(value, termOptions)303 : Convert.ChangeType(RetrieveValuePart(value, termOptions.Format), underlyingType, termOptions.Culture);304 }305 else306 {307 return underlyingType == typeof(string) ? value : null;308 }309 }310311 public static object StringToEnum(string value, Type enumType, TermOptions termOptions = null)312 {313 return enumType.GetIndividualEnumFlags().314 FirstOrDefault(x => GetEnumMatch(x, termOptions).IsMatch(value, GetEnumTerms(x, termOptions)));315 }316317 public static string[] GetEnumTerms(Enum value, TermOptions termOptions = null)318 {319 termOptions = termOptions ?? new TermOptions();320321 return value.GetType().IsDefined(typeof(FlagsAttribute), false)322 ? GetFlagsEnumTerms(value, termOptions)323 : GetIndividualEnumTerms(value, termOptions);324 }325326 private static string[] GetFlagsEnumTerms(Enum value, TermOptions termOptions)327 {328 return value.GetIndividualFlags().SelectMany(x => GetIndividualEnumTerms(x, termOptions)).ToArray();329 }330331 private static string[] GetIndividualEnumTerms(Enum value, TermOptions termOptions) ...

Full Screen

Full Screen

GetEnumTerms

Using AI Code Generation

copy

Full Screen

1{2 {3 public static IEnumerable<string> GetEnumTerms<TEnum>(this TermResolver termResolver)4 {5 return Enum.GetValues(typeof(TEnum)).Cast<Enum>().Select(x => termResolver.Resolve(x));6 }7 }8}9{10 {11 public static IEnumerable<string> GetEnumTerms<TEnum>(this TermResolver termResolver)12 {13 return Enum.GetValues(typeof(TEnum)).Cast<Enum>().Select(x => termResolver.Resolve(x));14 }15 }16}17{18 {19 public static IEnumerable<string> GetEnumTerms<TEnum>(this TermResolver termResolver)20 {21 return Enum.GetValues(typeof(TEnum)).Cast<Enum>().Select(x => termResolver.Resolve(x));22 }23 }24}25{26 {27 public static IEnumerable<string> GetEnumTerms<TEnum>(this TermResolver termResolver)28 {29 return Enum.GetValues(typeof(TEnum)).Cast<Enum>().Select(x => termResolver.Resolve(x));30 }31 }32}33{34 {35 public static IEnumerable<string> GetEnumTerms<TEnum>(this TermResolver termResolver)36 {37 return Enum.GetValues(typeof(TEnum)).Cast<Enum>().Select(x => termResolver.Resolve(x));38 }39 }40}41{42 {43 public static IEnumerable<string> GetEnumTerms<TEnum>(this TermResolver termResolver)

Full Screen

Full Screen

GetEnumTerms

Using AI Code Generation

copy

Full Screen

1{2 using Atata;3 using NUnit.Framework;4 {5 public void GetEnumTerms()6 {7 var enumTerms = TermResolver.Instance.GetEnumTerms(typeof(Colors));8 foreach (var enumTerm in enumTerms)9 {10 Log.Info(enumTerm.Key + " - " + enumTerm.Value);11 }12 }13 }14 {15 [Term("Red")]16 [Term("Red", "en-US")]17 [Term("Красный", "ru-RU")]18 [Term("Czerwony", "pl-PL")]19 [Term("Rot", "de-DE")]20 [Term("Green")]21 [Term("Green", "en-US")]22 [Term("Зеленый", "ru-RU")]23 [Term("Zielony", "pl-PL")]24 [Term("Grün", "de-DE")]25 [Term("Blue")]26 [Term("Blue", "en-US")]27 [Term("Синий", "ru-RU")]28 [Term("Niebieski", "pl-PL")]29 [Term("Blau", "de-DE")]30 }31}32{33 using Atata;34 using NUnit.Framework;35 {36 public void GetEnumTerm()37 {38 string redTerm = TermResolver.Instance.GetEnumTerm(Colors.Red);39 Log.Info("Red - " + redTerm);40 string greenTerm = TermResolver.Instance.GetEnumTerm(Colors.Green, "pl-PL");41 Log.Info("Green - " + greenTerm);42 string blueTerm = TermResolver.Instance.GetEnumTerm(Colors.Blue, "de-DE");43 Log.Info("Blue - " + blueTerm);44 }45 }46 {47 [Term("Red")]48 [Term("Red", "en-US")]49 [Term("Красный", "ru-RU")]50 [Term("Czerwony", "pl-PL")]51 [Term("Rot", "de-DE")]52 [Term("Green")]53 [Term("Green", "en

Full Screen

Full Screen

GetEnumTerms

Using AI Code Generation

copy

Full Screen

1using Atata;2using NUnit.Framework;3using System;4using System.Collections.Generic;5using System.Linq;6using System.Text;7using System.Threading.Tasks;8{9 {10 {11 [Term("Test Value 1")]12 [Term("Test Value 2")]13 [Term("Test Value 3")]14 }15 }16 {17 {18 [Term("Test Value 1")]19 [Term("Test Value 2")]20 [Term("Test Value 3")]21 }22 }23 {24 {25 [Term("Test Value 1")]26 [Term("Test Value 2")]27 [Term("Test Value 3")]28 }29 }30 {31 {32 [Term("Test Value 1")]33 [Term("Test Value 2")]34 [Term("Test Value 3")]35 }36 }37 {38 {39 [Term("Test Value 1")]40 [Term("Test Value 2")]41 [Term("Test Value 3")]42 }43 }44 {45 {46 [Term("Test Value 1")]47 [Term("Test Value 2")]48 [Term("Test Value 3")]49 }50 }51 {52 {53 [Term("Test Value 1")]54 [Term("Test Value 2")]55 [Term("Test Value 3")]56 }57 }58 {

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful