#####################################################################################
#
#  Copyright (c) Microsoft Corporation. All rights reserved.
#
#  This source code is subject to terms and conditions of the Shared Source License
#  for IronPython. A copy of the license can be found in the License.html file
#  at the root of this distribution. If you can not locate the Shared Source License
#  for IronPython, please send an email to ironpy@microsoft.com.
#  By using this source code in any fashion, you are agreeing to be bound by
#  the terms of the Shared Source License for IronPython.
#
#  You must not remove this notice, or any other, from this software.
#
######################################################################################

from Util.Debug import *

import re

# None tests

AssertError(TypeError, re.compile, None)

AssertError(TypeError, re.search, None, 'abc')
AssertError(TypeError, re.search, 'abc', None)

AssertError(TypeError, re.match, None, 'abc')
AssertError(TypeError, re.match, 'abc', None)

AssertError(TypeError, re.split, None, 'abc')
AssertError(TypeError, re.split, 'abc', None)

AssertError(TypeError, re.findall, None, 'abc')
AssertError(TypeError, re.findall, 'abc', None)

AssertError(TypeError, re.finditer,None, 'abc')
AssertError(TypeError, re.finditer, 'abc', None)

# Other exceptional input tests

AssertError(TypeError, re.sub, 'abc', None, 'abc')
AssertError(TypeError, re.sub, 'abc', None, None)
AssertError(TypeError, re.sub, None, 'abc', 'abc')
AssertError(TypeError, re.sub, 'abc', 'abc', None)

AssertError(TypeError, re.subn, 'abc', None, 'abc')
AssertError(TypeError, re.subn, 'abc', None, None)
AssertError(TypeError, re.subn, None, 'abc', 'abc')
AssertError(TypeError, re.subn, 'abc', 'abc', None)

AssertError(TypeError, re.escape, None)

x = '\n   #region Generated Foo\nblah\nblah#end region'
a = re.compile("^([ \t]+)#region Generated Foo.*?#end region", re.MULTILINE|re.DOTALL)

AreEqual(a.sub("xx", x), "\nxx")            # should match successfully
AreEqual(a.sub("\\x12", x), "\n\\x12")      # should match, but shouldn't un-escape for \x

a = re.compile('.')
AreEqual(a.groupindex, {})

p = re.compile('.')
z = []
for c in p.finditer('abc'):  z.append((c.start(), c.end()))
z.sort()
AreEqual(z, [(0,1), (1,2), (2,3)])

# BUG 658 sgmllib running on tutorial.htm throws
nonmatchingp = re.compile('x')
AreEqual(nonmatchingp.search('ecks', 1, 4), None)
# /BUG

# BUG 665 Regular expressions: RE_Pattern instance method match(text, pos, endpos) not implemented
AreEqual(p.match('foobar', 1,2).span(), (1,2))
# /BUG
startOfStr = re.compile('^')
AreEqual(startOfStr.match('foobar', 1), None)
AreEqual(startOfStr.match('foobar', 0,0).span(), (0,0))
# BUG 674
#AreEqual(startOfStr.match('foobar', 1,2), None)
# /BUG

# BUG
#AreEqual(startOfStr.match('foobar', endpos=3).span(), (0,0))
#/BUG

# check that groups in split RE are added properly
AreEqual(re.split('{(,)?}', '1 {} 2 {,} 3 {} 4'), ['1 ', None, ' 2 ', ',', ' 3 ', None, ' 4'])

# BUG 637
pnogrp = ','

ptwogrp = '((,))'
csv = '0,1,1,2,3,5,8,13,21,44'
AreEqual(re.split(pnogrp, csv, 1), ['0', csv[2:]])
AreEqual(re.split(pnogrp, csv, 2), ['0','1', csv[4:]])
AreEqual(re.split(pnogrp, csv, 1000), re.split(pnogrp, csv))
AreEqual(re.split(pnogrp, csv, 0), re.split(pnogrp, csv))
AreEqual(re.split(pnogrp, csv, -1), [csv])

ponegrp = '(,)'
AreEqual(re.split(ponegrp, csv, 1), ['0', ',', csv[2:]])
# /BUG

re.compile(re.escape("hi_"))

all = re.compile('(.*)')
AreEqual(all.search('abcdef', 3).group(0), 'def')

AssertError(IndexError, re.match("a[bcd]*b", 'abcbd').group, 1)
AreEqual(re.match('(a[bcd]*b)', 'abcbd').group(1), 'abcb')