Skip to content

Commit

Permalink
feat: tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
srghma committed Jun 10, 2020
1 parent b98e648 commit 6a9e94b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
15 changes: 8 additions & 7 deletions src/Halogen/VDom/DOM/Checkers.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ checkElementIsNodeType = checkElementIsNodeType'
getElementNodeType element = unsafePartial $ DOM.nodeType (DOM.Element.toNode element)

checkElementIsNodeType' :: DOM.NodeType -> DOM.Element -> Effect Unit
checkElementIsNodeType' expectedNodeType element =
checkElementIsNodeType' expectedNodeType element = do
let nodeType = getElementNodeType element
in when (nodeType /= expectedNodeType) (do
EFn.runEffectFn2 warnAny "Error at " { element }
throwException $ error $ "Expected element to be a " <> show expectedNodeType <> ", but got " <> show nodeType)
EFn.runEffectFn2 warnAny "checkElementIsNodeType" { nodeType, expectedNodeType, meta: { element } }
when (nodeType /= expectedNodeType) (do
EFn.runEffectFn2 warnAny "Error at " { element }
throwException $ error $ "Expected element to be a " <> show expectedNodeType <> ", but got " <> show nodeType)

checkIsTextNode :: DOM.Element -> Effect Unit
checkIsTextNode = checkElementIsNodeType DOM.NodeType.TextNode

checkTextContentIsEqTo :: String -> DOM.Element -> Effect Unit
checkTextContentIsEqTo expectedText element = do
textContent <- DOM.textContent (DOM.Element.toNode element)
EFn.runEffectFn2 warnAny "checkTextContentIsEqTo" { textContent, expectedText, meta: { element } }
when (textContent /= expectedText) (do
EFn.runEffectFn2 warnAny "Error at " { element }
throwException $ error $ "Expected element text content to equal to " <> quote expectedText <> ", but got " <> quote textContent)

--------------------------------------
Expand All @@ -57,14 +58,14 @@ checkTagNameIsEqualTo maybeNamespace elemName element = do
expectedTagName :: String
expectedTagName = toUpper $ fullAttributeName maybeNamespace elemName
let tagName = DOM.tagName element
EFn.runEffectFn2 warnAny "checkTagNameIsEqualTo" { expectedTagName, tagName, meta: { maybeNamespace, elemName, element } }
when (tagName /= expectedTagName) (do
EFn.runEffectFn2 warnAny "Error at " { element }
throwException (error $ "Expected element tagName equal to " <> show expectedTagName <> ", but got " <> show tagName))

checkChildrenLengthIsEqualTo :: Int -> DOM.Element -> Effect Unit
checkChildrenLengthIsEqualTo expectedLength element = do
(elementChildren :: DOM.NodeList) <- DOM.childNodes (DOM.Element.toNode element)
elementChildrenLength <- DOM.NodeList.length elementChildren
EFn.runEffectFn2 warnAny "checkChildrenLengthIsEqualTo" { elementChildrenLength, expectedLength, meta: { element, elementChildren } }
when (elementChildrenLength /= expectedLength) do
EFn.runEffectFn2 warnAny "Error at " { element, elementChildren }
(throwException (error $ "Expected element children count equal to " <> show expectedLength <> ", but got " <> show elementChildrenLength))
12 changes: 6 additions & 6 deletions src/Halogen/VDom/DOM/Prop/Checkers.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ checkAttributeExistsAndIsEqual maybeNamespace attributeName expectedElementValue
elementValue ← (EFn.runEffectFn3 Util.getAttribute (toNullable maybeNamespace) attributeName element) <#> toMaybe
case elementValue of
Nothingdo
EFn.runEffectFn2 warnAny "Error at " { element }
EFn.runEffectFn2 warnAny "checkAttributeExistsAndIsEqual -> missing" { element }
throwException $ error $ "Expected element to have an attribute " <> quote (fullAttributeName maybeNamespace (ElemName attributeName)) <> " eq to " <> quote expectedElementValue <> ", but it is missing"
Just elementValue' →
Just elementValue' → do
EFn.runEffectFn2 warnAny "checkAttributeExistsAndIsEqual -> not missing" { elementValue', expectedElementValue, meta: { maybeNamespace, attributeName, expectedElementValue, element } }
unless (elementValue' == expectedElementValue) (do
EFn.runEffectFn2 warnAny "Error at " { element }
throwException $ error $ "Expected element to have an attribute " <> quote (fullAttributeName maybeNamespace (ElemName attributeName)) <> " eq to " <> quote expectedElementValue <> ", but it was equal to " <> quote elementValue'
)

checkPropExistsAndIsEqual String PropValue DOM.Element Effect Unit
checkPropExistsAndIsEqual propName expectedPropValue element = do
let propValue = Fn.runFn2 unsafeGetProperty propName element
EFn.runEffectFn2 warnAny "checkPropExistsAndIsEqual" { propValue, expectedPropValue, meta: { element, propName } }
unless (Fn.runFn2 Util.refEq propValue expectedPropValue) (do
EFn.runEffectFn2 warnAny "Error at " { element, expectedPropValue }
throwException $ error $ "Expected element to have a prop " <> quote propName <> " eq to " <> quote (anyToString expectedPropValue) <> ", but it was equal to " <> quote (anyToString propValue)
)

Expand All @@ -50,8 +50,8 @@ mkExtraAttributeNames el = do
pure set

checkExtraAttributeNamesIsEmpty Set.Set String -> DOM.Element -> Effect Unit
checkExtraAttributeNamesIsEmpty extraAttributeNames element =
checkExtraAttributeNamesIsEmpty extraAttributeNames element = do
EFn.runEffectFn2 warnAny "checkExtraAttributeNamesIsEmpty" { extraAttributeNames, meta: { element } }
when (Set.size extraAttributeNames > 0) (do
EFn.runEffectFn2 warnAny "Error at " { element }
throwException $ error $ "Extra attributes from the server: " <> (Set.toArray extraAttributeNames # joinWith ", ")
)
2 changes: 1 addition & 1 deletion src/Halogen/VDom/DOM/Prop/Implementation.purs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Halogen.VDom.DOM.Prop.Types (ElemRef(..), EmitterInputBuilder, EventListe
import Halogen.VDom.DOM.Prop.Utils (removeProperty, setProperty, unsafeGetProperty)
import Halogen.VDom.Set as Set
import Halogen.VDom.Types (ElemName(..))
import Halogen.VDom.Util (STObject', fullAttributeName, warnAny)
import Halogen.VDom.Util (STObject', fullAttributeName)
import Halogen.VDom.Util as Util
import Prelude (Unit, bind, discard, pure, unit, (==))
import Web.DOM.Element (Element) as DOM
Expand Down

0 comments on commit 6a9e94b

Please sign in to comment.