1module Testable exposing
12 , compareFormattedColor
16 , formatColorWithAlpha
31import Dict exposing (Dict)
32import Element exposing (Color)
34import Html exposing (Html)
36import Internal.Model as Internal
38import Test exposing (Test)
40import Test.Runner.Failure
44 = El (List (Attr msg)) (Element msg)
45 | Row (List (Attr msg)) (List (Element msg))
46 | Column (List (Attr msg)) (List (Element msg))
47 | TextColumn (List (Attr msg)) (List (Element msg))
48 | Paragraph (List (Attr msg)) (List (Element msg))
54 = Attr (Element.Attribute msg)
55 | AttrTest (Surroundings -> Test)
56 | Batch (List (Attr msg))
60 , element : Element msg
61 , test : Surroundings -> () -> Expect.Expectation
66 { test : Surroundings -> () -> Expect.Expectation
68 , attr : Element.Attribute msg
88type alias Surroundings =
89 { siblings : List Found
91 , children : List Found
94 -- These values are needed to perform some types of tests.
95 , location : LayoutContext
112type alias BoundingBox =
132getIds : Element msg -> List String
134 "se-0" :: getElementId [ 0, 0 ] el
137getElementId : List Int -> Element msg -> List String
138getElementId level el =
143 attrID attrIndex attr =
146 getElementId (attrIndex :: -1 :: level) nearby.element
153 |> List.indexedMap attrID
156 childrenIDs children =
157 List.concat <| List.indexedMap (\i -> getElementId (i :: level)) children
161 id :: getElementId (0 :: level) child ++ attributeIDs attrs
163 Row attrs children ->
164 id :: childrenIDs children ++ attributeIDs attrs
166 Column attrs children ->
167 id :: childrenIDs children ++ attributeIDs attrs
169 TextColumn attrs children ->
170 id :: childrenIDs children ++ attributeIDs attrs
172 Paragraph attrs children ->
173 id :: childrenIDs children ++ attributeIDs attrs
186render : Element msg -> Html msg
188 Element.layout [ idAttr "0" ] <|
189 renderElement [ 0, 0 ] el
192idAttr : String -> Element.Attribute msg
194 Element.htmlAttribute (Html.Attributes.id ("se-" ++ id))
197renderElement : List Int -> Element msg -> Element.Element msg
198renderElement level el =
202 |> List.map String.fromInt
206 makeAttributes attrs =
208 |> List.indexedMap (renderAttribute level)
214 (id :: makeAttributes attrs)
215 (renderElement (0 :: level) child)
217 Row attrs children ->
219 (id :: makeAttributes attrs)
220 (List.indexedMap (\i -> renderElement (i :: level)) children)
222 Column attrs children ->
224 (id :: makeAttributes attrs)
225 (List.indexedMap (\i -> renderElement (i :: level)) children)
227 TextColumn attrs children ->
229 (id :: makeAttributes attrs)
230 (List.indexedMap (\i -> renderElement (i :: level)) children)
232 Paragraph attrs children ->
234 (id :: makeAttributes attrs)
235 (List.indexedMap (\i -> renderElement (i :: level)) children)
244renderAttribute : List Int -> Int -> Attr msg -> List (Element.Attribute msg)
245renderAttribute level attrIndex attr =
259 Nearby { location, element } ->
262 [ Element.above (renderElement (attrIndex :: -1 :: level) element) ]
265 [ Element.below (renderElement (attrIndex :: -1 :: level) element) ]
268 [ Element.onRight (renderElement (attrIndex :: -1 :: level) element) ]
271 [ Element.onLeft (renderElement (attrIndex :: -1 :: level) element) ]
274 [ Element.inFront (renderElement (attrIndex :: -1 :: level) element) ]
277 [ Element.behindContent (renderElement (attrIndex :: -1 :: level) element) ]
280 List.indexedMap (renderAttribute (attrIndex :: level)) batch
283 LabeledTest tested ->
291toTest : String -> Dict String Found -> Element msg -> Test
292toTest label harvested el =
295 Dict.get "se-0" harvested
300 [ Test.test "Find Root" <|
301 \_ -> Expect.fail "unable to find root"
305 Test.describe label <|
317levelToString : List Int -> String
320 |> List.map String.fromInt
322 |> (\x -> "se-" ++ x)
326 { siblings : List Found
328 , cache : Dict String Found
330 , element : Element msg
331 , location : LayoutContext
332 , parentSpacing : Int
335createTest { siblings, parent, cache, level, element, location, parentSpacing } =
339 |> Maybe.withDefault 0
344 testChildren : Found -> List (Element msg) -> List Test
345 testChildren found children =
348 -- Should check that this lookup doesn't fail.
349 -- Thoug if it does, it'll fail when the element itself is tested
352 Dict.get (levelToString (x :: level)) cache
354 (List.range 0 (List.length children))
356 List.foldl (applyChildTest found)
358 , upcoming = childrenFound
371 , previous : List Found
373 , upcoming : List Found
377 , previous : List Found
379 , upcoming : List Found
381 applyChildTest found child childTest =
382 -- { index, upcoming, previous, tests }
384 surroundingChildren =
385 case childTest.upcoming of
390 remaining ++ childTest.previous
394 { siblings = surroundingChildren
397 , level = childTest.index :: level
399 , parentSpacing = spacing
424 { index = childTest.index + 1
425 , tests = childTest.tests ++ childrenTests
427 case childTest.upcoming of
432 x :: childTest.previous
434 case childTest.upcoming of
442 tests : Found -> List (Attr msg) -> List (Element msg) -> List Test
443 tests self attributes children =
445 findBBox elem ( i, gathered ) =
458 case Dict.get (levelToString (i :: level)) cache of
462 Debug.log "el failed to find" elem
465 Debug.log "Failed to find child" (levelToString (i :: level))
477 List.foldl findBBox ( 0, [] ) children
484 -- Found -> Dict String Found -> List Int -> Int -> Surroundings -> Attr msg -> List Test
486 createAttributeTest self
490 { siblings = siblings
493 , children = childrenFoundData
494 , location = location
495 , parentSpacing = parentSpacing
502 ++ testChildren self children
504 case Dict.get id cache of
514 [ Test.test ("Unable to find " ++ id) (always <| Expect.fail "failed id lookup") ]
519 tests self attrs [ child ]
521 Row attrs children ->
522 tests self attrs children
524 Column attrs children ->
525 tests self attrs children
527 TextColumn attrs children ->
528 tests self attrs children
530 Paragraph attrs children ->
531 tests self attrs children
540applyLabels : List (Attr msg) -> List (Attr msg)
553 |> List.filterMap toLabel
556 applyLabel newLabel attr =
558 LabeledTest labeled ->
562 if newLabel == "" then
566 newLabel ++ ", " ++ labeled.label
572 List.map (applyLabel newLabels) attrs
575createAttributeTest : Found -> Dict String Found -> List Int -> Int -> Surroundings -> Attr msg -> List Test
576createAttributeTest parent cache level attrIndex surroundings attr =
579 levelToString (attrIndex :: level)
601 , level = attrIndex :: -1 :: level
602 , location = IsNearby nearby.location
603 , element = addAttribute (AttrTest (\context -> Test.test (nearby.label ++ " #" ++ indexLabel) (nearby.test context))) nearby.element
608 |> List.indexedMap (\i attribute -> createAttributeTest parent cache (attrIndex :: level) i surroundings attribute)
611 LabeledTest { label, test } ->
612 [ Test.test (label ++ " #" ++ indexLabel) (test surroundings)
616addAttribute : Attr msg -> Element msg -> Element msg
617addAttribute attr el =
620 El (attr :: attrs) child
622 Row attrs children ->
623 Row (attr :: attrs) children
625 Column attrs children ->
626 Column (attr :: attrs) children
628 TextColumn attrs children ->
629 TextColumn (attr :: attrs) children
631 Paragraph attrs children ->
632 Paragraph (attr :: attrs) children
648 { given : Maybe String
649 , description : String
650 , reason : Test.Runner.Failure.Reason
658 List.map Test.Runner.getFailureReason (runner.run ())
660 List.map2 Tuple.pair runner.labels ran
663 case Test.Runner.fromTest 100 seed tests of
664 Test.Runner.Plain rnrs ->
667 Test.Runner.Only rnrs ->
670 Test.Runner.Skipping rnrs ->
673 Test.Runner.Invalid invalid ->
676 Debug.log "Invalid tests" invalid
683compareFormattedColor : Color -> String -> Bool
684compareFormattedColor color expected =
685 formatColor color == expected || formatColorWithAlpha color == expected
688formatColorWithAlpha : Color -> String
689formatColorWithAlpha (Internal.Rgba red green blue alpha) =
691 ("rgba(" ++ String.fromInt (round (red * 255)))
692 ++ (", " ++ String.fromInt (round (green * 255)))
693 ++ (", " ++ String.fromInt (round (blue * 255)))
698 ("rgba(" ++ String.fromInt (round (red * 255)))
699 ++ (", " ++ String.fromInt (round (green * 255)))
700 ++ (", " ++ String.fromInt (round (blue * 255)))
701 ++ (", " ++ String.fromFloat alpha ++ ")")
704formatColor : Color -> String
705formatColor (Internal.Rgba red green blue alpha) =
707 ("rgb(" ++ String.fromInt (round (red * 255)))
708 ++ (", " ++ String.fromInt (round (green * 255)))
709 ++ (", " ++ String.fromInt (round (blue * 255)))
713 ("rgb(" ++ String.fromInt (round (red * 255)))
714 ++ (", " ++ String.fromInt (round (green * 255)))
715 ++ (", " ++ String.fromInt (round (blue * 255)))
719getSpacing : Element msg -> Maybe Int
722 getSpacingAttr attr found =
723 if found /= Nothing then
732 List.foldr getSpacingAttr Nothing attrs
738 List.foldr getSpacingAttr Nothing attrs
750 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