Skip to content

Commit

Permalink
Enhance tests for more specific error output using a custom function …
Browse files Browse the repository at this point in the history
…instead of Unquote; and then fix the test errors in Sword and Dagger test (wrong expectations).
  • Loading branch information
MaxWilson committed Jan 8, 2024
1 parent da6623c commit fef2df6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/Core/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ module String =
| index::rest ->
recur $"{workingCopy[0..(index-1)]} {workingCopy[index..]}" rest
recur str spaceNeededBefore)
/// diff two strings and return similarities and differences. E.g. diff "Happy Hogan 123" "Happy Hogan abc" = ("Happy Hogan ", "123", "abc")
let diff (lhs:string) (rhs:string) =
// you'd think there would be a built-in C# function for this but if there is I have forgotten what it is.
let returnFrom firstDiffIx =
let ix = firstDiffIx
let sub (s:string) = if ix >= s.Length then "" else s.Substring(ix)
lhs.Substring(0, ix), sub lhs, sub rhs
let rec recur jx =
if jx >= (min lhs.Length rhs.Length) || lhs[jx] <> rhs[jx] then returnFrom jx
else recur (jx + 1)
recur 0

type System.Object with
member this.ToUncameledString() = this.ToString() |> String.uncamel
Expand Down
33 changes: 23 additions & 10 deletions test/Chargen.Accept.fs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,15 @@ let parseKey (key: string) : Key =
let evalFor (selections: string list) offers =
let keys = selections |> List.map parseKey |> List.map (fun k -> k, Flag) |> Map.ofList
evaluate { OfferInput.fresh with selected = keys } offers |> snd

let testFor (selections: string list) expected offers =
let actual = evalFor selections offers
if actual <> expected then
let actualS, expectedS = actual |> toString, expected |> toString
let firstDiff = [0..actualS.Length-1]
let same, actual, expected = String.diff actualS expectedS
failtest $"Actual diverged from expected! After: \n{same}\n\nExpected: \n{expected}\nbut got:\n{actual}"

[<Tests>]
let units = testList "Unit.Chargen" [
let key = parseKey
Expand All @@ -308,37 +317,41 @@ let units = testList "Unit.Chargen" [
skill("Shield", +2)
])
]
test <@ nestedEither |> evalFor [] =
nestedEither |> testFor [] (
Either(None, [
false, key "Sword!", Leaf "Sword!"
false, key "Sword and Dagger", Leaf "Sword and Dagger"
false, key "Sword and Shield", Leaf "Sword and Shield"
]) @>
test <@ nestedEither |> evalFor ["Sword!"] =
])
)
nestedEither |> testFor ["Sword!"] (
Either(None, [
true, key "Sword!", Either(Some "Sword!", [
false, key "Sword!-Rapier", Leaf "Rapier +5" // note how Leveled is only Leveled if selected. When unselected it's a Leaf just like anything else.
false, key "Sword!-Broadsword", Leaf "Broadsword +5"
false, key "Sword!-Shortsword", Leaf "Shortsword +5"
])
]) @>
test <@ nestedEither |> evalFor ["Sword!"; "Sword!-Rapier"] =
])
)
nestedEither |> testFor ["Sword!"; "Sword!-Rapier"] (
Either(None, [
true, key "Sword!", Either(Some "Sword!", [
true, key "Sword!-Rapier", Leveled("Rapier +5", 0) // it's a Levelled, not a Leaf, because it's currently selected. Note that the level is 0, not +5, because it's the lowest level out of +5 to +5.
])
]) @>
test <@ nestedEither |> evalFor ["Sword and Dagger"] =
])
)
nestedEither |> testFor ["Sword and Dagger"] (
Either(None, [
true, key "Sword and Dagger", And(None, [
true, key "Sword and Dagger", And(Some "Sword and Dagger", [
Either(None, [
false, key "Sword and Dagger-Rapier", Leaf "Rapier +4"
false, key "Sword and Dagger-Broadsword", Leaf "Broadsword +4"
false, key "Sword and Dagger-Shortsword", Leaf "Shortsword +4"
])
Leveled("Main-gauche", +1)
Leveled("Main-gauche +1", 0)
])
]) @>
])
)
]
let proto1 = testCase "proto1" <| fun () ->
let key = parseKey
Expand Down

0 comments on commit fef2df6

Please sign in to comment.