Skip to content

Commit

Permalink
Python version now passes all unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Jan 22, 2011
1 parent 3ed6a78 commit 831743e
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 146 deletions.
3 changes: 3 additions & 0 deletions .settings/org.eclipse.ltk.core.refactoring.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Sat Jan 22 16:44:00 MSK 2011
eclipse.preferences.version=1
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
22 changes: 0 additions & 22 deletions python/unittest/test_cssex.py

This file was deleted.

16 changes: 0 additions & 16 deletions python/unittest/test_decorators.py

This file was deleted.

41 changes: 20 additions & 21 deletions python/unittest/test_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
@author: sergey
'''
import unittest

from zencoding import zen_core as zen
from zencoding import zen_resources
import zencoding.utils
import zencoding.resources

my_zen_settings = {
'html': {
Expand All @@ -16,14 +15,14 @@
}
}

zen.set_caret_placeholder('|')
zen_resources.set_vocabulary(my_zen_settings, zen_resources.VOC_USER)
zencoding.utils.set_caret_placeholder('|')
zencoding.resources.set_vocabulary(my_zen_settings, zencoding.resources.VOC_USER)

def expandAbbr(abbr, syntax='html', profile_name='plain'):
return zen.expand_abbreviation(abbr, syntax, profile_name)
return zencoding.expand_abbreviation(abbr, syntax, profile_name)

def extractAbbr(line):
return zen.extract_abbreviation(line)
return zencoding.utils.extract_abbreviation(line)

class Test(unittest.TestCase):

Expand Down Expand Up @@ -114,11 +113,11 @@ def testNonExistedTypes(self):
self.assertEqual('<bq><p></p></bq>', expandAbbr('bq>p', 'foo'))

def testTagHit(self):
self.assertEqual(True, zen.is_inside_tag('hello<div>world', 7))
self.assertEqual(True, zen.is_inside_tag('hello<br />world', 7))
self.assertEqual(True, zen.is_inside_tag('hello</p>world', 7))
self.assertEqual(False, zen.is_inside_tag('hello<div>world', 10))
self.assertEqual(False, zen.is_inside_tag('hello<div>world', 1))
self.assertEqual(True, zencoding.utils.is_inside_tag('hello<div>world', 7))
self.assertEqual(True, zencoding.utils.is_inside_tag('hello<br />world', 7))
self.assertEqual(True, zencoding.utils.is_inside_tag('hello</p>world', 7))
self.assertEqual(False, zencoding.utils.is_inside_tag('hello<div>world', 10))
self.assertEqual(False, zencoding.utils.is_inside_tag('hello<div>world', 1))

def testFormatting(self):
self.assertEqual('<blockquote>\n\t<p>|</p>\n</blockquote>', expandAbbr('bq>p', 'html', 'xhtml'));
Expand All @@ -141,22 +140,22 @@ def testGroups(self):
self.assertEqual('<div><i></i><b></b><i></i><b></b><span></span><em></em><span></span><em></em><span></span><em></em></div>', expandAbbr('div>(i+b)*2+(span+em)*3'));

def testEscaping(self):
self.assertEqual('<xsl:apply-templates select="\\$item \\| other"/>', zen.escape_text('<xsl:apply-templates select="$item | other"/>'))
self.assertEqual('<xsl:apply-templates select="item \\\\\\\\\\| other"/>', zen.escape_text('<xsl:apply-templates select="item \\\\| other"/>'))
self.assertEqual('<xsl:apply-templates select="\\$item \\| other"/>', zencoding.utils.escape_text('<xsl:apply-templates select="$item | other"/>'))
self.assertEqual('<xsl:apply-templates select="item \\\\\\\\\\| other"/>', zencoding.utils.escape_text('<xsl:apply-templates select="item \\\\| other"/>'))

def testUnescaping(self):
self.assertEqual('<xsl:apply-templates select="$item | other"/>', zen.unescape_text('<xsl:apply-templates select="\\$item \\| other"/>'))
self.assertEqual('<xsl:apply-templates select="item \\\\| other"/>', zen.unescape_text('<xsl:apply-templates select="item \\\\\\\\\\| other"/>'))
self.assertEqual('<xsl:apply-templates select="$item | other"/>', zencoding.utils.unescape_text('<xsl:apply-templates select="\\$item \\| other"/>'))
self.assertEqual('<xsl:apply-templates select="item \\\\| other"/>', zencoding.utils.unescape_text('<xsl:apply-templates select="item \\\\\\\\\\| other"/>'))

def testExtract(self):
abbr = 'ul#nav>li.$$item$$$*3>a+span'
abbr2 = 'table>tr>td[colspan=2 title="Hello world"]>span'

self.assertEqual(abbr, zen.extract_abbreviation(abbr));
self.assertEqual(abbr, zen.extract_abbreviation('<p>' + abbr))
self.assertEqual(abbr, zen.extract_abbreviation('hello ' + abbr))
self.assertEqual(abbr2, zen.extract_abbreviation('<div>' + abbr2))
self.assertEqual(abbr2, zen.extract_abbreviation('hello ' + abbr2))
self.assertEqual(abbr, zencoding.utils.extract_abbreviation(abbr));
self.assertEqual(abbr, zencoding.utils.extract_abbreviation('<p>' + abbr))
self.assertEqual(abbr, zencoding.utils.extract_abbreviation('hello ' + abbr))
self.assertEqual(abbr2, zencoding.utils.extract_abbreviation('<div>' + abbr2))
self.assertEqual(abbr2, zencoding.utils.extract_abbreviation('hello ' + abbr2))

def testShortNotation(self):
self.assertEqual('<div id="content"></div>', expandAbbr('#content'))
Expand Down
14 changes: 7 additions & 7 deletions python/unittest/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
@author: sergey
'''
import unittest
from zencoding import zen_core as zen
from zencoding import zen_file
import zencoding.utils
import zencoding.interface.file as zen_file

class Test(unittest.TestCase):

def testImageSize(self):
size = {'width': 25, 'height': 25}

self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.png')))
self.assertEqual({'width': 125, 'height': 125}, zen.get_image_size(zen_file.read('./assets/sample2.png')))
self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.gif')))
self.assertEqual({'width': 300, 'height': 300}, zen.get_image_size(zen_file.read('./assets/sample2.gif')))
self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.jpg')))
self.assertEqual(size, zencoding.utils.get_image_size(zen_file.read('./assets/sample.png')))
self.assertEqual({'width': 125, 'height': 125}, zencoding.utils.get_image_size(zen_file.read('./assets/sample2.png')))
self.assertEqual(size, zencoding.utils.get_image_size(zen_file.read('./assets/sample.gif')))
self.assertEqual({'width': 300, 'height': 300}, zencoding.utils.get_image_size(zen_file.read('./assets/sample2.gif')))
self.assertEqual(size, zencoding.utils.get_image_size(zen_file.read('./assets/sample.jpg')))


if __name__ == "__main__":
Expand Down
11 changes: 5 additions & 6 deletions python/unittest/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
@author: sergey
'''
import unittest
from zencoding import zen_resources
from zencoding import zen_actions
import zencoding.zen_core as zen
import zencoding.utils
import zencoding.resources as zen_resources

test_obj = {
'key1': {
Expand All @@ -31,9 +30,9 @@ def testNotExistedByKey(self):
def testNotExistedTopLevel(self):
self.assertFalse(zen_resources.has_deep_key(test_obj, 'key2'))

def testActions(self):
self.assertTrue('2.65', zen_actions.prettify_number(2.65345))
self.assertTrue('2', zen_actions.prettify_number(2.00065))
# def testActions(self):
self.assertTrue('2.65', zencoding.utils.prettify_number(2.65345))
self.assertTrue('2', zencoding.utils.prettify_number(2.00065))


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions python/unittest/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
@author: sergey
'''
from zencoding import zen_parser
from zencoding.parser.abbreviation import parse

if __name__ == "__main__":
print(zen_parser.parse('a.sample+b[title=Hello]>a+(span{Test}+div)+em'))
print(parse('a.sample+b[title=Hello]>a+(span{Test}+div)+em'))
print('=============')
print(zen_parser.parse('a.sample+b[title=Hello]>a+(span{Test}+div+(a+b)+a)+em'))
print(parse('a.sample+b[title=Hello]>a+(span{Test}+div+(a+b)+a)+em'))
print('=============')
print(zen_parser.parse('a.sample+b[title=Hello]>a+(span{Test}+div>(a>b)+a)+em'))
print(parse('a.sample+b[title=Hello]>a+(span{Test}+div>(a>b)+a)+em'))
print('=============')
12 changes: 3 additions & 9 deletions python/unittest/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@
@author: sergey
'''
from zencoding.zen_settings import zen_settings
from zencoding import zen_core as zen
from zencoding import zen_resources
import zencoding.utils

zen.set_caret_placeholder('|')
zencoding.utils.set_caret_placeholder('|')

def expandAbbr(abbr, doc_type='html', profile_name='plain'):
return zen.expand_abbreviation(abbr, doc_type, profile_name)

zen_resources.set_vocabulary(zen_settings, zen_resources.VOC_SYSTEM)
#print(zen_resources.get_abbreviation('html', 'a'))
#print(zen_resources.get_snippet('html', 'cc:ie6'))
#print(zen_resources.get_variable('lang'))

#print(expandAbbr('p.name+p+a'))
print(expandAbbr('span>{Hello world}'))
print(zencoding.expand_abbreviation('span>{Hello world}'))
16 changes: 3 additions & 13 deletions python/unittest/test_wrapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,13 @@
'''
import unittest
import sys
from zencoding import zen_core as zen
from zencoding import zen_resources
from zencoding.zen_settings import zen_settings
import zencoding.utils

zen.set_caret_placeholder('|')
zen_resources.set_vocabulary(zen_settings, zen_resources.VOC_SYSTEM)

def expandAbbr(abbr, doc_type='html', profile_name='plain'):
return zen.expand_abbreviation(abbr, doc_type, profile_name)

def extractAbbr(line):
return zen.extract_abbreviation(line)
zencoding.utils.set_caret_placeholder('|')


def wrap(abbr, content):
return zen.wrap_with_abbreviation(abbr, content)

return zencoding.wrap_with_abbreviation(abbr, content)

class Test(unittest.TestCase):

Expand Down
46 changes: 41 additions & 5 deletions python/zencoding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def filter(name=None, filter_func=None):
else:
# @zencoding.filter('somename') or @zencoding.filter(name='somename')
def dec(func):
return action(name, func)
return filter(name, func)
return dec
elif name != None and filter_func != None:
# zencoding.filter('somename', somefunc)
Expand All @@ -67,7 +67,7 @@ def run_action(name, *args, **kwargs):
zencoding.run_actions('expand_abbreviation', zen_editor)
zencoding.run_actions('wrap_with_abbreviation', zen_editor, 'div')
"""
from zencoding.actions import *
import_pack('zencoding.actions')
if name in __actions:
__actions[name](*args, **kwargs)

Expand All @@ -79,8 +79,7 @@ def run_filters(tree, profile, filter_list):
@param filter_list: str, list
@return: ZenNode
"""
from zencoding.filters import *

import_pack('zencoding.filters')
profile = utils.process_profile(profile)

if isinstance(filter_list, basestring):
Expand All @@ -93,11 +92,48 @@ def run_filters(tree, profile, filter_list):

return tree

def import_pack(name):
__import__(name, globals(), locals(), ['*'], -1)

def expand_abbreviation(abbr, syntax='html', profile_name='plain'):
"""
Expands abbreviation into a XHTML tag string
@type abbr: str
@return: str
"""
return utils.expand_abbreviation(abbr, syntax, profile_name)
tree_root = utils.parse_into_tree(abbr, syntax)
if tree_root:
tree = utils.rollout_tree(tree_root)
utils.apply_filters(tree, syntax, profile_name, tree_root.filters)
return utils.replace_variables(tree.to_string())

return ''

def wrap_with_abbreviation(abbr, text, syntax='html', profile='plain'):
"""
Wraps passed text with abbreviation. Text will be placed inside last
expanded element
@param abbr: Abbreviation
@type abbr: str
@param text: Text to wrap
@type text: str
@param syntax: Document type (html, xml, etc.)
@type syntax: str
@param profile: Output profile's name.
@type profile: str
@return {String}
"""
tree_root = utils.parse_into_tree(abbr, syntax)
if tree_root:
repeat_elem = tree_root.multiply_elem or tree_root.last
repeat_elem.set_paste_content(text)
repeat_elem.repeat_by_lines = bool(tree_root.multiply_elem)

tree = utils.rollout_tree(tree_root)
utils.apply_filters(tree, syntax, profile, tree_root.filters)
return utils.replace_variables(tree.to_string())

return None
46 changes: 3 additions & 43 deletions python/zencoding/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,6 @@ def process_profile(profile):

return _profile

def expand_abbreviation(abbr, syntax='html', profile_name='plain'):
"""
Expands abbreviation into a XHTML tag string
@type abbr: str
@return: str
"""
tree_root = parse_into_tree(abbr, syntax);
if tree_root:
tree = rollout_tree(tree_root)
apply_filters(tree, syntax, profile_name, tree_root.filters)
return replace_variables(tree.to_string())

return ''

def extract_abbreviation(text):
"""
Extracts abbreviations from text stream, starting from the end
Expand Down Expand Up @@ -513,35 +499,6 @@ def is_inside_tag(html, cursor_pos):

return False

def wrap_with_abbreviation(abbr, text, syntax='html', profile='plain'):
"""
Wraps passed text with abbreviation. Text will be placed inside last
expanded element
@param abbr: Abbreviation
@type abbr: str
@param text: Text to wrap
@type text: str
@param syntax: Document type (html, xml, etc.)
@type syntax: str
@param profile: Output profile's name.
@type profile: str
@return {String}
"""
tree_root = parse_into_tree(abbr, syntax)
if tree_root:
repeat_elem = tree_root.multiply_elem or tree_root.last
repeat_elem.set_paste_content(text)
repeat_elem.repeat_by_lines = bool(tree_root.multiply_elem)

tree = rollout_tree(tree_root)
apply_filters(tree, syntax, profile, tree_root.filters)
return replace_variables(tree.to_string())

return None

def get_caret_placeholder():
"""
Returns caret placeholder
Expand Down Expand Up @@ -1019,6 +976,9 @@ def to_string(self):
content = ''.join([item.to_string() for item in self.children])
return self.start + self.content + content + self.end

def __str__(self):
return self.to_string()

def paste_content(self, text, had_var=0):
"""
Paste content in context of current node. Pasting is a special case
Expand Down

0 comments on commit 831743e

Please sign in to comment.