Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a simple unit test

  • Loading branch information...
commit c813071e948e0c95017c03ae446e35b222d55643 1 parent 9b66f6c
@rstacruz authored
View
1  .gitignore
@@ -4,5 +4,6 @@ doc
.sourcescribe_index
*.swp
*.swo
+*.pyc
cscope.out
*~
View
20 TextMate/Sparkup.tmbundle/Support/sparkup.py
@@ -261,6 +261,7 @@ def __init__(self, options=None, str='', dialect=HtmlDialect()):
"""Constructor.
"""
+ self.tokens = []
self.str = str
self.options = options
self.dialect = dialect
@@ -904,20 +905,20 @@ def __init__(self):
# Methods
# ---------------------------------------------------------------------------
- def start(self, options=None):
+ def start(self, options=None, str=None, ret=None):
if (options):
self.options = Options(router=self, options=options, argv=None)
else:
self.options = Options(router=self, argv=sys.argv[1:], options=None)
if (self.options.has('help')):
- self.help()
+ return self.help()
elif (self.options.has('version')):
- self.version()
+ return self.version()
else:
- self.parse()
+ return self.parse(str=str, ret=ret)
def help(self):
print "Usage: %s [OPTIONS]" % sys.argv[0]
@@ -935,15 +936,17 @@ def help(self):
def version(self):
print "Uhm, yeah."
- def parse(self):
+ def parse(self, str=None, ret=None):
self.parser = Parser(self.options)
try:
# Read the files
- lines = []
# for line in fileinput.input(): lines.append(line.rstrip(os.linesep))
- lines = [sys.stdin.read()]
- lines = " ".join(lines)
+ if str is not None:
+ lines = str
+ else:
+ lines = [sys.stdin.read()]
+ lines = " ".join(lines)
except KeyboardInterrupt:
pass
@@ -955,6 +958,7 @@ def parse(self):
try:
self.parser.load_string(lines)
output = self.parser.render()
+ if ret: return output
sys.stdout.write(output)
except:
View
20 sparkup
@@ -261,6 +261,7 @@ class Parser:
"""Constructor.
"""
+ self.tokens = []
self.str = str
self.options = options
self.dialect = dialect
@@ -904,20 +905,20 @@ class Router:
# Methods
# ---------------------------------------------------------------------------
- def start(self, options=None):
+ def start(self, options=None, str=None, ret=None):
if (options):
self.options = Options(router=self, options=options, argv=None)
else:
self.options = Options(router=self, argv=sys.argv[1:], options=None)
if (self.options.has('help')):
- self.help()
+ return self.help()
elif (self.options.has('version')):
- self.version()
+ return self.version()
else:
- self.parse()
+ return self.parse(str=str, ret=ret)
def help(self):
print "Usage: %s [OPTIONS]" % sys.argv[0]
@@ -935,15 +936,17 @@ class Router:
def version(self):
print "Uhm, yeah."
- def parse(self):
+ def parse(self, str=None, ret=None):
self.parser = Parser(self.options)
try:
# Read the files
- lines = []
# for line in fileinput.input(): lines.append(line.rstrip(os.linesep))
- lines = [sys.stdin.read()]
- lines = " ".join(lines)
+ if str is not None:
+ lines = str
+ else:
+ lines = [sys.stdin.read()]
+ lines = " ".join(lines)
except KeyboardInterrupt:
pass
@@ -955,6 +958,7 @@ class Router:
try:
self.parser.load_string(lines)
output = self.parser.render()
+ if ret: return output
sys.stdout.write(output)
except:
View
1  sparkup.py
View
126 unittest.py
@@ -0,0 +1,126 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+import sys
+from cStringIO import StringIO
+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'}
+ }
+ cases = {
+ 'Simple test': {
+ 'options': 'default',
+ 'input': 'div',
+ 'output': '<div>$1</div>$0'
+ },
+ 'Class test': {
+ 'input': 'div.lol',
+ 'output': '<div class="lol">$1</div><!-- /.lol -->$0'
+ },
+ 'ID and class test': {
+ 'input': 'div.class#id',
+ 'output': '<div class="class" id="id">$1</div><!-- /#id -->$0'
+ },
+ 'ID and class test 2': {
+ 'input': 'div#id.class',
+ 'output': '<div class="class" id="id">$1</div><!-- /#id -->$0'
+ },
+ 'Attributes test': {
+ 'input': 'div#id.class[style=color:blue]',
+ 'output': '<div style="color:blue" class="class" id="id">$1</div><!-- /#id -->$0'
+ },
+ 'Multiple class test': {
+ 'input': 'div.c1.c2.c3',
+ 'output': '<div class="c1 c2 c3">$1</div><!-- /.c1.c2.c3 -->$0'
+ },
+ 'Shortcut test': {
+ 'input': 'input:button',
+ 'output': '<input type="button" class="button" value="$1" name="$2" />$0'
+ },
+ 'Shortcut synonym test': {
+ 'input': 'button',
+ 'output': '<input type="button" class="button" value="$1" name="$2" />$0'
+ },
+ 'Child test': {
+ 'input': 'div>ul>li',
+ 'output': "<div>\n <ul>\n <li>$1</li>\n </ul>\n</div>$0"
+ },
+ 'Sibling test': {
+ 'input': 'div#x + ul+ h3.class',
+ 'output': '<div id="x">$1</div><!-- /#x -->\n<ul>$2</ul>\n<h3 class="class">$3</h3>$0'
+ },
+ 'Child + sibling test': {
+ 'input': 'div > ul > li + span',
+ 'output': '<div>\n <ul>\n <li>$1</li>\n <span>$2</span>\n </ul>\n</div>$0'
+ },
+ 'Multiplier test 1': {
+ 'input': 'ul > li*3',
+ 'output': '<ul>\n <li>$1</li>\n <li>$2</li>\n <li>$3</li>\n</ul>$0'
+ },
+ 'Multiplier test 2': {
+ 'input': 'ul > li.item-$*3',
+ 'output': '<ul>\n <li class="item-1">$1</li>\n <li class="item-2">$2</li>\n <li class="item-3">$3</li>\n</ul>$0'
+ },
+ 'Multiplier test 3': {
+ 'input': 'ul > li.item-$*3 > a',
+ 'output': '<ul>\n <li class="item-1">\n <a href="$1">$2</a>\n </li>\n <li class="item-2">\n <a href="$3">$4</a>\n </li>\n <li class="item-3">\n <a href="$5">$6</a>\n </li>\n</ul>$0'
+ },
+ 'Ampersand test': {
+ 'input': 'td > tr.row-$*3 > td.cell-&*2',
+ 'output': '<td>\n <tr class="row-1">\n <td class="cell-1">$1</td>\n <td class="cell-2">$2</td>\n </tr>\n <tr class="row-2">\n <td class="cell-3">$3</td>\n <td class="cell-4">$4</td>\n </tr>\n <tr class="row-3">\n <td class="cell-5">$5</td>\n <td class="cell-6">$6</td>\n </tr>\n</td>$0'
+ },
+ 'Menu test': {
+ 'input': 'ul#menu > li*3 > a > span',
+ 'output': '<ul id="menu">\n <li>\n <a href="$1">\n <span>$2</span>\n </a>\n </li>\n <li>\n <a href="$3">\n <span>$4</span>\n </a>\n </li>\n <li>\n <a href="$5">\n <span>$6</span>\n </a>\n </li>\n</ul>$0'
+ },
+ 'Back test': {
+ 'input': 'ul#menu > li*3 > a < < div',
+ 'output': '<ul id="menu">\n <li>\n <a href="$1">$2</a>\n </li>\n <li>\n <a href="$3">$4</a>\n </li>\n <li>\n <a href="$5">$6</a>\n </li>\n</ul>\n<div>$7</div>$0'
+ },
+ 'Expand test': {
+ 'input': 'p#menu > table+ + ul',
+ 'output': '<p id="menu">\n <table cellspacing="0">\n <tr>\n <td>$1</td>\n </tr>\n </table>\n <ul>$2</ul>\n</p>$0'
+ },
+ }
+ def run(self):
+ """Run Forrest run!"""
+
+ 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:
+ 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| ")
+
+if __name__ == '__main__':
+ s = SparkupTest()
+ s.run()
Please sign in to comment.
Something went wrong with that request. Please try again.