From 75176fb4d134803f50849ce865d4ed3f064a5eea Mon Sep 17 00:00:00 2001 From: Kevin F Date: Fri, 21 Jun 2024 21:05:39 +0200 Subject: [PATCH] Add Suspect utils #9 --- src/Pyxpecto.fs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Pyxpecto.fs b/src/Pyxpecto.fs index 7c0de5b..3c18849 100644 --- a/src/Pyxpecto.fs +++ b/src/Pyxpecto.fs @@ -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 @@ -491,6 +491,37 @@ module Expect = let floatGreaterThanOrClose accuracy actual expected message = if actual] +module Suspect = + + /// + /// This function only verifies non-whitespace characters + /// + 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