dom-style-test-runner.coffee 1.31 KB
Newer Older
1 2 3 4 5 6 7 8
# Simple test runner for DOM-style CSS unit tests.
#
# Copyright © 2013 Aral Balkan

numAssertions = 0
numPassedAssertions = 0
numFailedAssertions = 0

9 10
# console.log document.styleSheets[0].cssRules[2]

11 12 13 14 15 16 17 18 19
divs = document.getElementsByTagName('div')

for div in divs
	dataAssertString = div.getAttribute('data-assert')
	if dataAssertString
		assertions = dataAssertString.split(';')
		for assertion in assertions
			assertion = assertion.trim()
			assertionTokens = assertion.split(':')
20 21 22 23 24 25 26

			# Allow for semi-colon at the end
			if assertionTokens.length != 2
				continue

			# Valid assertion, process it…
			numAssertions++
27 28 29 30 31
			propertyName = assertionTokens[0].trim()
			propertyValue = assertionTokens[1].trim()
			computedStyle = window.getComputedStyle(div)
			computedPropertyValue = computedStyle.getPropertyValue(propertyName)

32 33
			# console.log div.style

34 35 36 37 38
			if computedPropertyValue == propertyValue
				numPassedAssertions++
				div.style.backgroundColor = 'green'
			else
				numFailedAssertions++
39
				console.log 'Assertion failed: ' + propertyName + ' should have been ' + propertyValue + ' but was ' + computedPropertyValue + ' instead.'
40 41 42 43
				div.style.backgroundColor = 'red'

			div.style.color = 'white'

44 45
console.log(numAssertions + ' assertions tested. ' + numPassedAssertions + ' passed. ' + numFailedAssertions + ' failed.')