From 66678cc5ae3810321cebbeb5e8a34e42ad6958c7 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sun, 10 Jun 2018 01:12:31 +0200 Subject: [PATCH] fix ShapeRegionTests --- .../Drawing/Paths/ShapeRegionTests.cs | 121 ++++++++---------- 1 file changed, 52 insertions(+), 69 deletions(-) diff --git a/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs b/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs index 08eef5597d..40c5f950da 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/ShapeRegionTests.cs @@ -2,39 +2,64 @@ // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.Primitives; +using System; +using System.Collections.Generic; +using System.Numerics; +using Moq; +using SixLabors.Primitives; +using SixLabors.Shapes; +using Xunit; namespace SixLabors.ImageSharp.Tests.Drawing.Paths { - using System; - - using Moq; - using SixLabors.Primitives; - using SixLabors.Shapes; - - using Xunit; - public class ShapeRegionTests { - private readonly Mock pathMock; + public abstract class MockPath : IPath + { + public abstract RectangleF Bounds { get; } + public IPath AsClosedPath() => this; + + public abstract SegmentInfo PointAlongPath(float distanceAlongPath); + public abstract PointInfo Distance(PointF point); + public abstract IEnumerable Flatten(); + public abstract bool Contains(PointF point); + public abstract IPath Transform(Matrix3x2 matrix); + public abstract PathTypes PathType { get; } + public abstract int MaxIntersections { get; } + public abstract float Length { get; } + + public int FindIntersections(PointF start, PointF end, PointF[] buffer, int offset) + { + return this.FindIntersections(start, end, buffer, 0); + } + + public int FindIntersections(PointF s, PointF e, Span buffer) + { + Assert.Equal(this.TestYToScan, s.Y); + Assert.Equal(this.TestYToScan, e.Y); + Assert.True(s.X < this.Bounds.Left); + Assert.True(e.X > this.Bounds.Right); + + this.TestFindIntersectionsInvocationCounter++; + + return this.TestFindIntersectionsResult; + } + + public int TestFindIntersectionsInvocationCounter { get; private set; } + public virtual int TestYToScan => 10; + public virtual int TestFindIntersectionsResult => 3; + } + + private readonly Mock pathMock; private readonly RectangleF bounds; public ShapeRegionTests() { - this.pathMock = new Mock(); + this.pathMock = new Mock() { CallBase = true }; this.bounds = new RectangleF(10.5f, 10, 10, 10); this.pathMock.Setup(x => x.Bounds).Returns(this.bounds); - // wire up the 2 mocks to reference eachother - this.pathMock.Setup(x => x.AsClosedPath()).Returns(() => this.pathMock.Object); - } - - [Fact] - public void ShapeRegionWithPathCallsAsShape() - { - new ShapeRegion(this.pathMock.Object); - - this.pathMock.Verify(x => x.AsClosedPath()); } [Fact] @@ -68,59 +93,17 @@ namespace SixLabors.ImageSharp.Tests.Drawing.Paths [Fact] public void ShapeRegionFromPathScanYProxyToShape() { - int yToScan = 10; - var region = new ShapeRegion(this.pathMock.Object); + MockPath path = this.pathMock.Object; + int yToScan = path.TestYToScan; + var region = new ShapeRegion(path); - this.pathMock - .Setup( - x => x.FindIntersections( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny())).Callback( - (s, e, b, o) => - { - Assert.Equal(yToScan, s.Y); - Assert.Equal(yToScan, e.Y); - Assert.True(s.X < this.bounds.Left); - Assert.True(e.X > this.bounds.Right); - }).Returns(0); - - int i = region.Scan(yToScan, new float[0], Configuration.Default); - - this.pathMock.Verify( - x => x.FindIntersections(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), - Times.Once); - } + int i = region.Scan(yToScan, new float[path.TestFindIntersectionsResult], Configuration.Default); - [Fact] - public void ShapeRegionFromShapeScanYProxyToShape() - { - int yToScan = 10; - var region = new ShapeRegion(this.pathMock.Object); - - this.pathMock - .Setup( - x => x.FindIntersections( - It.IsAny(), - It.IsAny(), - It.IsAny(), - It.IsAny())).Callback( - (s, e, b, o) => - { - Assert.Equal(yToScan, s.Y); - Assert.Equal(yToScan, e.Y); - Assert.True(s.X < this.bounds.Left); - Assert.True(e.X > this.bounds.Right); - }).Returns(0); - - int i = region.Scan(yToScan, new float[0], Configuration.Default); - - this.pathMock.Verify( - x => x.FindIntersections(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), - Times.Once); + Assert.Equal(path.TestFindIntersectionsResult, i); + Assert.Equal(1, path.TestFindIntersectionsInvocationCounter); } + [Fact] public void ShapeRegionFromShapeConvertsBoundsProxyToShape() {