Skip to content

Commit 92e1ad3

Browse files
authored
Merge pull request #108 from wilsonjord/fix-scope-overwritten
fix: getScope level overwritten by nested assertion blocks
2 parents 93cc467 + 1c921b2 commit 92e1ad3

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

source/fluentasserts/results/source/scopes.d

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ auto getScope(const(Token)[] tokens, size_t line) nothrow {
3535
}
3636

3737
if (foundScope) {
38-
if (token.text == "should" || token.text == "Assert" || type == "assert" || type == ";") {
38+
if (!foundAssert && (token.text == "should" || token.text == "Assert" || type == "assert" || type == ";")) {
3939
foundAssert = true;
4040
scopeLevel = paranthesisCount;
4141
}
4242

43-
if (type == "}" && paranthesisCount <= scopeLevel) {
43+
if (foundAssert && type == "}" && paranthesisCount < scopeLevel) {
4444
beginToken = paranthesisLevels[paranthesisCount];
4545
endToken = i + 1;
4646

@@ -112,3 +112,19 @@ unittest {
112112
113113
}`);
114114
}
115+
116+
@("getScope returns full parent scope when it contains other nested blocks")
117+
unittest {
118+
const(Token)[] tokens = [];
119+
splitMultilinetokens(fileToDTokens("testdata/values.d"), tokens);
120+
121+
auto result = getScope(tokens, 120);
122+
auto identifierStart = getPreviousIdentifier(tokens, result.begin);
123+
124+
tokens[identifierStart .. result.end].tokensToString.strip.should.equal(`unittest {
125+
auto a = 1;
126+
a.shoud.equal(1);
127+
receive(2.seconds, (int m) { m.should.equal(1); });
128+
129+
}`);
130+
}

testdata/values.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,9 @@ unittest {
114114
a.should.equal(2);
115115
}
116116
}
117+
118+
unittest {
119+
auto a = 1;
120+
a.shoud.equal(1);
121+
receive(2.seconds, (int m) { m.should.equal(1); });
122+
}

0 commit comments

Comments
 (0)