[Cucumber for Java] fix name of test nodes
authorAndrey Vokin <andrey.vokin@jetbrains.com>
Sun, 27 Oct 2019 14:47:31 +0000 (15:47 +0100)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Mon, 11 Nov 2019 17:12:03 +0000 (17:12 +0000)
Node should have names:
 - Scenario Outline,
 - Scenario: Line: X
 - <Keyword> step name

GitOrigin-RevId: 78a38fd9c36d908e4877f7e68c67e4b82b5a0df0

plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm2Adapter.java
plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvmSMConverter.java
plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvmSMFormatter.java
plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvmSMFormatterUtil.java
plugins/cucumber-jvm-formatter3/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm3Adapter.java
plugins/cucumber-jvm-formatter5/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm5Adapter.java

index 7c08a7c8d06deaf7c84d4c51e8c2c15575e9b1b7..ba4ed98b02f5d9c697223c8a1e9d5c76710e3fc4 100644 (file)
@@ -159,10 +159,23 @@ public class CucumberJvm2Adapter {
       if (myRealStep.isHook()) {
         stepName = "Hook: " + myRealStep.getHookType().toString();
       } else {
-        stepName = myRealStep.getStepText();
+        stepName = getStepKeyword() + " " + myRealStep.getStepText();
       }
       return stepName;
     }
+
+    private String getStepKeyword() {
+      try {
+        String filePath = myRealStep.getStepLocation().substring(0, myRealStep.getStepLocation().indexOf(':'));
+        String line = CucumberJvmSMFormatterUtil.getStepKeyword(filePath, myRealStep.getStepLine());
+        if (line != null) {
+          return line;
+        }
+      }
+      catch (Throwable ignored) {
+      }
+      return "Given";
+    }
   }
 
   private static PickleEvent getPickleEvent(TestCase testCase) {
index 7e74b8916c9182cb7d56ff1d476a5e40aee5ee73..488d9b42a9dfeb253c0ad8cf35e1b37c79828fc1 100644 (file)
@@ -50,7 +50,7 @@ public class CucumberJvmSMConverter {
           !currentFilePath.equals(getEventUri(event))) {
         closeCurrentScenarioOutline();
         currentScenarioOutlineLine = mainScenarioLine;
-        currentScenarioOutlineName = event.getTestCase().getScenarioName();
+        currentScenarioOutlineName = "Scenario Outline: " + event.getTestCase().getScenarioName();
         outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event) + ":" + currentScenarioOutlineLine,
                    currentScenarioOutlineName);
         outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), "", EXAMPLES_CAPTION);
@@ -61,11 +61,11 @@ public class CucumberJvmSMConverter {
     currentFilePath = getEventUri(event);
 
     outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event) + ":" + event.getTestCase().getLine(),
-               event.getTestCase().getScenarioName());
+               getScenarioName(event.getTestCase()));
   }
 
   protected void handleTestCaseFinished(CucumberJvmAdapter.IdeaTestCaseEvent event) {
-    outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), event.getTestCase().getScenarioName());
+    outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), getScenarioName(event.getTestCase()));
     outCommand(TEMPLATE_SCENARIO_FINISHED, getCurrentTime());
   }
 
@@ -159,7 +159,7 @@ public class CucumberJvmSMConverter {
     if (testCase.isScenarioOutline()) {
       return SCENARIO_OUTLINE_CAPTION + testCase.getLine();
     }
-    return testCase.getScenarioName();
+    return "Scenario: " + testCase.getScenarioName();
   }
 
   protected String getEventUri(CucumberJvmAdapter.IdeaTestCaseEvent event) {
index 03fa713186b125a4a120a0a698ffbaec64a2d599..57a476af66a4cdfc4ab350880bda73f168c56e42 100644 (file)
@@ -46,7 +46,7 @@ public class CucumberJvmSMFormatter implements Formatter, Reporter {
 
   @SuppressWarnings("UnusedDeclaration")
   public CucumberJvmSMFormatter(Appendable appendable) {
-    this.appendable = System.err;
+    this.appendable = System.out;
     queue = new ArrayDeque<String>();
     currentSteps = new ArrayDeque<Step>();
     outCommand(TEMPLATE_ENTER_THE_MATRIX, getCurrentTime());
@@ -154,7 +154,7 @@ public class CucumberJvmSMFormatter implements Formatter, Reporter {
       scenarioPassed = false;
       String message = "Undefined step: " + getName(currentStep);
       String details = "";
-      outCommand(TEMPLATE_TEST_FAILED, true, getCurrentTime(), details, message, stepFullName, "error = 'true'");
+      outCommand(TEMPLATE_TEST_FAILED, true, getCurrentTime(), details, message, stepFullName, "");
     }
     else if (result.equals(Result.SKIPPED)) {
       skippedStepCount++;
@@ -167,7 +167,7 @@ public class CucumberJvmSMFormatter implements Formatter, Reporter {
 
     final String currentTime = getCurrentTime();
     final Long duration = result.getDuration();
-    double durationInSeconds = 1.0 * (duration == null ? 0 : duration.longValue()) / MILLION;
+    long durationInSeconds = (duration == null ? 0 : duration.longValue()) / MILLION;
     outCommand(TEMPLATE_TEST_FINISHED, true, currentTime, String.valueOf(durationInSeconds), stepFullName);
   }
 
@@ -322,7 +322,7 @@ public class CucumberJvmSMFormatter implements Formatter, Reporter {
   }
 
   private static String getName(Step step) {
-    return step.getKeyword() + " " + step.getName();
+    return step.getKeyword() + step.getName();
   }
 
   private static String getName(Feature feature) {
index 8a2f67e443bf5aa935cc52ca1c0e7245f9d1b58c..39848de4f5774b09ff2d929f6f7901ac38d062b6 100644 (file)
@@ -1,5 +1,10 @@
 package org.jetbrains.plugins.cucumber.java.run;
 
+import cucumber.api.TestStep;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 
@@ -90,4 +95,34 @@ public class CucumberJvmSMFormatterUtil {
     }
     return featureHeader;
   }
+
+  public static String getStepKeyword(String filePath, int lineNumber) throws IOException {
+    String line = readLineNumber(filePath, lineNumber);
+    if (line != null) {
+      return line.trim().split(" ", 2)[0];
+    }
+    return null;
+  }
+
+  private static String readLineNumber(String filePath, int lineNumber) throws IOException {
+    int currentLineNumber = 0;
+    FileReader fileStream = null;
+    try {
+      fileStream = new FileReader(filePath);
+      BufferedReader bufferedReader = new BufferedReader(fileStream);
+      String line;
+      while ((line = bufferedReader.readLine()) != null) {
+        currentLineNumber++;
+        if (currentLineNumber == lineNumber) {
+          return line;
+        }
+      }
+    }
+    finally {
+      if (fileStream != null) {
+        fileStream.close();
+      }
+    }
+    return null;
+  }
 }
index 7021d5a998bd19099d0a4b61b186815b1441dfcf..c213e52b22c4f6be3bb8197f891a815a76546b66 100644 (file)
@@ -116,7 +116,7 @@ public class CucumberJvm3Adapter {
 
     @Override
     public String getScenarioName() {
-      return "Scenario: " + myRealTestCase.getName();
+      return myRealTestCase.getName();
     }
   }
 
@@ -161,10 +161,30 @@ public class CucumberJvm3Adapter {
       if (myRealStep instanceof HookTestStep) {
         stepName = "Hook: " + ((HookTestStep)myRealStep).getHookType().toString();
       } else {
-        stepName = ((PickleStepTestStep) myRealStep).getPickleStep().getText();
+        stepName = getStepKeyword() + " " + ((PickleStepTestStep) myRealStep).getPickleStep().getText();
       }
       return stepName;
     }
+
+    private String getStepKeyword() {
+      try {
+        PickleStepTestStep pickleStep = (PickleStepTestStep) myRealStep;
+        String location = pickleStep.getStepLocation();
+        if (location.startsWith("file://")) {
+          location = location.substring("file://".length());
+        }
+        else if (location.startsWith("file:")) {
+          location = location.substring("file:".length());
+        }
+        String[] fileAndLine = location.split(":");
+        String result = CucumberJvmSMFormatterUtil.getStepKeyword(fileAndLine[0], Integer.parseInt(fileAndLine[1]));
+        if (result != null) {
+          return result;
+        }
+      } catch (Throwable ignored) {
+      }
+      return "Given";
+    }
   }
 
   private static PickleEvent getPickleEvent(TestCase testCase) {
index e748f6ca632431b890f7f04431ba2ca807d3f97b..b84b3a83a348a607bf0cf09026ba6857db5b0020 100644 (file)
@@ -1,7 +1,7 @@
 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 package org.jetbrains.plugins.cucumber.java.run;
 
-import gherkin.events.PickleEvent;
+import gherkin.pickles.Pickle;
 import io.cucumber.plugin.event.*;
 
 import java.lang.reflect.Field;
@@ -89,8 +89,8 @@ public class CucumberJvm5Adapter {
 
     @Override
     public boolean isScenarioOutline() {
-      PickleEvent pickleEvent = getPickleEvent(myRealTestCase);
-      return pickleEvent != null && pickleEvent.pickle.getLocations().size() > 1;
+      Pickle pickle = getPickleEvent(myRealTestCase);
+      return pickle != null && pickle.getLocations().size() > 1;
     }
 
     @Override
@@ -100,9 +100,9 @@ public class CucumberJvm5Adapter {
 
     @Override
     public int getScenarioOutlineLine() {
-      PickleEvent pickleEvent = getPickleEvent(myRealTestCase);
-      if (pickleEvent != null) {
-        return pickleEvent.pickle.getLocations().get(pickleEvent.pickle.getLocations().size() - 1).getLine();
+      Pickle pickle = getPickleEvent(myRealTestCase);
+      if (pickle != null) {
+        return pickle.getLocations().get(pickle.getLocations().size() - 1).getLine();
       }
       return 0;
     }
@@ -114,7 +114,7 @@ public class CucumberJvm5Adapter {
 
     @Override
     public String getScenarioName() {
-      return "Scenario: " + myRealTestCase.getName();
+      return myRealTestCase.getName();
     }
   }
 
@@ -159,17 +159,21 @@ public class CucumberJvm5Adapter {
       if (myRealStep instanceof HookTestStep) {
         stepName = "Hook: " + ((HookTestStep)myRealStep).getHookType().toString();
       } else {
-        stepName = ((PickleStepTestStep) myRealStep).getStepText();
+        PickleStepTestStep pickleStep = (PickleStepTestStep)myRealStep;
+        stepName = pickleStep.getStep().getKeyWord() + pickleStep.getStepText();
       }
       return stepName;
     }
   }
 
-  private static PickleEvent getPickleEvent(TestCase testCase) {
+  private static Pickle getPickleEvent(TestCase testCase) {
     try {
-      Field pickleEventField = testCase.getClass().getDeclaredField("pickleEvent");
-      pickleEventField.setAccessible(true);
-      return (PickleEvent)pickleEventField.get(testCase);
+      Field cucumberPickleField = testCase.getClass().getDeclaredField("pickle");
+      cucumberPickleField.setAccessible(true);
+      Object cucumberPickle = cucumberPickleField.get(testCase);
+      Field pickleField = cucumberPickle.getClass().getDeclaredField("pickle");
+      pickleField.setAccessible(true);
+      return (Pickle) pickleField.get(cucumberPickle);
     }
     catch (Exception ignored) {
     }