예제 #1
0
def test__indented_blank_lines():

    code = '''#newline
if cond:
  [unplate.begin(template)] @ """

  """ [unplate.end]
'''

    unplate.compile_anon(code)
예제 #2
0
def test__complain_on_missing_space_after_hash():

    code = """
  templ = unplate.template(
    # space
    #no space
    # space
  )
  """

    with pytest.raises(unplate.UnplateSyntaxError):
        unplate.compile_anon(code)
예제 #3
0
def test__complain_on_non_isolated_dedent():

    code = """
  [unplate.begin(template)]
  # >>> for x in l:
    # >>> pass
  # <<< I should not be here
  [unplate.end]
  """

    with pytest.raises(unplate.UnplateSyntaxError):
        unplate.compile_anon(code)
예제 #4
0
def test__complain_on_missing_space_after_prompt():

    code = """
  [unplate.begin(template)]
  # >>> 'space'
  # >>>'no space'
  # >>> 'space'
  [unplate.end]
  """

    with pytest.raises(unplate.UnplateSyntaxError):
        unplate.compile_anon(code)
예제 #5
0
def test__unbalanced_indentation():

    code = """#newline
[unplate.begin(template)] @ '''

>>> oops_no_colon
  >>> stmt
<<<

''' [unplate.end]
"""

    with pytest.raises(unplate.UnplateSyntaxError):
        unplate.compile_anon(code)
예제 #6
0
def test__expr_interpolation():

    code = """#newline
s = "interpolated"
t = unplate.template(
  # {{ s }}
)
assert t == s + '\\n'
  """

    exec(unplate.compile_anon(code))
예제 #7
0
def test__sigils():

    code = """#newline
[unplate.begin(template)] @ r'''
I have a sigil
''' [unplate.end]

assert template == 'I have a sigil\\n', template
"""

    exec(unplate.compile_anon(code))
예제 #8
0
def test__string_templates():

    code = """#newline
[unplate.begin(template)] @ '''
first line
last line
''' [unplate.end]

assert template == 'first line\\nlast line\\n', repr(template)
"""

    exec(unplate.compile_anon(code))
예제 #9
0
def test_builder_misspelling():

    code = '''
[unplate.template(my_template)] @ """
I'm not correct.
Template builders begin with `unplate.begin`,
not `unplate.template`.
""" [unplate.end]
'''

    with pytest.raises(AttributeError):
        exec(unplate.compile_anon(code))
예제 #10
0
def test__bug_0001():

    code = '''
if cond:
  [unplate.begin(template)] @ """
  >>> if cond:
        >>> if cond:
          {{ expr }}
        <<<
        {{ expr }}
  <<<
  """ [unplate.end]
'''

    tku.tokenize_string(unplate.compile_anon(code))
예제 #11
0
def test__custom_interpolation():

    options = unplate.options.Options()
    options.interpolation_open = '<PYTHON>'
    options.interpolation_close = '</PYTHON>'

    code = """#newline
x = 3
template = unplate.template(
  # three is <PYTHON> x </PYTHON> is three
)
assert template == 'three is 3 is three\\n', template
"""

    exec(unplate.compile_anon(code, options))
예제 #12
0
def test__string_templates_indented():

    code = """#newline

if True:
  [unplate.begin(template)] @ '''
  no indent
    one indent
  no indent
  ''' [unplate.end]

  assert template == 'no indent\\n  one indent\\nno indent\\n', repr(template)
"""

    exec(unplate.compile_anon(code))
예제 #13
0
def test__multiple_indent():

    code = """#newline

if True:
  if True:
    if True:

      [unplate.begin(template)] @ '''
      a
        b
      >>> x = 3
        {{ x }}
      c
      ''' [unplate.end]

      assert template == 'a\\n  b\\n  3\\nc\\n', repr(template)
"""

    exec(unplate.compile_anon(code))
예제 #14
0
def unplate(item, ctx):

    # because unplate requires the exact tokens '[unplate.template(' in
    # order to work, we can't write '[unplate_module.template(', so
    # we need to make the following assignment
    # TODO: this is gonna act funny if the payload contains a triple-quote
    unplate = unplate_module
    # add three underscores after 'template' to reduce change of namespace
    # conflict with the payload code
    code = f"""
[unplate.begin(template___)] @ '''
{ item['payload'] }
''' [unplate.end]
  """

    ctx = {**ctx}
    exec(unplate_module.compile_anon(code, options=unplate_options), ctx)

    item['payload'] = ctx['template___']
    return item
예제 #15
0
def test__stmt_interpolation():

    code = """#newline
[unplate.begin(template)]
# first line
# >>> for i in range(3):
  # >>> j = i + 1
  # {{ j }}
# <<<
# last line
[unplate.end]

expected = '''first line
1
2
3
last line
'''

assert template == expected, repr(template)
"""

    exec(unplate.compile_anon(code))