def test_dt_file(self, test_id, sagews, src_file): print(("src_file=", src_file)) import sys from sage.doctest.sources import FileDocTestSource from sage.doctest.control import DocTestDefaults FDS = FileDocTestSource(src_file, DocTestDefaults()) doctests, extras = FDS.create_doctests(globals()) id = test_id excount = 0 dtn = 0 print(("{} doctests".format(len(doctests)))) for dt in doctests: print(("doctest number", dtn)) dtn += 1 exs = dt.examples excount += len(exs) for ex in exs: c = ex.sage_source print(("code", c)) w = ex.want print(("want", w)) use_pattern = False # handle ellipsis in wanted output if '...' in w: use_pattern = True # special case for bad "want" value at end of banner() wf = w.find('"help()" for help') if wf > 0: w = w[:wf] + '...' m = conftest.message.execute_code(code=c, id=id) sagews.send_json(m) if len(w) > 0: typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == id if 'stdout' in mesg: #assert 'stdout' in mesg output = mesg['stdout'] print(("outp", output)) elif 'stderr' in mesg: output = mesg['stderr'] # bypass err line number reporting in CoCalc if w.startswith('Traceback'): otf = output.find('Traceback') if otf > 0: output = output[otf:] print(("outp", output)) else: assert 0 if use_pattern: assert doctest._ellipsis_match(w, output) else: assert output.strip() == w.strip() conftest.recv_til_done(sagews, id) id += 1 print(("{} examples".format(excount))) conftest.test_id.id = id
def test_dt_file(self, test_id, sagews, src_file): print("src_file=",src_file) import sys from sage.doctest.sources import FileDocTestSource from sage.doctest.control import DocTestDefaults FDS = FileDocTestSource(src_file,DocTestDefaults()) doctests, extras = FDS.create_doctests(globals()) id = test_id excount = 0 dtn = 0 print "{} doctests".format(len(doctests)) for dt in doctests: print "doctest number", dtn dtn += 1 exs = dt.examples excount += len(exs) for ex in exs: c = ex.sage_source print("code",c) w = ex.want print("want",w) use_pattern = False # handle ellipsis in wanted output if '...' in w: use_pattern = True # special case for bad "want" value at end of banner() wf = w.find('"help()" for help') if wf > 0: w = w[:wf]+'...' m = conftest.message.execute_code(code = c, id = id) sagews.send_json(m) if len(w) > 0: typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == id if 'stdout' in mesg: #assert 'stdout' in mesg output = mesg['stdout'] print("outp",output) elif 'stderr' in mesg: output = mesg['stderr'] # bypass err line number reporting in CoCalc if w.startswith('Traceback'): otf = output.find('Traceback') if otf > 0: output = output[otf:] print("outp",output) else: assert 0 if use_pattern: assert doctest._ellipsis_match(w ,output) else: assert output.strip() == w.strip() conftest.recv_til_done(sagews, id) id += 1 print "{} examples".format(excount) conftest.test_id.id = id
def test_issue594(self, test_id, sagews): code = """G = Graph(sparse=True) G.allow_multiple_edges(True) G.add_edge(1,2) G.add_edge(2,3) G.add_edge(3,1) for i in range(2): print ("BEFORE PLOT %s"%i) G.show() print ("AFTER PLOT %s"%i)""" m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 12 messages from worksheet client, including final done:true json_wanted = 6 jstep = 0 blob_wanted = 2 while json_wanted > 0 and blob_wanted > 0: typ, mesg = sagews.recv() assert typ == 'json' or typ == 'blob' if typ == 'json': assert mesg['id'] == test_id json_wanted -= 1 jstep += 1 if jstep == 1: assert 'stdout' in mesg assert 'BEFORE PLOT 0' in mesg['stdout'] continue elif jstep == 2: assert 'file' in mesg continue elif jstep == 3: assert 'stdout' in mesg assert 'AFTER PLOT 0' in mesg['stdout'] continue elif jstep == 4: assert 'stdout' in mesg assert 'BEFORE PLOT 1' in mesg['stdout'] continue elif jstep == 5: assert 'file' in mesg continue elif jstep == 6: assert 'stdout' in mesg assert 'AFTER PLOT 1' in mesg['stdout'] continue else: blob_wanted -= 1 file_uuid = mesg[:SHA_LEN].decode() assert file_uuid == conftest.uuidsha1(mesg[SHA_LEN:]) m = conftest.message.save_blob(sha1=file_uuid) sagews.send_json(m) continue conftest.recv_til_done(sagews, test_id)
def test_issue594(self, test_id, sagews): code = """G = Graph(sparse=True) G.allow_multiple_edges(True) G.add_edge(1,2) G.add_edge(2,3) G.add_edge(3,1) for i in range(2): print ("BEFORE PLOT %s"%i) G.show() print ("AFTER PLOT %s"%i)""" m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 12 messages from worksheet client, including final done:true json_wanted = 6 jstep = 0 blob_wanted = 2 while json_wanted > 0 and blob_wanted > 0: typ, mesg = sagews.recv() assert typ == 'json' or typ == 'blob' if typ == 'json': assert mesg['id'] == test_id json_wanted -= 1 jstep += 1 if jstep == 1: assert 'stdout' in mesg assert 'BEFORE PLOT 0' in mesg['stdout'] continue elif jstep == 2: assert 'file' in mesg continue elif jstep == 3: assert 'stdout' in mesg assert 'AFTER PLOT 0' in mesg['stdout'] continue elif jstep == 4: assert 'stdout' in mesg assert 'BEFORE PLOT 1' in mesg['stdout'] continue elif jstep == 5: assert 'file' in mesg continue elif jstep == 6: assert 'stdout' in mesg assert 'AFTER PLOT 1' in mesg['stdout'] continue else: blob_wanted -= 1 file_uuid = mesg[:SHA_LEN] assert file_uuid == conftest.uuidsha1(mesg[SHA_LEN:]) m = conftest.message.save_blob(sha1=file_uuid) sagews.send_json(m) continue conftest.recv_til_done(sagews, test_id)
def test_show_doc(self, test_id, sagews): # issue 476 code = "show?" patn = "import smc_sagews.graphics\nsmc_sagews.graphics.graph_to_d3_jsonable?" m = conftest.message.execute_code(code = code, id = test_id) sagews.send_json(m) typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'code' in mesg assert 'source' in mesg['code'] assert re.sub('\s+','',patn) in re.sub('\s+','',mesg['code']['source']) conftest.recv_til_done(sagews, test_id)
def test_bad_mult(self, test_id, sagews): # warn about possible missing '*' with patterns like 3x^2 and 5(1+x) code = ("x=1\ny=3x^2x") m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 2 messages from worksheet client # 1 stderr typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'implicit multiplication' in mesg['stderr'] # 2 done conftest.recv_til_done(sagews, test_id)
def test_non_ascii(self, test_id, sagews): # assign to hbar to trigger non-ascii warning code = "ħ = 9" m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 2 messages from worksheet client # 1 stderr Error in lines 1-1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'Error in lines 1-1' in mesg['stderr'] assert 'should be replaced by < " >' not in mesg['stderr'] conftest.recv_til_done(sagews, test_id)
def test_bad_quote(self, test_id, sagews): # assign x to U+201C (could use U+201D) to trigger bad quote warning code = "“" m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 2 messages from worksheet client # 1 stderr Error in lines 1-1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'Error in lines 1-1' in mesg['stderr'] assert 'should be replaced by < " >' in mesg['stderr'] # 2 done conftest.recv_til_done(sagews, test_id)
def test_bad_quote(self, test_id, sagews): # assign x to one of U+201C or U+201D to trigger bad quote warning code = ("x = " + unichr(8220) + "\nx").encode('utf-8') m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 2 messages from worksheet client # 1 stderr Error in lines 1-1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'Error in lines 1-1' in mesg['stderr'] assert 'should be replaced by < " >' in mesg['stderr'] # 2 done conftest.recv_til_done(sagews, test_id)
def test_bad_quote(self, test_id, sagews): # assign x to U+201C (could use U+201D) to trigger bad quote warning code = "“" m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 2 messages from worksheet client # 1 stderr Error in lines 1-1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'non-ascii' in mesg['stderr'] if 'should be replaced by < " >' not in mesg['stderr']: typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'should be replaced by < " >' in mesg['stderr'] conftest.recv_til_done(sagews, test_id)
def test_non_ascii(self, test_id, sagews): # assign x to hbar to trigger non-ascii warning code = ("x = " + unichr(295) + "\nx").encode('utf-8') m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 3 messages from worksheet client, including final done:true # 1 stderr Error in lines 1-1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'Error in lines 1-1' in mesg['stderr'] # 2 stderr WARNING: Code contains non-ascii characters typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'WARNING: Code contains non-ascii characters' in mesg['stderr'] # 3 done conftest.recv_til_done(sagews, test_id)
def test_non_ascii(self, test_id, sagews): # assign x to hbar to trigger non-ascii warning code = ("x = " + unichr(295) + "\nx").encode('utf-8') m = conftest.message.execute_code(code = code, id = test_id) sagews.send_json(m) # expect 3 messages from worksheet client, including final done:true # 1 stderr Error in lines 1-1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'Error in lines 1-1' in mesg['stderr'] # 2 stderr WARNING: Code contains non-ascii characters typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stderr' in mesg assert 'WARNING: Code contains non-ascii characters' in mesg['stderr'] # 3 done conftest.recv_til_done(sagews, test_id)
def test_show_doc(self, test_id, sagews): # issue 476 code = "show?" patn = dedent(""" import smc_sagews.graphics smc_sagews.graphics.graph_to_d3_jsonable?""") m = conftest.message.execute_code(code = code, id = test_id) sagews.send_json(m) # ignore stderr message about deprecation warning for ix in [0,1]: typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id if 'stderr' in mesg: continue assert 'code' in mesg assert 'source' in mesg['code'] assert re.sub('\s+','',patn) in re.sub('\s+','',mesg['code']['source']) conftest.recv_til_done(sagews, test_id) break
def test_show_doc(self, test_id, sagews): # issue 476 code = "show?" patn = dedent(""" import smc_sagews.graphics smc_sagews.graphics.graph_to_d3_jsonable?""") m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # ignore stderr message about deprecation warning for ix in [0, 1]: typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id if 'stderr' in mesg: continue assert 'code' in mesg assert 'source' in mesg['code'] assert re.sub('\s+', '', patn) in re.sub('\s+', '', mesg['code']['source']) conftest.recv_til_done(sagews, test_id) break
def test_issue594(self, test_id, sagews): code = """G = Graph(sparse=True) G.allow_multiple_edges(True) G.add_edge(1,2) G.add_edge(2,3) G.add_edge(3,1) for i in range(2): print ("BEFORE PLOT %s"%i) G.show() print ("AFTER PLOT %s"%i)""" m = conftest.message.execute_code(code=code, id=test_id) sagews.send_json(m) # expect 12 messages from worksheet client, including final done:true # 1 stdout BEFORE PLOT 0 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stdout' in mesg assert 'BEFORE PLOT 0' in mesg['stdout'] # 2 blob file typ, mesg = sagews.recv() assert typ == 'blob' file_uuid = mesg[:SHA_LEN] assert file_uuid == conftest.uuidsha1(mesg[SHA_LEN:]) m = conftest.message.save_blob(sha1=file_uuid) sagews.send_json(m) # 3 filename typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'file' in mesg # 4 stdout AFTER PLOT 0 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stdout' in mesg assert 'AFTER PLOT 0' in mesg['stdout'] # 5 stdout BEFORE PLOT 1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stdout' in mesg assert 'BEFORE PLOT 1' in mesg['stdout'] # 6 blob file typ, mesg = sagews.recv() assert typ == 'blob' file_uuid = mesg[:SHA_LEN] assert file_uuid == conftest.uuidsha1(mesg[SHA_LEN:]) m = conftest.message.save_blob(sha1=file_uuid) sagews.send_json(m) # 7 filename typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'file' in mesg # 8 stdout AFTER PLOT 1 typ, mesg = sagews.recv() assert typ == 'json' assert mesg['id'] == test_id assert 'stdout' in mesg assert 'AFTER PLOT 1' in mesg['stdout'] # 9 stdout newline conftest.recv_til_done(sagews, test_id)