Skip to content

Commit

Permalink
Create line ending assertion helper for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tboby committed Oct 25, 2024
1 parent 0d6c9ff commit 2a952a0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
32 changes: 32 additions & 0 deletions tests/Helpers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Ionide.KeepAChangelog.Tasks.Test.Helpers

open System.Runtime.CompilerServices
open Faqt
open Faqt.AssertionHelpers

[<Extension>]
type Assertions =

/// Asserts that the subject is equal to the specified string when CLRF is replaced with LF in both raw and
/// escaped forms.
[<Extension>]
static member BeLineEndingEquivalent(t: Testable<string>, expected: string, ?because) : And<string> =
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)
20 changes: 6 additions & 14 deletions tests/IntegrationTests.fs
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down Expand Up @@ -40,11 +41,6 @@ module Utils =
workingDirectory = Workspace.fixtures.``.``
)

type StringHelper =
[<Extension>]
static member ReplaceEscapedNewLines(s: string) =
s.ReplaceLineEndings().Replace("\\r\\n", "\\n")

[<TestClass>]
type IntegrationTests() =

Expand Down Expand Up @@ -90,9 +86,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "0.1.0",
Expand All @@ -115,9 +110,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "0.1.0",
Expand All @@ -140,9 +134,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "1.0.0",
Expand All @@ -165,9 +158,8 @@ type IntegrationTests() =
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName

stdout
.ReplaceEscapedNewLines()
.Should()
.Be(
.BeLineEndingEquivalent(
"""{
"Properties": {
"Version": "1.0.0",
Expand Down
1 change: 1 addition & 0 deletions tests/Ionide.KeepAChangelog.Tasks.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<Compile Include="Workspace.fs" />
<Compile Include="Helpers.fs" />
<Compile Include="UnitTests.fs" />
<Compile Include="IntegrationTests.fs" />
</ItemGroup>
Expand Down
4 changes: 3 additions & 1 deletion tests/UnitTests.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module Tests.UnitTests

open Ionide.KeepAChangelog.Tasks.Test
open Moq
open Microsoft.Build.Framework
open Ionide.KeepAChangelog.Tasks
open Faqt
open Faqt.Operators
open Microsoft.VisualStudio.TestTools.UnitTesting
open Workspace
open Helpers

type TestContext = {
BuildEngine: Mock<IBuildEngine>
Expand Down Expand Up @@ -121,7 +123,7 @@ type UnitTests() =

%myTask.LatestReleaseNotes
.Should()
.Be(
.BeLineEndingEquivalent(
"""### Added
- Created the package
Expand Down

0 comments on commit 2a952a0

Please sign in to comment.