File size: 2,941 Bytes
9438a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""Dışarıdan eklenen dosyalarla ilgili düzeltmeler (kullanıcı otomasyonunda bulunan sorunlar)."""
from __future__ import annotations

import os
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
os.environ.setdefault("SANDBOX_MODE", "local")

from app.models import JobRequest, looks_typescript  # noqa: E402
from app.sandbox.registry import get_runner  # noqa: E402

CALC = "package com.example;\r\n\r\npublic class Calculator {\r\n    public int twice(int x) { return 2 * x; }\r\n}\r\n"
INV = ("package com.example.inventory;\r\n\r\nimport java.util.*;\r\n\r\npublic class InventoryManager {\r\n"
       "    private final Map<String, Integer> stock = new HashMap<>();\r\n"
       "    public void add(String id, int n) { stock.merge(id, n, Integer::sum); }\r\n"
       "    public int get(String id) { return stock.getOrDefault(id, 0); }\r\n}\r\n")


def test_java_helper_named_after_its_public_class(tmp_path):
    j = get_runner("java")
    wd = j.materialize(CALC, "package com.example;\npublic class CalculatorTest {}\n", "unit",
                       extra={"Test02.java": INV}, source_name="Test01.java")
    assert (wd / "src/main/com/example/inventory/InventoryManager.java").exists()
    assert not list(wd.rglob("Test02.java"))
    if j.framework() == "missing":
        return
    junit5 = j.framework() == "junit5"
    test = ("package com.example;\n" + ("import org.junit.jupiter.api.Test;\nimport static org.junit.jupiter.api.Assertions.*;\n"
            if junit5 else "import org.junit.Test;\nimport static org.junit.Assert.*;\n")
            + "public class CalculatorTest {\n  @Test " + ("void" if junit5 else "public void")
            + " twice() { assertEquals(4, new Calculator().twice(2)); }\n}\n")
    r = j.run(CALC, test, extra={"Test02.java": INV}, source_name="Test01.java")
    assert r.compiled, r.compile_output
    assert r.passed == r.total == 1


def test_python_alias_not_collected_by_pytest(tmp_path):
    p = get_runner("python")
    assert p.alias("test_weather_client.py") is None and p.alias("weather_test.py") is None
    assert p.alias("weather.py")[0] == "weather.py"
    code = "def add(a, b):\n    return a + b\n"
    r = p.run(code, "from solution import add\n\ndef test_add():\n    assert add(1, 2) == 3\n",
              source_name="test_math_utils.py")
    assert r.passed == r.total == 1


def test_typescript_in_js_file_runs_as_typescript():
    ts = "export interface P { id: string }\nexport class T {\n  f(raw: any): P { return { id: String(raw) }; }\n}\n"
    assert JobRequest(language="javascript", code=ts).language == "typescript"
    assert JobRequest(language="javascript", code="export function f(a, b) { return { a: 1 }; }").language == "javascript"
    assert JobRequest(language="javascript", code=ts, test_kind="ui").language == "javascript"
    assert not looks_typescript("const type = 'x';\nlet y = c ? a : b;\n")