Skip to content

Commit f62242e

Browse files
committed
Reorg'ed tests so test classes share a single BrowsingContext and parser
1 parent b4c8d32 commit f62242e

28 files changed

+137
-20
lines changed

src/CompareResult.cs renamed to src/Core/CompareResult.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Egil.AngleSharp.Diffing
1+
namespace Egil.AngleSharp.Diffing.Core
22
{
33
public enum CompareResult
44
{

src/Strategies/AttributeStrategies/AttributeComparer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Egil.AngleSharp.Diffing.Core;
44

55
namespace Egil.AngleSharp.Diffing.Strategies.AttributeStrategies
6-
{
6+
{
77
public static class AttributeComparer
88
{
99
private const string IGNORE_CASE_POSTFIX = ":ignorecase";

tests/Core/ComparisonSourceTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace Egil.AngleSharp.Diffing.Core
55
{
66
public class ComparisonSourceTest : DiffingTestBase
77
{
8+
public ComparisonSourceTest(DiffingTestFixture fixture) : base(fixture)
9+
{
10+
}
11+
812
[Fact(DisplayName = "Two sources are equal if all their properties are equal")]
913
public void Test1()
1014
{

tests/Core/DiffingEngineTestBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ namespace Egil.AngleSharp.Diffing.Core
55
{
66
public abstract class DiffingEngineTestBase : DiffingTestBase
77
{
8+
public DiffingEngineTestBase(DiffingTestFixture fixture) : base(fixture)
9+
{
10+
}
11+
812
protected static HtmlDifferenceEngine CreateHtmlDiffEngine(
913
Func<DiffContext, SourceCollection, SourceCollection, IEnumerable<Comparison>>? nodeMatcher = null,
1014
Func<DiffContext, SourceMap, SourceMap, IEnumerable<AttributeComparison>>? attrMatcher = null,

tests/Core/HtmlDifferenceEngineTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ namespace Egil.AngleSharp.Diffing.Core
88
{
99
public class HtmlDifferenceEngineTest : DiffingEngineTestBase
1010
{
11+
public HtmlDifferenceEngineTest(DiffingTestFixture fixture) : base(fixture)
12+
{
13+
}
14+
1115
[Fact(DisplayName = "Unmatched nodes in control/test are returned as missing/unexpected diffs")]
1216
public void UnmatchedNodesBecomesMissingUnexpectedDiffs()
1317
{

tests/Core/SourceCollectionTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace Egil.AngleSharp.Diffing.Core
77
{
88
public class SourceCollectionTest : DiffingTestBase
99
{
10+
public SourceCollectionTest(DiffingTestFixture fixture) : base(fixture)
11+
{
12+
}
13+
1014
[Theory(DisplayName = "Source type is correct saved in the collection")]
1115
[InlineData(ComparisonSourceType.Control)]
1216
[InlineData(ComparisonSourceType.Test)]

tests/Core/SourceMapTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace Egil.AngleSharp.Diffing.Core
77
{
88
public class SourceMapTest : DiffingTestBase
99
{
10+
public SourceMapTest(DiffingTestFixture fixture) : base(fixture)
11+
{
12+
}
13+
1014
[Fact(DisplayName = "When initialized with a non-element an exception is thrown")]
1115
public void Test0()
1216
{

tests/DiffingStrategyPipelineTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ public class DiffingStrategyPipelineTest : DiffingTestBase
1010
{
1111
private DiffContext _dummyContext = new DiffContext(null, null);
1212

13+
public DiffingStrategyPipelineTest(DiffingTestFixture fixture) : base(fixture)
14+
{
15+
}
16+
1317
private FilterDecision NegateDecision(FilterDecision decision) => decision switch
1418
{
1519
FilterDecision.Keep => FilterDecision.Exclude,

tests/Core/DiffingTestBase.cs renamed to tests/DiffingTestBase.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22
using AngleSharp;
33
using AngleSharp.Dom;
44
using AngleSharp.Html.Parser;
5+
using Egil.AngleSharp.Diffing.Core;
6+
using Xunit;
57

6-
namespace Egil.AngleSharp.Diffing.Core
8+
namespace Egil.AngleSharp.Diffing
79
{
8-
public abstract class DiffingTestBase
10+
public abstract class DiffingTestBase : IClassFixture<DiffingTestFixture>
911
{
10-
private readonly IBrowsingContext _context;
11-
private readonly IHtmlParser _htmlParser;
12-
private readonly IDocument _document;
12+
private readonly DiffingTestFixture _testFixture;
1313

1414
protected INodeList EmptyNodeList => ToNodeList("");
1515

16-
protected DiffingTestBase()
16+
public DiffingTestBase(DiffingTestFixture fixture)
1717
{
18-
var config = Configuration.Default.WithCss();
19-
_context = BrowsingContext.New(config);
20-
_htmlParser = _context.GetService<IHtmlParser>();
21-
_document = _context.OpenNewAsync().Result;
18+
_testFixture = fixture;
2219
}
2320

2421
protected INodeList ToNodeList(string? htmlsnippet)
2522
{
26-
var fragment = _htmlParser.ParseFragment(htmlsnippet, _document.Body);
23+
var fragment = _testFixture.Parse(htmlsnippet);
2724
return fragment;
2825
}
2926

@@ -34,7 +31,7 @@ protected IEnumerable<ComparisonSource> ToComparisonSourceList(string html)
3431

3532
protected INode ToNode(string htmlsnippet)
3633
{
37-
var fragment = _htmlParser.ParseFragment(htmlsnippet, _document.Body);
34+
var fragment = _testFixture.Parse(htmlsnippet);
3835
return fragment[0];
3936
}
4037

tests/DiffingTestFixture.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using AngleSharp;
2+
using AngleSharp.Dom;
3+
using AngleSharp.Html.Parser;
4+
5+
namespace Egil.AngleSharp.Diffing
6+
{
7+
public class DiffingTestFixture
8+
{
9+
private readonly IBrowsingContext _context;
10+
private readonly IHtmlParser _htmlParser;
11+
private readonly IDocument _document;
12+
13+
public DiffingTestFixture()
14+
{
15+
var config = Configuration.Default.WithCss();
16+
_context = BrowsingContext.New(config);
17+
_htmlParser = _context.GetService<IHtmlParser>();
18+
_document = _context.OpenNewAsync().Result;
19+
}
20+
21+
public INodeList Parse(string? html)
22+
{
23+
return _htmlParser.ParseFragment(html, _document.Body);
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)