def test_type(): """Test type command""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[2] expr_list = (("type('ABCDEFGHIJKLMNOPQRSTUVXYZ')", 'A'), ("type(v26^c)", 'N'), ("type(v69)", 'X')) run_list(expr_list, record)
def test_max_width(): """Test max width and field alignment""" record = MasterRecord() record.update(test_data) # Fake config and change default param MAX_LINE_WIDTH record.mst = Lyer() record.mst.config.INPUT_ENCODING = 'utf-8' record.mst.config.OUTPUT_ENCODING = 'utf-8' record.mst.config.MAX_LINE_WIDTH = 40 record.mst.config.YACC_DEBUG = False expr_list = ( ("v44", 'Methodology of plant eco-physiology: \nproceedings of the Montpellier \nSymposium' ), ("v44(0,0)", 'Methodology of plant eco-physiology: \nproceedings of the Montpellier \nSymposium' ), ("v44(10)", ' Methodology of plant \neco-physiology: proceedings of the \nMontpellier Symposium' ), ("v44(0,5)", 'Methodology of plant eco-physiology: \n proceedings of the Montpellier \n Symposium' ), ("v44(10,5)", ' Methodology of plant \n eco-physiology: proceedings of the \n Montpellier Symposium' ), # special case of unbreakable line ("'%s'" % ('a' * 120), u'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ), ) run_list(expr_list, record)
def test_replace(): """Test repalce() function""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("replace(v24,'plants','fruits')", 'Techniques for the measurement of transpiration of individual fruits' ), ("replace(v24,'','')", 'Techniques for the measurement of transpiration of individual plants' ), ("replace(v24,'measurement of ','')", 'Techniques for the transpiration of individual plants'), ("mdu,v26,'!',replace(v26^b,'Unesco','UnEsCo')/", "PARIS, UNESCO, -1965. !UnEsCo\n"), ("mdu,v26,'!',replace(v26,'Unesco','UnEsCo')/", "PARIS, UNESCO, -1965. !^aParis^bUnEsCo^c-1965\n"), ("mhu,v26,'!',replace(v26^b,'Unesco','UnEsCo')/", "PARIS, UNESCO, -1965!UnEsCo\n"), ("mhu,v26,'!',replace(v26,'Unesco','UnEsCo')/", "PARIS, UNESCO, -1965!^aParis^bUnEsCo^c-1965\n"), ("mpu,v26,'!',replace(v26^b,'Unesco','UnEsCo')/", "^APARIS^BUNESCO^C-1965!UnEsCo\n"), ("mpu,v26,'!',replace(v26,'Unesco','UnEsCo')/", "^APARIS^BUNESCO^C-1965!^aParis^bUnEsCo^c-1965\n"), ) run_list(expr_list, record)
def test_mfn_expressions(): """Test MFN expressions""" #record = MasterRecord(mfn=4) #record.update(test_data) mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[4] expr_list = ( ("mfn", '000004'), ("MFN", '000004'), ("mfn(0)", '4'), ("mfn(1)", '4'), ("mfn(2)", '04'), ("mfn=1", "False"), ("MFN>1", "True"), ("mfn=4", "True"), ("mfn<>4", "False"), ("not mfn=4", "False"), ("not mfn<>4", "True"), # Format ("mfn/", "000004\n"), ("f(mfn+1,0,0)/", "5\n"), ("s(mfn,' ',v24)", "000004 <An> Electric hygrometer apparatus for measuring water-vapour loss from \nplants in the field" ), ("ref(mfn,v24)", "<An> Electric hygrometer apparatus for measuring water-vapour loss from plants \nin the field" ), ("if mfn=4 then mfn,/, fi", "000004\n"), ) run_list(expr_list, record)
def test_if(): """Test IFs statements and boolean functions""" record = MasterRecord() record.update(test_data) expr_list = ( ('if p(v26) then v26^a fi', 'Paris'), ('if p(v25) then v26^a fi', ''), ('if p(v26) then v26^a else v26^b fi', 'Paris'), ('if p(v25) then v26^a else v26^b fi', 'Unesco'), ('if a(v26) then v26^a fi', ''), ('if a(v25) then v26^a fi', 'Paris'), ('if a(v26) then v26^a else v26^b fi', 'Unesco'), ('if a(v25) then v26^a else v26^b fi', 'Paris'), ('if p(v26) then v26^a fi', 'Paris'), ('if p(v26) then v26^a,v26^b fi', 'ParisUnesco'), ('if p(v26) then v26^a v26^b fi', 'ParisUnesco'), # field expression ("if v26>'' then v26^a fi", 'Paris'), ("if v50:'bib' then v26^a fi", 'Paris'), # test for heading and trailing spaces ("if 1=1 then 'ok' fi ", "ok"), (" if 1=1 then 'ok' fi", "ok"), (" if 1=1 then 'ok' fi ", "ok"), ("if nocc(v70)=2 then 'OK' else 'NOK' fi", "OK"), ) run_list(expr_list, record)
def test_spacing(): """Test for vertical spacing commands: # / %""" record = MasterRecord() record.update(test_data) expr_list = ( ('v26^b v26^a', 'UnescoParis'), ('v26^b v26^a', 'UnescoParis'), ('v26^b/v26^a', 'Unesco\nParis'), ('v26^b///v26^a', 'Unesco\nParis'), ('v26^b#v26^a', 'Unesco\nParis'), ('v26^b/#v26^a', 'Unesco\n\nParis'), ('v26^b##v26^a', 'Unesco\n\nParis'), ('v26^b###%v26^a', 'Unesco\nParis'), ('v26^bx10v26^a', 'Unesco Paris'), ('v26^bX10v26^a', 'Unesco Paris'), ('v26^bX100v26^a', "Unesco\nParis"), ('v26^bC0v26^a', "Unesco\nParis"), ("v26^bC7v26^a", "UnescoParis"), ("v26^bC8v26^a", "Unesco Paris"), # ignore tabulation that exceeds MAX_WIDTH ("v26^bC120v26^a", "UnescoParis"), ("v26^bC5v26^a", "Unesco\n Paris"), ("### X3 v26^b", "\n\n\n Unesco"), ("###,X3,v26^b", "\n\n\n Unesco"), ("v26^a#", "Paris\n"), ("v26^a///", "Paris\n"), ("v26^a#", "Paris\n"), ("v26^a%##v26^b%##v26^c", "Paris\nUnesco\n\n-1965"), ) run_list(expr_list, record)
def test_boolean_expr(): """Test Boolean expressions""" record = MasterRecord() record.update(test_data) expr_list = ( ("1<>2", "True"), ("1=2", "False"), ("1>2", "False"), ("1 > 2", "False"), ("1> 2", "False"), ("1 >2", "False"), ("1<2", "True"), ("1>=2", "False"), ("2>=1", "True"), ("2<=1", "False"), ("2>=1", "True"), ("1 > 2", "False"), ("1>2 or 2>1", "True"), ("1>2 and 2>1", "False"), ("1<2 or 2>1", "True"), ("1<2 and 2>1", "True"), ("1<2 xor 2>1", "False"), ("1>2 xor 2>1", "True"), ("not 1<2", "False"), ("not (1<2)", "False"), ) run_list(expr_list, record)
def test_repeatable_literals(): """Test repeatable literals""" record = MasterRecord() record.update(test_data) expr_list = ( ('|Do not show|v1', ''), ('|Show|v26', 'Show^aParis^bUnesco^c-1965'), ('mpu,|Show|v26^a', 'SHOWPARIS'), ('v1|Do not show|', ''), ('v26|Show|', '^aParis^bUnesco^c-1965Show'), ('|Do not show|v26^z', ''), ('|Show|v26^a', 'ShowParis'), ('v26^z|Do not show|', ''), ('v26^b|Show|', 'UnescoShow'), ('| Author:|v70', ' Author:Magalhaes, A.C. Author:Franco, C.M.'), ('| Author:|+v70', 'Magalhaes, A.C. Author:Franco, C.M.'), ('|<Left> |v70| <Right>|', u'<Left> Magalhaes, A.C. <Right><Left> Franco, C.M. <Right>'), ('|<Left> |+v70| <Right>|', u'Magalhaes, A.C. <Right><Left> Franco, C.M. <Right>'), ('|<Left> |v70+| <Right>|', u'<Left> Magalhaes, A.C. <Right><Left> Franco, C.M.'), ('|<Left> |+v70+| <Right>|', u'Magalhaes, A.C. <Right><Left> Franco, C.M.'), ('|Show |v26^a|; |/', 'Show Paris; \n'), ('|Show |+v26^a|; |/', 'Paris; \n'), ('|Show |v26^a+|; |/', 'Show Paris\n'), ('|Show |+v26^a+|; |/', 'Paris\n'), ('|Show |v26^a(5,5)|; |/', ' Show Paris; \n'), ('|Show |+v26^a(5,5)|; |/', ' Paris; \n'), ('|<antes>|+v70,|meio|+v26+|depois|/', 'Magalhaes, A.C.<antes>Franco, C.M.^aParis^bUnesco^c-1965\n'), ) run_list(expr_list, record)
def test_add_repeatable_field(): """Test the addition of a repeatable field""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = (("proc('a70|Beltrano!'),(v70/)", u'Magalhaes, A.C.\nFranco, C.M.\nBeltrano\n'), ) run_list(expr_list, record)
def test_datex(): """Test datex() function""" from time import strftime, localtime mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = (("datex('1147780749')", strftime('%Y%m%d %H%M%S %w %j', localtime(1147780749))), ) run_list(expr_list, record)
def test_mstname(): """Test mstname command""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("mstname", 'cds'), ("mdu,mstname", 'CDS'), ) run_list(expr_list, record)
def test_proc_gsplit(): """Test proc gsplit""" record = MasterRecord() record.v1 = 'Perez, J.; Garcia, Maria; Machado, A.' expr_list = ( ("proc('gsplit=1=;'),v1", u'Perez, J. Garcia, Maria Machado, A.'), ("proc('Gsplit=1=;'),v1", u'Perez, J. Garcia, Maria Machado, A.'), ) run_list(expr_list, record)
def test_mid(): """Test mid() function""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ('mid(v24,1,1)', 'T'), ('mid(v24,0,2)', 'Te'), ('mid(v24,-1,1)', 'T'), ) run_list(expr_list, record)
def test_iocc(): """Test iocc command""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = (( "(f(iocc,3,0),'.',v70/)", " 1.Magalhaes, A.C.\n 2.Franco, C.M.\n" ), ("(f(iocc,3,0),'.',v24/)", " 1.Techniques for the measurement of transpiration of individual plants\n" ), ("(f(iocc,2,0),'-',v70/)", " 1-Magalhaes, A.C.\n 2-Franco, C.M.\n")) run_list(expr_list, record)
def test_left(): """Test left() function""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ('left(v24,0)', ''), ('left(v24,-1)', ''), ('left(v24,10)', 'Techniques'), ('left(v24,14)', 'Techniques for'), ) run_list(expr_list, record)
def test_boolean_functions(): """Test boolean evaluation functions P() and A()""" record = MasterRecord() record.update(test_data) expr_list = ( ('p(v26)', 'True'), ('a(v26)', 'False'), ('P(v26)', 'True'), ('A(v26)', 'False'), ) run_list(expr_list, record)
def test_date(): """Test date() function""" from time import strftime mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("date(DATETIME)", strftime('%d/%m/%y %H:%M:%S')), ("date(DATEONLY)", strftime('%d/%m/%y')), ("date", strftime('%Y%m%d %H%M%S %w %j')), ) run_list(expr_list, record)
def test_npost(): """Test npost command""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("npost('1966.')", "50"), ("npost('PARIS, UNESCO , 1966.')", "1"), ("npost('PARIS, UNESCO, 1976.')", "5"), ("npost('BLA')", "0"), ) run_list(expr_list, record)
def test_proc_gizmo(): """Test proc gizmo""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] test_db = join('fixtures', 'test') expr_list = ( ("proc('G%s,24'),v24" % test_db, u'Techniques for the measurement of xxx of individual yyys'), ("proc('G%s,69'),v69" % test_db, u'Paper on: <yyy physiology><yyy xxx><measurement and instruments>'), ) run_list(expr_list, record)
def test_field_index(): """Test field index""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("v70[1]", 'Magalhaes, A.C.'), ("v70[2]", 'Franco, C.M.'), ("v70[1..2]", 'Magalhaes, A.C.Franco, C.M.'), ("v70[LAST]", 'Franco, C.M.'), ("v70[1..LAST]", 'Magalhaes, A.C.Franco, C.M.'), ) run_list(expr_list, record)
def test_proc(): """Test proc a, h and d""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("proc('d91','a91#Simple Field#'),v91", u'Simple Field'), ("proc('d93','h93 20 Fixed size text'),v93", u'Fixed size text '), ("proc('d92','d93','a93#content field 93#'),v93", 'content field 93'), ("proc('h92 10 ^aaaaaa^bbbbbb^cccccc'),v92", u'^aaaaaa^bbbbbb^cccccc'), ("proc('d92','d93','d91')", ''), ) run_list(expr_list, record)
def test_newline(): """Test newline command""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("newline('<BR>'),v70#", "Magalhaes, A.C.Franco, C.M.<BR>"), ("newline('<BR>'),v70#newline('\r\n'),v70#", "Magalhaes, A.C.Franco, C.M.<BR>Magalhaes, A.C.Franco, C.M.\r\n"), ("newline('\n'),v70#", "Magalhaes, A.C.Franco, C.M.\n"), ) run_list(expr_list, record)
def test_field_expressions(): """Test field access expressions""" record = MasterRecord() record.update(test_data) expr_list = ( ("v30^a,v30^b", 'p. 211-224illus.'), ("v26^a", 'Paris'), ("v26^a*2", 'ris'), ("v26^a.2", 'Pa'), ("v26^a*1.3", 'ari'), ) run_list(expr_list, record)
def test_break(): """Test break""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("(f(iocc,1,0),'=',v70,break),", u'1=Magalhaes, A.C.'), ("(v70,ref(mfn,break))", 'Magalhaes, A.C.'), ("(if iocc=1 and p(v70) then '+ occ'/,break else v26^a|-|,v26^b,/, fi,)", '+ occ\n'), ("(if iocc=2 and p(v70) then '+ occ'/,break else v26^a|-|,v26^b,/, fi,)", 'Paris-Unesco\n+ occ\n'), ) run_list(expr_list, record)
def test_include(): """Test include""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] test_pft = join('fixtures', 'test.pft') test2_pft = join('fixtures', 'test2.pft') expr_list = ( ("@%s" % test2_pft, u'P. 211-224, ILLUS. \nUnEsCo\nTechnique\n'), ("v70[1],' - ',@%s" % test_pft, u'Magalhaes, A.C. - P. 211-224, ILLUS. !UnEsCo!Technique\nCDS\n000001\n' ), ) run_list(expr_list, record)
def test_numerical_expr(): """Test Numerical expressions""" record = MasterRecord() record.update(test_data) expr_list = ( ('0.155e+3', '155.0'), ('1e-3', '0.001'), ('2*3+9', '15'), ("2*(3+9)", '24'), ("10-(4*(2-1))", "6"), ("15*0.001", "0.015"), ) run_list(expr_list, record)
def test_ref(): """Test ref() function""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("ref(1,v70)", 'Magalhaes, A.C.Franco, C.M.'), ("REF(1,v70)", 'Magalhaes, A.C.Franco, C.M.'), ("REF(2,v70)", 'Bosian, G.'), ("ref(1+1,v70)", 'Bosian, G.'), ("ref(mfn,v70)", 'Magalhaes, A.C.Franco, C.M.'), ("ref(val(v30)-200,v24)", 'Measurement of water stress in plants'), ) run_list(expr_list, record)
def test_repeatable_groups2(): """Test repetable groups for record 85""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[85] expr_list = ( ("# v70 if p(v71) then ## v71 / fi", "\nSanyal, Bikas C.Case, John H.Dow, Philip S.Jackman, Mary Elizabeth\n\nUniversity of ZambiaIIEP\n" ), ("(v70 # if p(v71) then v71,## fi)", "Sanyal, Bikas C.\nUniversity of Zambia\n\nCase, John H.\nIIEP\n\nDow, Philip S.\nJackman, Mary Elizabeth\n" ), ) run_list(expr_list, record)
def test_while(): """Test while""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("e1:=1,e2:=2,while e1<=e2 (f(e1,2,0),'-',v70[e1],e1:=e1+1)", u' 1-Magalhaes, A.C. 2-Franco, C.M.'), ("e1:=1,e2:=5,while e1<=e2 (f(e1,2,0),'. Test Loop\n',e1:=e1+1)", u' 1. Test Loop\n 2. Test Loop\n 3. Test Loop\n 4. Test Loop\n 5. Test Loop\n' ), ("e1:=1,e2:=nocc(v70),while e1<=e2(f(e1,1,0),'. ', v70[e1]/e1:=e1+1,),", "1. Magalhaes, A.C.\n2. Franco, C.M.\n"), ) run_list(expr_list, record)
def test_comments(): """Test c-style comments""" mf = MasterFile(join("..", "sample", "cds.mst")) record = mf[1] expr_list = ( ("v70,/* ok */v26", u'Magalhaes, A.C.Franco, C.M.^aParis^bUnesco^c-1965'), ("v70,/* ok */", u'Magalhaes, A.C.Franco, C.M.'), ("/* ok */v26", '^aParis^bUnesco^c-1965'), ("/* ok */ ", ""), ("/* */ ", ""), ("/**/ ", ""), ) run_list(expr_list, record)