Pentole/tests/path_tests.fs
2024-12-12 10:37:42 +01:00

83 lines
2.4 KiB
Forth

module Tests.Path
open NUnit.Framework
open Pentole
open Pentole.TestsExtensions
open Pentole.Path
[<Test>]
let constructor_test () =
"/" |> Path.of_string |> Result.isOk |> isTrue
'\000' |> string |> Path.of_string |> Result.isOk |> isFalse
[<Test>]
let absolute_test () =
let is_absolute (p: IPath) =
p.string_value
|> System.IO.Path.IsPathFullyQualified
let test (s:string) =
s
|> Path.of_string
|> Result.map is_absolute
"/" |> 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 () =
let test (s:string) =
s
|> Path.of_string
|> Result.map FileSystem.resolve
|> Result.Unsafe.get
let p (n: string ) = Path.of_string n |> Result.Unsafe.get
"/" |> 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
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 () =
let p (n: string ) = Path.of_string n |> Result.Unsafe.get
let test (s:string) =
s
|> Path.of_string
|> Result.Unsafe.get
|> parent
"/" |> 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/") |> isTrue
FileSystem.exists (p "/IHOPE_this_DOESNT_exist/") |> isFalse