Skip to content

Commit

Permalink
Merge pull request #121 from krymtkts:feature/refactor
Browse files Browse the repository at this point in the history
Refactor and add tests for Pocof functions.
  • Loading branch information
krymtkts authored Jan 13, 2024
2 parents ad4acae + db3e5c6 commit e10e5ec
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 92 deletions.
265 changes: 217 additions & 48 deletions src/pocof.Test/Pocof.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,55 @@ open pocof.Pocof
open pocof.PocofData
open pocof.PocofScreen
open PocofUI
open System.Management.Automation

let toObj x = x |> (PSObject.AsPSObject >> Obj)

let mapToObj x = x |> List.map toObj

let initState () : InternalState =
{ QueryState =
{ Query = ""
Cursor = 0
WindowBeginningCursor = 0
WindowWidth = 0 }
QueryCondition =
{ Matcher = MATCH
Operator = OR
CaseSensitive = false
Invert = false }
PropertySearch = NoSearch
Notification = ""
SuppressProperties = false
Properties = [ "Name"; "LastModified"; "Path" ]
Prompt = "query"
FilteredCount = 0
ConsoleWidth = 60
Refresh = Required }

let state = initState ()
let writeScreen _ _ _ = ()

let pos = { Y = 0; Height = 0 } // NOTE: not used in this test.

let propMap = Map.empty

let results = [ "a"; "b"; "c"; "d"; "e" ] |> List.map box

type MockGetKey(keys: ConsoleKeyInfo list list) =
let mutable keys = keys

member __.getKey() =
match keys with
| [] -> failwith "no keys remains. probably test is broken."
| x :: xs ->
keys <- xs
x

member __.check() =
match keys with
| [] -> ()
| _ -> failwith "keys remains. probably test is broken."

module calculateWindowBeginningCursor =
[<Fact>]
Expand Down Expand Up @@ -55,54 +104,6 @@ module calculateWindowBeginningCursor =
actual |> shouldEqual 0

module loop =
open System.Management.Automation

let initState () : InternalState =
{ QueryState =
{ Query = ""
Cursor = 0
WindowBeginningCursor = 0
WindowWidth = 0 }
QueryCondition =
{ Matcher = MATCH
Operator = OR
CaseSensitive = false
Invert = false }
PropertySearch = NoSearch
Notification = ""
SuppressProperties = false
Properties = [ "Name"; "LastModified"; "Path" ]
Prompt = "query"
FilteredCount = 0
ConsoleWidth = 60
Refresh = Required }

let state = initState ()
let writeScreen _ _ _ = ()

let pos = { Y = 0; Height = 0 } // NOTE: not used in this test.

let propMap = Map.empty

let toObj = PSObject.AsPSObject >> Obj

let results = [ "a"; "b"; "c"; "d"; "e" ] |> List.map box

type MockGetKey(keys: ConsoleKeyInfo list list) =
let mutable keys = keys

member __.getKey() =
match keys with
| [] -> failwith "no keys remains. probably test is broken."
| x :: xs ->
keys <- xs
x

member __.check() =
match keys with
| [] -> ()
| _ -> failwith "keys remains. probably test is broken."

[<Fact>]
let ``should return result when finishing.`` () =
let input = results |> List.map toObj
Expand Down Expand Up @@ -237,3 +238,171 @@ module loop =
:: (generateLine 80 (rui.height - 1))

rui.screen |> shouldEqual expected

module interact =
type MockGetKey(keys: ConsoleKeyInfo list list) =
let mutable keys = keys

member __.getKey() =
match keys with
| [] -> failwith "no keys remains. probably test is broken."
| x :: xs ->
keys <- xs
x

member __.check() =
match keys with
| [] -> ()
| _ -> failwith "keys remains. probably test is broken."

[<Fact>]
let ``should return result with NonInteractive mode.`` () =
let config: InternalConfig =
{ NotInteractive = true
Layout = TopDown
Keymaps = pocof.PocofAction.defaultKeymap }

let input = results |> List.map toObj
let pos = { Y = 0; Height = 0 }
let rui = new MockRawUI()

let actual = interact config state pos (fun () -> rui) (fun _ -> Seq.empty) input
actual |> List.length |> shouldEqual 5

let expected =
[ "a"; "b"; "c"; "d"; "e" ]
|> List.map (PSObject.AsPSObject >> box)

actual |> shouldEqual expected

[<Fact>]
let ``should return result when interaction finished in Interactive mode and TopDown Layout.`` () =
let config: InternalConfig =
{ NotInteractive = false
Layout = TopDown
Keymaps = pocof.PocofAction.defaultKeymap }

let input = results |> List.map toObj
let pos = { Y = 0; Height = 0 }
let rui = new MockRawUI()

let actual = interact config state pos (fun () -> rui) (fun _ -> Seq.empty) input
actual |> List.length |> shouldEqual 5

let expected =
[ "a"; "b"; "c"; "d"; "e" ]
|> List.map (PSObject.AsPSObject >> box)

actual |> shouldEqual expected

[<Fact>]
let ``should return result when interaction finished in Interactive mode and BottomUp Layout.`` () =
let config: InternalConfig =
{ NotInteractive = false
Layout = BottomUp
Keymaps = pocof.PocofAction.defaultKeymap }

let input = results |> List.map toObj
let pos = { Y = 0; Height = 0 }
let rui = new MockRawUI()

let actual = interact config state pos (fun () -> rui) (fun _ -> Seq.empty) input
actual |> List.length |> shouldEqual 5

let expected =
[ "a"; "b"; "c"; "d"; "e" ]
|> List.map (PSObject.AsPSObject >> box)

actual |> shouldEqual expected

module buildInput =
open System.Collections

let mapToObj x =
x |> List.map (PSObject.AsPSObject >> Obj)

[<Fact>]
let ``should return the list with added Obj`` () =
let expected = [ 3; 2; 1 ] |> mapToObj

buildInput
[]
([ 1; 2; 3 ]
|> List.map PSObject.AsPSObject
|> Array.ofList)
|> shouldEqual expected

[<Fact>]
let ``should return the list with added Obj to head.`` () =
let expected = [ 3; 2; 1; 0 ] |> mapToObj

let input = [ 0 ] |> mapToObj

buildInput
input
([ 1; 2; 3 ]
|> List.map PSObject.AsPSObject
|> Array.ofList)
|> shouldEqual expected

[<Fact>]
let ``should return the list with added Dict`` () =
let expected =
[ DictionaryEntry("c", 3)
DictionaryEntry("b", 2)
DictionaryEntry("a", 1) ]
|> List.map Dict

let inputObject =
let h = new OrderedHashtable()
h.Add("a", 1)
h.Add("b", 2)
h.Add("c", 3)
[| h |> PSObject.AsPSObject |]

buildInput [] inputObject |> shouldEqual expected

module buildProperties =
[<Fact>]
let ``should return the set with added input properties.`` () =
let expected = set [ "a"; "b"; "c" ]
let input = set []

let inputObject =
let o = new PSObject()
o.Properties.Add(new PSNoteProperty("a", 1))
o.Properties.Add(new PSNoteProperty("b", 2))
o.Properties.Add(new PSNoteProperty("c", 3))
[| o |]

buildProperties input inputObject
|> shouldEqual expected

[<Fact>]
let ``should return the set with added input properties without duplication.`` () =
let expected = set [ "a"; "b"; "c" ]
let input = set [ "a"; "c" ]

let inputObject =
let o = new PSObject()
o.Properties.Add(new PSNoteProperty("a", 1))
o.Properties.Add(new PSNoteProperty("b", 2))
o.Properties.Add(new PSNoteProperty("c", 3))
[| o |]

buildProperties input inputObject
|> shouldEqual expected

[<Fact>]
let ``should return the set with added the keys of hashtable.`` () =
let expected = [ "Key"; "Value" ] |> Set.ofList

let input = set []

let inputObject =
let h = new OrderedHashtable()
h.Add("a", 1)
[| h |> PSObject.AsPSObject |]

buildProperties input inputObject
|> shouldEqual expected
Loading

0 comments on commit e10e5ec

Please sign in to comment.