@@ -10,6 +10,7 @@ module Testable.Element exposing
1010 , below
1111 , centerX
1212 , centerY
13+ , clip
1314 , column
1415 , el
1516 , expectRoundedEquality
@@ -37,10 +38,14 @@ module Testable.Element exposing
3738 , rgb
3839 , rgba
3940 , row
41+ , scrollbarX
42+ , scrollbarY
43+ , scrollbars
4044 , shrink
4145 , spacing
4246 , text
4347 , textColumn
48+ , toProgram
4449 , transparent
4550 , width
4651 )
@@ -64,11 +69,16 @@ import Testable
6469import Testable.Runner
6570
6671
67−layout : List (Testable.Attr msg) -> Testable.Element Testable.Runner.Msg -> Testable.Runner.TestableProgram
72+{-| This is used when we want to run all tests in a test suite instead of one at a time.
73+-}
74+toProgram : Testable.Runner.Testable -> Testable.Runner.TestableProgram
75+toProgram testable =
76+ Testable.Runner.program [ testable ]
77+
78+
79+layout : List (Testable.Attr msg) -> Testable.Element Testable.Runner.Msg -> Testable.Runner.Testable
6880layout attrs elem =
69− Testable.Runner.program
70− [ ( "Tests Run", elem )
71− ]
81+ Testable.Runner.testable "Test" elem
7282
7383
7484text : String -> Testable.Element msg
@@ -126,8 +136,31 @@ link :
126136 }
127137 -> Testable.Element msg
128138link attrs details =
129− -- TODO: this is not actually a link element!!
130− Testable.El (implicitWidthHeightShrink attrs) details.label
139+ Testable.Link details.url
140+ (implicitWidthHeightShrink attrs)
141+ details.label
142+
143+
144+clip =
145+ Testable.Attr Element.clip
146+
147+
148+{-| -}
149+scrollbars : Testable.Attr msg
150+scrollbars =
151+ Testable.Attr Element.scrollbars
152+
153+
154+{-| -}
155+scrollbarY : Testable.Attr msg
156+scrollbarY =
157+ Testable.Attr Element.scrollbarY
158+
159+
160+{-| -}
161+scrollbarX : Testable.Attr msg
162+scrollbarX =
163+ Testable.Attr Element.scrollbarX
131164
132165
133166{-| Old labeling mechanism that i removed to hastily
@@ -301,6 +334,11 @@ paddingXY x y =
301334 }
302335
303336
337+listIf ls =
338+ List.filter Tuple.first ls
339+ |> List.map Tuple.second
340+
341+
304342widthHelper maybeMin maybeMax len =
305343 let
306344 addMin l =
@@ -402,36 +440,93 @@ widthHelper maybeMin maybeMax len =
402440 parentAvailableWidth =
403441 context.parent.bbox.width - (context.self.bbox.padding.left + context.self.bbox.padding.right)
404442 in
405− [ case context.parentLayout of
406− Testable.IsNearby _ ->
407− Testable.true "Nearby Element has fill width"
408− ((floor context.parent.bbox.width == floor context.self.bbox.width)
409− || minMaxTest (floor context.self.bbox.width)
410− )
411−
412− Testable.InColumn ->
413− Testable.true "Element within column has fill width"
414− ((floor parentAvailableWidth == floor context.self.bbox.width)
415− || minMaxTest (floor context.self.bbox.width)
416− )
443+ List.concat
444+ [ case context.parentLayout of
445+ Testable.IsNearby _ ->
446+ [ Testable.true "Nearby Element has fill width"
447+ ((floor context.parent.bbox.width == floor context.self.bbox.width)
448+ || minMaxTest (floor context.self.bbox.width)
449+ )
450+ ]
417451
418− Testable.InEl ->
419− Testable.true "Element within element has fill width" <|
420− (floor parentAvailableWidth == floor context.self.bbox.width)
421− || minMaxTest (floor context.self.bbox.width)
452+ Testable.InColumn ->
453+ [ Testable.true "Element within column has fill width"
454+ ((floor parentAvailableWidth == floor context.self.bbox.width)
455+ || minMaxTest (floor context.self.bbox.width)
456+ )
457+ ]
458+
459+ Testable.InEl ->
460+ [ Testable.true "Element within element has fill width" <|
461+ (floor parentAvailableWidth == floor context.self.bbox.width)
462+ || minMaxTest (floor context.self.bbox.width)
463+ ]
464+
465+ _ ->
466+ let
467+ spacePerPortion =
468+ parentAvailableWidth / toFloat (List.length context.siblings + 1)
469+ in
470+ [ Testable.true "element has fill width" <|
471+ (floor spacePerPortion == floor context.self.bbox.width)
472+ || minMaxTest (floor context.self.bbox.width)
473+ ]
474+ , [ Testable.lessThanOrEqual "not larger than parent"
475+ context.self.bbox.width
476+ context.parent.bbox.width
477+ ]
478+ , case context.selfElement of
479+ Testable.Paragraph _ _ ->
480+ -- A paragraph with width fill
481+ -- we want to test for correct text wrapping here.
482+ --
483+ let
484+ childWidth child =
485+ child.bbox.width
486+
487+ totalChildren =
488+ context.children
489+ |> List.map childWidth
490+ |> List.append (List.map .width context.self.textMetrics)
491+ |> List.sum
492+
493+ horizontalPadding =
494+ context.self.bbox.padding.left + context.self.bbox.padding.right
495+
496+ totalCalculatedWidth =
497+ totalChildren + horizontalPadding
498+
499+ expectedLines =
500+ if boxChildrenHeight > 20 then
501+ ceiling (boxChildrenHeight / 20)
502+
503+ else
504+ ceiling (totalCalculatedWidth / context.self.bbox.width)
505+
506+ boxChildrenHeight =
507+ context.children
508+ |> List.map
509+ (\c ->
510+ c.bbox.height
511+ )
512+ |> List.sum
513+ in
514+ [ Testable.true "text wrapping, checking height"
515+ -- all we're doing here is testing if wrapping is occurring
516+ -- not the exact height, which is a little tricky to calculate perfectly without more information.
517+ (if expectedLines > 1 then
518+ context.self.bbox.height >= (20 * 2)
519+
520+ else
521+ -- then it's less than 2 ~ line heights
522+ -- there's some weird variance here which makes it hard to be exact.
523+ context.self.bbox.height < (20 * 2)
524+ )
525+ ]
422526
423− _ ->
424− let
425− spacePerPortion =
426− parentAvailableWidth / toFloat (List.length context.siblings + 1)
427− in
428− Testable.true "element has fill width" <|
429− (floor spacePerPortion == floor context.self.bbox.width)
430− || minMaxTest (floor context.self.bbox.width)
431− , Testable.lessThanOrEqual "not larger than parent"
432− context.self.bbox.width
433− context.parent.bbox.width
434− ]
527+ _ ->
528+ []
529+ ]
435530 }
436531
437532 Shrink ->
@@ -461,14 +556,49 @@ widthHelper maybeMin maybeMax len =
461556 horizontalPadding =
462557 context.self.bbox.padding.left + context.self.bbox.padding.right
463558 in
464− [ expectRoundedEquality
465− { expected = totalChildren + horizontalPadding
466− , found = context.self.bbox.width
467− }
468− , Testable.lessThanOrEqual "not larger than parent"
469− context.self.bbox.width
470− context.parent.bbox.width
471− ]
559+ listIf
560+ [ ( (context.parentLayout /= Testable.InParagraph)
561+ && (context.parentLayout /= Testable.InTextCol)
562+ , Testable.rounded "equals children"
563+ { expected = totalChildren + horizontalPadding
564+ , found = context.self.bbox.width
565+ }
566+ )
567+ , ( context.parentLayout /= Testable.AtRoot
568+ , Testable.lessThanOrEqual "not larger than parent"
569+ context.self.bbox.width
570+ context.parent.bbox.width
571+ )
572+ ]
573+
574+ Testable.Link _ _ _ ->
575+ let
576+ childWidth child =
577+ child.bbox.width
578+
579+ totalChildren =
580+ context.children
581+ |> List.map childWidth
582+ |> List.append (List.map .width context.self.textMetrics)
583+ |> List.sum
584+
585+ horizontalPadding =
586+ context.self.bbox.padding.left + context.self.bbox.padding.right
587+ in
588+ listIf
589+ [ ( (context.parentLayout /= Testable.InParagraph)
590+ && (context.parentLayout /= Testable.InTextCol)
591+ , Testable.rounded "equals children"
592+ { expected = totalChildren + horizontalPadding
593+ , found = context.self.bbox.width
594+ }
595+ )
596+ , ( context.parentLayout /= Testable.AtRoot
597+ , Testable.lessThanOrEqual "not larger than parent"
598+ context.self.bbox.width
599+ context.parent.bbox.width
600+ )
601+ ]
472602
473603 Testable.Row rowAttrs _ ->
474604 -- width of row is the sum of all children widths
@@ -492,14 +622,19 @@ widthHelper maybeMin maybeMax len =
492622 totalSpacing =
493623 toFloat spacingAmount * (toFloat (List.length context.children) - 1)
494624 in
495− [ expectRoundedEquality
496− { expected = totalChildren + horizontalPadding + totalSpacing
497− , found = context.self.bbox.width
498− }
499− , Testable.lessThanOrEqual "not larger than parent"
500− context.self.bbox.width
501− context.parent.bbox.width
502− ]
625+ listIf
626+ [ ( True
627+ , expectRoundedEquality
628+ { expected = totalChildren + horizontalPadding + totalSpacing
629+ , found = context.self.bbox.width
630+ }
631+ )
632+ , ( context.parentLayout /= Testable.AtRoot
633+ , Testable.lessThanOrEqual "not larger than parent"
634+ context.self.bbox.width
635+ context.parent.bbox.width
636+ )
637+ ]
503638
504639 Testable.Column _ _ ->
505640 -- The width of the column is the width of the widest child.
@@ -523,14 +658,19 @@ widthHelper maybeMin maybeMax len =
523658 |> List.map childWidth
524659 |> List.append textChildren
525660 in
526− [ expectRoundedEquality
527− { expected = Maybe.withDefault 0 (List.maximum allChildren)
528− , found = context.self.bbox.width
529− }
530− , Testable.lessThanOrEqual "not larger than parent"
531− context.self.bbox.width
532− context.parent.bbox.width
533− ]
661+ listIf
662+ [ ( True
663+ , expectRoundedEquality
664+ { expected = Maybe.withDefault 0 (List.maximum allChildren)
665+ , found = context.self.bbox.width
666+ }
667+ )
668+ , ( context.parentLayout /= Testable.AtRoot
669+ , Testable.lessThanOrEqual "not larger than parent"
670+ context.self.bbox.width
671+ context.parent.bbox.width
672+ )
673+ ]
534674
535675 Testable.TextColumn _ _ ->
536676 -- The width of the column is the width of the widest child.
@@ -554,14 +694,19 @@ widthHelper maybeMin maybeMax len =
554694 |> List.map childWidth
555695 |> List.append textChildren
556696 in
557− [ expectRoundedEquality
558− { expected = Maybe.withDefault 0 (List.maximum allChildren)
559− , found = context.self.bbox.width
560− }
561− , Testable.lessThanOrEqual "not larger than parent"
562− context.self.bbox.width
563− context.parent.bbox.width
564− ]
697+ listIf
698+ [ ( True
699+ , expectRoundedEquality
700+ { expected = Maybe.withDefault 0 (List.maximum allChildren)
701+ , found = context.self.bbox.width
702+ }
703+ )
704+ , ( context.parentLayout /= Testable.AtRoot
705+ , Testable.lessThanOrEqual "not larger than parent"
706+ context.self.bbox.width
707+ context.parent.bbox.width
708+ )
709+ ]
565710
566711 Testable.Paragraph _ _ ->
567712 -- This should be the size it's text,
@@ -579,14 +724,19 @@ widthHelper maybeMin maybeMax len =
579724 horizontalPadding =
580725 context.self.bbox.padding.left + context.self.bbox.padding.right
581726 in
582− [ expectRoundedEquality
583− { expected = totalChildren + horizontalPadding
584− , found = context.self.bbox.width
585− }
586− , Testable.lessThanOrEqual "not larger than parent"
587− context.self.bbox.width
588− context.parent.bbox.width
589− ]
727+ listIf
728+ [ ( True
729+ , expectRoundedEquality
730+ { expected = totalChildren + horizontalPadding
731+ , found = context.self.bbox.width
732+ }
733+ )
734+ , ( context.parentLayout /= Testable.AtRoot
735+ , Testable.lessThanOrEqual "not larger than parent"
736+ context.self.bbox.width
737+ context.parent.bbox.width
738+ )
739+ ]
590740
591741 Testable.Text _ ->
592742 []
@@ -740,18 +890,17 @@ heightHelper maybeMin maybeMax len =
740890 childHeight child =
741891 child.bbox.height
742892 in
743− [ if List.isEmpty context.children then
893+ if List.isEmpty context.children then
744894 -- context.self.textMetrics
745895 -- |> List.map Testable.textHeight
746896 -- |> List.sum
747897 -- TODO: apparently the font metrics we have are for the literal characters rendered
748898 -- not for the font itself.
749− -- so for now, we are stubbing this in as 20, which is the default font size.
750− Testable.true "expected multiple of 20"
751− ((round context.self.bbox.height |> modBy 20) == 0)
899+ [ Testable.todo "calculate height from actual text metrics"
900+ ]
752901
753− else
754− expectRoundedEquality
902+ else
903+ [ expectRoundedEquality
755904 { expected =
756905 context.children
757906 |> List.map childHeight
@@ -759,28 +908,54 @@ heightHelper maybeMin maybeMax len =
759908 |> (\h -> h + context.self.bbox.padding.top + context.self.bbox.padding.bottom)
760909 , found = context.self.bbox.height
761910 }
762− , Testable.lessThanOrEqual "not larger than parent"
763− context.self.bbox.height
764− context.parent.bbox.height
765− ]
911+ , Testable.lessThanOrEqual "not larger than parent"
912+ context.self.bbox.height
913+ context.parent.bbox.height
914+ ]
915+
916+ Testable.Link _ _ _ ->
917+ let
918+ childHeight child =
919+ child.bbox.height
920+ in
921+ if List.isEmpty context.children then
922+ -- context.self.textMetrics
923+ -- |> List.map Testable.textHeight
924+ -- |> List.sum
925+ -- TODO: apparently the font metrics we have are for the literal characters rendered
926+ -- not for the font itself.
927+ [ Testable.todo "calculate height from actual text metrics"
928+ ]
929+
930+ else
931+ [ expectRoundedEquality
932+ { expected =
933+ context.children
934+ |> List.map childHeight
935+ |> List.sum
936+ |> (\h -> h + context.self.bbox.padding.top + context.self.bbox.padding.bottom)
937+ , found = context.self.bbox.height
938+ }
939+ , Testable.lessThanOrEqual "not larger than parent"
940+ context.self.bbox.height
941+ context.parent.bbox.height
942+ ]
766943
767944 Testable.Row _ _ ->
768945 let
769946 childHeight child =
770947 child.bbox.height
771948 in
772− [ if List.isEmpty context.children then
949+ if List.isEmpty context.children then
773950 -- context.self.textMetrics
774951 -- |> List.map Testable.textHeight
775952 -- |> List.sum
776953 -- TODO: apparently the font metrics we have are for the literal characters rendered
777954 -- not for the font itself.
778− -- so for now, we are stubbing this in as 20, which is the default font size.
779− Testable.true "expected multiple of 20"
780− ((round context.self.bbox.height |> modBy 20) == 0)
955+ [ Testable.todo "calculate height from actual text metrics" ]
781956
782− else
783− expectRoundedEquality
957+ else
958+ [ expectRoundedEquality
784959 { expected =
785960 context.children
786961 |> List.map childHeight
@@ -789,10 +964,10 @@ heightHelper maybeMin maybeMax len =
789964 |> (\h -> h + context.self.bbox.padding.top + context.self.bbox.padding.bottom)
790965 , found = context.self.bbox.height
791966 }
792− , Testable.lessThanOrEqual "not larger than parent"
793− context.self.bbox.height
794− context.parent.bbox.height
795− ]
967+ , Testable.lessThanOrEqual "not larger than parent"
968+ context.self.bbox.height
969+ context.parent.bbox.height
970+ ]
796971
797972 Testable.Column colAttrs _ ->
798973 let
@@ -824,16 +999,10 @@ heightHelper maybeMin maybeMax len =
824999 ]
8251000
8261001 Testable.TextColumn _ _ ->
827− [ Testable.lessThanOrEqual "not larger than parent"
828− context.self.bbox.height
829− context.parent.bbox.height
830− ]
1002+ []
8311003
8321004 Testable.Paragraph _ _ ->
833− [ Testable.lessThanOrEqual "not larger than parent"
834− context.self.bbox.height
835− context.parent.bbox.height
836− ]
1005+ []
8371006
8381007 Testable.Text _ ->
8391008 []