<commit_before>import datetime

from django.test import TestCase
from django.template import Library, Template, Context, add_to_builtins,\
        TemplateSyntaxError, VariableDoesNotExist
from django.db import models
import tagcon
register = Library()


class Link(models.Model):

    url = models.URLField()

    def __unicode__(self):
        return u'<%s>' % self.url


class KeywordTag(tagcon.TemplateTag):

    limit = tagcon.IntegerArg(default=5)

    def render(self, context):
        self.resolve(context)
        return 'The limit is %d' % self.args.limit


class KeywordNoDefaultTag(tagcon.TemplateTag):

    limit = tagcon.IntegerArg()

    def render(self, context):
        self.resolve(context)
        return 'The limit is %d' % self.args.limit


class NoArgumentTag(tagcon.TemplateTag):

    def render(self, context):
        return 'No arguments here'

class SinglePositionalTag(tagcon.TemplateTag):
    _ = tagcon.IntegerArg(name="single_arg", default=5)

    def render(self, context):
        return '%s' % self.args.single_arg


class ArgumentTypeTag(tagcon.TemplateTag):

    age = tagcon.IntegerArg(null=True)
    name_ = tagcon.StringArg(null=True)
    url = tagcon.ModelInstanceArg(model=Link, required=False,
                                        null=True)
    date = tagcon.DateArg(null=True)
    time = tagcon.TimeArg(null=True)
    datetime = tagcon.DateTimeArg(null=True)

    def render(self, context):
        self.resolve(context)
        order = 'name age url date time datetime'.split()
        return ' '.join([str(self.args[x]) for x in order if self.args[x] is not
                         None])

add_to_builtins(KeywordTag.__module__)
add_to_builtins(NoArgumentTag.__module__)
add_to_builtins(SinglePositionalTag.__module__)
add_to_builtins(ArgumentTypeTag.__module__)

render = lambda t: Template(t).render(Context())


class TagExecutionTests(TestCase):

    def test_no_args(self):
        """A tag with keyword arguments works with or without the argument as
        long as a default value is set"""

        self.assertEqual(Template('{% keyword limit 200 %}').render(Context()),
                         'The limit is 200')

        self.assertEqual(Template('{% keyword %}').render(Context()),
                         'The limit is %d' %
                         KeywordTag._keyword_args['limit'].default)

        self.assertEqual(Template('{% single_positional 10 %}').render(Context()),
                         u"10")

        self.assertRaises(tagcon.TemplateTagValidationError,
                          render,
                          '{% keyword_no_default %}')

        # what if we change the arg to be null=True?
        KeywordNoDefaultTag._keyword_args['limit'].null = True<commit_msg>Added KeywordNoDefaultTag to default tags.<commit_after>import datetime

from django.test import TestCase
from django.template import Library, Template, Context, add_to_builtins,\
        TemplateSyntaxError, VariableDoesNotExist
from django.db import models
import tagcon
register = Library()


class Link(models.Model):

    url = models.URLField()

    def __unicode__(self):
        return u'<%s>' % self.url


class KeywordTag(tagcon.TemplateTag):

    limit = tagcon.IntegerArg(default=5)

    def render(self, context):
        self.resolve(context)
        return 'The limit is %d' % self.args.limit


class KeywordNoDefaultTag(tagcon.TemplateTag):

    limit = tagcon.IntegerArg()

    def render(self, context):
        self.resolve(context)
        return 'The limit is %d' % self.args.limit


class NoArgumentTag(tagcon.TemplateTag):

    def render(self, context):
        return 'No arguments here'

class SinglePositionalTag(tagcon.TemplateTag):
    _ = tagcon.IntegerArg(name="single_arg", default=5)

    def render(self, context):
        return '%s' % self.args.single_arg


class ArgumentTypeTag(tagcon.TemplateTag):

    age = tagcon.IntegerArg(null=True)
    name_ = tagcon.StringArg(null=True)
    url = tagcon.ModelInstanceArg(model=Link, required=False,
                                        null=True)
    date = tagcon.DateArg(null=True)
    time = tagcon.TimeArg(null=True)
    datetime = tagcon.DateTimeArg(null=True)

    def render(self, context):
        self.resolve(context)
        order = 'name age url date time datetime'.split()
        return ' '.join([str(self.args[x]) for x in order if self.args[x] is not
                         None])

add_to_builtins(KeywordTag.__module__)
add_to_builtins(KeywordNoDefaultTag.__module__)
add_to_builtins(NoArgumentTag.__module__)
add_to_builtins(SinglePositionalTag.__module__)
add_to_builtins(ArgumentTypeTag.__module__)

render = lambda t: Template(t).render(Context())


class TagExecutionTests(TestCase):

    def test_no_args(self):
        """A tag with keyword arguments works with or without the argument as
        long as a default value is set"""

        self.assertEqual(Template('{% keyword limit 200 %}').render(Context()),
                         'The limit is 200')

        self.assertEqual(Template('{% keyword %}').render(Context()),
                         'The limit is %d' %
                         KeywordTag._keyword_args['limit'].default)

        self.assertEqual(Template('{% single_positional 10 %}').render(Context()),
                         u"10")

        self.assertRaises(tagcon.TemplateTagValidationError,
                          render,
                          '{% keyword_no_default %}')

        # what if we change the arg to be null=True?
        KeywordNoDefaultTag._keyword_args['limit'].null = True
{'fraction_non_alphanumeric': 0.1063182387685699, 'fraction_numerical': 0.004295686414891713, 'mean_word_length': 2.8221614227086183, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4282224', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1532, 'n_words': 372}