f# conventions

This commit is contained in:
Francesco Mecca 2024-12-12 10:37:42 +01:00
parent b116e29fea
commit b069be6af0
6 changed files with 82 additions and 74 deletions

View file

@ -13,7 +13,7 @@ module Result =
| Ok o -> | Ok o ->
match fun_ y with | Ok o' -> Ok (o, o') | Error e -> Error e 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 = let zip a b =
match (a, b) with match (a, b) with

View file

@ -1,39 +1,47 @@
module Pentole.TestsExtensions namespace Pentole
open NUnit.Framework open NUnit.Framework
type Assert with module TestsExtensions =
let inline okEquals<'ok, 'err> (expected: 'ok) (got: Result<'ok, 'err>) =
static member inline ok_is_equal<'ok, 'err> (expected: 'ok) (got: Result<'ok, 'err>) =
match got with match got with
| Ok got -> Assert.AreEqual (expected, got) | 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) = 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"
let inline isTrue (got: bool) =
Assert.IsTrue got Assert.IsTrue got
static member inline is_false (got: bool) = let inline isFalse (got: bool) =
Assert.IsFalse got Assert.IsFalse got
static member inline ok_is_true (r: Result<bool, 'a>) = let inline areEqual expected (got: 'a) =
match r with
| Ok got -> Assert.is_true got
| Error e -> Assert.Fail $"Expected truth value, got: {e}"
static member inline ok_is_false (r: Result<bool, 'e>) =
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) =
Assert.AreEqual (expected, got) Assert.AreEqual (expected, got)
static member inline are_seq_equal (expected: 'a seq) (got: 'a seq) = let inline areNotEqual expected (got: 'a) =
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) =
Assert.AreNotEqual (expected, got) 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)

View file

@ -8,23 +8,23 @@ open Pentole.BinaryPrefix.Bytes
[<Test>] [<Test>]
let equality_test () = let equality_test () =
1.0.KB |> Assert.are_equal (1000.0.bytes) 1.0.KB |> areEqual (1000.0.bytes)
1.0.KiB.bytes |> Assert.are_equal (1024.0.bytes.bytes) 1.0.KiB.bytes |> areEqual (1024.0.bytes.bytes)
[<Test>] [<Test>]
let ``Bit.bytes returns correct values`` () = let ``Bit.bytes returns correct values`` () =
Assert.are_equal 0.125 1.0.bits.bytes areEqual 0.125 1.0.bits.bytes
Assert.are_equal 125.0 1.0.Kb.bytes areEqual 125.0 1.0.Kb.bytes
Assert.are_equal 125000.0 1.0.Mb.bytes areEqual 125000.0 1.0.Mb.bytes
Assert.are_equal 125000000.0 1.0.Gb.bytes areEqual 125000000.0 1.0.Gb.bytes
Assert.are_equal 125000000000.0 1.0.Tb.bytes areEqual 125000000000.0 1.0.Tb.bytes
Assert.are_equal 125000000000000.0 1.0.Pb.bytes areEqual 125000000000000.0 1.0.Pb.bytes
Assert.are_equal 0.250 2.0.bits.bytes areEqual 0.250 2.0.bits.bytes
Assert.are_equal 250.0 2.0.Kb.bytes areEqual 250.0 2.0.Kb.bytes
Assert.are_equal 250000.0 2.0.Mb.bytes areEqual 250000.0 2.0.Mb.bytes
Assert.are_equal 500000000.0 4.0.Gb.bytes areEqual 500000000.0 4.0.Gb.bytes
Assert.are_equal 500000000000.0 4.0.Tb.bytes areEqual 500000000000.0 4.0.Tb.bytes
Assert.are_equal 500000000000000.0 4.0.Pb.bytes areEqual 500000000000000.0 4.0.Pb.bytes

View file

@ -8,8 +8,8 @@ open Pentole.Path
[<Test>] [<Test>]
let constructor_test () = let constructor_test () =
"/" |> Path.of_string |> Result.isOk |> Assert.is_true "/" |> Path.of_string |> Result.isOk |> isTrue
'\000' |> string |> Path.of_string |> Result.isOk |> Assert.is_false '\000' |> string |> Path.of_string |> Result.isOk |> isFalse
[<Test>] [<Test>]
let absolute_test () = let absolute_test () =
@ -24,20 +24,20 @@ let absolute_test () =
|> Path.of_string |> Path.of_string
|> Result.map is_absolute |> Result.map is_absolute
"/" |> test |> Assert.ok_is_true "/" |> test |> okEquals true
"//" |> test |> Assert.ok_is_true "//" |> test |> okEquals true
"tmp/" |> test |> Assert.ok_is_false "tmp/" |> test |> okEquals false
"tmp" |> test |> Assert.ok_is_false "tmp" |> test |> okEquals false
"~" |> test |> Assert.ok_is_false "~" |> test |> okEquals false
"." |> test |> Assert.ok_is_false "." |> test |> okEquals false
".." |> test |> Assert.ok_is_false ".." |> test |> okEquals false
"../" |> test |> Assert.ok_is_false "../" |> test |> okEquals false
"../../" |> test |> Assert.ok_is_false "../../" |> test |> okEquals false
"..a" |> test |> Assert.ok_is_false "..a" |> test |> okEquals false
":a" |> test |> Assert.ok_is_false ":a" |> test |> okEquals false
"\\a" |> test |> Assert.ok_is_false "\\a" |> test |> okEquals false
"/etc/../" |> test |> Assert.ok_is_true "/etc/../" |> test |> okEquals true
"/tmp/Char:é" |> test |> Assert.ok_is_true "/tmp/Char:é" |> test |> okEquals true
[<Test>] [<Test>]
let resolve_test () = let resolve_test () =
@ -48,18 +48,18 @@ let resolve_test () =
|> Result.Unsafe.get |> Result.Unsafe.get
let p (n: string ) = Path.of_string n |> Result.Unsafe.get let p (n: string ) = Path.of_string n |> Result.Unsafe.get
"/" |> test |> Assert.ok_is_equal (p "/") "/" |> test |> okEquals (p "/")
"/etc/../" |> test |> Assert.ok_is_equal (p "/") "/etc/../" |> test |> okEquals (p "/")
"/etc/../etc" |> test |> Assert.ok_is_equal (p "/etc") "/etc/../etc" |> test |> okEquals (p "/etc")
[<Test>] [<Test>]
let equality_test () = let equality_test () =
let p (n: string ) = Path.of_string n |> Result.Unsafe.get let p (n: string ) = Path.of_string n |> Result.Unsafe.get
Assert.are_equal (p "/etc") (p "/etc/") areEqual (p "/etc") (p "/etc/")
Assert.are_not_equal (p "etc") (p "/etc/") areNotEqual (p "etc") (p "/etc/")
Assert.are_not_equal (p "etc") (p "/etc") areNotEqual (p "etc") (p "/etc")
Assert.are_equal (Path.of_string "/tmp" ) ( Path.of_string "/tmp/") areEqual (Path.of_string "/tmp" ) ( Path.of_string "/tmp/")
Assert.is_true (Path.of_string "/tmp" = Path.of_string "/tmp/") isTrue (Path.of_string "/tmp" = Path.of_string "/tmp/")
[<Test>] [<Test>]
let parent_test () = let parent_test () =
@ -70,14 +70,14 @@ let parent_test () =
|> Result.Unsafe.get |> Result.Unsafe.get
|> parent |> parent
"/" |> test |> Assert.are_equal (p "/") "/" |> test |> areEqual (p "/")
"/etc/" |> test |> Assert.are_equal (p "/") "/etc/" |> test |> areEqual (p "/")
"/etc/conf" |> test |> Assert.are_equal (p "/etc") "/etc/conf" |> test |> areEqual (p "/etc")
"/etc/../etc" |> test |> Assert.are_equal (p "/etc/../") "/etc/../etc" |> test |> areEqual (p "/etc/../")
"etc/../etc" |> test |> Assert.are_equal (p "etc/../") "etc/../etc" |> test |> areEqual (p "etc/../")
[<Test>] [<Test>]
let exitsts_test () = let exitsts_test () =
let p (n: string ) = Path.of_string n |> Result.Unsafe.get let p (n: string ) = Path.of_string n |> Result.Unsafe.get
FileSystem.exists (p "/tmp/") |> Assert.is_true FileSystem.exists (p "/tmp/") |> isTrue
FileSystem.exists (p "/IHOPE_this_DOESNT_exist/") |> Assert.is_false FileSystem.exists (p "/IHOPE_this_DOESNT_exist/") |> isFalse

View file

@ -13,4 +13,4 @@ let err_test () =
match got with match got with
| Ok _ -> Assert.Fail "Expected an error" | Ok _ -> Assert.Fail "Expected an error"
| Error exn -> Assert.are_equal exn.Message msg | Error exn -> areEqual exn.Message msg

View file

@ -8,4 +8,4 @@ open Pentole
[<Test>] [<Test>]
let split_test () = let split_test () =
let target = "a/b/c" let target = "a/b/c"
Assert.are_seq_equal (target.Split("/")) (String.split "/" target) seqEqual (target.Split("/")) (String.split "/" target)