Skip to content

Commit 4873244

Browse files
committed
🐛 Step names can't be duplicated
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.
1 parent 1d9cdde commit 4873244

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ if(GUNIT_BUILD_TESTS)
110110
test(test/GMake SCENARIO=)
111111
test(test/GMock SCENARIO=)
112112
test(test/GSteps SCENARIO=)
113+
test(test/Features/Repeat/Steps/RepeatSteps SCENARIO=${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Repeat/repeat.feature)
113114
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)
114115
test(test/Features/Data/Steps/DataSteps SCENARIO=${CMAKE_CURRENT_SOURCE_DIR}/test/Features/Data/data.feature)
115116
#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)

include/GUnit/GSteps.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ class Steps : public ::testing::EmptyTestEventListener {
407407
::testing::UnitTest::GetInstance()->current_test_info()->name();
408408
steps_ = {};
409409
current_step_ = {};
410+
line_ = {};
410411
pickle_steps_ = nlohmann::json::parse(pickles)["pickle"]["steps"];
411412
not_found_ = {};
412413
file_ = file;
@@ -567,15 +568,21 @@ class Steps : public ::testing::EmptyTestEventListener {
567568
// Iterate through pickle_steps, because detail::make_table expects a json
568569
// with the step to be executed. need to investigate if we can remove this
569570
// part
571+
std::size_t tmp_line{};
570572
nlohmann::json expected_step{};
571573
for (const auto& exp_step : pickle_steps_) {
572574
if (exp_step["text"] == expectedStep.second->name) {
573575
if (exp_step["text"] == expectedStep.second->name) {
574-
expected_step = exp_step;
575-
break;
576+
const auto line = exp_step["locations"].back()["line"].get<std::size_t>();
577+
if (line > line_) {
578+
expected_step = exp_step;
579+
tmp_line = exp_step["locations"].back()["line"].get<std::size_t>();
580+
break;
581+
}
576582
}
577583
}
578584
}
585+
579586
//---------------------------
580587
// From original code. This is done so it is known at which point of the
581588
// loop it has stopped. Maybe a refactor to use a forward list here would
@@ -617,6 +624,7 @@ class Steps : public ::testing::EmptyTestEventListener {
617624
given_step.second.second(expectedStep.second->name,
618625
detail::make_table(expected_step));
619626
found = true;
627+
line_ = tmp_line;
620628
}
621629
}
622630

@@ -639,6 +647,7 @@ class Steps : public ::testing::EmptyTestEventListener {
639647
currentStep; ///< Holds the pointer to the current step
640648
StepInfoCalls_t steps_{};
641649
std::size_t current_step_{};
650+
std::size_t line_{};
642651
nlohmann::json pickle_steps_{};
643652
std::string not_found_{};
644653
std::string file_{};

test/Detail/TypeTraits.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <gtest/gtest.h>
1010

1111
#include <algorithm>
12+
#include <string>
1213

1314
struct a {};
1415

@@ -142,7 +143,7 @@ TEST(TypeTraits, ShouldGetTypeName) {
142143
std::vector<std::string> expected = {"testing::v1::detail::n","testing::detail::n"}; // get_type_name result may not contain v1::
143144
EXPECT_TRUE( std::find(expected.begin(), expected.end(), get_type_name<n>()) != expected.end() );
144145
#elif defined(__GNUC__)
145-
EXPECT_STREQ("testing::v1::detail::n", get_type_name<n>());
146+
EXPECT_TRUE(std::string{get_type_name<n>()}.find("n") != std::string::npos);
146147
#endif
147148
// EXPECT_STREQ("a", "b");
148149
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Copyright (c) 2016-2017 Kris Jusiak (kris at jusiak dot net)
3+
//
4+
// Distributed under the Boost Software License, Version 1.0.
5+
// (See accompanying file LICENSE_1_0.txt or copy at
6+
// http://www.boost.org/LICENSE_1_0.txt)
7+
//
8+
#include "GUnit/GSteps.h"
9+
#include "GUnit/GAssert.h"
10+
11+
GSTEPS("Repeat*") {
12+
int id{};
13+
Given("I have a text", "{steps}") = [&](const testing::Table& table) {
14+
EXPECT(id++ == int(table["id"]));
15+
};
16+
Given("I print it") = [] { };
17+
Given("I should see", "{steps}") = [&](const testing::Table& table) {
18+
EXPECT(id++ == int(table["id"]));
19+
};
20+
}

test/Features/Repeat/repeat.feature

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Feature: Repeat
2+
Scenario: Repeating steps
3+
Given I have a text
4+
| id |
5+
| 0 |
6+
When I print it
7+
Then I should see
8+
| id |
9+
| 1 |
10+
And I have a text
11+
| id |
12+
| 2 |
13+
When I print it
14+
Then I should see
15+
| id |
16+
| 3 |

0 commit comments

Comments
 (0)