Best Webtau code snippet using org.testingisdocumenting.webtau.cfg.ConfigValue.convertToSnakeCase
Source:ConfigValue.java
...37 return new ConfigValue(key, description, true, () -> defaultValue);38 }39 private ConfigValue(String key, String description, boolean isBoolean, Supplier<Object> defaultValueSupplier) {40 this.key = key;41 this.prefixedUpperCaseKey = ENV_VAR_PREFIX + convertToSnakeCase(key);42 this.description = description;43 this.isBoolean = isBoolean;44 this.defaultValueSupplier = defaultValueSupplier;45 this.valuesPerPersonaId = new HashMap<>();46 this.valuesPerPersonaId.put(Persona.DEFAULT_PERSONA_ID, new ArrayDeque<>());47 reset();48 }49 public void set(String source, Object value) {50 set(source, Persona.getCurrentPersona().getId(), value);51 }52 public void set(String source, String personaId, Object value) {53 Deque<Value> values = getOrCreatePersonaValues(personaId);54 values.addFirst(new Value(source, value));55 }56 public void reset() {57 valuesPerPersonaId.clear();58 valuesPerPersonaId.put(Persona.DEFAULT_PERSONA_ID, new ArrayDeque<>());59 }60 public void accept(String source, Map<String, ?> configValues) {61 accept(source, Persona.DEFAULT_PERSONA_ID, configValues);62 }63 public void accept(String source, String personaId, Map<String, ?> configValues) {64 if (configValues.containsKey(key)) {65 set(source, personaId, configValues.get(key));66 } else if (configValues.containsKey(prefixedUpperCaseKey)) {67 set(source, personaId, configValues.get(prefixedUpperCaseKey));68 }69 }70 public boolean match(String configKey) {71 return configKey.equals(key) || configKey.equals(prefixedUpperCaseKey);72 }73 public String getKey() {74 return key;75 }76 public String getPrefixedUpperCaseKey() {77 return prefixedUpperCaseKey;78 }79 public String getDescription() {80 return description;81 }82 public String getSource() {83 return (isDefault() ? "default" : currentOrDefaultPersonaValuesForRead().getFirst().getSourceId());84 }85 public List<String> getSources() {86 return (isDefault() ?87 Collections.singletonList("default") :88 currentOrDefaultPersonaValuesForRead().stream().map(Value::getSourceId).collect(Collectors.toList()));89 }90 public boolean isBoolean() {91 return isBoolean;92 }93 public Object getAsObject() {94 return isDefault() ? defaultValueSupplier.get():95 currentOrDefaultPersonaValuesForRead().getFirst().getValue();96 }97 public String getAsString() {98 return convertToString(getAsObject());99 }100 public Path getAsPath() {101 return isDefault() ? (Path) defaultValueSupplier.get() : Paths.get(getAsObject().toString());102 }103 public int getAsInt() {104 if (isDefault()) {105 return (int) defaultValueSupplier.get();106 }107 Object first = getAsObject();108 return first instanceof Integer ?109 (int) first :110 Integer.parseInt(first.toString());111 }112 public long getAsLong() {113 if (isDefault()) {114 return (long) defaultValueSupplier.get();115 }116 Object first = getAsObject();117 return first instanceof Long ?118 (long) first :119 Long.parseLong(first.toString());120 }121 public boolean getAsBoolean() {122 if (isDefault()) {123 return (boolean) defaultValueSupplier.get();124 }125 Object first = getAsObject();126 return first.toString().equalsIgnoreCase("true");127 }128 @SuppressWarnings("unchecked")129 public <T> List<T> getAsList() {130 return (List<T>) getAsObject();131 }132 @SuppressWarnings("unchecked")133 public Map<String, Object> getAsMap() {134 return (Map<String, Object>) getAsObject();135 }136 @Override137 public String toString() {138 return valuesPerPersonaId.keySet().stream().map(this::renderPersonaValues).collect(Collectors.joining("\n"));139 }140 public Map<String, Object> toMap() {141 Map<String, Object> result = new LinkedHashMap<>();142 result.put("key", key);143 result.put("value", getAsObject());144 result.put("source", getSource());145 return result;146 }147 public boolean isDefault() {148 return currentOrDefaultPersonaValuesForRead().isEmpty();149 }150 public boolean nonDefault() {151 return ! isDefault();152 }153 public Object getDefaultValue() {154 return defaultValueSupplier.get();155 }156 private String renderPersonaValues(String personaId) {157 String title = personaId.isEmpty() ? "" : "persona " + personaId + ":\n";158 return title + key + ": " + personaIdOrDefaultPersonaValuesForRead(personaId).stream()159 .map(Value::toString)160 .collect(Collectors.joining(", "));161 }162 private String convertToString(Object value) {163 return value == null ? "" : value.toString();164 }165 private String convertToSnakeCase(String key) {166 return key.replaceAll(CAMEL_CASE_PATTERN, "$1_$2")167 .toUpperCase();168 }169 private Deque<Value> currentOrDefaultPersonaValuesForRead() {170 return personaIdOrDefaultPersonaValuesForRead(Persona.getCurrentPersona().getId());171 }172 private Deque<Value> personaIdOrDefaultPersonaValuesForRead(String personaId) {173 Deque<Value> values = valuesPerPersonaId.get(personaId);174 return values != null ? values : valuesPerPersonaId.get(Persona.DEFAULT_PERSONA_ID);175 }176 private Deque<Value> getOrCreatePersonaValues(String personaId) {177 return valuesPerPersonaId.computeIfAbsent(personaId, (k) -> new ArrayDeque<>());178 }179 private static class Value {...
convertToSnakeCase
Using AI Code Generation
1import org.testingisdocumenting.webtau.cfg.ConfigValue2def config = new ConfigValue("config")3def configWithCustomDelimiter = new ConfigValue("config", "_")4def configValue = config.withDefault("default")5def configValueWithCustomDelimiter = configWithCustomDelimiter.withDefault("default")6def configValueWithCustomDelimiterAndDefaultValue = configWithCustomDelimiter.withDefault("default")7def configValueWithCustomDelimiterAndDefaultNull = configWithCustomDelimiter.withDefault(null)8def configValueWithCustomDelimiterAndDefaultValueAndCustomConverter = configWithCustomDelimiter.withDefault("default").convert(convertToSnakeCase)9def configValueWithCustomDelimiterAndDefaultNullAndCustomConverter = configWithCustomDelimiter.withDefault(null).convert(convertToSnakeCase)10def convertToSnakeCase(String value) {11 value.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase()12}13import org.testingisdocumenting.webtau.cfg.ConfigValue14def config = new ConfigValue("config")15def configWithCustomDelimiter = new ConfigValue("config", "_")16def configValue = config.withDefault("default")17def configValueWithCustomDelimiter = configWithCustomDelimiter.withDefault("default")18def configValueWithCustomDelimiterAndDefaultValue = configWithCustomDelimiter.withDefault("default")19def configValueWithCustomDelimiterAndDefaultNull = configWithCustomDelimiter.withDefault(null)20def configValueWithCustomDelimiterAndDefaultValueAndCustomConverter = configWithCustomDelimiter.withDefault("default").convert(convertToSnakeCase)21def configValueWithCustomDelimiterAndDefaultNullAndCustomConverter = configWithCustomDelimiter.withDefault(null).convert(convertToSnakeCase)22def convertToSnakeCase(String value) {23 value.replaceAll("([a-z])([A-Z])", "$1_$2").toLowerCase()24}25import org.testingisdocumenting.webtau.cfg.ConfigValue26def config = new ConfigValue("config")27def configWithCustomDelimiter = new ConfigValue("config", "_")28def configValue = config.withDefault("default")29def configValueWithCustomDelimiter = configWithCustomDelimiter.withDefault("default")30def configValueWithCustomDelimiterAndDefaultValue = configWithCustomDelimiter.withDefault("default")
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!