perf(snapshot): capture full target states in correlated native read
This commit is contained in:
1 parent
8723076cb6
commit
ca8ac2a5d2
4 files changed
+276
-22
No files matched your search
@@ -0,0 +1,129 @@
|
||||
"""Full selected C/A frames share a correlated read with untouched baselines."""
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
import test_native_snapshot_reader as reader
|
||||
|
||||
|
||||
STATE = 'C\tu1\tR1\t0\t0\t0\t1\t1|0|0|UNCONNECTED%000A\r\nA\tu1\tValue\tOLD\t\t0\t0\t1\t2\t3\t0\t1\r\n'
|
||||
|
||||
|
||||
def encode(text):
|
||||
return ''.join(f'%{ord(c):04X}' if ord(c) < 32 or c == '%' or ord(c) > 126 else c for c in text)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('fault', ['', 'missing', 'duplicate', 'foreign', 'owner', 'no_attributes',
|
||||
'bad_attribute', 'duplicate_attribute', 'truncated_state', 'after_end', 'wrong_version'])
|
||||
def test_selected_capture_requires_exact_target_set_and_complete_fields(tmp_path, fault):
|
||||
state = STATE
|
||||
if fault == 'owner':
|
||||
state = state.replace('A\tu1', 'A\tother')
|
||||
elif fault == 'no_attributes':
|
||||
state = state[:state.index('A\t')]
|
||||
elif fault == 'bad_attribute':
|
||||
state = state.replace('\tOLD\t', '\tOLD\textra\t')
|
||||
elif fault == 'duplicate_attribute':
|
||||
state += state[state.index('A\t'):]
|
||||
elif fault == 'truncated_state':
|
||||
state = state[:-2]
|
||||
target = 'T\t' + ('other' if fault == 'foreign' else 'u1') + '\t' + encode(state) + '\r\n'
|
||||
text = reader.FRAME.replace('NR1\t', 'NR2\t', 1)
|
||||
text = text.replace(reader.PAIR, ('' if fault in {'missing', 'after_end'} else target) + reader.PAIR)
|
||||
if fault == 'duplicate':
|
||||
text = text.replace(reader.PAIR, target + reader.PAIR)
|
||||
elif fault == 'after_end':
|
||||
text += target
|
||||
elif fault == 'wrong_version':
|
||||
text = text.replace('NR2\t', 'NR1\t', 1)
|
||||
path = tmp_path/'capture.tsv'
|
||||
path.write_bytes(text.encode('ascii'))
|
||||
output = _parse_capture(tmp_path, path)
|
||||
assert output.splitlines() == (['True'] if fault else ['False', '2', 'u1', 'u2', 'True'])
|
||||
|
||||
|
||||
def _parse_capture(tmp_path, path):
|
||||
(tmp_path/'state.tsv').write_bytes(STATE.encode('ascii'))
|
||||
return reader.run(tmp_path, '''
|
||||
Dim Selected,Result,Failure
|
||||
Set Selected=CreateObject("Scripting.Dictionary"):Selected.Add "u1",True
|
||||
On Error Resume Next
|
||||
Set Result=QPParseNativeCapture(QPReadUtf8(''' + reader.quote(path) + '''),"nonce","c:\\board.prj","MAIN:1",Selected)
|
||||
Failure=Err.Number
|
||||
On Error GoTo 0
|
||||
WScript.Echo CStr(Failure<>0)
|
||||
If Failure=0 Then
|
||||
WScript.Echo Result("total")
|
||||
WScript.Echo Join(Result("selected").Keys,",")
|
||||
WScript.Echo Join(Result("rows").Keys,",")
|
||||
WScript.Echo CStr(Result("selected")("u1")=QPReadUtf8(''' + reader.quote(tmp_path/'state.tsv') + '''))
|
||||
End If
|
||||
''')
|
||||
|
||||
|
||||
def test_generated_capture_uses_original_selected_snapshot_and_untouched_reader(tmp_path):
|
||||
source=tmp_path/'source.vbs'
|
||||
source.write_text(reader.MAIN.read_text(encoding='ascii')+'\n'+reader.ADAPTER.read_text(encoding='ascii'),encoding='ascii')
|
||||
generated=tmp_path/'generated.vbs';result=tmp_path/'capture.tsv'
|
||||
reader.run(tmp_path, '''
|
||||
Dim QP_Root,Selected,Text
|
||||
QP_Root=''' + reader.quote(tmp_path) + '''
|
||||
Set Selected=CreateObject("Scripting.Dictionary"):Selected.Add "u1",True
|
||||
Text=QPBuildNativeReadScript(QPReadUtf8(''' + reader.quote(source) + '''),Selected,"nonce","c:\\board.prj","MAIN:1","OWNED",''' + reader.quote(result) + ''',"stop-token",True,True)
|
||||
QPWriteUtf8 ''' + reader.quote(generated) + ''',Text
|
||||
''')
|
||||
fake=reader.NATIVE_FAKES.replace('Dim App,Comp', 'Dim App,Comp,Attr,Pin').replace(
|
||||
'App.ActiveView.Components.Add Comp,True',
|
||||
'Set Attr=New FullAttribute:Comp.Attributes.Add Attr,True\n'
|
||||
' Set Pin=New FakePin:Set Pin.Component=Comp:Pin.Number="1":Pin.X=0:Pin.Y=0:Set Pin.Connection=Nothing\n'
|
||||
' App.ActiveView.Pins.Add Pin,True\n App.ActiveView.Components.Add Comp,True',1)
|
||||
text=generated.read_text(encoding='utf-8-sig').replace('Set QP_App=GetObject(,"Viewdraw.Application")','Set QP_App=MakeNativeFake()')
|
||||
text+='\nConst VDM_COMP=128\nConst VDM_COMPPIN=4096\nConst VD_ALL=0\n'+fake
|
||||
generated.write_text(text,encoding='ascii')
|
||||
completed=subprocess.run(['cscript.exe','//nologo',str(generated)],capture_output=True,text=True,timeout=15)
|
||||
assert completed.returncode==0,completed.stdout+completed.stderr
|
||||
text=result.read_bytes().decode('utf-8-sig')
|
||||
assert text.startswith('NR2\tnonce\t') and '\r\nT\tu1\tC%0009u1' in text
|
||||
assert reader.PAIR in text and text.endswith('END\tnonce\t2\r\n')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('changed', ['', 'part', 'value', 'refdes'])
|
||||
def test_capture_consumes_native_targets_without_external_pin_read_and_rechecks_scope(tmp_path, changed):
|
||||
state = STATE.replace('\tOLD\t', '\tSAME\t') + 'A\tu1\tPart Number\tOLD\t\t0\t0\t1\t2\t3\t0\t1\r\n'
|
||||
if changed == 'part':
|
||||
state = state.replace('Part Number\tOLD', 'Part Number\tOTHER')
|
||||
elif changed == 'value':
|
||||
state = state.replace('Value\tSAME', 'Value\tCHANGED')
|
||||
elif changed == 'refdes':
|
||||
state = state.replace('C\tu1\tR1', 'C\tu1\tR9')
|
||||
(tmp_path/'native-target.tsv').write_bytes(state.encode('ascii'))
|
||||
fake=reader.NATIVE_FAKES.replace('Dim App,Comp', 'Dim App,Comp,Attr').replace(
|
||||
'App.ActiveView.Components.Add Comp,True',
|
||||
'Set Attr=New FullAttribute:Comp.Attributes.Add Attr,True\n'
|
||||
' Set Attr=New FullAttribute:Attr.Name="Part Number":Attr.Value="OLD":Attr.InstanceValue="OLD":Comp.Attributes.Add Attr,True\n'
|
||||
' App.ActiveView.Components.Add Comp,True',1)
|
||||
def replace_native(code):
|
||||
import re
|
||||
code=re.sub(r'(?ms)^Function QPUseNativeSnapshot\([^\n]*\n.*?^End Function',
|
||||
'Function QPUseNativeSnapshot()\n QPUseNativeSnapshot=True\nEnd Function',code)
|
||||
return re.sub(r'(?ms)^Function QPNativeCapture\([^\n]*\n.*?^End Function', '''Function QPNativeCapture(ByRef Selected)
|
||||
Dim Result,Targets
|
||||
Set Result=CreateObject("Scripting.Dictionary"):Set Targets=CreateObject("Scripting.Dictionary")
|
||||
Targets.Add "u1",QPReadUtf8(QP_Fso.BuildPath(QP_Root,"native-target.tsv"))
|
||||
Result.Add "total",2:Result.Add "text","":Result.Add "selected",Targets
|
||||
Set QPNativeCapture=Result
|
||||
End Function''',code)
|
||||
output=reader.run_composed_fixture(tmp_path, '''
|
||||
Dim Command,Failure
|
||||
Set Command=CreateObject("Scripting.Dictionary")
|
||||
Command.Add "project_path","c:\\board.prj":Command.Add "old_part_number","OLD":Command.Add "new_part_number","NEW"
|
||||
Command.Add "job_id","job":Command.Add "value_mode","preserve":Command.Add "custom_value","":Command.Add "sync_nc","1"
|
||||
Command.Add "target_scope","targets":Command.Add "target_selectors",QPEncode("u1" & Chr(31) & "R1" & Chr(31) & "SAME")
|
||||
On Error Resume Next
|
||||
QPCaptureAndSelect Command,QP_Fso.BuildPath(QP_Root,"snapshot.tsv")
|
||||
Failure=Err.Number
|
||||
On Error GoTo 0
|
||||
WScript.Echo CStr(Failure<>0)
|
||||
WScript.Echo CStr(QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root,"snapshot.tsv")))
|
||||
''',native_app=fake,routines_transform=replace_native)
|
||||
assert output==(['True','False'] if changed else ['False','True'])
|
||||
Reference in new issue
Block a user