1665: Visual highlighter overlap tests r=micahhahn a=omnibs

Adds new tests for `Nri.Ui.Highlighter.V5`'s `selectShortest` function, making scenarios easier to inspect and craft visually.

No API changes anywhere.

Co-authored-by: Juliano Solanho <juliano.solanho@gmail.com>
This commit is contained in:
nora-dingbot[bot] 2024-04-03 19:59:12 +00:00 committed by GitHub
commit a61ed6a1de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 145 additions and 111 deletions

View File

@ -27,7 +27,6 @@ spec =
, describe "markdown highlightable behavior" markdownContentTests
, describe "markdown highlight name behavior" markdownHighlightNameTests
, describe "joinAdjacentInteractiveHighlights" joinAdjacentInteractiveHighlightsTests
, describe "selectShortest" selectShortestTests
, describe "overlapping highlights" overlappingHighlightTests
]
@ -727,116 +726,6 @@ joinAdjacentInteractiveHighlightsTests =
]
selectShortestTests : List Test
selectShortestTests =
let
init highlightables =
Highlighter.init
{ id = "test-highlighter-container"
, highlightables = highlightables
, marker = markerModel (Just "A")
, joinAdjacentInteractiveHighlights = False
, sorter = Sort.alphabetical
}
in
[ test "without any highlightables, returns Nothing" <|
\() ->
init []
|> Highlighter.selectShortest (.highlightables >> List.Extra.getAt 0)
|> Expect.equal Nothing
, test "with 1 unmarked highlightable, returns Nothing" <|
\() ->
init [ Highlightable.initInteractive [] 0 "hello" ]
|> Highlighter.selectShortest (.highlightables >> List.Extra.getAt 0)
|> Expect.equal Nothing
, test "with 1 marked highlightable, returns the singular mark type" <|
\() ->
init [ Highlightable.initInteractive [ marker (Just "B") ] 0 "hello" ]
|> Highlighter.selectShortest (.highlightables >> List.Extra.getAt 0)
|> Expect.equal (Just "B")
, test "with 1 multi-marked highlightable, returns one of the mark types depending on sort order" <|
\() ->
init
[ Highlightable.initInteractive
[ marker (Just "C")
, marker (Just "B")
, marker (Just "D")
]
0
"hello"
]
|> Highlighter.selectShortest (.highlightables >> List.Extra.getAt 0)
|> Expect.equal (Just "B")
, test "with multiple marked highlightables, selecting the first highlightable, returns the shortest mark type" <|
\() ->
init
[ Highlightable.initInteractive
[ marker (Just "C")
, marker (Just "B")
]
0
"hello"
, Highlightable.initInteractive
[ marker (Just "B")
]
1
"world"
, Highlightable.initInteractive
[ marker (Just "C")
]
2
"!"
]
|> Highlighter.selectShortest (.highlightables >> List.Extra.getAt 0)
|> Expect.equal (Just "C")
, test "with multiple marked highlightables, selecting the second highlightable, returns the shortest mark type" <|
\() ->
init
[ Highlightable.initInteractive
[ marker (Just "B")
]
0
"hello"
, Highlightable.initInteractive
[ marker (Just "B")
, marker (Just "C")
]
1
"world"
, Highlightable.initInteractive
[ marker (Just "C")
]
2
"!"
]
|> Highlighter.selectShortest (.highlightables >> List.Extra.getAt 1)
|> Expect.equal (Just "C")
, test "with multiple marked highlightables, selecting the third highlightable, returns the shortest mark type" <|
\() ->
init
[ Highlightable.initInteractive
[ marker (Just "B")
]
0
"hello"
, Highlightable.initInteractive
[ marker (Just "B")
, marker (Just "C")
]
1
"world"
, Highlightable.initInteractive
[ marker (Just "C")
, marker (Just "B")
]
2
"!"
]
|> Highlighter.selectShortest (.highlightables >> List.Extra.getAt 2)
|> Expect.equal (Just "C")
]
overlappingHighlightTests : List Test
overlappingHighlightTests =
let

View File

@ -0,0 +1,145 @@
module Spec.Nri.Ui.HighlighterOverlaps exposing (spec)
import Expect exposing (Expectation)
import List.Extra
import Maybe.Extra
import Nri.Ui.Colors.V1 as Colors
import Nri.Ui.Highlightable.V3 as Highlightable
import Nri.Ui.Highlighter.V5 as Highlighter
import Nri.Ui.HighlighterTool.V1 as Tool
import Sort
import Test exposing (..)
spec : Test
spec =
describe "Highlighter#selectShortest"
[ test "no overlap" <|
\_ ->
init2
|> marker "### "
|> marker " ***"
|> cursor " ^ "
|> expect "*"
, test "shortest at start" <|
\_ ->
init2
|> marker "# "
|> marker "***"
|> cursor "^ "
|> expect "#"
, test "shortest in the middle" <|
\_ ->
init2
|> marker " # "
|> marker "***"
|> cursor " ^ "
|> expect "#"
, test "shortest at the end" <|
\_ ->
init2
|> marker " #"
|> marker "***"
|> cursor " ^"
|> expect "#"
, test "shortest intersects from left" <|
\_ ->
init2
|> marker "#### "
|> marker " ***"
|> cursor " ^ "
|> expect "*"
, test "shortest intersects from right" <|
\_ ->
init2
|> marker " ####"
|> marker "*** "
|> cursor " ^ "
|> expect "*"
, test "shortest is non-contiguous" <|
\_ ->
init2
|> marker " #### "
|> marker "* * *"
|> cursor " ^ "
|> expect "*"
, test "longest is non-contiguous" <|
\_ ->
init2
|> marker " #### "
|> marker "***** * *****"
|> cursor " ^ "
|> expect "#"
]
init2 : String -> String -> String -> Highlighter.Model String
init2 marker1 marker2 cursor_ =
let
highlightables =
List.Extra.zip (String.toList marker1) (String.toList marker2)
|> List.indexedMap
(\idx ( a, b ) ->
Highlightable.initInteractive
(markers a b)
idx
" "
)
cursorIndex =
String.toList cursor_
|> List.Extra.elemIndex '^'
|> Maybe.withDefault -1
in
Highlighter.init
{ id = "test-highlighter-container"
, highlightables = highlightables
, marker = Tool.Marker (mkMarker "unused")
, joinAdjacentInteractiveHighlights = False
, sorter = Sort.alphabetical
}
|> Highlighter.update (Highlighter.Pointer (Highlighter.Over cursorIndex))
|> (\( model, _, _ ) -> model)
marker : String -> (String -> a) -> a
marker str f =
f str
cursor : String -> (String -> a) -> a
cursor str f =
f str
expect : String -> Highlighter.Model String -> Expectation
expect cursorName highlighter =
Highlighter.selectShortest Highlighter.hoveredHighlightable highlighter
|> Expect.equal (Just cursorName)
markers : Char -> Char -> List (Tool.MarkerModel String)
markers a b =
Maybe.Extra.values
[ if a == ' ' then
Nothing
else
Just (mkMarker (String.fromChar a))
, if b == ' ' then
Nothing
else
Just (mkMarker (String.fromChar b))
]
mkMarker : String -> Tool.MarkerModel String
mkMarker str =
Tool.buildMarker
{ highlightColor = Colors.magenta
, hoverColor = Colors.magenta
, hoverHighlightColor = Colors.magenta
, kind = str
, name = Just str
}