Ejemplo n.º 1
0
def load_solution_function(file, parameters=1):
    try:
        path, file = os.path.split(os.path.abspath(file))
        sys.path.append(path)
        if file.endswith('.py'):
            module_name = file[:-3]
        elif file.endswith('.pyc'):
            module_name = file[:-4]
        else:
            module_name = file
        module = importlib.import_module(module_name)
        function = inspect.getmembers(module, inspect.isfunction)[0][1]
        assert len(inspect.signature(function).parameters) == parameters
        return function
    except ImportError:
        raise AdventureVerificationError(
            _('I can\'t import a module from {}. Are you sure the file exists '
              'and it\'s a valid python file?').format(file))
    except IndexError:
        raise AdventureVerificationError(
            _('I can\'t find a function definition in {}. Read the adventure '
              'again and use the template proposed there.').format(file))
    except AssertionError:
        raise AdventureVerificationError(
            _('I found a function in your file but it doesn\'t accept the '
              'required number of parameters ({}). If you define several '
              'functions in your file, make sure the solution function is the '
              'first one.'.format(parameters)))
Ejemplo n.º 2
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = 'Python'
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument)))
     wrong_argument = 'ython'
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Ejemplo n.º 3
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = get_random_string(self.dictionary, 4, 6) + '^'
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument)))
     wrong_argument = get_random_string(self.dictionary, 4, 6)
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Ejemplo n.º 4
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = 'hello' * random.randint(1, 5) + random.choice(
         ['python', 'pyschool'])
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument)))
     wrong_argument = random.choice(['python', 'pyschool'])
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Ejemplo n.º 5
0
 def test(cls, sourcefile):
     """Test against the provided file"""
     suite = unittest.TestSuite()
     raw_program = codecs.open(sourcefile).read()
     suite.addTest(TestOutput(raw_program, sourcefile))
     result = unittest.TextTestRunner().run(suite)
     if not result.wasSuccessful():
         raise AdventureVerificationError()
Ejemplo n.º 6
0
 def test(self, file):
     function = load_solution_function(file)
     correct_argument = '{}{}{}'.format(
         random.randint(0, 9),
         ' ' * random.randint(0, 5),
         random.randint(0, 9)
     )
     if not function(correct_argument):
         raise AdventureVerificationError(
             _("Your function didn't return True when executed with a "
               "correct argument '{}'.".format(correct_argument))
         )
     wrong_argument = str(random.randint(0, 9))
     if function(wrong_argument):
         raise AdventureVerificationError(
             _("Your function returned True when executed with a wrong "
               "argument '{}'.".format(wrong_argument)))
Ejemplo n.º 7
0
 def test(self, file):
     function = load_solution_function(file)
     prefix = get_random_string(self.dictionary, 1, 5) + ','
     correct_argument = '{}{},{}'.format(
         prefix,
         get_random_string(self.dictionary, 1, 5),
         get_random_string(self.dictionary, 1, 5),
     )
     result = function(correct_argument)
     if result != prefix:
         raise AdventureVerificationError(
             _("Your function didn't return the expected string '{}' when "
               "executed with '{}'. "
               "It returned '{}'.".format(prefix, correct_argument, result))
         )