From 2a952a0ab2488b4851af2d990f02e82ef52224d3 Mon Sep 17 00:00:00 2001 From: Thomas Boby Date: Fri, 25 Oct 2024 08:42:15 +0100 Subject: [PATCH] Create line ending assertion helper for tests --- tests/Helpers.fs | 32 +++++++++++++++++++ tests/IntegrationTests.fs | 20 ++++-------- tests/Ionide.KeepAChangelog.Tasks.Test.fsproj | 1 + tests/UnitTests.fs | 4 ++- 4 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 tests/Helpers.fs diff --git a/tests/Helpers.fs b/tests/Helpers.fs new file mode 100644 index 0000000..33e5fa6 --- /dev/null +++ b/tests/Helpers.fs @@ -0,0 +1,32 @@ +module Ionide.KeepAChangelog.Tasks.Test.Helpers + +open System.Runtime.CompilerServices +open Faqt +open Faqt.AssertionHelpers + +[] +type Assertions = + + /// Asserts that the subject is equal to the specified string when CLRF is replaced with LF in both raw and + /// escaped forms. + [] + static member BeLineEndingEquivalent(t: Testable, expected: string, ?because) : And = + use _ = t.Assert() + + if isNull expected then + nullArg (nameof expected) + + if isNull t.Subject then + t.With("Expected", expected).With("But was", t.Subject).Fail(because) + + let expectedNormalised = expected.Replace("\r\n", "\n").Replace("\\r\\n", "\\n") + + let subjectNormalised = t.Subject.Replace("\r\n", "\n").Replace("\\r\\n", "\\n") + + if subjectNormalised <> expectedNormalised then + t + .With("Expected", expectedNormalised) + .With("But was", subjectNormalised) + .Fail(because) + + And(t) diff --git a/tests/IntegrationTests.fs b/tests/IntegrationTests.fs index a1184db..19ab843 100644 --- a/tests/IntegrationTests.fs +++ b/tests/IntegrationTests.fs @@ -1,13 +1,14 @@ module Tests.IntegrationTests open System.IO -open System.Runtime.CompilerServices open System.Threading.Tasks +open Ionide.KeepAChangelog.Tasks.Test open Microsoft.VisualStudio.TestTools.UnitTesting open BlackFox.CommandLine open Faqt open SimpleExec open Workspace +open Helpers module Utils = let packAndGetPackageProperties projectName = @@ -40,11 +41,6 @@ module Utils = workingDirectory = Workspace.fixtures.``.`` ) -type StringHelper = - [] - static member ReplaceEscapedNewLines(s: string) = - s.ReplaceLineEndings().Replace("\\r\\n", "\\n") - [] type IntegrationTests() = @@ -90,9 +86,8 @@ type IntegrationTests() = let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName stdout - .ReplaceEscapedNewLines() .Should() - .Be( + .BeLineEndingEquivalent( """{ "Properties": { "Version": "0.1.0", @@ -115,9 +110,8 @@ type IntegrationTests() = let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName stdout - .ReplaceEscapedNewLines() .Should() - .Be( + .BeLineEndingEquivalent( """{ "Properties": { "Version": "0.1.0", @@ -140,9 +134,8 @@ type IntegrationTests() = let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName stdout - .ReplaceEscapedNewLines() .Should() - .Be( + .BeLineEndingEquivalent( """{ "Properties": { "Version": "1.0.0", @@ -165,9 +158,8 @@ type IntegrationTests() = let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName stdout - .ReplaceEscapedNewLines() .Should() - .Be( + .BeLineEndingEquivalent( """{ "Properties": { "Version": "1.0.0", diff --git a/tests/Ionide.KeepAChangelog.Tasks.Test.fsproj b/tests/Ionide.KeepAChangelog.Tasks.Test.fsproj index 311c781..bc17ce0 100644 --- a/tests/Ionide.KeepAChangelog.Tasks.Test.fsproj +++ b/tests/Ionide.KeepAChangelog.Tasks.Test.fsproj @@ -7,6 +7,7 @@ + diff --git a/tests/UnitTests.fs b/tests/UnitTests.fs index b66825b..43a096e 100644 --- a/tests/UnitTests.fs +++ b/tests/UnitTests.fs @@ -1,5 +1,6 @@ module Tests.UnitTests +open Ionide.KeepAChangelog.Tasks.Test open Moq open Microsoft.Build.Framework open Ionide.KeepAChangelog.Tasks @@ -7,6 +8,7 @@ open Faqt open Faqt.Operators open Microsoft.VisualStudio.TestTools.UnitTesting open Workspace +open Helpers type TestContext = { BuildEngine: Mock @@ -121,7 +123,7 @@ type UnitTests() = %myTask.LatestReleaseNotes .Should() - .Be( + .BeLineEndingEquivalent( """### Added - Created the package