Skip to content
Browse files

[util] Test case for `util.remove_comments`

1 parent 7c779e5 commit d445c210eab79effb9f15d5881083686a2661c38 @iblis17 iblis17 committed
Showing with 39 additions and 7 deletions.
  1. +16 −6 couchapp/util.py
  2. +23 −1 tests/test_util.py
View
22 couchapp/util.py
@@ -505,17 +505,27 @@ def hook_uri(uri, cfg):
script_uri = uri
return ShellScript(script_uri)
-regex_comment = r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"'
-re_comment = re.compile(regex_comment, re.DOTALL | re.MULTILINE)
+RE_COMMENT = re.compile(
+ r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"'
+ , re.DOTALL | re.MULTILINE)
-def remove_comments(t):
+
+def remove_comments(text):
+ '''
+ remove comments string in json text
+
+ :param str text: the json text
+ '''
def replace(m):
+ '''
+ :param m: the regex match object
+ '''
s = m.group(0)
- if s.startswith("/"):
- return ""
+ if s.startswith('/'):
+ return ''
return s
- return re.sub(re_comment, replace, t)
+ return re.sub(RE_COMMENT, replace, text)
def sh_open(cmd, bufsize=0):
View
24 tests/test_util.py
@@ -3,7 +3,7 @@
import os
from couchapp.util import discover_apps, iscouchapp, rcpath, split_path
-from couchapp.util import sh_open
+from couchapp.util import sh_open, remove_comments
from mock import patch
@@ -113,3 +113,25 @@ def test_sh_open():
out, err = sh_open('echo mock')
assert out.startswith('mock'), out
assert not err, err
+
+
+def test_remove_comments():
+ text = '''{
+ "mock": 42 // truth
+}'''
+ expect = '{\n "mock": 42 \n}'
+
+ ret = remove_comments(text)
+ assert ret == expect
+
+ # testing for multiline comments
+ text = '''{
+ "mock": 42, // truth
+ /* foo
+ bar
+ */
+ "fake": true}'''
+ ret = remove_comments(text)
+ expect = '{\n "mock": 42, \n \n "fake": true}'
+
+ assert ret == expect

0 comments on commit d445c21

Please sign in to comment.
Something went wrong with that request. Please try again.