Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dotnet/Cucumber.Query/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public IEnumerable<Attachment> FindAttachmentsBy(TestRunHookFinished testRunHook

public Location? FindLocationOf(Pickle pickle)
{
if (pickle.Location != null)
return pickle.Location;
var lineage = FindLineageBy(pickle);
if (lineage == null)
return null;
Expand Down
45 changes: 45 additions & 0 deletions dotnet/Cucumber.QueryTest/QueryTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Io.Cucumber.Messages.Types;
using Cucumber.Query;
using System.Text;

namespace Cucumber.QueryTest;

Expand Down Expand Up @@ -48,5 +49,49 @@ public void OmitsTestCaseStartedIfFinishedAndWillBeRetried()
Assert.AreEqual(1, _query.TestCasesStartedCount);
}

[TestMethod]
public void FindLocationOfFallsBackToScenarioLocationWhenPickleHasNoLocation()
{
var envelopes = LoadEnvelopes("minimal.ndjson");
var query = BuildQuery(envelopes);
var pickle = FindPickle(envelopes);

Assert.AreEqual(pickle.Location, query.FindLocationOf(StripLocation(pickle)));
}

[TestMethod]
public void FindLocationOfFallsBackToExampleRowLocationWhenPickleHasNoLocation()
{
var envelopes = LoadEnvelopes("examples-tables.ndjson");
var query = BuildQuery(envelopes);
var pickle = FindPickle(envelopes);

Assert.AreEqual(pickle.Location, query.FindLocationOf(StripLocation(pickle)));
}

private static Cucumber.Query.Query BuildQuery(IEnumerable<Envelope> envelopes)
{
var repository = Repository.CreateWithAllFeatures();
foreach (var envelope in envelopes)
repository.Update(envelope);
return new Cucumber.Query.Query(repository);
}

private static Pickle FindPickle(IEnumerable<Envelope> envelopes) =>
envelopes.First(e => e.Pickle != null).Pickle;

private static Pickle StripLocation(Pickle pickle) =>
new Pickle(pickle.Id, pickle.Uri, null, pickle.Name, pickle.Language, pickle.Steps,
pickle.Tags, pickle.AstNodeIds);

private static IReadOnlyList<Envelope> LoadEnvelopes(string source)
{
var path = Path.Combine(TestFolderHelper.TestFolder, "src", source);
return File.ReadAllLines(path, Encoding.UTF8)
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(NdjsonSerializer.Deserialize)
.ToList();
}

private static string RandomId() => Guid.NewGuid().ToString();
}
72 changes: 42 additions & 30 deletions javascript/src/Query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,7 @@ describe('Query', () => {

describe('#findLineageBy', () => {
it('returns correct lineage for a minimal scenario', async () => {
const envelopes: ReadonlyArray<Envelope> = (
await fs.readFile(path.join(import.meta.dirname, '../../testdata/src/minimal.ndjson'), {
encoding: 'utf-8',
})
)
.split('\n')
.filter((line) => !!line)
.map((line) => JSON.parse(line))
const envelopes = await loadEnvelopes('minimal.ndjson')
for (const envelope of envelopes) {
cucumberQuery.update(envelope)
}
Expand All @@ -115,17 +108,7 @@ describe('Query', () => {
})

it('returns correct lineage for a pickle from an examples table', async () => {
const envelopes: ReadonlyArray<Envelope> = (
await fs.readFile(
path.join(import.meta.dirname, '../../testdata/src/examples-tables.ndjson'),
{
encoding: 'utf-8',
}
)
)
.split('\n')
.filter((line) => !!line)
.map((line) => JSON.parse(line))
const envelopes = await loadEnvelopes('examples-tables.ndjson')
for (const envelope of envelopes) {
cucumberQuery.update(envelope)
}
Expand All @@ -149,17 +132,7 @@ describe('Query', () => {
})

it('returns correct lineage for a pickle with background-derived steps', async () => {
const envelopes: ReadonlyArray<Envelope> = (
await fs.readFile(
path.join(import.meta.dirname, '../../testdata/src/rules-backgrounds.ndjson'),
{
encoding: 'utf-8',
}
)
)
.split('\n')
.filter((line) => !!line)
.map((line) => JSON.parse(line))
const envelopes = await loadEnvelopes('rules-backgrounds.ndjson')
for (const envelope of envelopes) {
cucumberQuery.update(envelope)
}
Expand All @@ -184,4 +157,43 @@ describe('Query', () => {
} satisfies Lineage)
})
})

describe('#findLocationOf', () => {
it('falls back to the scenario location when the pickle has no location', async () => {
const envelopes = await loadEnvelopes('minimal.ndjson')
for (const envelope of envelopes) {
cucumberQuery.update(envelope)
}
const pickle = envelopes.find((envelope) => envelope.pickle).pickle

assert.deepStrictEqual(
cucumberQuery.findLocationOf({ ...pickle, location: undefined }),
pickle.location
)
})

it('falls back to the example row location when the pickle has no location', async () => {
const envelopes = await loadEnvelopes('examples-tables.ndjson')
for (const envelope of envelopes) {
cucumberQuery.update(envelope)
}
const pickle = envelopes.find((envelope) => envelope.pickle).pickle

assert.deepStrictEqual(
cucumberQuery.findLocationOf({ ...pickle, location: undefined }),
pickle.location
)
})
})
})

async function loadEnvelopes(filename: string): Promise<ReadonlyArray<Envelope>> {
return (
await fs.readFile(path.join(import.meta.dirname, '../../testdata/src', filename), {
encoding: 'utf-8',
})
)
.split('\n')
.filter((line) => !!line)
.map((line) => JSON.parse(line))
}
3 changes: 3 additions & 0 deletions javascript/src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ export default class Query {
}

public findLocationOf(pickle: Pickle): Location | undefined {
if (pickle.location) {
return pickle.location
}
const lineage = this.findLineageBy(pickle)
if (lineage?.example) {
return lineage.example.location
Expand Down
Loading