How to use nextIndex method of com.tngtech.jgiven.report.model.StepFormatter class

Best JGiven code snippet using com.tngtech.jgiven.report.model.StepFormatter.nextIndex

Source:StepFormatter.java Github

copy

Full Screen

...124 String argumentName = nextCharExists ? nextName( stepDescription.substring( i + 1 ) ) : "";125 boolean namedArgumentExists = argumentName.length() > 0;126 boolean namedArgumentMatch = namedArgumentExists && isArgument( argumentName );127 boolean enumArgumentMatch =128 nextCharExists && arguments.size() > nextIndex( stepDescription.substring( i + 1 ), arguments.size() );129 boolean singleDollarCountIndexExists = singlePlaceholderCounter < arguments.size();130 if( dollarMatch ) {131 // e.g $$132 if( escapedDollarMatch ) {133 formattedWords.add( new Word( "$" ) );134 i += 1;135 // e.g $argument136 } else if( namedArgumentMatch ) {137 int argumentIndex = getArgumentIndexByName( argumentName, 0 );138 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );139 i += argumentName.length();140 // e.g $1141 } else if( enumArgumentMatch ) {142 int argumentIndex = nextIndex( stepDescription.substring( i + 1 ), arguments.size() );143 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );144 i += Integer.toString( argumentIndex ).length();145 // e.g $argumentNotKnown - gets replaced with running counter146 } else if( singleDollarCountIndexExists && namedArgumentExists ) {147 int argumentIndex = singlePlaceholderCounter;148 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );149 singlePlaceholderCounter += 1;150 i += argumentName.length();151 // e.g $152 } else if( singleDollarCountIndexExists ) {153 int argumentIndex = singlePlaceholderCounter;154 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );155 singlePlaceholderCounter += 1;156 // e.g ($notKnown || $) && counter > argument.size157 } else {158 formattedWords.add( new Word( '$' + argumentName ) );159 i += argumentName.length();160 }161 // unfortunately we need this after every argument so the Joiner can use .join(' ') on the formattedWords162 dropNextWhitespace = true;163 // if no placeholder was detected, check dropNextWhitespace rule and append the next character164 } else {165 if (dropNextWhitespace && stepDescription.charAt( i ) == ' ') {166 dropNextWhitespace = false;167 } else {168 currentWords.append( stepDescription.charAt( i ) );169 }170 }171 }172 flushCurrentWord( currentWords, formattedWords, false );173 formattedWords.addAll( getRemainingArguments( usedArguments ) );174 return formattedWords;175 }176 /**177 * Greedy search for the next String from the start in the {@param description}178 * until a non JavaIdentifierPart or $ is found179 *180 * @param description the searchable {@link String}181 * @return a {@link String} consisting only of JavaIdentifiableParts182 */183 private static String nextName( String description ) {184 StringBuilder result = new StringBuilder();185 for( int i = 0; i < description.length(); i++ ) {186 char c = description.charAt( i );187 if( Character.isJavaIdentifierPart( c ) && c != '$' ) {188 result.append( c );189 } else {190 break;191 }192 }193 return result.toString();194 }195 private int getArgumentIndexByName( String argumentName, int defaultIndex ) {196 for( int i = 0; i < arguments.size(); i++ ) {197 if( arguments.get( i ).name.equals( argumentName ) ) {198 return i;199 }200 }201 return defaultIndex;202 }203 private boolean isArgument( String argumentName ) {204 for( NamedArgument arg : arguments ) {205 if( arg.name.equals( argumentName ) ) {206 return true;207 }208 }209 return false;210 }211 /**212 * Looks up the argument by index via {@link #argumentIndexToWord(int)}, adds it to formattedWords and usedArguments213 * and flushes the previously accumulated text via {@link #flushCurrentWord(StringBuilder, List, boolean)}.214 *215 * @param index is searchable index216 * @param currentWords this {@link StringBuilder} holds the previously accumulated words217 * @param formattedWords this is the resulting List of Words218 * @param usedArguments this is the set which tracks which arguments were already used219 */220 private void addArgumentByIndex( int index, StringBuilder currentWords, List<Word> formattedWords, Set<String> usedArguments ) {221 flushCurrentWord( currentWords, formattedWords, true );222 Word argument = argumentIndexToWord( index );223 formattedWords.add( argument );224 usedArguments.add( argument.getArgumentInfo().getArgumentName() );225 }226 /**227 * Gets the argument based on the index, uses {@link #toDefaultStringFormat(Object)} and {@link #formatters} to format228 * the value and name of the Word accordingly229 *230 * @param index is the searchable index in {@link #arguments}231 * @return the {@link Word}232 */233 private Word argumentIndexToWord( int index ) {234 Object value = arguments.get( index ).value;235 String defaultFormattedValue = toDefaultStringFormat( value );236 ObjectFormatter<?> formatter = formatters.get( index );237 String formattedValue = formatUsingFormatterOrNull( formatter, value );238 String argumentName = WordUtil.fromSnakeCase( arguments.get( index ).name );239 return Word.argWord( argumentName, defaultFormattedValue, formattedValue );240 }241 /**242 * Appends the accumulated words to the resulting words. Trailing whitespace is removed because of the243 * postprocessing that inserts custom whitespace244 *245 * @param currentWords is the {@link StringBuilder} of the accumulated words246 * @param formattedWords is the list that is being appended to247 */248 private static void flushCurrentWord( StringBuilder currentWords, List<Word> formattedWords, boolean cutWhitespace ) {249 if( currentWords.length() > 0 ) {250 if( cutWhitespace && currentWords.charAt( currentWords.length() - 1 ) == ' ' ) {251 currentWords.setLength( currentWords.length() - 1 );252 }253 formattedWords.add( new Word( currentWords.toString() ) );254 currentWords.setLength( 0 );255 }256 }257 /**258 * Returns the next index of the argument by decrementing 1 from the possibly parsed number259 *260 * @param description this String will be searched from the start for a number261 * @param defaultIndex this will be returned if the match does not succeed262 * @return the parsed index or the defaultIndex263 */264 private static int nextIndex( String description, int defaultIndex ) {265 Pattern startsWithNumber = Pattern.compile( "(\\d+).*" );266 Matcher matcher = startsWithNumber.matcher( description );267 if( matcher.matches() ) {268 return Integer.parseInt( matcher.group( 1 ) ) - 1;269 }270 return defaultIndex;271 }272 private List<Word> getRemainingArguments( Set<String> usedArguments ) {273 List<Word> remainingArguments = Lists.newArrayList();274 for( int i = 0; i < arguments.size(); i++ ) {275 Object value = arguments.get( i ).value;276 String formattedValue = formatUsingFormatterOrNull( formatters.get( i ), value );277 if( !usedArguments.contains( arguments.get( i ).name ) ) {278 if( formattedValue == null...

Full Screen

Full Screen

nextIndex

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.report.model;2import java.util.HashMap;3import java.util.Map;4public class StepFormatter {5 private Map<String, Integer> stepIndexMap = new HashMap<String, Integer>();6 public String nextIndex(String step) {7 int index = stepIndexMap.getOrDefault(step, 0) + 1;8 stepIndexMap.put(step, index);9 return Integer.toString(index);10 }11}12package com.tngtech.jgiven.report.model;13import org.junit.Test;14import static org.assertj.core.api.Assertions.assertThat;15public class StepFormatterTest {16 public void testNextIndex() {17 StepFormatter stepFormatter = new StepFormatter();18 assertThat(stepFormatter.nextIndex("Step 1")).isEqualTo("1");19 assertThat(stepFormatter.nextIndex("Step 1")).isEqualTo("2");20 assertThat(stepFormatter.nextIndex("Step 2")).isEqualTo("1");21 assertThat(stepFormatter.nextIndex("Step 1")).isEqualTo("3");22 assertThat(stepFormatter.nextIndex("Step 2")).isEqualTo("2");23 assertThat(stepFormatter.nextIndex("Step 2")).isEqualTo("3");24 }25}

Full Screen

Full Screen

nextIndex

Using AI Code Generation

copy

Full Screen

1StepFormatter stepFormatter = new StepFormatter();2String step = "I have a step with index {0} and {1}";3int index = 0;4String nextIndex = stepFormatter.nextIndex(step, index);5System.out.println(nextIndex);6StepFormatter stepFormatter = new StepFormatter();7String step = "I have a step with index {0} and {1}";8int index = 1;9String nextIndex = stepFormatter.nextIndex(step, index);10System.out.println(nextIndex);11StepFormatter stepFormatter = new StepFormatter();12String step = "I have a step with index {0} and {1}";13int index = 0;14String nextIndex = stepFormatter.nextIndex(step, index);15System.out.println(nextIndex);16StepFormatter stepFormatter = new StepFormatter();17String step = "I have a step with index {0} and {1}";18int index = 1;19String nextIndex = stepFormatter.nextIndex(step, index);20System.out.println(nextIndex);21StepFormatter stepFormatter = new StepFormatter();22String step = "I have a step with index {0} and {1}";23int index = 0;24String nextIndex = stepFormatter.nextIndex(step, index);25System.out.println(nextIndex);26StepFormatter stepFormatter = new StepFormatter();27String step = "I have a step with index {0} and {1}";28int index = 1;29String nextIndex = stepFormatter.nextIndex(step, index);30System.out.println(nextIndex);31StepFormatter stepFormatter = new StepFormatter();32String step = "I have a step with index {0} and {1}";33int index = 0;34String nextIndex = stepFormatter.nextIndex(step, index);35System.out.println(nextIndex);

Full Screen

Full Screen

nextIndex

Using AI Code Generation

copy

Full Screen

1StepFormatter stepFormatter = new StepFormatter();2String step = "Given I have a step with <a> and <b>";3List<String> params = Arrays.asList("a", "b");4String actual = stepFormatter.nextIndex(step, params);5assertEquals("Given I have a step with <1> and <2>", actual);6}7}

Full Screen

Full Screen

nextIndex

Using AI Code Generation

copy

Full Screen

1 String stepName = "I am a step";2 String stepNameWithIndex = StepFormatter.nextIndex(stepName);3 System.out.println(stepNameWithIndex);4 String stepName = "I am a step 1";5 String stepNameWithIndex = StepFormatter.nextIndex(stepName);6 System.out.println(stepNameWithIndex);7 String stepName = "I am a step 2";8 String stepNameWithIndex = StepFormatter.nextIndex(stepName);9 System.out.println(stepNameWithIndex);10 String stepName = "I am a step 3";11 String stepNameWithIndex = StepFormatter.nextIndex(stepName);12 System.out.println(stepNameWithIndex);13 String stepName = "I am a step 4";14 String stepNameWithIndex = StepFormatter.nextIndex(stepName);15 System.out.println(stepNameWithIndex);16 String stepName = "I am a step 5";17 String stepNameWithIndex = StepFormatter.nextIndex(stepName);18 System.out.println(stepNameWithIndex);19 String stepName = "I am a step 6";20 String stepNameWithIndex = StepFormatter.nextIndex(stepName);21 System.out.println(stepNameWithIndex);

Full Screen

Full Screen

nextIndex

Using AI Code Generation

copy

Full Screen

1import spock.lang.Specification2class StepFormatterSpec extends Specification {3 def "nextIndex returns unique index"() {4 def formatter = new StepFormatter()5 def firstIndex = formatter.nextIndex()6 def secondIndex = formatter.nextIndex()7 }8}

Full Screen

Full Screen

nextIndex

Using AI Code Generation

copy

Full Screen

1def stepFormatter = new com.tngtech.jgiven.report.model.StepFormatter()2try {3 nextStepIndex = stepFormatter.nextIndex(scenario, step)4} catch (Exception e) {5}6if (nextStepIndex < 10) {7} else {8}

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