Exemple #1
0
    def test_root(self):
        import jstc.compiler
        compiler = jstc.compiler.Compiler(
            overrides=dict(inline=True, precompile=False))
        with fso.push() as overlay:
            self.writecontent({
                'test/one/template.hbs': 'template "one".',
                'test/two/template.hbs': 'template "two".',
            })
            self.assertEqual(
                compiler.render_assets('jstc:test/one/template.hbs',
                                       'test/one'), '''\
<script type="text/x-handlebars" data-template-name="template">template "one".</script>\
''')
            self.assertEqual(
                compiler.render_assets('jstc:test/two/template.hbs',
                                       'test/two'), '''\
<script type="text/x-handlebars" data-template-name="template">template "two".</script>\
''')
            self.assertEqual(
                compiler.render_assets([
                    'jstc:test/one/template.hbs', 'jstc:test/two/template.hbs'
                ], 'test'), '''\
<script type="text/x-handlebars" data-template-name="one/template">template "one".</script>\
<script type="text/x-handlebars" data-template-name="two/template">template "two".</script>\
''')
Exemple #2
0
    def test_render_space_dedent(self):
        import jstc.compiler
        compiler = jstc.compiler.Compiler(
            overrides=dict(inline=True, precompile=False))
        with fso.push() as overlay:
            self.writecontent({
                'test.hbs':
                '''\
            ##! dedent; space: dedent
              {{#if value}}
                <span>
                  {{value}}
                </span>
              {{else}}
                <span>default</span>
              {{/if}}
          '''
            })
            self.assertEqual(
                compiler.render_assets('jstc:test.hbs'), '''\
<script type="text/x-handlebars" data-template-name="test/dedent">{{#if value}}
  <span>
    {{value}}
  </span>
{{else}}
  <span>default</span>
{{/if}}</script>\
''')
Exemple #3
0
    def test_template_filter(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs':
                'hello!',
                'test/goodbye.hbs':
                '''\
##! __here__
  so long!
##! friend
  ciao!
'''
            })
            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False), '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="goodbye/friend">ciao!</script>\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
            self.assertEqual(
                jstc.render_assets(
                    'jstc:test/**.hbs',
                    force_inline=True,
                    force_precompile=False,
                    template_filter=lambda text, attrs: 'ciao' not in text),
                '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
Exemple #4
0
    def test_render_trim_deprecated(self):
        import jstc.compiler
        compiler = jstc.compiler.Compiler(
            overrides=dict(inline=True, precompile=False))
        with fso.push() as overlay:
            self.writecontent({
                'test.hbs':
                '''\
            ##! 0-default
              <span>
                text
              </span>
            ##! 1-trim; trim
              <span>
                text
              </span>
            ##! 2-notrim; !trim
              <span>
                text
              </span>
          '''
            })
            self.assertEqual(
                compiler.render_assets('jstc:test.hbs'), '''\
<script type="text/x-handlebars" data-template-name="test/0-default"><span>
  text
</span></script>\
<script type="text/x-handlebars" data-template-name="test/1-trim"><span>
  text
</span></script>\
<script type="text/x-handlebars" data-template-name="test/2-notrim">  <span>
    text
  </span>
</script>\
''')
Exemple #5
0
    def test_script_wrapper(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello, world!',
                'test/hello/name.hbs': 'hello, {{name}}!',
            })
            compiled = jstc.render_assets(
                'jstc:test/**.hbs',
                force_inline=True,
                force_precompile=True,
                script_wrapper=lambda script, *args, **kw: '<SCRIPT>' + script
                + '</SCRIPT>')
            if 'text/x-handlebars' in compiled:
                raise unittest.SkipTest(
                    'handlebars executable not available (use "npm install handlebars")'
                )
            self.assertMultiLineEqual(
                compiled, '''\
<SCRIPT>(function(){var t=Handlebars.template,ts=Handlebars.templates=Handlebars.templates||{};ts["hello"]=t({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
    return "hello, world!";
},"useData":true});ts["hello/name"]=t({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) {
    var helper;

  return "hello, "
    + container.escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : {},{"name":"name","hash":{},"data":data}) : helper)))
    + "!";
},"useData":true});})();</SCRIPT>''')
Exemple #6
0
    def test_collision_pertemplate(self):
        import jstc.compiler
        compiler = jstc.compiler.Compiler(
            defaults=dict(collision='ignore'),
            overrides=dict(inline=True, precompile=False),
        )
        with fso.push() as overlay:
            self.writecontent({
                'test/one/template.hbs':
                '''\
            ##! a
              template "one/a".
            ##! b
              template "one/b".
          ''',
                'test/two/template.hbs':
                '''\
            ##! a; collision: ignore
              template "two/a".
            ##! b; collision: override
              template "two/b".
          ''',
            })
            self.assertEqual(
                compiler.render_assets([
                    'jstc:test/one/template.hbs', 'jstc:test/two/template.hbs'
                ], ['test/one', 'test/two']), '''\
<script type="text/x-handlebars" data-template-name="template/a">template "one/a".</script>\
<script type="text/x-handlebars" data-template-name="template/b">template "two/b".</script>\
''')
Exemple #7
0
    def test_asset_filter(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello!',
                'test/goodbye.hbs': 'so long!',
            })
            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False), '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
            self.assertEqual(
                jstc.render_assets(
                    'jstc:test/**.hbs',
                    force_inline=True,
                    force_precompile=False,
                    asset_filter=lambda name: name == 'test/hello.hbs'), '''\
<script type="text/x-handlebars" data-template-name="hello">hello!</script>\
''')
            self.assertEqual(
                jstc.render_assets(
                    'jstc:test/**.hbs',
                    force_inline=True,
                    force_precompile=False,
                    asset_filter=lambda name: name != 'test/hello.hbs'), '''\
<script type="text/x-handlebars" data-template-name="goodbye">so long!</script>\
''')
Exemple #8
0
 def test_commentedOutImport_simple(self):
     with fso.push() as overlay:
         with open('black.less', 'wb') as fp:
             fp.write('.b { color: #000; }')
         with open('white.less', 'wb') as fp:
             fp.write('.w { color: #fff; }')
         self.assertEqual(
             Compiler().compile('''
       @import "black.less";
       // @import "no-such-asset";
       @import "white.less";
     '''), '.b{color:#000;}\n.w{color:#fff;}\n')
Exemple #9
0
 def test_compiler(self):
     import jstc.compiler
     compiler = jstc.compiler.Compiler(
         defaults=dict(inline=True, precompile=True))
     with fso.push() as overlay:
         self.writecontent({
             'test/template.mustache': 'A mustache template.',
         })
         self.assertEqual(
             compiler.render_assets('jstc:engines/test/template.mustache',
                                    'engines/test'),
             '<script type="text/x-mustache" data-template-name="template">A mustache template.</script>'
         )
Exemple #10
0
 def test_commentedOutImport_interleaved(self):
     with fso.push() as overlay:
         with open('black.less', 'wb') as fp:
             fp.write('.b { color: #000; }')
         with open('white.less', 'wb') as fp:
             fp.write('.w { color: #fff; }')
         self.assertEqual(
             Compiler().compile('''
       // @import "no-such-asset";
       @import "black.less";
       .r { color: #f00; } // @import "another-bad-asset"; .g { color: #0f0; }
       @import "white.less";
     '''), '.b{color:#000;}\n.r{color:#f00;}\n.w{color:#fff;}\n')
Exemple #11
0
 def setUp(self):
   super(TestRss, self).setUp()
   os.chdir(os.path.join(os.path.dirname(__file__), '..'))
   self.sender = genemail.DebugSender()
   self.logput = six.StringIO()
   self.log    = logging.getLogger()
   self.log.addHandler(logging.StreamHandler(self.logput))
   self.options  = aadict.d2ar(yaml.load(framework.defaultOptions))
   self.options.name     = 'testName'
   self.options.label    = 'testLabel'
   self.options.reposUrl = 'https://svn.example.com/repos'
   self.options.admin    = ['*****@*****.**']
   self.options.genemail.sender = self.sender
   self.fso = fso.push()
 def setUp(self):
   os.chdir(os.path.join(os.path.dirname(__file__), '..'))
   self.logput = six.StringIO()
   self.log    = logging.getLogger()
   self.log.setLevel(logging.DEBUG)
   self.log.addHandler(logging.StreamHandler(self.logput))
   self.options  = aadict.d2ar(yaml.load(framework.defaultOptions))
   self.options.name     = 'testName'
   self.options.label    = 'testLabel'
   self.options.reposUrl = 'https://svn.example.com/repos'
   self.options.admin    = ['*****@*****.**']
   self.fso = fso.push()
   self.tmpdir = '/tmp/svnpublish-unittest-engine-restsync'
   if not os.path.exists(self.tmpdir):
     os.makedirs(self.tmpdir)
Exemple #13
0
 def test_nodirchange_relative_iniherit(self):
     with fso.push() as overlay:
         with open('base.ini', 'wb') as fp:
             fp.write(
                 textwrap.dedent('''\
       [DEFAULT]
       alpha = beta
     '''))
         with open('config.ini', 'wb') as fp:
             fp.write(
                 textwrap.dedent('''\
       [DEFAULT]
       %inherit = base.ini
     '''))
         result = loader.load('config.ini')
         self.assertEqual(result.get('DEFAULT').get('alpha'), 'beta')
Exemple #14
0
 def test_collision_error(self):
     import jstc.compiler
     compiler = jstc.compiler.Compiler(
         overrides=dict(inline=True, precompile=False))
     with fso.push() as overlay:
         self.writecontent({
             'test/one/template.hbs': 'template "one".',
             'test/two/template.hbs': 'template "two".',
         })
         with self.assertRaises(jstc.TemplateCollision) as cm:
             compiler.render_assets([
                 'jstc:test/one/template.hbs', 'jstc:test/two/template.hbs'
             ], ['test/one', 'test/two'])
         self.assertEqual(
             str(cm.exception),
             ''''text/x-handlebars' template 'template' is already defined'''
         )
Exemple #15
0
  def setUp(self):
    self.fso      = fso.push()
    self.iostack  = []
    self.uucnt    = 0

    # todo: use `stub` to stub these out...
    self.real_raw_input = __builtin__.raw_input
    __builtin__.raw_input = self.raw_input
    self.real_getpass = getpass.getpass
    getpass.getpass = self.raw_input
    self.real_uuid4 = uuid.uuid4
    uuid.uuid4 = self.uuid4
    self.real_gettext = gettext.gettext
    gettext.gettext = self.gettext
    self.real_out = base._out
    base._out = self.output
    self.real_request = requests.request
    requests.request = self.request
Exemple #16
0
    def test_render_simple(self):
        import jstc.compiler
        compiler = jstc.compiler.Compiler(
            overrides=dict(inline=True, precompile=False))
        with fso.push() as overlay:
            self.writecontent({
                'test/common/hello.hbs':
                '''\
            ##! __here__
              Hello, world!
            ##! name
              Hello, {{name}}!
          '''
            })
            self.assertEqual(
                compiler.render_assets('jstc:test/common/hello.hbs', 'test'),
                '''\
<script type="text/x-handlebars" data-template-name="common/hello">Hello, world!</script>\
<script type="text/x-handlebars" data-template-name="common/hello/name">Hello, {{name}}!</script>\
''')
Exemple #17
0
    def test_name_transform(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello!',
                'test/goodbye.hbs': 'so long!',
            })

            def mynt(name, root):
                return (name[2:].replace('d', 'd-').split('.')[0],
                        'text/x-mustache')

            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False,
                                   name_transform=mynt), '''\
<script type="text/x-mustache" data-template-name="st/good-bye">so long!</script>\
<script type="text/x-mustache" data-template-name="st/hello">hello!</script>\
''')
Exemple #18
0
    def test_comments(self):
        import jstc.compiler
        compiler = jstc.compiler.Compiler(
            overrides=dict(inline=True, precompile=False))
        with fso.push() as overlay:
            self.writecontent({
                'test/application.hbs':
                '''\
            <div>
              ## TODO: super-secret comment!
              Nothing to see here.
            </div>
          '''
            })
            self.assertEqual(
                compiler.render_assets('jstc:test/application.hbs', 'test'),
                '''\
<script type="text/x-handlebars" data-template-name="application"><div>
  Nothing to see here.
</div>\
</script>\
''')
Exemple #19
0
    def test_template_transform(self):
        import jstc
        with fso.push() as overlay:
            self.writecontent({
                'test/hello.hbs': 'hello!',
                'test/goodbye.hbs': 'so long!',
            })

            def mytt(text, attrs):
                if attrs.name == 'hello':
                    text = 'hello, world!'
                    attrs.id = 'HW'
                else:
                    attrs.type = 'template/jst'
                return (text, attrs)

            self.assertEqual(
                jstc.render_assets('jstc:test/**.hbs',
                                   force_inline=True,
                                   force_precompile=False,
                                   template_transform=mytt), '''\
<script type="template/jst" data-template-name="goodbye">so long!</script>\
<script type="text/x-handlebars" data-template-name="hello" id="HW">hello, world!</script>\
''')
Exemple #20
0
 def setUp(self):
   self.fso = fso.push()