#!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys import sparkup class SparkupTest: options = { 'textmate': True, 'no-last-newline': True, 'post-tag-guides': True, } options = { 'default': {'textmate': True, 'no-last-newline': True, 'post-tag-guides': True}, 'guides': {'textmate': True, 'no-last-newline': True, 'post-tag-guides': True, 'start-guide-format': 'Begin %s'}, 'namespaced-elements': {'textmate': True, 'no-last-newline': True, 'post-tag-guides': True, 'namespaced-elements': True } } cases = { 'Simple test': { 'options': 'default', 'input': 'div', 'output': '
$1
$0' }, 'Class test': { 'input': 'div.lol', 'output': '
$1
$0' }, 'ID and class test': { 'input': 'div.class#id', 'output': '
$1
$0' }, 'ID and class test 2': { 'input': 'div#id.class', 'output': '
$1
$0' }, 'Attributes test': { 'input': 'div#id.class[style=color:blue]', 'output': '
$1
$0' }, 'Multiple attributes test': { 'input': 'div[align=center][style=color:blue][rel=none]', 'output': '
$1
$0' }, 'Multiple class test': { 'input': 'div.c1.c2.c3', 'output': '
$1
$0' }, 'Shortcut test': { 'input': 'input:button', 'output': '$0' }, 'Shortcut synonym test': { 'input': 'button', 'output': '$0', }, 'Child test': { 'input': 'div>ul>li', 'output': "
\n \n
$0" }, 'Sibling test': { 'input': 'div#x + ul+ h3.class', 'output': '
$1
\n\n

$3

$0' }, 'Child + sibling test': { 'input': 'div > ul > li + span', 'output': '
\n \n
$0' }, 'Multiplier test 1': { 'input': 'ul > li*3', 'output': '$0' }, 'Multiplier test 2': { 'input': 'ul > li.item-$*3', 'output': '$0' }, 'Multiplier test 3': { 'input': 'ul > li.item-$*3 > a', 'output': '$0' }, 'Ampersand test': { 'input': 'td > tr.row-$*3 > td.cell-&*2', 'output': '\n \n $1\n $2\n \n \n $3\n $4\n \n \n $5\n $6\n \n$0' }, 'Menu test': { 'input': 'ul#menu > li*3 > a > span', 'output': '$0' }, 'Back test': { 'input': 'ul#menu > li*3 > a < < div', 'output': '\n
$7
$0' }, 'Expand test': { 'input': 'p#menu > table+ + ul', 'output': '\n

$0' }, 'Text with dot test': { 'input': 'p { text.com }', 'output': '

text.com

$0' }, 'Attribute with dot test': { 'input': 'p [attrib=text.com]', 'output': '

$1

$0' }, 'PHP tag test': { 'input': 'php', 'output': '$0', }, 'Eruby tag test': { 'input': 'erb:p', 'output': '<%= %>$0', }, 'ERB block test': { 'input': 'erb:b', 'output': '<% $2 %>\n $1\n<% end %>$0' }, 'Tag name case (#49)': { 'input': 'groupId{foobar}', 'output': 'foobar$0' }, 'Nested curly braces test': { 'input': 'p{{{ title }}}', 'output': '

{{ title }}

$0' }, 'Nested curly braces test (#54)': { 'input': 'html>head>title{${title}}', 'output': '\n \n ${title}\n \n$0' }, 'HTML component element with dash test': { 'input': 'my-html-component', 'output': '$1$0' }, 'XML namespaced element': { 'options': 'namespaced-elements', 'input': 'namespaced-ul', 'output': '$1$0' }, # Add: text test, broken test, multi-attribute tests, indentation test, start and end comments test } def run(self): """Run Forrest run!""" failures = 0 print("Test results:") for name, case in self.cases.iteritems(): try: options_key = case['options'] except: options_key = 'default' try: options = self.options[options_key] except: options = self.options['default'] # Output buffer r = sparkup.Router() input = case['input'] output = r.start(options=options, str=input, ret=True) del r # Did it work? result = output == case['output'] if result: result_str = " OK " else: result_str = "FAIL" print(" - %-30s [%s]" % (name, result_str)) if not result: failures += 1 print("= %s" % input.replace("\n", "\n= ")) print("Actual output (condensed):") print(" | '%s'" % output.replace("\n", r"\n").replace('"', '\"')) print("Actual output:") print(" | %s" % output.replace("\n", "\n | ")) print("Expected:") print(" | %s" % case['output'].replace("\n", "\ n| ")) return failures if __name__ == '__main__': s = SparkupTest() sys.exit(s.run())