1module Testable exposing (Attr(..), BoundingBox, Element(..), Found, LayoutContext(..), Location(..), Style, Surroundings, addAttribute, applyLabels, compareFormattedColor, createAttributeTest, createTest, formatColor, formatColorWithAlpha, getElementId, getIds, getSpacing, idAttr, levelToString, render, renderAttribute, renderElement, runTests, toTest)
5import Dict exposing (Dict)
6import Element exposing (Color)
8import Html exposing (Html)
10import Internal.Model as Internal
12import Test exposing (Test)
14import Test.Runner.Failure
18 = El (List (Attr msg)) (Element msg)
19 | Row (List (Attr msg)) (List (Element msg))
20 | Column (List (Attr msg)) (List (Element msg))
21 | TextColumn (List (Attr msg)) (List (Element msg))
22 | Paragraph (List (Attr msg)) (List (Element msg))
28 = Attr (Element.Attribute msg)
29 | AttrTest (Surroundings -> Test)
30 | Batch (List (Attr msg))
34 , element : Element msg
35 , test : Surroundings -> () -> Expect.Expectation
40 { test : Surroundings -> () -> Expect.Expectation
42 , attr : Element.Attribute msg
62type alias Surroundings =
63 { siblings : List Found
65 , children : List Found
68 -- These values are needed to perform some types of tests.
69 , location : LayoutContext
86type alias BoundingBox =
106getIds : Element msg -> List String
108 "se-0" :: getElementId [ 0, 0 ] el
111getElementId : List Int -> Element msg -> List String
112getElementId level el =
117 attrID attrIndex attr =
120 getElementId (attrIndex :: -1 :: level) nearby.element
127 |> List.indexedMap attrID
130 childrenIDs children =
131 List.concat <| List.indexedMap (\i -> getElementId (i :: level)) children
135 id :: getElementId (0 :: level) child ++ attributeIDs attrs
137 Row attrs children ->
138 id :: childrenIDs children ++ attributeIDs attrs
140 Column attrs children ->
141 id :: childrenIDs children ++ attributeIDs attrs
143 TextColumn attrs children ->
144 id :: childrenIDs children ++ attributeIDs attrs
146 Paragraph attrs children ->
147 id :: childrenIDs children ++ attributeIDs attrs
160render : Element msg -> Html msg
162 Element.layout [ idAttr "0" ] <|
163 renderElement [ 0, 0 ] el
166idAttr : String -> Element.Attribute msg
168 Element.htmlAttribute (Html.Attributes.id ("se-" ++ id))
171renderElement : List Int -> Element msg -> Element.Element msg
172renderElement level el =
176 |> List.map String.fromInt
180 makeAttributes attrs =
182 |> List.indexedMap (renderAttribute level)
188 (id :: makeAttributes attrs)
189 (renderElement (0 :: level) child)
191 Row attrs children ->
193 (id :: makeAttributes attrs)
194 (List.indexedMap (\i -> renderElement (i :: level)) children)
196 Column attrs children ->
198 (id :: makeAttributes attrs)
199 (List.indexedMap (\i -> renderElement (i :: level)) children)
201 TextColumn attrs children ->
203 (id :: makeAttributes attrs)
204 (List.indexedMap (\i -> renderElement (i :: level)) children)
206 Paragraph attrs children ->
208 (id :: makeAttributes attrs)
209 (List.indexedMap (\i -> renderElement (i :: level)) children)
218renderAttribute : List Int -> Int -> Attr msg -> List (Element.Attribute msg)
219renderAttribute level attrIndex attr =
233 Nearby { location, element } ->
236 [ Element.above (renderElement (attrIndex :: -1 :: level) element) ]
239 [ Element.below (renderElement (attrIndex :: -1 :: level) element) ]
242 [ Element.onRight (renderElement (attrIndex :: -1 :: level) element) ]
245 [ Element.onLeft (renderElement (attrIndex :: -1 :: level) element) ]
248 [ Element.inFront (renderElement (attrIndex :: -1 :: level) element) ]
251 [ Element.behindContent (renderElement (attrIndex :: -1 :: level) element) ]
254 List.indexedMap (renderAttribute (attrIndex :: level)) batch
257 LabeledTest tested ->
265toTest : String -> Dict String Found -> Element msg -> Test
266toTest label harvested el =
269 Dict.get "se-0" harvested
274 [ Test.test "Find Root" <|
275 \_ -> Expect.fail "unable to find root"
279 Test.describe label <|
291levelToString : List Int -> String
294 |> List.map String.fromInt
296 |> (\x -> "se-" ++ x)
300 { siblings : List Found
302 , cache : Dict String Found
304 , element : Element msg
305 , location : LayoutContext
306 , parentSpacing : Int
309createTest { siblings, parent, cache, level, element, location, parentSpacing } =
313 |> Maybe.withDefault 0
318 testChildren : Found -> List (Element msg) -> List Test
319 testChildren found children =
322 -- Should check that this lookup doesn't fail.
323 -- Thoug if it does, it'll fail when the element itself is tested
326 Dict.get (levelToString (x :: level)) cache
328 (List.range 0 (List.length children))
330 List.foldl (applyChildTest found)
332 , upcoming = childrenFound
345 , previous : List Found
347 , upcoming : List Found
351 , previous : List Found
353 , upcoming : List Found
355 applyChildTest found child childTest =
356 -- { index, upcoming, previous, tests }
358 surroundingChildren =
359 case childTest.upcoming of
364 remaining ++ childTest.previous
368 { siblings = surroundingChildren
371 , level = childTest.index :: level
373 , parentSpacing = spacing
398 { index = childTest.index + 1
399 , tests = childTest.tests ++ childrenTests
401 case childTest.upcoming of
406 x :: childTest.previous
408 case childTest.upcoming of
416 tests : Found -> List (Attr msg) -> List (Element msg) -> List Test
417 tests self attributes children =
419 findBBox elem ( i, gathered ) =
432 case Dict.get (levelToString (i :: level)) cache of
436 Debug.log "el failed to find" elem
439 Debug.log "Failed to find child" (levelToString (i :: level))
451 List.foldl findBBox ( 0, [] ) children
458 -- Found -> Dict String Found -> List Int -> Int -> Surroundings -> Attr msg -> List Test
460 createAttributeTest self
464 { siblings = siblings
467 , children = childrenFoundData
468 , location = location
469 , parentSpacing = parentSpacing
476 ++ testChildren self children
478 case Dict.get id cache of
488 [ Test.test ("Unable to find " ++ id) (always <| Expect.fail "failed id lookup") ]
493 tests self attrs [ child ]
495 Row attrs children ->
496 tests self attrs children
498 Column attrs children ->
499 tests self attrs children
501 TextColumn attrs children ->
502 tests self attrs children
504 Paragraph attrs children ->
505 tests self attrs children
514applyLabels : List (Attr msg) -> List (Attr msg)
527 |> List.filterMap toLabel
530 applyLabel newLabel attr =
532 LabeledTest labeled ->
536 if newLabel == "" then
540 newLabel ++ ", " ++ labeled.label
546 List.map (applyLabel newLabels) attrs
549createAttributeTest : Found -> Dict String Found -> List Int -> Int -> Surroundings -> Attr msg -> List Test
550createAttributeTest parent cache level attrIndex surroundings attr =
553 levelToString (attrIndex :: level)
575 , level = attrIndex :: -1 :: level
576 , location = IsNearby nearby.location
577 , element = addAttribute (AttrTest (\context -> Test.test (nearby.label ++ " #" ++ indexLabel) (nearby.test context))) nearby.element
582 |> List.indexedMap (\i attribute -> createAttributeTest parent cache (attrIndex :: level) i surroundings attribute)
585 LabeledTest { label, test } ->
586 [ Test.test (label ++ " #" ++ indexLabel) (test surroundings)
590addAttribute : Attr msg -> Element msg -> Element msg
591addAttribute attr el =
594 El (attr :: attrs) child
596 Row attrs children ->
597 Row (attr :: attrs) children
599 Column attrs children ->
600 Column (attr :: attrs) children
602 TextColumn attrs children ->
603 TextColumn (attr :: attrs) children
605 Paragraph attrs children ->
606 Paragraph (attr :: attrs) children
622 { given : Maybe String
623 , description : String
624 , reason : Test.Runner.Failure.Reason
632 List.map Test.Runner.getFailureReason (runner.run ())
634 List.map2 Tuple.pair runner.labels ran
637 case Test.Runner.fromTest 100 seed tests of
638 Test.Runner.Plain rnrs ->
641 Test.Runner.Only rnrs ->
644 Test.Runner.Skipping rnrs ->
647 Test.Runner.Invalid invalid ->
650 Debug.log "Invalid tests" invalid
657compareFormattedColor : Color -> String -> Bool
658compareFormattedColor color expected =
659 formatColor color == expected || formatColorWithAlpha color == expected
662formatColorWithAlpha : Color -> String
663formatColorWithAlpha (Internal.Rgba red green blue alpha) =
665 ("rgba(" ++ String.fromInt (round (red * 255)))
666 ++ (", " ++ String.fromInt (round (green * 255)))
667 ++ (", " ++ String.fromInt (round (blue * 255)))
672 ("rgba(" ++ String.fromInt (round (red * 255)))
673 ++ (", " ++ String.fromInt (round (green * 255)))
674 ++ (", " ++ String.fromInt (round (blue * 255)))
675 ++ (", " ++ String.fromFloat alpha ++ ")")
678formatColor : Color -> String
679formatColor (Internal.Rgba red green blue alpha) =
681 ("rgb(" ++ String.fromInt (round (red * 255)))
682 ++ (", " ++ String.fromInt (round (green * 255)))
683 ++ (", " ++ String.fromInt (round (blue * 255)))
687 ("rgb(" ++ String.fromInt (round (red * 255)))
688 ++ (", " ++ String.fromInt (round (green * 255)))
689 ++ (", " ++ String.fromInt (round (blue * 255)))
693getSpacing : Element msg -> Maybe Int
696 getSpacingAttr attr found =
697 if found /= Nothing then
706 List.foldr getSpacingAttr Nothing attrs
712 List.foldr getSpacingAttr Nothing attrs
724 TextColumn attrs _ ->
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list