Example #1
0
def test_nested_mustache_resolution():
  # straight
  oe = Environment(foo = '{{bar}}', bar = '{{baz}}', baz = 'hello')
  for pattern in ('{{foo}}', '{{bar}}', '{{baz}}', 'hello'):
    resolved, unbound = MustacheParser.resolve('%s world' % pattern, oe)
    assert resolved == 'hello world'
    assert unbound == []

  # in structs
  class Process(Struct):
    name = Required(String)
    cmdline = String

  class Task(Struct):
    name = Default(String, '{{processes[0].name}}')
    processes = List(Process)

  task = Task(processes = [Process(name="hello"), Process(name="world")])
  assert task.name().get() == 'hello'

  # iterably
  resolve_string = '{{foo[{{bar}}]}}'
  resolve_list = List(String)(["hello", "world"])
  resolved, unbound = MustacheParser.resolve(resolve_string, Environment(foo=resolve_list, bar=0))
  assert resolved == 'hello'
  assert unbound == []
  resolved, unbound = MustacheParser.resolve(resolve_string, Environment(foo=resolve_list, bar="0"))
  assert resolved == 'hello'
  assert unbound == []
  resolved, _ = MustacheParser.resolve(resolve_string, Environment(foo=resolve_list, bar=1))
  assert resolved == 'world'
  resolved, unbound = MustacheParser.resolve(resolve_string, Environment(foo=resolve_list, bar=2))
  assert resolved == '{{foo[2]}}'
  assert unbound == [ref('foo[2]')]
Example #2
0
 def interpolate(self):
   if not isinstance(self._value, Compatibility.stringy):
     return self.__class__(self.coerce(self._value)), []
   else:
     joins, unbound = MustacheParser.resolve(self._value, *self.scopes())
     if unbound:
       return self.__class__(joins), [ref for ref in unbound if not self.modulo().covers(ref)]
     else:
       self_copy = self.copy()
       self_copy._value = self_copy.coerce(joins)
       return self_copy, unbound
Example #3
0
def test_nested_mustache_resolution():
    # straight
    oe = Environment(foo='{{bar}}', bar='{{baz}}', baz='hello')
    for pattern in ('{{foo}}', '{{bar}}', '{{baz}}', 'hello'):
        resolved, unbound = MustacheParser.resolve('%s world' % pattern, oe)
        assert resolved == 'hello world'
        assert unbound == []

    # in structs
    class Process(Struct):
        name = Required(String)
        cmdline = String

    class Task(Struct):
        name = Default(String, '{{processes[0].name}}')
        processes = List(Process)

    task = Task(processes=[Process(name="hello"), Process(name="world")])
    assert task.name().get() == 'hello'

    # iterably
    resolve_string = '{{foo[{{bar}}]}}'
    resolve_list = List(String)(["hello", "world"])
    resolved, unbound = MustacheParser.resolve(
        resolve_string, Environment(foo=resolve_list, bar=0))
    assert resolved == 'hello'
    assert unbound == []
    resolved, unbound = MustacheParser.resolve(
        resolve_string, Environment(foo=resolve_list, bar="0"))
    assert resolved == 'hello'
    assert unbound == []
    resolved, _ = MustacheParser.resolve(resolve_string,
                                         Environment(foo=resolve_list, bar=1))
    assert resolved == 'world'
    resolved, unbound = MustacheParser.resolve(
        resolve_string, Environment(foo=resolve_list, bar=2))
    assert resolved == '{{foo[2]}}'
    assert unbound == [ref('foo[2]')]
Example #4
0
def test_mustache_resolve_cycles():
  with pytest.raises(MustacheParser.Uninterpolatable):
    MustacheParser.resolve('{{foo[{{bar}}]}} {{baz}}',
       Environment(foo = List(String)(["{{foo[{{bar}}]}}", "world"])), Environment(bar = 0))
Example #5
0
def test_mustache_resolve_cycles():
    with pytest.raises(MustacheParser.Uninterpolatable):
        MustacheParser.resolve(
            '{{foo[{{bar}}]}} {{baz}}',
            Environment(foo=List(String)(["{{foo[{{bar}}]}}", "world"])),
            Environment(bar=0))