Skip to content

Commit

Permalink
Merge pull request #20 from rscarvalho/findall-fix-19
Browse files Browse the repository at this point in the history
Fixing bug affecting #19 (findall)
  • Loading branch information
Rodolfo Carvalho committed May 25, 2015
2 parents 411a7b4 + 2546277 commit 3365541
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
9 changes: 3 additions & 6 deletions assets/src/coffee/controllers/regex_parser.coffee
Expand Up @@ -55,16 +55,13 @@ ctrl = (_, RegexResource, RegexBuilder,
pickTemplate('error')

$scope.hasResult = ->
$scope.isResult() and
$scope.currentResult.result != undefined and
$scope.currentResult.result != null
$scope.isResult() and $scope.currentResult.result?

checkResultType = (type) ->
$scope.isResult() and $scope.currentResult.match_type == type
$scope.isResult() and $scope.currentResult.result_type == type

$scope.isError = ->
$scope.currentResult != null and
$scope.currentResult.result_type == 'error'
$scope.currentResult?.result_type == 'error'

$scope.isResult = -> not $scope.isError()
$scope.isFindall = -> checkResultType('findall')
Expand Down
18 changes: 12 additions & 6 deletions pyregex/service.py
Expand Up @@ -4,6 +4,8 @@
import logging
from multiprocessing import Process, Queue

SINGLE_PROCESS = False

class InvalidRegexError(Exception):
def __init__(self, error=None, *args, **kwargs):
super(InvalidRegexError, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -46,11 +48,15 @@ def x(pattern, match_type, flags, test_string, q):

queue = Queue()
args = (self.pattern, self.match_type, self.flags, test_string, queue)
p = Process(target=x, args=args)
p.start()
p.join(self.REGEX_TIMEOUT)
if p.is_alive():
p.terminate()
raise UnprocessibleRegexError()

if SINGLE_PROCESS:
x(*args)
else:
p = Process(target=x, args=args)
p.start()
p.join(self.REGEX_TIMEOUT)
if p.is_alive():
p.terminate()
raise UnprocessibleRegexError()
return queue.get()

8 changes: 8 additions & 0 deletions tests/api_tests.py
Expand Up @@ -34,6 +34,14 @@ def test_regexTestFindall(self):
self.assertEqual('findall', json_body['result_type'])
self.assertEqual(['Hello', 'World'], json_body['result'])

def test_regexTestFindall2(self):
params = regex_params(r'[\w\']+', u'Hey, you - what are you doing here!?', 0, 'findall')
request = build_request('/api/regex/test/', params)
response = request.get_response(app)

json_body = self.get_json_response(response)
self.assertEqual('findall', json_body['result_type'])
self.assertEqual(['Hey', 'you', 'what', 'are', 'you', 'doing', 'here'], json_body['result'])

def test_regexTestFindallNotAMatch(self):
params = regex_params(r'\d+', u'Hello, World!',
Expand Down
8 changes: 3 additions & 5 deletions tests/client/controllers/regex_parser_controller_spec.coffee
Expand Up @@ -47,7 +47,7 @@ describe "RegexParserController", ->
$httpBackend = _$httpBackend_
baseUrl = 'http://localhost:5000/api'
$httpBackend.expectGET(baseUrl + '/regex/test/?' + data).
respond(result_type: 'success', match_type: 'search')
respond(result_type: 'search')

expect($scope.processing).toBe false
$scope.getResults()
Expand All @@ -57,16 +57,14 @@ describe "RegexParserController", ->
expect($scope.processing).toBe false

result = $scope.currentResult
expect(result.result_type).toBe 'success'
expect(result.match_type).toBe 'search'
expect(result.result_type).toBe 'search'

$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()

it "should have correct values for status functions", ->
result = $scope.currentResult
result.result_type = 'success'
result.match_type = 'match'
result.result_type = 'match'

expect($scope.isError()).toBe(false)
expect($scope.isResult()).toBe(true)
Expand Down
7 changes: 7 additions & 0 deletions tests/service_tests.py
Expand Up @@ -40,6 +40,13 @@ def test_findall(self):
result = svc.test("testing")
self.assertIsNone(result)


def test_findall2(self):
svc = RegexService(r'[\w\']+', 'findall', 0)
result = svc.test('Hey, you - what are you doing here!?')
self.assertIsNotNone(result)
self.assertEqual(result, ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here'])

def test_search(self):
svc = RegexService(r'\d+', 'search', 0)
result = svc.test("1984")
Expand Down

0 comments on commit 3365541

Please sign in to comment.