Skip to content

Commit

Permalink
Merge pull request #96 from krymtkts:feature/refactor
Browse files Browse the repository at this point in the history
Refactor code and fix naming inconsistencies
  • Loading branch information
krymtkts authored Dec 23, 2023
2 parents 98f06af + 8d873d6 commit aaf9832
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 71 deletions.
34 changes: 15 additions & 19 deletions src/pocof.Test/PocofData.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ module ``Action fromString should returns`` =
String.lower a.Name
String.upper a.Name ]
|> List.map Action.fromString
|> List.iter (fun s ->
s
|> shouldEqual (Ok(FSharpValue.MakeUnion(a, [||]) :?> Action))))
|> List.iter (shouldEqual (Ok(FSharpValue.MakeUnion(a, [||]) :?> Action))))

let ``Error Unknown.``<'a> (fromString: string -> 'a) =
shouldFail (fun () -> fromString "Unknown" |> ignore)
Expand All @@ -41,9 +39,7 @@ let ``known matchers.``<'a> (fromString: string -> 'a) =
String.lower a.Name
String.upper a.Name ]
|> List.map fromString
|> List.iter (fun s ->
s
|> shouldEqual (FSharpValue.MakeUnion(a, [||]) :?> 'a)))
|> List.iter (shouldEqual (FSharpValue.MakeUnion(a, [||]) :?> 'a)))

module ``Matcher fromString should returns`` =
[<Fact>]
Expand Down Expand Up @@ -229,7 +225,7 @@ module invokeAction =
Operator = OR
CaseSensitive = false
Invert = false }
PropertySearch = NonSearch
PropertySearch = NoSearch
Notification = ""
SuppressProperties = false }

Expand Down Expand Up @@ -259,7 +255,7 @@ module invokeAction =
|> shouldEqual (
{ state with
Query = ":name "
PropertySearch = NonSearch },
PropertySearch = NoSearch },
{ X = 6; Y = 0 },
Required
)
Expand All @@ -270,7 +266,7 @@ module invokeAction =
let state =
{ state with
Query = ":name"
PropertySearch = NonSearch }
PropertySearch = NoSearch }

let position: Position = { X = 0; Y = 0 }

Expand Down Expand Up @@ -344,7 +340,7 @@ module invokeAction =
|> shouldEqual (
{ state with
Query = ":name"
PropertySearch = NonSearch },
PropertySearch = NoSearch },
{ X = 0; Y = 0 },
Required
)
Expand All @@ -361,7 +357,7 @@ module invokeAction =
|> shouldEqual (
{ state with
Query = ":name"
PropertySearch = NonSearch },
PropertySearch = NoSearch },
{ X = 0; Y = 0 },
NotRequired
)
Expand All @@ -371,14 +367,14 @@ module invokeAction =
invokeAction
{ state with
Query = "query"
PropertySearch = NonSearch }
PropertySearch = NoSearch }
{ X = 5; Y = 0 }
[]
BeginningOfLine
|> (shouldEqual (
{ state with
Query = "query"
PropertySearch = NonSearch },
PropertySearch = NoSearch },
{ X = 0; Y = 0 },
NotRequired
))
Expand Down Expand Up @@ -423,14 +419,14 @@ module invokeAction =
invokeAction
{ state with
Query = "query"
PropertySearch = NonSearch }
PropertySearch = NoSearch }
{ X = 0; Y = 0 }
[]
EndOfLine
|> shouldEqual (
{ state with
Query = "query"
PropertySearch = NonSearch },
PropertySearch = NoSearch },
{ X = 5; Y = 0 },
NotRequired
)
Expand All @@ -441,7 +437,7 @@ module invokeAction =
invokeAction
{ state with
Query = ":name "
PropertySearch = NonSearch }
PropertySearch = NoSearch }
{ X = 6; Y = 0 }
[]
DeleteBackwardChar
Expand All @@ -458,14 +454,14 @@ module invokeAction =
invokeAction
{ state with
Query = ":name"
PropertySearch = NonSearch }
PropertySearch = NoSearch }
{ X = 0; Y = 0 }
[]
DeleteBackwardChar
|> shouldEqual (
{ state with
Query = ":name"
PropertySearch = NonSearch },
PropertySearch = NoSearch },
{ X = 0; Y = 0 },
NotRequired
)
Expand All @@ -483,7 +479,7 @@ module invokeAction =
|> shouldEqual (
{ state with
Query = "name "
PropertySearch = NonSearch },
PropertySearch = NoSearch },
{ X = 0; Y = 0 },
Required
)
Expand Down
6 changes: 3 additions & 3 deletions src/pocof.Test/PocofQuery.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let initState () : PocofData.InternalState =
Operator = PocofData.Operator.OR
CaseSensitive = false
Invert = false }
PropertySearch = PocofData.PropertySearch.NonSearch
PropertySearch = PocofData.PropertySearch.NoSearch
Notification = ""
SuppressProperties = false }

Expand All @@ -29,7 +29,7 @@ module props =

[<Fact>]
let ``should returns OK with empty list.`` () =
PocofQuery.props { state with PropertySearch = PocofData.PropertySearch.NonSearch } entries
PocofQuery.props { state with PropertySearch = PocofData.PropertySearch.NoSearch } entries
|> shouldEqual (Ok [])

[<Fact>]
Expand Down Expand Up @@ -224,7 +224,7 @@ module run =
let props = Map []

open System.Collections
let mapToDict = List.map (PocofData.Dict)
let mapToDict = List.map PocofData.Dict

let entries =
mapToDict [ DictionaryEntry("John", "Doe")
Expand Down
4 changes: 2 additions & 2 deletions src/pocof.Test/PocofUI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module ``Buff writeScreen`` =
Operator = AND
CaseSensitive = true
Invert = false }
PropertySearch = NonSearch
PropertySearch = NoSearch
Notification = ""
SuppressProperties = false }

Expand All @@ -78,7 +78,7 @@ module ``Buff writeScreen`` =
Operator = OR
CaseSensitive = false
Invert = true }
PropertySearch = NonSearch
PropertySearch = NoSearch
Notification = ""
SuppressProperties = false }

Expand Down
2 changes: 1 addition & 1 deletion src/pocof/Action.fs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ module PocofAction =
| _ -> None

let toKeyPattern (s: string) =
match s.Split '+' |> List.ofSeq |> List.rev with
match s |> String.split "+" |> List.ofSeq |> List.rev with
| [] -> failwith "Unreachable pass."
| [ k ] ->
match toEnum<ConsoleKey> k with
Expand Down
28 changes: 16 additions & 12 deletions src/pocof/Data.fs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ module LanguageExtension =
static member inline lower(s: string) = s.ToLower()
static member inline upper(s: string) = s.ToUpper()
static member inline startsWith (value: string) (s: string) = s.StartsWith(value)
static member inline split (separator: string) (s: string) = s.Split(separator.ToCharArray())
static member inline equals (opt: StringComparison) (value: string) (s: string) = s.Equals(value, opt)

let inline swap (l, r) = (r, l)
let inline alwaysTrue _ = true

module PocofData =
open System
Expand All @@ -60,13 +65,12 @@ module PocofData =

let unwrap (entries: Entry list) =
entries
|> List.map (fun o ->
match o with
|> List.map (function
| Dict (dct) -> dct :> obj
| Obj (o) -> o)


let private tryFromStringExcludes<'a> (excludes: Set<string>) s =
let inline private tryFromStringExcludes<'a> (excludes: Set<string>) s =
let name = String.lower s

match FSharpType.GetUnionCases typeof<'a>
Expand All @@ -76,7 +80,7 @@ module PocofData =
| Some u -> Ok <| (FSharpValue.MakeUnion(u, [||]) :?> 'a)
| _ -> Error <| sprintf "Unknown case '%s'." s

let private fromString<'a> s =
let inline private fromString<'a> s =
let name = String.lower s

match FSharpType.GetUnionCases typeof<'a>
Expand All @@ -85,7 +89,7 @@ module PocofData =
| Some u -> FSharpValue.MakeUnion(u, [||]) :?> 'a
| _ -> failwithf "Unknown case '%s'." s

let private toString (x: 'a) =
let inline private toString (x: 'a) =
match FSharpValue.GetUnionFields(x, typeof<'a>) with
| case, _ -> case.Name

Expand Down Expand Up @@ -141,7 +145,7 @@ module PocofData =
static member fromString = fromString<Layout>

type PropertySearch =
| NonSearch
| NoSearch
| Search of string
| Rotate of string * int * string list

Expand Down Expand Up @@ -205,11 +209,11 @@ module PocofData =
| _ -> None

let private getCurrentProperty (query: string) (x: int) =
let s = query.[..x].Split [| ' ' |] |> Seq.last
let s = query.[..x] |> String.split " " |> Seq.last

match s with
| Prefix ":" p -> Search <| p
| _ -> NonSearch
| Prefix ":" p -> Search p
| _ -> NoSearch

let initConfig (p: IncomingParameters) =
// TODO: Eliminate the possibility of failure from here.
Expand Down Expand Up @@ -255,9 +259,9 @@ module PocofData =
{ state with PropertySearch = getCurrentProperty state.Query <| p.X - 1 }, p, changed

let private moveHead (state: InternalState) (pos: Position) =
{ state with PropertySearch = NonSearch },
{ state with PropertySearch = NoSearch },
{ pos with X = 0 },
(pos.X <> 0 && state.PropertySearch <> NonSearch)
(pos.X <> 0 && state.PropertySearch <> NoSearch)
|> Refresh.ofBool

let private moveTail (state: InternalState) (pos: Position) =
Expand Down Expand Up @@ -360,7 +364,7 @@ module PocofData =
Required

match state.PropertySearch with
| NonSearch -> state, pos, NotRequired
| NoSearch -> state, pos, NotRequired
| Search keyword ->
let candidate, candidates =
props
Expand Down
Loading

0 comments on commit aaf9832

Please sign in to comment.