def test_import_command():
    tree = parse_string('@import math\n'
                        '@from random import choice\n'
                        '@command\n'
                        'def foo(arg):'
                        '    return math.sqrt(choice([1]))')
    assert tree.commands.foo.generate('') == 1
Esempio n. 2
0
def test_computed_input():
    tree = parse_string("foo$name(10)")
    session = tree[0]
    assert len(session) == 2
    assert session[0].type == "output"
    assert session[1].type == "input-command"
    assert session[1].name == "name"
    assert session[1].args == "10"
def test_computed_input():
    tree = parse_string('foo$name(10)')
    session = tree[0]
    assert len(session) == 2
    assert session[0].type == 'output'
    assert session[1].type == 'input-command'
    assert session[1].name == 'name'
    assert session[1].args == '10'
Esempio n. 4
0
def test_computed_input():
    tree = parse_string('foo$name(10)')
    session = tree[0]
    assert len(session) == 2
    assert session[0].type == 'output'
    assert session[1].type == 'input-command'
    assert session[1].name == 'name'
    assert session[1].args == '10'
Esempio n. 5
0
def test_import_command():
    tree = parse_string(
        '@import math\n'
        '@from random import choice\n'
        '@command\n'
        'def foo(arg):'
        '    return math.sqrt(choice([1]))')
    assert tree.commands.foo.generate('') == 1
Esempio n. 6
0
def test_multiline_with_pipes():
    tree = parse_string(
        '|foo\n'
        '|\n'
        '|bar'
    )
    assert len(tree) == 1
    assert len(tree[0]) == 1
    assert tree[0][0] == 'foo\n\nbar'
Esempio n. 7
0
def test_use_command():
     tree = parse_string('''
@command
def foo(arg):
     return 'computed value'

foo: $foo
''')
     assert len(tree) == 1
     assert 'foo' in tree.commands
     assert tree[0, 1].data == '$foo'
def test_use_command():
    tree = parse_string('''
@command
def foo(arg):
     return 'computed value'

foo: $foo
''')
    assert len(tree) == 1
    assert 'foo' in tree.commands
    assert tree[0, 1].data == '$foo'
Esempio n. 9
0
def test_use_command():
    tree = parse_string(
        """
@command
def foo(arg):
     return 'computed value'

foo: $foo
"""
    )
    assert len(tree) == 1
    assert "foo" in tree.commands
    assert tree[0, 1].data == "$foo"
Esempio n. 10
0
def grade(source, iospec, lang=None, *,
          fast=True, path=None, raises=False, sandbox=False, timeout=None):
    """
    Grade the string of source code by comparing the results of all inputs and
    outputs in the given template structure.


    Parameters
    ----------

    source : str or file object
        The source string for the code or a file object
    iospec : IOSpec parse tree
        The expected template for correct answers.
    lang : str
        Programming language for the given source code. The judge accepts the
        following languages. Users can register plugins to support additional
        languages or to override the default behavior or accepted languages.

        +===========+==========================================================+
        | Value     | Description                                              |
        +===========+==========================================================+
        | python    | For Python 3.x code. Default runner.                     |
        +-----------+----------------------------------------------------------+
        | python2   | Executes in a separate Python 2 interpreter.             |
        +-----------+----------------------------------------------------------+
        | python3   | Executes in a separate Python 3 interpreter. Used mostly |
        |           | for debugging since it lack in features and performance  |
        |           | compared to the default "python" runner.                 |
        +-----------+----------------------------------------------------------+
        | tcc       | Compile C code with the tiny C compiler                  |
        +-----------+----------------------------------------------------------+
        | clang     | Compile C code with clang                                |
        +-----------+----------------------------------------------------------+
        | gcc       | Compile C code with gcc                                  |
        +-----------+----------------------------------------------------------+
        | C         | Uses the first method available: tcc, clang or gcc       |
        +-----------+----------------------------------------------------------+

    sandbox : bool
        If True, code will run in a sandboxed environment as the `nobody` user.
        It is necessary to have your system properly configured in order to do
        this.
    timeout : float
        Maximum time (in seconds) for the complete test to run.

    Specific languages may support additional keyword arguments. The most common
    ones are shown bellow

    namespace (python): dict
        The global namespace used to run a python script.


    Returns
    -------

    A :cls:`ejudge.Feedback` instance.
    """

    manager = get_manager(lang, source, path)
    manager.is_sandboxed = sandbox

    # Normalize inputs
    if isinstance(iospec, str):
        iospec = parse_string(iospec)
    if not iospec:
        raise ValueError('cannot grade an iospec that has no cases')

    with manager.keep_cwd():
        kwargs = {'raises': raises, 'timeout': timeout, 'fast': fast}

        if sandbox:
            imports = manager.modules()
            result = run_sandbox(
                grade_from_lang,
                args=(manager.lang, manager.source, iospec.to_json()),
                kwargs=kwargs,
                imports=imports,
            )
            result = Feedback.from_json(result)
        else:
            # noinspection PyArgumentList
            result = grade_from_manager(manager, iospec, **kwargs)
    return result
Esempio n. 11
0
def test_open_file():
    src = "foo<bar>"
    assert parse(io.StringIO(src)) == parse_string(src)
Esempio n. 12
0
def test_import_command():
    tree = parse_string(
        "@import math\n" "@from random import choice\n" "@command\n" "def foo(arg):" "    return math.sqrt(choice([1]))"
    )
    assert tree.commands.foo.generate("") == 1
Esempio n. 13
0
 def iospec(self):
     return parse_string(self.base_iospec_source)
Esempio n. 14
0
def test_simple_io():
    tree = parse_string('foo<bar>\nfoobar')
    case = tree[0]
    assert case[0] == 'foo'
    assert case[1] == 'bar'
    assert case[2] == 'foobar'
Esempio n. 15
0
def test_broken_io():
    with pytest.raises(IoSpecSyntaxError):
        parse_string('foo<bar\nfoobar')
Esempio n. 16
0
def test_simple_io():
    tree = parse_string('foo<bar>\nfoobar')
    case = tree[0]
    assert case[0] == 'foo'
    assert case[1] == 'bar'
    assert case[2] == 'foobar'
Esempio n. 17
0
def test_broken_io():
    with pytest.raises(IoSpecSyntaxError):
        parse_string('foo<bar\nfoobar')
Esempio n. 18
0
def grade(source,
          iospec,
          lang=None,
          *,
          fast=True,
          path=None,
          raises=False,
          sandbox=False,
          timeout=None):
    """
    Grade the string of source code by comparing the results of all inputs and
    outputs in the given template structure.


    Parameters
    ----------

    source : str or file object
        The source string for the code or a file object
    iospec : IOSpec parse tree
        The expected template for correct answers.
    lang : str
        Programming language for the given source code. The judge accepts the
        following languages. Users can register plugins to support additional
        languages or to override the default behavior or accepted languages.

        +===========+==========================================================+
        | Value     | Description                                              |
        +===========+==========================================================+
        | python    | For Python 3.x code. Default runner.                     |
        +-----------+----------------------------------------------------------+
        | python2   | Executes in a separate Python 2 interpreter.             |
        +-----------+----------------------------------------------------------+
        | python3   | Executes in a separate Python 3 interpreter. Used mostly |
        |           | for debugging since it lack in features and performance  |
        |           | compared to the default "python" runner.                 |
        +-----------+----------------------------------------------------------+
        | tcc       | Compile C code with the tiny C compiler                  |
        +-----------+----------------------------------------------------------+
        | clang     | Compile C code with clang                                |
        +-----------+----------------------------------------------------------+
        | gcc       | Compile C code with gcc                                  |
        +-----------+----------------------------------------------------------+
        | C         | Uses the first method available: tcc, clang or gcc       |
        +-----------+----------------------------------------------------------+

    sandbox : bool
        If True, code will run in a sandboxed environment as the `nobody` user.
        It is necessary to have your system properly configured in order to do
        this.
    timeout : float
        Maximum time (in seconds) for the complete test to run.

    Specific languages may support additional keyword arguments. The most common
    ones are shown bellow

    namespace (python): dict
        The global namespace used to run a python script.


    Returns
    -------

    A :cls:`ejudge.Feedback` instance.
    """

    manager = get_manager(lang, source, path)
    manager.is_sandboxed = sandbox

    # Normalize inputs
    if isinstance(iospec, str):
        iospec = parse_string(iospec)
    if not iospec:
        raise ValueError('cannot grade an iospec that has no cases')

    with manager.keep_cwd():
        kwargs = {'raises': raises, 'timeout': timeout, 'fast': fast}

        if sandbox:
            imports = manager.modules()
            result = run_sandbox(
                grade_from_lang,
                args=(manager.lang, manager.source, iospec.to_json()),
                kwargs=kwargs,
                imports=imports,
            )
            result = Feedback.from_json(result)
        else:
            # noinspection PyArgumentList
            result = grade_from_manager(manager, iospec, **kwargs)
    return result
Esempio n. 19
0
def test_simple_io():
    tree = parse_string("foo<bar>\nfoobar")
    case = tree[0]
    assert case[0] == "foo"
    assert case[1] == "bar"
    assert case[2] == "foobar"
Esempio n. 20
0
def test_multiline_with_pipes():
    tree = parse_string('|foo\n' '|\n' '|bar')
    assert len(tree) == 1
    assert len(tree[0]) == 1
    assert tree[0][0] == 'foo\n\nbar'
Esempio n. 21
0
def iospec_hello(iospec_source_hello):
    return iospec.parse_string(iospec_source_hello)
Esempio n. 22
0
def test_multiline_with_pipes():
    tree = parse_string("|foo\n" "|\n" "|bar")
    assert len(tree) == 1
    assert len(tree[0]) == 1
    assert tree[0][0] == "foo\n\nbar"
Esempio n. 23
0
def test_simple_render_back(source):
    parsed = parse_string(source)
    assert source.rstrip() == parsed.source().rstrip()
Esempio n. 24
0
def test_open_file():
    src = 'foo<bar>'
    assert parse(io.StringIO(src)) == parse_string(src)
Esempio n. 25
0
def test_simple_render_back(source):
    parsed = parse_string(source)
    assert source.rstrip() == parsed.source().rstrip()