Skip to content

Commit 2a952a0

Browse files
committed
Create line ending assertion helper for tests
1 parent 0d6c9ff commit 2a952a0

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

tests/Helpers.fs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Ionide.KeepAChangelog.Tasks.Test.Helpers
2+
3+
open System.Runtime.CompilerServices
4+
open Faqt
5+
open Faqt.AssertionHelpers
6+
7+
[<Extension>]
8+
type Assertions =
9+
10+
/// Asserts that the subject is equal to the specified string when CLRF is replaced with LF in both raw and
11+
/// escaped forms.
12+
[<Extension>]
13+
static member BeLineEndingEquivalent(t: Testable<string>, expected: string, ?because) : And<string> =
14+
use _ = t.Assert()
15+
16+
if isNull expected then
17+
nullArg (nameof expected)
18+
19+
if isNull t.Subject then
20+
t.With("Expected", expected).With("But was", t.Subject).Fail(because)
21+
22+
let expectedNormalised = expected.Replace("\r\n", "\n").Replace("\\r\\n", "\\n")
23+
24+
let subjectNormalised = t.Subject.Replace("\r\n", "\n").Replace("\\r\\n", "\\n")
25+
26+
if subjectNormalised <> expectedNormalised then
27+
t
28+
.With("Expected", expectedNormalised)
29+
.With("But was", subjectNormalised)
30+
.Fail(because)
31+
32+
And(t)

tests/IntegrationTests.fs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
module Tests.IntegrationTests
22

33
open System.IO
4-
open System.Runtime.CompilerServices
54
open System.Threading.Tasks
5+
open Ionide.KeepAChangelog.Tasks.Test
66
open Microsoft.VisualStudio.TestTools.UnitTesting
77
open BlackFox.CommandLine
88
open Faqt
99
open SimpleExec
1010
open Workspace
11+
open Helpers
1112

1213
module Utils =
1314
let packAndGetPackageProperties projectName =
@@ -40,11 +41,6 @@ module Utils =
4041
workingDirectory = Workspace.fixtures.``.``
4142
)
4243

43-
type StringHelper =
44-
[<Extension>]
45-
static member ReplaceEscapedNewLines(s: string) =
46-
s.ReplaceLineEndings().Replace("\\r\\n", "\\n")
47-
4844
[<TestClass>]
4945
type IntegrationTests() =
5046

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

9288
stdout
93-
.ReplaceEscapedNewLines()
9489
.Should()
95-
.Be(
90+
.BeLineEndingEquivalent(
9691
"""{
9792
"Properties": {
9893
"Version": "0.1.0",
@@ -115,9 +110,8 @@ type IntegrationTests() =
115110
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
116111

117112
stdout
118-
.ReplaceEscapedNewLines()
119113
.Should()
120-
.Be(
114+
.BeLineEndingEquivalent(
121115
"""{
122116
"Properties": {
123117
"Version": "0.1.0",
@@ -140,9 +134,8 @@ type IntegrationTests() =
140134
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
141135

142136
stdout
143-
.ReplaceEscapedNewLines()
144137
.Should()
145-
.Be(
138+
.BeLineEndingEquivalent(
146139
"""{
147140
"Properties": {
148141
"Version": "1.0.0",
@@ -165,9 +158,8 @@ type IntegrationTests() =
165158
let! struct (stdout, _) = Utils.packAndGetPackageProperties projectName
166159

167160
stdout
168-
.ReplaceEscapedNewLines()
169161
.Should()
170-
.Be(
162+
.BeLineEndingEquivalent(
171163
"""{
172164
"Properties": {
173165
"Version": "1.0.0",

tests/Ionide.KeepAChangelog.Tasks.Test.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<Compile Include="Workspace.fs" />
10+
<Compile Include="Helpers.fs" />
1011
<Compile Include="UnitTests.fs" />
1112
<Compile Include="IntegrationTests.fs" />
1213
</ItemGroup>

tests/UnitTests.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
module Tests.UnitTests
22

3+
open Ionide.KeepAChangelog.Tasks.Test
34
open Moq
45
open Microsoft.Build.Framework
56
open Ionide.KeepAChangelog.Tasks
67
open Faqt
78
open Faqt.Operators
89
open Microsoft.VisualStudio.TestTools.UnitTesting
910
open Workspace
11+
open Helpers
1012

1113
type TestContext = {
1214
BuildEngine: Mock<IBuildEngine>
@@ -121,7 +123,7 @@ type UnitTests() =
121123

122124
%myTask.LatestReleaseNotes
123125
.Should()
124-
.Be(
126+
.BeLineEndingEquivalent(
125127
"""### Added
126128
127129
- Created the package

0 commit comments

Comments
 (0)