From b069be6af0e2a33322ad8054508f81367815d068 Mon Sep 17 00:00:00 2001 From: Francesco Mecca Date: Thu, 12 Dec 2024 10:37:42 +0100 Subject: [PATCH] f# conventions --- Pentole/result.fs | 2 +- Pentole/test_extensions.fs | 60 ++++++++++++++++++++---------------- tests/bytes_tests.fs | 28 ++++++++--------- tests/path_tests.fs | 62 +++++++++++++++++++------------------- tests/result_tests.fs | 2 +- tests/string_tests.fs | 2 +- 6 files changed, 82 insertions(+), 74 deletions(-) diff --git a/Pentole/result.fs b/Pentole/result.fs index eb68969..a40b2f9 100644 --- a/Pentole/result.fs +++ b/Pentole/result.fs @@ -13,7 +13,7 @@ module Result = | Ok o -> match fun_ y with | Ok o' -> Ok (o, o') | Error e -> Error e - let fromOption = function | Some s -> Ok s | None -> Error () + let ofOption = function | Some s -> Ok s | None -> Error () let zip a b = match (a, b) with diff --git a/Pentole/test_extensions.fs b/Pentole/test_extensions.fs index b7eae5a..8d195da 100644 --- a/Pentole/test_extensions.fs +++ b/Pentole/test_extensions.fs @@ -1,39 +1,47 @@ -module Pentole.TestsExtensions +namespace Pentole open NUnit.Framework -type Assert with - - static member inline ok_is_equal<'ok, 'err> (expected: 'ok) (got: Result<'ok, 'err>) = +module TestsExtensions = + let inline okEquals<'ok, 'err> (expected: 'ok) (got: Result<'ok, 'err>) = match got with | Ok got -> Assert.AreEqual (expected, got) - | Error e -> Assert.Fail $"Expected 'ok, got: {e}" + | Error e -> Assert.Fail $"Expected Ok {expected}, got: {e}" - static member inline is_true (got: bool) = - Assert.IsTrue got + let inline someEquals<'some> (expected: 'some) (got: Option<'some>) = + match got with + | Some got -> Assert.AreEqual (expected, got) + | None -> Assert.Fail $"Expected Some {expected}, got: None" - static member inline is_false (got: bool) = - Assert.IsFalse got + let inline isTrue (got: bool) = + Assert.IsTrue got - static member inline ok_is_true (r: Result) = - match r with - | Ok got -> Assert.is_true got - | Error e -> Assert.Fail $"Expected truth value, got: {e}" + let inline isFalse (got: bool) = + Assert.IsFalse got - static member inline ok_is_false (r: Result) = - match r with - | Ok got -> Assert.is_false got - | Error e -> Assert.Fail $"Expected truth value, got: {e}" - - static member inline are_equal expected (got: 'a) = + let inline areEqual expected (got: 'a) = Assert.AreEqual (expected, got) - static member inline are_seq_equal (expected: 'a seq) (got: 'a seq) = - let s_exp = Seq.map id expected - let g_exp = Seq.map id got - CollectionAssert.AreEqual (s_exp, g_exp) - - static member inline are_not_equal expected (got: 'a) = + let inline areNotEqual expected (got: 'a) = Assert.AreNotEqual (expected, got) - static member inline fail = Assert.Fail + let inline seqEqual (expected: 'a seq) (got: 'a seq) = + CollectionAssert.AreEqual (expected, got) + + let inline seqNotEqual (expected: 'a seq) (got: 'a seq) = + CollectionAssert.AreNotEqual (expected, got) + + let inline setEqual (expected: 'a seq) (got: 'a seq) = + CollectionAssert.AreEquivalent (expected, got) + + let inline setNotEqual (expected: 'a seq) (got: 'a seq) = + CollectionAssert.AreNotEquivalent (expected, got) + + let inline seqHasNoNull (got: 'a seq) = + CollectionAssert.AllItemsAreNotNull got + + let inline seqContains (needle: 'a) (haystack: 'a seq) = + CollectionAssert.Contains (needle, haystack) + + let inline seqNotContains (needle: 'a) (haystack: 'a seq) = + CollectionAssert.DoesNotContain (needle, haystack) diff --git a/tests/bytes_tests.fs b/tests/bytes_tests.fs index aeeca3b..fe982e8 100644 --- a/tests/bytes_tests.fs +++ b/tests/bytes_tests.fs @@ -8,23 +8,23 @@ open Pentole.BinaryPrefix.Bytes [] let equality_test () = - 1.0.KB |> Assert.are_equal (1000.0.bytes) - 1.0.KiB.bytes |> Assert.are_equal (1024.0.bytes.bytes) + 1.0.KB |> areEqual (1000.0.bytes) + 1.0.KiB.bytes |> areEqual (1024.0.bytes.bytes) [] let ``Bit.bytes returns correct values`` () = - Assert.are_equal 0.125 1.0.bits.bytes - Assert.are_equal 125.0 1.0.Kb.bytes - Assert.are_equal 125000.0 1.0.Mb.bytes - Assert.are_equal 125000000.0 1.0.Gb.bytes - Assert.are_equal 125000000000.0 1.0.Tb.bytes - Assert.are_equal 125000000000000.0 1.0.Pb.bytes + areEqual 0.125 1.0.bits.bytes + areEqual 125.0 1.0.Kb.bytes + areEqual 125000.0 1.0.Mb.bytes + areEqual 125000000.0 1.0.Gb.bytes + areEqual 125000000000.0 1.0.Tb.bytes + areEqual 125000000000000.0 1.0.Pb.bytes - Assert.are_equal 0.250 2.0.bits.bytes - Assert.are_equal 250.0 2.0.Kb.bytes - Assert.are_equal 250000.0 2.0.Mb.bytes - Assert.are_equal 500000000.0 4.0.Gb.bytes - Assert.are_equal 500000000000.0 4.0.Tb.bytes - Assert.are_equal 500000000000000.0 4.0.Pb.bytes + areEqual 0.250 2.0.bits.bytes + areEqual 250.0 2.0.Kb.bytes + areEqual 250000.0 2.0.Mb.bytes + areEqual 500000000.0 4.0.Gb.bytes + areEqual 500000000000.0 4.0.Tb.bytes + areEqual 500000000000000.0 4.0.Pb.bytes diff --git a/tests/path_tests.fs b/tests/path_tests.fs index 7a7c67b..a96654f 100644 --- a/tests/path_tests.fs +++ b/tests/path_tests.fs @@ -8,8 +8,8 @@ open Pentole.Path [] let constructor_test () = - "/" |> Path.of_string |> Result.isOk |> Assert.is_true - '\000' |> string |> Path.of_string |> Result.isOk |> Assert.is_false + "/" |> Path.of_string |> Result.isOk |> isTrue + '\000' |> string |> Path.of_string |> Result.isOk |> isFalse [] let absolute_test () = @@ -24,20 +24,20 @@ let absolute_test () = |> Path.of_string |> Result.map is_absolute - "/" |> test |> Assert.ok_is_true - "//" |> test |> Assert.ok_is_true - "tmp/" |> test |> Assert.ok_is_false - "tmp" |> test |> Assert.ok_is_false - "~" |> test |> Assert.ok_is_false - "." |> test |> Assert.ok_is_false - ".." |> test |> Assert.ok_is_false - "../" |> test |> Assert.ok_is_false - "../../" |> test |> Assert.ok_is_false - "..a" |> test |> Assert.ok_is_false - ":a" |> test |> Assert.ok_is_false - "\\a" |> test |> Assert.ok_is_false - "/etc/../" |> test |> Assert.ok_is_true - "/tmp/Char:é" |> test |> Assert.ok_is_true + "/" |> test |> okEquals true + "//" |> test |> okEquals true + "tmp/" |> test |> okEquals false + "tmp" |> test |> okEquals false + "~" |> test |> okEquals false + "." |> test |> okEquals false + ".." |> test |> okEquals false + "../" |> test |> okEquals false + "../../" |> test |> okEquals false + "..a" |> test |> okEquals false + ":a" |> test |> okEquals false + "\\a" |> test |> okEquals false + "/etc/../" |> test |> okEquals true + "/tmp/Char:é" |> test |> okEquals true [] let resolve_test () = @@ -48,18 +48,18 @@ let resolve_test () = |> Result.Unsafe.get let p (n: string ) = Path.of_string n |> Result.Unsafe.get - "/" |> test |> Assert.ok_is_equal (p "/") - "/etc/../" |> test |> Assert.ok_is_equal (p "/") - "/etc/../etc" |> test |> Assert.ok_is_equal (p "/etc") + "/" |> test |> okEquals (p "/") + "/etc/../" |> test |> okEquals (p "/") + "/etc/../etc" |> test |> okEquals (p "/etc") [] let equality_test () = let p (n: string ) = Path.of_string n |> Result.Unsafe.get - Assert.are_equal (p "/etc") (p "/etc/") - Assert.are_not_equal (p "etc") (p "/etc/") - Assert.are_not_equal (p "etc") (p "/etc") - Assert.are_equal (Path.of_string "/tmp" ) ( Path.of_string "/tmp/") - Assert.is_true (Path.of_string "/tmp" = Path.of_string "/tmp/") + areEqual (p "/etc") (p "/etc/") + areNotEqual (p "etc") (p "/etc/") + areNotEqual (p "etc") (p "/etc") + areEqual (Path.of_string "/tmp" ) ( Path.of_string "/tmp/") + isTrue (Path.of_string "/tmp" = Path.of_string "/tmp/") [] let parent_test () = @@ -70,14 +70,14 @@ let parent_test () = |> Result.Unsafe.get |> parent - "/" |> test |> Assert.are_equal (p "/") - "/etc/" |> test |> Assert.are_equal (p "/") - "/etc/conf" |> test |> Assert.are_equal (p "/etc") - "/etc/../etc" |> test |> Assert.are_equal (p "/etc/../") - "etc/../etc" |> test |> Assert.are_equal (p "etc/../") + "/" |> test |> areEqual (p "/") + "/etc/" |> test |> areEqual (p "/") + "/etc/conf" |> test |> areEqual (p "/etc") + "/etc/../etc" |> test |> areEqual (p "/etc/../") + "etc/../etc" |> test |> areEqual (p "etc/../") [] let exitsts_test () = let p (n: string ) = Path.of_string n |> Result.Unsafe.get - FileSystem.exists (p "/tmp/") |> Assert.is_true - FileSystem.exists (p "/IHOPE_this_DOESNT_exist/") |> Assert.is_false + FileSystem.exists (p "/tmp/") |> isTrue + FileSystem.exists (p "/IHOPE_this_DOESNT_exist/") |> isFalse diff --git a/tests/result_tests.fs b/tests/result_tests.fs index d0655d8..11a87b5 100644 --- a/tests/result_tests.fs +++ b/tests/result_tests.fs @@ -13,4 +13,4 @@ let err_test () = match got with | Ok _ -> Assert.Fail "Expected an error" - | Error exn -> Assert.are_equal exn.Message msg + | Error exn -> areEqual exn.Message msg diff --git a/tests/string_tests.fs b/tests/string_tests.fs index 97e7617..1647b1d 100644 --- a/tests/string_tests.fs +++ b/tests/string_tests.fs @@ -8,4 +8,4 @@ open Pentole [] let split_test () = let target = "a/b/c" - Assert.are_seq_equal (target.Split("/")) (String.split "/" target) + seqEqual (target.Split("/")) (String.split "/" target)