def test_id2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)

        self.assertEqual('test_id1', snippet.pattern().id(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
             id="test_id1"'''))

        self.assertEqual(None, snippet.pattern().id(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
            aid="test_id1"'''))

        self.assertEqual(None, snippet.pattern().id(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
            idw="test_id1"'''))

        self.assertEqual(None, snippet.pattern().id('''  output="F" context="TestCtx2" allow_error="F" processor="highlight"  '''))
    def test_id1(self):
        snippet = Snippet(TestSnippetPattern.DATA1)

        self.assertEqual('test_id1', snippet.pattern().id(''' [Python 3]    output=F context=TestCtx2 allow_error=F processor=highlight
             id=test_id1'''))

        self.assertEqual(None, snippet.pattern().id(''' [Python 3]    output=F context=TestCtx2 allow_error=F processor=highlight
            aid=test_id1'''))

        self.assertEqual(None, snippet.pattern().id(''' [Python 3]   output=F context=TestCtx2 allow_error=F processor=highlight
            idw=test_id1'''))

        self.assertEqual(None, snippet.pattern().id('''  output=F context=TestCtx2 allow_error=F processor=highlight  '''))
    def test_language2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)

        self.assertEqual('Python 3', snippet.pattern().language(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
             id="test_id1"'''))

        self.assertEqual(None, snippet.pattern().language(''' aalang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
            id="test_id1"'''))

        self.assertEqual(None, snippet.pattern().language(''' langaa="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
            id="test_id1"'''))

        self.assertEqual(None, snippet.pattern().language('''  output="F" context="TestCtx2" allow_error="F" processor="highlight"
            id="test_id1"'''))
    def test_entry3(self):
        snippet = Snippet(TestSnippetPattern.DATA3)

        entry = snippet.pattern().entry()
        entries = [x for x in entry.finditer(TestSnippetPattern.EXAMPLE3)]

        self.assertEqual(2, len(entries))

        entry = entries[0]
        self.assertNotEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.CODE))

        code = '''
    for i in range(10):
        print(i)

        print('Test')
    '''

        self.assertEqual(code, entry.group(GroupName.CODE))

        settings = '''echo[T], output[T], id[Test]'''

        self.assertEqual(settings, entry.group(GroupName.CODE_SETTINGS))

        entry = entries[1]
        self.assertEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.CODE))

        settings = '''id[Test]'''

        self.assertEqual(settings, entry.group(GroupName.OUTPUT_SETTINGS))
    def test_entry1(self):
        snippet = Snippet(TestSnippetPattern.DATA1)

        entry = snippet.pattern().entry()
        entries = [x for x in entry.finditer(TestSnippetPattern.EXAMPLE1)]

        self.assertEqual(2, len(entries))

        entry = entries[0]
        self.assertNotEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.CODE))

        code = '''
for i in range(10):
    print(i)

print('Test')
'''

        self.assertEqual(code, entry.group(GroupName.CODE))

        settings = '''[Python 3] echo=T output=T id=Abcde5'''

        self.assertEqual(settings, entry.group(GroupName.CODE_SETTINGS))

        entry = entries[1]
        self.assertEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.CODE))

        settings = '''id=Abcde5'''

        self.assertEqual(settings, entry.group(GroupName.OUTPUT_SETTINGS))
    def test_echo_lines2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)

        lines = snippet.pattern().echo_lines('echo_lines="5,6, 10"')
        self.assertEqual(2, len(lines))
        self.assertFalse(lines[0])
        self.assertEqual(3, len(lines[1]))
        self.assertEqual(6, lines[1][1])

        lines = snippet.pattern().echo_lines('echo_lines="1, 5-15, 23"')
        self.assertEqual(2, len(lines))
        self.assertFalse(lines[0])
        self.assertEqual(13, len(lines[1]))
        self.assertEqual(7, lines[1][3])

        lines = snippet.pattern().echo_lines('echo_lines="! 1, 5 - 15, 23"')
        self.assertEqual(2, len(lines))
        self.assertTrue(lines[0])
        self.assertEqual(13, len(lines[1]))
        self.assertEqual(7, lines[1][3])

        with self.assertRaises(ValueError):
            lines = snippet.pattern().echo_lines('echo_lines="! 1, 5 - 15, 23 aqqw"')
    def test_default_settings2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)
        entry = snippet.pattern().default_settings()
        entries = [x for x in entry.finditer(TestSnippetPattern.EXAMPLE2)]

        self.assertEqual(1, len(entries))

        entry = entries[0]

        self.assertNotEqual(None, entry.group(GroupName.DEFAULT_SETTINGS_SNIPPET))

        settings = ''' lang="Python 3" '''

        self.assertEqual(settings, entry.group(GroupName.DEFAULT_SETTINGS))
    def test_timeout2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)

        self.assertEqual(2.5, snippet.pattern().timeout(''' lang="Python 3"    output="F" context="TestCtx2" timeout="2500" allow_error="F" processor="highlight"
             id="test_id1"'''))

        self.assertEqual(None, snippet.pattern().timeout(''' lang="Python 3"  aatimeout="2500"   output="F" context="TestCtx2" allow_error="F" processor="highlight"
            aid="test_id1"'''))

        self.assertEqual(None, snippet.pattern().timeout(''' lang="Python 3"  timeoutaa="2500"  output="F" context="TestCtx2" allow_error="F" processor="highlight"
            idw="test_id1"'''))

        self.assertEqual(None, snippet.pattern().timeout('''  output="F" context="TestCtx2" allow_error="F" processor="highlight"  '''))

        with self.assertRaises(TimeoutValueError):
            snippet.pattern().timeout(''' lang="Python 3"  timeout="invalid"  output="F" context="TestCtx2" allow_error="F" processor="highlight"
            idw="test_id1"''')
    def test_timeout1(self):
        snippet = Snippet(TestSnippetPattern.DATA1)

        self.assertEqual(2.5, snippet.pattern().timeout(''' [Python 3]    output=F context=TestCtx2 timeout=2500 allow_error=F processor=highlight
             id="test_id1"'''))

        self.assertEqual(None, snippet.pattern().timeout(''' [Python 3]  aatimeout=2500   output=F context=TestCtx2 allow_error="F" processor=highlight
            aid="test_id1"'''))

        self.assertEqual(None, snippet.pattern().timeout(''' [Python 3]  timeoutaa=2500  output=F context=TestCtx2 allow_error=F processor=highlight
            idw="test_id1"'''))

        self.assertEqual(None, snippet.pattern().timeout('''  output=F context=TestCtx2 allow_error=F processor=highlight  '''))

        with self.assertRaises(TimeoutValueError):
            snippet.pattern().timeout(''' [Python 3]  timeout=invalid  output=F context=TestCtx2 allow_error=F processor=highlight
            idw=test_id1''')
    def test_output_type2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)

        self.assertTrue(snippet.pattern().output_type(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" output_type="Stdout, Image" processor="highlight"
             id="test_id1"''').is_enabled('Stdout'))

        self.assertTrue(snippet.pattern().output_type(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" output_type="Stdout, Image" processor="highlight"
             id="test_id1"''').is_enabled('Image'))

        self.assertFalse(snippet.pattern().output_type(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" output_type="Stdout, Image" processor="highlight"
             id="test_id1"''').is_enabled('Stderr'))

        self.assertFalse(snippet.pattern().output_type(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" output_type="Stdout, Image" processor="highlight"
             id="test_id1"''').is_enabled('Text'))

        self.assertTrue(snippet.pattern().output_type(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
             id="test_id1"''').is_enabled('Stdout'))

        self.assertTrue(snippet.pattern().output_type(''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
             id="test_id1"''').is_enabled('Image'))
    def test_output_type1(self):
        snippet = Snippet(TestSnippetPattern.DATA1)

        self.assertTrue(snippet.pattern().output_type(''' [Python 3]    output=F context=TestCtx2 allow_error=F output_type=Stdout,Image processor=highlight
             id=test_id1''').is_enabled('Stdout'))

        self.assertTrue(snippet.pattern().output_type(''' [Python 3]    output=F context=TestCtx2 allow_error=F output_type=Stdout,Image processor=highlight
             id=test_id1''').is_enabled('Image'))

        self.assertFalse(snippet.pattern().output_type(''' [Python 3]    output=F context=TestCtx2 allow_error=F output_type=Stdout,Image processor=highlight
             id=test_id1''').is_enabled('Stderr'))

        self.assertFalse(snippet.pattern().output_type(''' [Python 3]    output=F context=TestCtx2 allow_error=F output_type=Stdout,Image processor=highlight
             id=test_id1''').is_enabled('Text'))

        self.assertTrue(snippet.pattern().output_type(''' [Python 3]    output=F context=TestCtx2 allow_error=F processor=highlight
             id=test_id1''').is_enabled('Stdout'))

        self.assertTrue(snippet.pattern().output_type(''' [Python 3]    output=F context=TestCtx2 allow_error=F processor=highlight
             id=test_id1''').is_enabled('Image'))
    def test_error2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)

        self.assertEqual(None, snippet.pattern().error(''' lang="Python 3"    output="F" context="TestCtx2"  processor="highlight"
                     id="test_id1"'''))

        self.assertTrue(snippet.pattern().error(''' lang="Python 3"    output="F" context="TestCtx2" echo="T" allow_error="1" processor="highlight"
                     id="test_id1"'''))

        self.assertEqual(None, snippet.pattern().error(''' lang="Python 3"    output="F" aecho="1" echoqq="False" context="TestCtx2" aallow_error="F" allow_errorq="1" processor="highlight"
                    id="test_id1"'''))

        self.assertTrue(snippet.pattern().error('allow_error="T"'))
        self.assertTrue(snippet.pattern().error('allow_error="1"'))
        self.assertTrue(snippet.pattern().error('allow_error="True"'))
        self.assertTrue(snippet.pattern().error('allow_error="Y"'))
        self.assertTrue(snippet.pattern().error('allow_error="t"'))
        self.assertTrue(snippet.pattern().error('allow_error="y"'))
        self.assertTrue(snippet.pattern().error('allow_error="YES"'))
        self.assertTrue(snippet.pattern().error('allow_error="yes"'))

        self.assertFalse(snippet.pattern().error('allow_error="F"'))
        self.assertFalse(snippet.pattern().error('allow_error="False"'))
        self.assertFalse(snippet.pattern().error('allow_error="0"'))
        self.assertFalse(snippet.pattern().error('allow_error="N"'))
        self.assertFalse(snippet.pattern().error(' allow_error="No"'))
        self.assertFalse(snippet.pattern().error('allow_error="no" '))

        with self.assertRaises(InvalidBoolValueError):
            snippet.pattern().error('allow_error="X"')
    def test_error1(self):
        snippet = Snippet(TestSnippetPattern.DATA1)

        self.assertEqual(None, snippet.pattern().error(''' [Python 3]    output=F context=TestCtx2  processor=highlight
                     id=test_id1'''))

        self.assertTrue(snippet.pattern().error(''' [Python 3]    output=F context=TestCtx2 echo=T allow_error=1 processor=highlight
                     id=test_id1'''))

        self.assertEqual(None, snippet.pattern().error(''' [Python 3]   output=F aecho=1 echoqq=False context=TestCtx2 aallow_error=F allow_errorq=1 processor=highlight
                    id=test_id1'''))

        self.assertTrue(snippet.pattern().error('allow_error=T'))
        self.assertTrue(snippet.pattern().error('allow_error=1'))
        self.assertTrue(snippet.pattern().error('allow_error=True'))
        self.assertTrue(snippet.pattern().error('allow_error=Y'))
        self.assertTrue(snippet.pattern().error('allow_error=t'))
        self.assertTrue(snippet.pattern().error('allow_error=y'))
        self.assertTrue(snippet.pattern().error('allow_error=YES'))
        self.assertTrue(snippet.pattern().error('allow_error=yes'))

        self.assertFalse(snippet.pattern().error('allow_error=F'))
        self.assertFalse(snippet.pattern().error('allow_error=False'))
        self.assertFalse(snippet.pattern().error('allow_error=0'))
        self.assertFalse(snippet.pattern().error('allow_error=N'))
        self.assertFalse(snippet.pattern().error(' allow_error=No'))
        self.assertFalse(snippet.pattern().error('allow_error=no '))

        with self.assertRaises(InvalidBoolValueError):
            snippet.pattern().error('allow_error=X')
    def test_output1(self):
        snippet = Snippet(TestSnippetPattern.DATA1)

        self.assertEqual(None, snippet.pattern().output(''' [Python 3]  context=TestCtx2 allow_error=F processor=highlight
                     id=test_id1'''))

        self.assertFalse(snippet.pattern().output(''' [Python 3]output=F context=TestCtx2 echo=T allow_error=F processor=highlight
                     id=test_id1'''))

        self.assertEqual(None, snippet.pattern().output(''' [Python 3]   qqoutput=F outputww=0 context=TestCtx2 allow_error=F processor=highlight
                    id=test_id1'''))

        self.assertTrue(snippet.pattern().output('output=T'))
        self.assertTrue(snippet.pattern().output('output=1'))
        self.assertTrue(snippet.pattern().output('output=True'))
        self.assertTrue(snippet.pattern().output('output=Y'))
        self.assertTrue(snippet.pattern().output('output=t'))
        self.assertTrue(snippet.pattern().output('output=y'))
        self.assertTrue(snippet.pattern().output('output=YES'))
        self.assertTrue(snippet.pattern().output('output=yes'))

        self.assertFalse(snippet.pattern().output('output=F'))
        self.assertFalse(snippet.pattern().output('output=False '))
        self.assertFalse(snippet.pattern().output('output=0'))
        self.assertFalse(snippet.pattern().output('output=N'))
        self.assertFalse(snippet.pattern().output(' output=No'))
        self.assertFalse(snippet.pattern().output('output=no '))

        with self.assertRaises(InvalidBoolValueError):
            snippet.pattern().output('output="X"')
    def test_entry2(self):
        snippet = Snippet(TestSnippetPattern.DATA2)
        entry = snippet.pattern().entry()
        entries = [x for x in entry.finditer(TestSnippetPattern.EXAMPLE2)]

        self.assertEqual(4, len(entries))

        entry = entries[0]
        self.assertNotEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.CODE))

        code = """
%matplotlib inline
import matplotlib
import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return x**2

x = np.linspace(0, 3*np.pi, 500)
plt.plot(x, np.sin(f(x)))
plt.title('A simple chirp')
print('before')
plt.show()
print('after')
    """

        self.assertEqual(code, entry.group(GroupName.CODE))

        settings = ''' output="T" timeout="20000" echo="True" output_type="All" processor="highlight" lines="6:11,13"'''

        self.assertEqual(settings, entry.group(GroupName.CODE_SETTINGS))

        entry = entries[1]
        self.assertNotEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.CODE))

        code = """
for i in range(5):
    print(i)

j = 100
j
    """

        self.assertEqual(code, entry.group(GroupName.CODE))

        settings = ''' lang="Python 3"    output="F" context="TestCtx2" allow_error="F" processor="highlight"
             id="test_id1"'''

        self.assertEqual(settings, entry.group(GroupName.CODE_SETTINGS))

        entry = entries[2]
        self.assertEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.CODE))

        settings = '''
            id="test_id1" '''

        self.assertEqual(settings, entry.group(GroupName.OUTPUT_SETTINGS))

        entry = entries[3]
        self.assertNotEqual(None, entry.group(GroupName.CODE_SNIPPET))
        self.assertEqual(None, entry.group(GroupName.OUTPUT_SNIPPET))
        self.assertNotEqual(None, entry.group(GroupName.CODE))

        code = """
        !sqlite:///:memory:;
        CREATE TABLE test (id, aa, bb);
        INSERT INTO test VALUES (1, 'test1', 'test2');
        INSERT INTO test VALUES (2, 'test2', 'test2');
        SELECT * FROM test;
        SELECT 5;
        DELETE FROM test WHERE id = 2;
        SELECT * FROM test;
    """

        self.assertEqual(code, entry.group(GroupName.CODE))

        settings = ''' lang="SQL" lines="!1"'''

        self.assertEqual(settings, entry.group(GroupName.CODE_SETTINGS))