Pentole/tests/path_tests.fs

84 lines
2.4 KiB
Forth
Raw Normal View History

2024-08-29 11:35:03 +02:00
module Tests.Path
open NUnit.Framework
2024-12-07 12:17:48 +01:00
open Pentole
2024-12-12 11:37:59 +01:00
open Pentole.Assert
2024-08-29 11:35:03 +02:00
open Pentole.Path
2024-09-05 15:02:32 +02:00
[<Test>]
let constructor_test () =
2024-12-12 10:37:42 +01:00
"/" |> Path.of_string |> Result.isOk |> isTrue
'\000' |> string |> Path.of_string |> Result.isOk |> isFalse
2024-12-07 13:55:55 +01:00
2024-08-29 11:35:03 +02:00
[<Test>]
let absolute_test () =
2024-12-07 13:55:55 +01:00
let is_absolute (p: IPath) =
p.string_value
|> System.IO.Path.IsPathFullyQualified
2024-08-29 11:35:03 +02:00
let test (s:string) =
s
|> Path.of_string
|> Result.map is_absolute
2024-12-12 10:37:42 +01:00
"/" |> 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
2024-08-29 11:35:03 +02:00
[<Test>]
let resolve_test () =
let test (s:string) =
s
|> Path.of_string
2024-12-07 11:56:29 +01:00
|> Result.map FileSystem.resolve
2024-12-07 12:17:48 +01:00
|> Result.Unsafe.get
let p (n: string ) = Path.of_string n |> Result.Unsafe.get
2024-08-29 11:35:03 +02:00
2024-12-12 10:37:42 +01:00
"/" |> test |> okEquals (p "/")
"/etc/../" |> test |> okEquals (p "/")
"/etc/../etc" |> test |> okEquals (p "/etc")
2024-08-29 11:35:03 +02:00
[<Test>]
let equality_test () =
2024-12-07 12:17:48 +01:00
let p (n: string ) = Path.of_string n |> Result.Unsafe.get
2024-12-12 10:37:42 +01:00
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/")
2024-12-07 13:55:55 +01:00
2024-08-29 11:35:03 +02:00
[<Test>]
let parent_test () =
2024-12-07 13:55:55 +01:00
let p (n: string ) = Path.of_string n |> Result.Unsafe.get
2024-08-29 11:35:03 +02:00
let test (s:string) =
s
|> Path.of_string
2024-12-07 13:55:55 +01:00
|> Result.Unsafe.get
2024-08-29 11:35:03 +02:00
|> parent
2024-12-12 10:37:42 +01:00
"/" |> 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/../")
2024-12-07 14:27:39 +01:00
[<Test>]
let exitsts_test () =
let p (n: string ) = Path.of_string n |> Result.Unsafe.get
2024-12-12 10:37:42 +01:00
FileSystem.exists (p "/tmp/") |> isTrue
FileSystem.exists (p "/IHOPE_this_DOESNT_exist/") |> isFalse