Skip to content

Commit

Permalink
Add Suspect utils #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Jun 21, 2024
1 parent 0db8556 commit 75176fb
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/Pyxpecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ open TranspilerHelper
module Assert =
module NET =

let private firstDiff s1 s2 =
let firstDiff s1 s2 =
let s1 = Seq.append (Seq.map Some s1) (Seq.initInfinite (fun _ -> None))
let s2 = Seq.append (Seq.map Some s2) (Seq.initInfinite (fun _ -> None))
Seq.mapi2 (fun i s p -> i,s,p) s1 s2
Expand Down Expand Up @@ -491,6 +491,37 @@ module Expect =
let floatGreaterThanOrClose accuracy actual expected message =
if actual<expected then floatClose accuracy actual expected message


/// This module contains assertion helpers which are Fable.Pyxpecto specific. They cannot be used in combination with Mocha and Expecto.
[<RequireQualifiedAccess>]
module Suspect =

/// <summary>
/// This function only verifies non-whitespace characters
/// </summary>
let stringEqual actual expected message =
let pattern = @"\s+"
let regex = System.Text.RegularExpressions.Regex(pattern, Text.RegularExpressions.RegexOptions.Singleline)
let actual = regex.Replace(actual, "")
let expected = regex.Replace(expected, "")
Expect.equal actual expected message

/// Expects the `actual` sequence to equal the `expected` one.
let sequenceEqual actual expected message =
match Assert.NET.firstDiff actual expected with
| _,None,None -> ()
| i,Some a, Some e ->
failtestf "%s. Sequence does not match at position %i. Expected item: %O, but got %O."
message i e a
| i,None,Some e ->
failtestf "%s. Sequence actual shorter than expected, at pos %i for expected item %O."
message i e
| i,Some a,None ->
failtestf "%s. Sequence actual longer than expected, at pos %i found item %O."
message i a

let fail message = failtestf "Test failed! %s" message

type Language =
| TypeScript
| Python
Expand Down

0 comments on commit 75176fb

Please sign in to comment.