Best JGiven code snippet using com.tngtech.jgiven.report.analysis.CaseArgumentAnalyser.setParameterNames
Source:CaseArgumentAnalyser.java
...56 List<List<String>> explicitParameterValues = getExplicitParameterValues(scenarioModel.getScenarioCases());57 List<String> argumentNames =58 findArgumentNames(joinedArgs, explicitParameterValues, scenarioModel.getExplicitParameters());59 List<List<Word>> arguments = getFirstWords(joinedArgs);60 setParameterNames(joinedArgs, argumentNames);61 scenarioModel.setDerivedParameters(argumentNames);62 for (int caseCounter = 0; caseCounter < arguments.size(); caseCounter++) {63 scenarioModel.getCase(caseCounter).setDerivedArguments(getFormattedValues(arguments.get(caseCounter)));64 }65 }66 private List<List<String>> getExplicitParameterValues(List<ScenarioCaseModel> scenarioCases) {67 List<List<String>> explicitParameterValues = Lists.newArrayListWithExpectedSize(scenarioCases.size());68 for (ScenarioCaseModel caseModel : scenarioCases) {69 explicitParameterValues.add(caseModel.getExplicitArguments());70 }71 return explicitParameterValues;72 }73 /**74 * Finds for each JoinedArgs set the best fitting name.75 * <p>76 * First it is tried to find a name from the explicitParameterNames list, by comparing the argument values77 * with the explicit case argument values. If no matching value can be found, the name of the argument is taken.78 */79 private List<String> findArgumentNames(List<List<JoinedArgs>> joinedArgs,80 List<List<String>> explicitParameterValues,81 List<String> explicitParameterNames) {82 List<String> argumentNames = Lists.newArrayListWithExpectedSize(joinedArgs.get(0).size());83 Multiset<String> paramNames = TreeMultiset.create();84 arguments:85 for (int argumentCounter = 0; argumentCounter < joinedArgs.get(0).size(); argumentCounter++) {86 parameters:87 for (int paramCounter = 0; paramCounter < explicitParameterNames.size(); paramCounter++) {88 String paramName = explicitParameterNames.get(paramCounter);89 boolean formattedValueMatches = true;90 boolean valueMatches = true;91 for (int caseCounter = 0; caseCounter < joinedArgs.size(); caseCounter++) {92 JoinedArgs args = joinedArgs.get(caseCounter).get(argumentCounter);93 String parameterValue = explicitParameterValues.get(caseCounter).get(paramCounter);94 String formattedValue = args.words.get(0).getFormattedValue();95 if (!formattedValue.equals(parameterValue)) {96 formattedValueMatches = false;97 }98 String value = args.words.get(0).getValue();99 if (!value.equals(parameterValue)) {100 valueMatches = false;101 }102 if (!formattedValueMatches && !valueMatches) {103 continue parameters;104 }105 }106 // on this point either all formatted values match or all values match (or both)107 argumentNames.add(paramName);108 paramNames.add(paramName);109 continue arguments;110 }111 argumentNames.add(null);112 }113 Set<String> usedNames = Sets.newHashSet();114 for (int argumentCounter = 0; argumentCounter < joinedArgs.get(0).size(); argumentCounter++) {115 String name = argumentNames.get(argumentCounter);116 if (name == null || paramNames.count(name) > 1) {117 String origName = getArgumentName(joinedArgs, argumentCounter);118 name = findFreeName(usedNames, origName);119 argumentNames.set(argumentCounter, name);120 }121 usedNames.add(name);122 }123 return argumentNames;124 }125 private String getArgumentName(List<List<JoinedArgs>> joinedArgs, int argumentCounter) {126 return joinedArgs.get(0).get(argumentCounter).words.get(0).getArgumentInfo().getArgumentName();127 }128 private String findFreeName(Set<String> usedNames, String origName) {129 String name = origName;130 int counter = 2;131 while (usedNames.contains(name)) {132 name = origName + counter;133 counter++;134 }135 usedNames.add(name);136 return name;137 }138 private List<List<Word>> getFirstWords(List<List<JoinedArgs>> joinedArgs) {139 List<List<Word>> result = Lists.newArrayList();140 for (int i = 0; i < joinedArgs.size(); i++) {141 result.add(Lists.newArrayList());142 }143 for (int i = 0; i < joinedArgs.size(); i++) {144 for (int j = 0; j < joinedArgs.get(i).size(); j++) {145 result.get(i).add(joinedArgs.get(i).get(j).words.get(0));146 }147 }148 return result;149 }150 List<List<JoinedArgs>> joinEqualArguments(List<List<Word>> differentArguments) {151 List<List<JoinedArgs>> joined = Lists.newArrayList();152 for (int i = 0; i < differentArguments.size(); i++) {153 joined.add(Lists.newArrayList());154 }155 if (differentArguments.get(0).isEmpty()) {156 return joined;157 }158 for (int caseCounter = 0; caseCounter < differentArguments.size(); caseCounter++) {159 joined.get(caseCounter).add(new JoinedArgs(differentArguments.get(caseCounter).get(0)));160 }161 int numberOfArgs = differentArguments.get(0).size();162 outer:163 for (int i = 1; i < numberOfArgs; i++) {164 inner:165 for (int j = 0; j < joined.get(0).size(); j++) {166 for (int caseCounter = 0; caseCounter < differentArguments.size(); caseCounter++) {167 Word newWord = differentArguments.get(caseCounter).get(i);168 Word joinedWord = joined.get(caseCounter).get(j).words.get(0);169 if (!newWord.getFormattedValue().equals(joinedWord.getFormattedValue())) {170 continue inner;171 }172 }173 for (int caseCounter = 0; caseCounter < differentArguments.size(); caseCounter++) {174 joined.get(caseCounter).get(j).words.add(differentArguments.get(caseCounter).get(i));175 }176 continue outer;177 }178 for (int caseCounter = 0; caseCounter < differentArguments.size(); caseCounter++) {179 joined.get(caseCounter).add(new JoinedArgs(differentArguments.get(caseCounter).get(i)));180 }181 }182 return joined;183 }184 /**185 * A scenario model is structural identical if all cases have exactly the same186 * steps, except for values of step arguments.187 * <p>188 * This is implemented by comparing all cases with the first one189 */190 private boolean isStructuralIdentical(ScenarioModel scenarioModel) {191 ScenarioCaseModel firstCase = scenarioModel.getScenarioCases().get(0);192 for (int caseCounter = 1; caseCounter < scenarioModel.getScenarioCases().size(); caseCounter++) {193 ScenarioCaseModel caseModel = scenarioModel.getScenarioCases().get(caseCounter);194 if (stepsAreDifferent(firstCase, caseModel)) {195 return false;196 }197 }198 return true;199 }200 boolean stepsAreDifferent(ScenarioCaseModel firstCase, ScenarioCaseModel secondCase) {201 return stepsAreDifferent(firstCase.getSteps(), secondCase.getSteps());202 }203 boolean stepsAreDifferent(List<StepModel> firstSteps, List<StepModel> secondSteps) {204 if (firstSteps.size() != secondSteps.size()) {205 return true;206 }207 for (int stepCounter = 0; stepCounter < firstSteps.size(); stepCounter++) {208 StepModel firstStep = firstSteps.get(stepCounter);209 StepModel secondStep = secondSteps.get(stepCounter);210 if (firstStep.getWords().size() != secondStep.getWords().size()) {211 return true;212 }213 if (attachmentsAreStructurallyDifferent(firstStep.getAttachments(), secondStep.getAttachments())) {214 return true;215 }216 if (wordsAreDifferent(firstStep, secondStep)) {217 return true;218 }219 if (stepsAreDifferent(firstStep.getNestedSteps(), secondStep.getNestedSteps())) {220 return true;221 }222 }223 return false;224 }225 /**226 * Attachments are only structurally different if one step has an inline attachment227 * and the other step either has no inline attachment or the inline attachment is228 * different.229 */230 boolean attachmentsAreStructurallyDifferent(List<AttachmentModel> firstAttachments,231 List<AttachmentModel> otherAttachments) {232 if (firstAttachments.size() != otherAttachments.size()) {233 return true;234 }235 for (int i = 0; i < firstAttachments.size(); i++) {236 if (attachmentIsStructurallyDifferent(firstAttachments.get(i), otherAttachments.get(i))) {237 return true;238 }239 }240 return false;241 }242 boolean attachmentIsStructurallyDifferent(AttachmentModel firstAttachment, AttachmentModel otherAttachment) {243 if (isInlineAttachment(firstAttachment) != isInlineAttachment(otherAttachment)) {244 return true;245 }246 if (isInlineAttachment(firstAttachment)) {247 return !firstAttachment.getValue().equals(otherAttachment.getValue());248 }249 return false;250 }251 private boolean isInlineAttachment(AttachmentModel attachmentModel) {252 return attachmentModel != null && attachmentModel.isShowDirectly();253 }254 private boolean wordsAreDifferent(StepModel firstStep, StepModel stepModel) {255 for (int wordCounter = 0; wordCounter < firstStep.getWords().size(); wordCounter++) {256 Word firstWord = firstStep.getWord(wordCounter);257 Word word = stepModel.getWord(wordCounter);258 if (firstWord.isArg() != word.isArg()) {259 return true;260 }261 if (!firstWord.isArg() && !firstWord.getValue().equals(word.getValue())) {262 return true;263 }264 if (firstWord.isArg() && firstWord.isDataTable()265 && !firstWord.getArgumentInfo().getDataTable().equals(word.getArgumentInfo().getDataTable())) {266 return true;267 }268 }269 return false;270 }271 private void setParameterNames(List<List<JoinedArgs>> differentArguments, List<String> argumentNames) {272 AssertionUtil273 .assertTrue(argumentNames.size() == differentArguments.get(0).size(), "Number of argument names is wrong");274 for (int argumentCounter = 0; argumentCounter < argumentNames.size(); argumentCounter++) {275 for (List<JoinedArgs> differentArgument : differentArguments) {276 for (Word word : differentArgument.get(argumentCounter).words) {277 word.getArgumentInfo().setParameterName(argumentNames.get(argumentCounter));278 }279 }280 }281 }282 private List<String> getFormattedValues(List<Word> words) {283 List<String> formattedValues = Lists.newArrayListWithExpectedSize(words.size());284 for (Word word : words) {285 formattedValues.add(word.getFormattedValue());...
setParameterNames
Using AI Code Generation
1import com.tngtech.jgiven.annotation.Description2import com.tngtech.jgiven.annotation.ProvidedScenarioState3import com.tngtech.jgiven.annotation.ScenarioState4import com.tngtech.jgiven.junit.ScenarioTest5import com.tngtech.jgiven.report.analysis.CaseArgumentAnalyser6import com.tngtech.jgiven.report.model.GivenCase7import com.tngtech.jgiven.report.model.ScenarioCase8import com.tngtech.jgiven.report.model.ScenarioModel9import com.tngtech.jgiven.report.model.ThenCase10import com.tngtech.jgiven.report.model.WhenCase11import com.tngtech.jgiven.tags.Issue12import org.junit.Test13import static com.tngtech.jgiven.report.model.ScenarioCaseType.GIVEN14import static com.tngtech.jgiven.report.model.ScenarioCaseType.THEN15import static com.tngtech.jgiven.report.model.ScenarioCaseType.WHEN16class CaseArgumentAnalyserTest extends ScenarioTest<CaseArgumentAnalyserTest> {17 @Issue("#14")18 void arguments_are_analysed_for_scenario_with_given_when_then() {19 given().a_scenario_model_with_$_case("given")20 and().a_scenario_model_with_$_case("when")21 and().a_scenario_model_with_$_case("then")22 when().the_arguments_are_analysed()23 then().the_arguments_are_analysed_correctly()24 }25 @Issue("#14")26 void arguments_are_analysed_for_scenario_with_given_when() {27 given().a_scenario_model_with_$_case("given")28 and().a_scenario_model_with_$_case("when")29 when().the_arguments_are_analysed()30 then().the_arguments_are_analysed_correctly()31 }32 @Issue("#14")33 void arguments_are_analysed_for_scenario_with_given_then() {34 given().a_scenario_model_with_$_case("given")35 and().a_scenario_model_with_$_case("then")36 when().the_arguments_are_analysed()37 then().the_arguments_are_analysed_correctly()38 }39 @Issue("#14")
setParameterNames
Using AI Code Generation
1com.tngtech.jgiven.report.analysis.CaseArgumentAnalyser analyser = new com.tngtech.jgiven.report.analysis.CaseArgumentAnalyser();2analyser.setParameterNames(new String[] { "arg1", "arg2" });3com.tngtech.jgiven.report.model.ScenarioModel scenario = new com.tngtech.jgiven.report.model.ScenarioModel();4scenario.setParameterNames(new String[] { "arg1", "arg2" });5com.tngtech.jgiven.report.model.CaseModel caseModel = new com.tngtech.jgiven.report.model.CaseModel();6caseModel.setParameterNames(new String[] { "arg1", "arg2" });7com.tngtech.jgiven.report.model.StepModel step = new com.tngtech.jgiven.report.model.StepModel();8step.setParameterNames(new String[] { "arg1", "arg2" });9com.tngtech.jgiven.report.model.ArgumentModel argument = new com.tngtech.jgiven.report.model.ArgumentModel();10argument.setParameterNames(new String[] { "arg1", "arg2" });11com.tngtech.jgiven.report.model.ArgumentModel$Argument argument = new com.tngtech.jgiven.report.model.ArgumentModel$Argument();12argument.setParameterNames(new String[] { "arg1", "arg2" });13com.tngtech.jgiven.report.model.CaseModel$Case case = new com.tngtech.jgiven.report.model.CaseModel$Case();14case.setParameterNames(new String[] { "arg1", "arg2" });15com.tngtech.jgiven.report.model.StepModel$Step step = new com.tngtech.jgiven.report.model.StepModel$Step();16step.setParameterNames(new String[] { "arg1
setParameterNames
Using AI Code Generation
1public void setParameterNames(String[] parameterNames) {2 this.parameterNames = parameterNames;3}4public void setParameterNames(String[] parameterNames) {5 this.parameterNames = parameterNames;6}7public void setParameterNames(String[] parameterNames) {8 this.parameterNames = parameterNames;9}10public void setParameterNames(String[] parameterNames) {11 this.parameterNames = parameterNames;12}13public void setParameterNames(String[] parameterNames) {14 this.parameterNames = parameterNames;15}16public void setParameterNames(String[] parameterNames) {17 this.parameterNames = parameterNames;18}19public void setParameterNames(String[] parameterNames) {20 this.parameterNames = parameterNames;21}22public void setParameterNames(String[] parameterNames) {23 this.parameterNames = parameterNames;24}25public void setParameterNames(String[] parameterNames) {26 this.parameterNames = parameterNames;27}28public void setParameterNames(String[] parameterNames) {29 this.parameterNames = parameterNames;30}31public void setParameterNames(String[] parameterNames) {32 this.parameterNames = parameterNames;33}34public void setParameterNames(String[] parameterNames) {
setParameterNames
Using AI Code Generation
1public class CaseArgumentAnalyserTest extends JGivenTest<CaseArgumentAnalyserTest> {2 public void setParameterNames() {3 CaseArgumentAnalyser caseArgumentAnalyser = new CaseArgumentAnalyser();4 caseArgumentAnalyser.setParameterNames(Arrays.asList("first", "second"));5 assertThat(caseArgumentAnalyser.getParameterNames()).containsExactly("first", "second");6 }7}
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!!