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 ->
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

View file

@ -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<bool, 'a>) =
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<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) =
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)

View file

@ -8,23 +8,23 @@ open Pentole.BinaryPrefix.Bytes
[<Test>]
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)
[<Test>]
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

View file

@ -8,8 +8,8 @@ open Pentole.Path
[<Test>]
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
[<Test>]
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
[<Test>]
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")
[<Test>]
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/")
[<Test>]
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/../")
[<Test>]
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

View file

@ -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

View file

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