File size: 3,943 Bytes
d044abe
 
 
 
fa26615
 
 
 
 
 
 
d044abe
fa26615
d044abe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa26615
d044abe
 
 
 
fa26615
 
d044abe
 
 
 
 
 
bebe819
d044abe
 
 
 
 
 
fa26615
d044abe
bebe819
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
56
57
58
59
60
61
62
63
64
65
66
67
import re
from decimal import Decimal,InvalidOperation


def extract_code(text,preserve_indentation=False):
    text=text.strip('\r\n') if preserve_indentation else text.strip()
    # Closing fences must occupy their own line; backticks inside a string are code.
    blocks=re.findall(r'^[ \t]*```[^\r\n`]*\r?\n(.*?)^[ \t]*```[ \t]*(?=\r?$)',text,re.S|re.M)
    if len(blocks)==1:
        body=blocks[0].rstrip('\r\n') if preserve_indentation else blocks[0].strip()
        return body+'\n'
    if len(blocks)>1:raise ValueError('Multiple code blocks are ambiguous')
    if re.search(r'^[ \t]*```',text,re.M):raise ValueError('Unclosed code fence')
    return text+'\n'


def compare_output(actual,expected):
    # Remove one terminal newline only: spaces in string results are observable.
    value=actual.removesuffix('\n').removesuffix('\r')
    if isinstance(expected,int):
        try:return Decimal(value.strip())==expected and value.strip()!=''
        except InvalidOperation:return False
    return value==expected


class FunctionRunner:
    def evaluate(self,row,code,hidden,sandbox):
        inputs=[t['input'] for t in hidden['tests']]
        response=sandbox.run({'language':row['language'],'kind':row['metadata']['family'],
             'code':code,'inputs':inputs,'timeout_seconds':row['timeout_seconds']},
             timeout=50+len(inputs)*(row['timeout_seconds']+2))
        results=[]
        observations=response['observations']
        for i,test in enumerate(hidden['tests']):
            actual=observations[i] if i<len(observations) else None
            passed=bool(actual and actual['exit_code']==0 and not actual['timeout'] and not actual['output_limit'] and compare_output(actual['stdout'],test['expected']))
            results.append({'test':i,'passed':passed,'actual':actual})
        return {'syntax_success':response['syntax_success'],'passed':bool(results) and all(t['passed'] for t in results),
                'tests':results,'compilation':response.get('compilation')}


class BrowserRunner:
    def evaluate(self,row,code,hidden,sandbox):
        from runners.semantic import matches
        scenarios=[];expected=[]
        for t in hidden['tests']:
            widths=sorted(set(c.get('viewport',800) for c in t['checks']))
            for width in widths:
                checks=[dict(c,kind='html_text') if row['language']=='html' and c['kind']=='text' else c
                        for c in t['checks'] if c.get('viewport',800)==width]
                # Deliberately do not send expected values into the sandbox.
                scenarios.append({'viewport':width,'steps':t.get('steps',[]),
                    'checks':[{k:v for k,v in c.items() if k not in ('value','viewport')} for c in checks]})
                expected.append(checks)
        response=sandbox.run({'language':row['language'],'kind':row['metadata']['family'],'code':code,
            'fixture':row['metadata'].get('fixture',''),'base_css':row['metadata'].get('base_css',''),
            'scenarios':scenarios,'timeout_seconds':row['timeout_seconds']},timeout=270)
        results=[]
        for i,checks in enumerate(expected):
            actual=response['observations'][i] if i<len(response['observations']) else None
            for j,c in enumerate(checks):
                observed=actual['actual'][j] if actual and j<len(actual['actual']) else None
                target=actual['baseline'][j] if actual and c['kind']=='unchanged' else c.get('value',True)
                passed=bool(actual and not actual['error'] and j<len(actual['actual']) and matches(c,observed,target))
                results.append({'scenario':i,'test':j,'passed':passed,'actual':observed,'error':actual['error'] if actual else 'syntax failure'})
        return {'syntax_success':response['syntax_success'],'passed':bool(results) and all(r['passed'] for r in results),'tests':results,'candidate_timeout':response.get('candidate_timeout',False)}