Skip to content

Commit

Permalink
Use individual arguments in custom check functions
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Sep 21, 2023
1 parent b85b4cf commit e396a75
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 44 deletions.
24 changes: 9 additions & 15 deletions tested/judge/programmed.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
from tested.internationalization import get_i18n_string
from tested.judge.execution import execute_file, filter_files
from tested.judge.utils import BaseExecutionResult, copy_from_paths_to_path, run_command
from tested.languages.generation import (
custom_oracle_arguments,
generate_custom_evaluator,
generate_statement,
)
from tested.languages.generation import generate_custom_evaluator, generate_statement
from tested.oracles.common import BooleanEvalResult
from tested.serialisation import Value
from tested.serialisation import FunctionCall, FunctionType, Value
from tested.testsuite import CustomCheckOracle
from tested.utils import get_identifier

Expand Down Expand Up @@ -214,17 +210,15 @@ def _evaluate_python(
exec(evaluator_code, global_env)

# Call the oracle.
literal_expected = generate_statement(eval_bundle, expected)
literal_actual = generate_statement(eval_bundle, actual)
arguments = custom_oracle_arguments(oracle)
literal_arguments = generate_statement(eval_bundle, arguments)
check_function_call = FunctionCall(
type=FunctionType.FUNCTION,
name=oracle.function.name,
arguments=[expected, actual, *oracle.arguments],
)
literal_function_call = generate_statement(eval_bundle, check_function_call)

with _catch_output() as (stdout_, stderr_):
exec(
f"__tested_test__result = {oracle.function.name}("
f"{literal_expected}, {literal_actual}, {literal_arguments})",
global_env,
)
exec(f"__tested_test__result = {literal_function_call}", global_env)

stdout_ = stdout_.getvalue()
stderr_ = stderr_.getvalue()
Expand Down
12 changes: 2 additions & 10 deletions tested/languages/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pygments.lexers import get_lexer_by_name

from tested.configs import Bundle
from tested.datatypes import AllTypes, BasicObjectTypes, BasicSequenceTypes
from tested.datatypes import AllTypes, BasicObjectTypes
from tested.dodona import ExtendedMessage
from tested.internationalization import get_i18n_string
from tested.judge.planning import PlannedExecutionUnit
Expand All @@ -40,7 +40,6 @@
Expression,
FunctionType,
Identifier,
SequenceType,
Statement,
Value,
VariableType,
Expand Down Expand Up @@ -294,12 +293,6 @@ def generate_selector(
return selector_filename


def custom_oracle_arguments(oracle: CustomCheckOracle) -> Value:
return SequenceType(
type=BasicSequenceTypes.SEQUENCE, data=oracle.arguments # pyright: ignore
)


def generate_custom_evaluator(
bundle: Bundle,
destination: Path,
Expand All @@ -321,13 +314,12 @@ def generate_custom_evaluator(
evaluator_name = conventionalize_namespace(
bundle.language, evaluator.function.file.stem
)
arguments = custom_oracle_arguments(evaluator)

function = PreparedFunctionCall(
type=FunctionType.FUNCTION,
namespace=Identifier(evaluator_name),
name=evaluator.function.name,
arguments=[expected_value, actual_value, arguments],
arguments=[expected_value, actual_value, *evaluator.arguments],
has_root_namespace=False,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/exercises/echo-function/evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public static EvaluationResult Evaluate(Object actual) {
return new EvaluationResult(correct, "correct", actual != null ? actual.ToString() : "", messages);
}

public static EvaluationResult EvaluateValue(Object expected, Object actual, IList arguments) {
public static EvaluationResult EvaluateValue(Object expected, Object actual) {
var messages = new List<Message>() {new Tested.Message("Hallo")};
return new EvaluationResult(expected == actual, expected.ToString(), actual != null ? actual.ToString() : "", messages);
}

public static EvaluationResult EvaluateValueDsl(Object expected, Object actual, IList arguments) {
public static EvaluationResult EvaluateValueDsl(Object expected, Object actual) {
var messages = new List<Message>() {new Tested.Message("Hallo")};
return new EvaluationResult(expected == actual, null, null, messages, "{5, 5}", "{4, 4}");
}
Expand Down
6 changes: 3 additions & 3 deletions tests/exercises/echo-function/evaluation/Evaluator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ evaluate value =
}


evaluate_value :: String -> String -> [String] -> EvaluationResult
evaluate_value expected actual arguments =
evaluate_value :: String -> String -> EvaluationResult
evaluate_value expected actual =
let correct = if actual == expected then True else False
in evaluationResult {
result = correct,
readableExpected = Just expected,
readableActual = Just actual,
messages = [message "Hallo"]
}
}
4 changes: 2 additions & 2 deletions tests/exercises/echo-function/evaluation/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public static EvaluationResult evaluate(Object actual) {
.build();
}

public static EvaluationResult evaluateValue(Object expected, Object actual, List<?> arguments) {
public static EvaluationResult evaluateValue(Object expected, Object actual) {
return EvaluationResult.builder(expected.equals(actual))
.withReadableExpected(expected.toString())
.withReadableActual(actual != null ? actual.toString() : "")
.withMessage(new EvaluationResult.Message("Hallo"))
.build();
}

public static EvaluationResult evaluateValueDsl(Object expected, Object actual, List<?> arguments) {
public static EvaluationResult evaluateValueDsl(Object expected, Object actual) {
return EvaluationResult.builder(expected.equals(actual))
.withDslExpected("{5, 5}")
.withDslActual("{4, 4}")
Expand Down
4 changes: 2 additions & 2 deletions tests/exercises/echo-function/evaluation/Evaluator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Evaluator {
}

@JvmStatic
fun evaluateValue(expected: Any, actual: Any?, arguments: List<Any?>?): EvaluationResult {
fun evaluateValue(expected: Any, actual: Any?): EvaluationResult {
return EvaluationResult.Builder(result = expected == actual,
readableExpected = expected.toString(),
readableActual = actual?.toString() ?: "")
Expand All @@ -19,7 +19,7 @@ class Evaluator {
}

@JvmStatic
fun evaluateValueDsl(expected: Any, actual: Any?, arguments: List<Any?>?): EvaluationResult {
fun evaluateValueDsl(expected: Any, actual: Any?): EvaluationResult {
return EvaluationResult.Builder(result = expected == actual,
dslExpected = "{5, 5}",
dslActual = "{4, 4}")
Expand Down
4 changes: 2 additions & 2 deletions tests/exercises/echo-function/evaluation/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function evaluate(actual) {
}
}

function evaluateValue(expected, actual, args) {
function evaluateValue(expected, actual) {
return {
"result": expected === actual,
"readable_expected": expected,
Expand All @@ -17,7 +17,7 @@ function evaluateValue(expected, actual, args) {
}
}

function evaluateValueDsl(expected, actual, args) {
function evaluateValueDsl(expected, actual) {
return {
"result": expected === actual,
"dsl_expected": "{5, 5}",
Expand Down
5 changes: 3 additions & 2 deletions tests/exercises/echo-function/evaluation/evaluator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# noinspection PyUnresolvedReferences
from evaluation_utils import EvaluationResult, Message


Expand All @@ -6,11 +7,11 @@ def evaluate(actual):
return EvaluationResult(correct, "correct", actual, [Message("Hallo")])


def evaluate_value(expected, actual, args):
def evaluate_value(expected, actual):
return EvaluationResult(expected == actual, expected, actual, [Message("Hallo")])


def evaluate_value_dsl(expected, actual, args):
def evaluate_value_dsl(expected, actual):
return EvaluationResult(
result=expected == actual,
messages=[Message("Hallo")],
Expand Down
5 changes: 3 additions & 2 deletions tests/exercises/echo/evaluation/evaluator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# noinspection PyUnresolvedReferences
from evaluation_utils import EvaluationResult


def evaluate_correct(expected, actual, arguments):
def evaluate_correct(expected, actual):
return EvaluationResult(expected.strip() == actual.strip())


def evaluate_wrong(expected, actual, arguments):
def evaluate_wrong(_expected, _actual):
return EvaluationResult(False)
4 changes: 2 additions & 2 deletions tests/exercises/lotto/evaluation/evaluator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
# noinspection PyUnresolvedReferences
from evaluation_utils import EvaluationResult, Message


Expand Down Expand Up @@ -39,8 +40,7 @@ def valid_lottery_numbers(number_str, count=6, maximum=42):
return True, None


def evaluate(expected, actual, arguments):
count, maximum = arguments
def evaluate(expected, actual, count, maximum):
valid, message = valid_lottery_numbers(actual, count, maximum)
messages = [Message(message)] if message else []
if valid:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functionality.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ def test_expected_no_return_and_got_none(language: str, tmp_path: Path, pytestco

result = execute_config(conf)
updates = assert_valid_output(result, pytestconfig)
assert updates.find_status_enum() == ["correct"]
assert updates.find_status_enum() == []


@pytest.mark.parametrize("language", ALL_SPECIFIC_LANGUAGES)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_oracles.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from tested.configs import create_bundle
from tested.datatypes import BasicObjectTypes, BasicSequenceTypes, BasicStringTypes
from tested.dodona import Status
from tested.oracles.common import OracleConfig, OracleResult
from tested.oracles.common import OracleConfig
from tested.oracles.exception import evaluate as evaluate_exception
from tested.oracles.text import evaluate_file, evaluate_text
from tested.oracles.value import evaluate as evaluate_value
Expand Down

0 comments on commit e396a75

Please sign in to comment.