def test_json_python(self): """ Tests the --json-include and --python-include of CLI for dprepro and pyprepro plus the pyprepro function call. Also test with multiple JSONs to address a prior bug """ GOLD = read('test_gold/json_include.gold') cmd = ('--json-include test_files/json_include_params.json ' '--python-include test_files/python_include_params.py ' '--json-include test_files/json_include_params2.json ' 'test_files/json_include.inp test_output/json_include0.inp') pyprepro._pyprepro_cli(shsplit(cmd)) # CLI self.assert_(compare_lines(GOLD,read('test_output/json_include0.inp'))) # Function self.assert_(compare_lines(GOLD,pyprepro.pyprepro('test_files/json_include.inp', json_include=['test_files/json_include_params.json', 'test_files/json_include_params2.json'], python_include='test_files/python_include_params.py'))) cmd = ('--no-warn ' '--json-include test_files/json_include_params.json ' '--python-include test_files/python_include_params.py ' '--json-include test_files/json_include_params2.json ' 'test_files/dakota_aprepro.1 ' # Needs something. This doesn't matter 'test_files/json_include.inp ' 'test_output/json_include1.inp') pyprepro._dprepro_cli(shsplit(cmd)) self.assert_(compare_lines(GOLD,read('test_output/json_include1.inp')))
def test_cli_no_infile(self): cmd = 'fake_file_' + ''.join( random.choice('123456789') for _ in range(10)) with CLI_Error() as E: pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(E.exit_code != 0) self.assert_(E.stderr.strip( ) == 'ERROR: `infile` must be a file or `-` to read from stdin')
def test_white_space(self): """ Check that trialing \\ before a code block works as expected """ # Test via the command line since python (not pyprepro) gets wonky around # some of the strings cmd = ' --no-warn test_files/white_space.inp test_output/white_space.out' pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(compare_lines(read('test_output/white_space.out'), read('test_gold/white_space.gold')))
def test_inline_comparison_and_assignment(self): """ Tests for things like: { A += 1 } # Assignment { A <= 1 } # comparison and also test how this handles quotes and '=' in a function call """ cmd = ['test_files/inline_comparison_and_assignment_colons.inp', 'test_output/inline_comparison_and_assignment_colons.out'] pyprepro._pyprepro_cli(cmd) self.assert_(compare_lines(\ read('test_output/inline_comparison_and_assignment_colons.out'), read('test_gold/inline_comparison_and_assignment_colons.gold')))
def test_json(self): """ Tests the --json-include of CLI for dprepro and pyprepro plus the pyprepro function call """ GOLD = read('test_gold/json_include.gold') cmd = '--json-include test_files/json_include_params.json test_files/json_include.inp test_output/json_include0.inp' pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(compare_lines(GOLD,read('test_output/json_include0.inp'))) self.assert_(compare_lines(GOLD,pyprepro.pyprepro('test_files/json_include.inp',json_include='test_files/json_include_params.json'))) cmd = '--no-warn --json-include test_files/json_include_params.json test_files/dakota_aprepro.1 test_files/json_include.inp test_output/json_include1.inp' pyprepro._dprepro_cli(shsplit(cmd)) self.assert_(compare_lines(GOLD,read('test_output/json_include1.inp')))
def test_change_code_delims(self): """ Test changing the code delimiters. Also test when you do *not* do it """ # This no longer works with setting the code-blocks. That is fine. It is # implicitly tested via the "right" tests # wcmd = 'test_files/new_delim.inp test_output/new_delim_wrong.out' # pyprepro._pyprepro_cli(shsplit(wcmd)) # assert compare_lines( # read('test_output/new_delim_wrong.out'), # read('test_gold/new_delim_wrong.gold')) ccmd = '--inline "[[ ]]" --code "$" --code-block "<{* *>}" test_files/new_delim.inp test_output/new_delim_right.out' pyprepro._pyprepro_cli(shsplit(ccmd)) self.assert_(compare_lines(\ read('test_output/new_delim_right.out'), read('test_gold/new_delim_right.gold')))
def test_cli_include(self): """ Tests that variables in a `--include FILE` statement are defined at all, defined as immutable, and NOT printed See the test_files/cli_include_main.inp test_files/cli_include_inc.inp files. """ cmd = "--var cparam=10 --include test_files/cli_include_inc.inp test_files/cli_include_main.inp test_output/cli_include.out" pyprepro._pyprepro_cli(shsplit(cmd)) out = read('test_output/cli_include.out') gold = read('test_gold/cli_include.gold') self.assert_(compare_lines(out,gold))
def test_unicode(self): """ Test a file with lots of unicode """ cmd = "test_files/unicode.inp test_output/unicode.out" gold=u"""\ Testing unicode: ° ☀ ☁ ☂ ☃ ☄ ★ ☆ ☇ ☈ ☉ ☊ ☋ ☌ ☍ ☎ ☏ ☐ ☑ ☒ ☓ ☚ ☛ ☜ ☝ ☞ ☟ ☠ ☡ ☢ ☣ ☤ ☥ ☦ ☧ ☨ ☩ ☪ ☫ ☬ ☭ ☮ ☯ ☰ ☱ ☲ ☳ ☴ ☵ ¡ ¢ £ ¤ ¥ ¦ § ¨ © ª « ¬ ® ¯ ° ± ² ³ ´ µ ¶ · ¸ ¹ º » ¼ ½ ¾ ¿ À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï Ð Ñ Ò Ó Ô Õ Ö × Ø Ù Ú Û Ü Ý Þ ß à á â ã ä å æ ç è é ê ë ì í î ï ð ñ ò ó ô õ ö ÷ ø ù ú û ü ý þ ÿ """ pyprepro._pyprepro_cli(shsplit(cmd)) output = read('test_output/unicode.out') self.assert_(compare_lines(output,gold) )
def test_dakota_include(self): """ Tests the new --dakota-include syntax even when *NOT* using the default stylings (of aprepro) """ tpl = """ x = [[x = 5]] {{% i1x_y = 99 tester = True %}} Tester: [[tester]] 1x:y: [[i1x_y]] """ gold = """ x = 0.08889860404 Tester: True 1x:y: 3.046260756 """ res1 = pyprepro.pyprepro(tpl,dakota_include='test_files/dakota_aprepro.1', code_block='{{% %}}',inline='[[ ]]',warn=False) res2 = pyprepro.pyprepro(tpl,dakota_include='test_files/dakota_default.1', code_block='{{% %}}',inline='[[ ]]',warn=False) self.assert_(res1 == res2) self.assert_(compare_lines(res1,gold)) with open('test_output/tmp.tpl','wt') as F: F.write(tpl) cmd = ['--no-warn', '--code-block','{{% %}}', '--inline','[[ ]]', '--dakota-include','test_files/dakota_aprepro.1', 'test_output/tmp.tpl','test_output/dakota_include.out'] pyprepro._pyprepro_cli(cmd) compare_lines(gold,read('test_output/dakota_include.out')) cmd = ['--no-warn', '--code-block','{{% %}}', '--inline','[[ ]]', '--dakota-include','test_files/dakota_default.1', 'test_output/tmp.tpl','test_output/dakota_include.out'] pyprepro._pyprepro_cli(cmd) compare_lines(gold,read('test_output/dakota_include.out'))
def test_CLI_vars(self): """ Test variables defined at the CLI and make sure they are correctly set as immutables """ # Clean cmd = 'test_files/cli_var.inp test_output/cli_var.inp.0' pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(compare_lines(\ read('test_output/cli_var.inp.0'), read('test_gold/cli_var.gold.0'))) # Set the first one (A) # The rest should follow since A is immutable cmd = '--var "A=1.2" test_files/cli_var.inp test_output/cli_var.inp.1' pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(compare_lines(\ read('test_output/cli_var.inp.1'), read('test_gold/cli_var.gold.1'))) # Set C. A & B should still be the original (1.5) but from C down, # it should be based on that. # Also test with more spaces cmd = '--var "C = 1.2" test_files/cli_var.inp test_output/cli_var.inp.2' pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(compare_lines(\ read('test_output/cli_var.inp.2'), read('test_gold/cli_var.gold.2'))) # Set "other" to an int cmd = '--var "other = 13" test_files/cli_var.inp test_output/cli_var.inp.3' pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(compare_lines(\ read('test_output/cli_var.inp.3'), read('test_gold/cli_var.gold.3'))) # set "other" to a string # NOTICE: you do not quote it. As it is bash, it is assumed to be a string cmd = '--var "other = text" test_files/cli_var.inp test_output/cli_var.inp.4' pyprepro._pyprepro_cli(shsplit(cmd)) self.assert_(compare_lines(\ read('test_output/cli_var.inp.4'), read('test_gold/cli_var.gold.4')))
def test_include_statements(self): """ test include statements from here """ input = """\ {% I = Immutable(1) M = 3 %} % include('test_files/inline_include.inp') ii:{ii}, ii = ii + 1: {ii = ii+1} mm:{mm}, mm = mm + 1: {mm = mm+1} """ gold = """\ -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= in template Paramaters defined outside. Test immutability too I: 1; I+1: 2; I = 2*I: 1 M: 3; M+1: 4; M = 2*M: 6 Define parameters here 3.141592654 1.570796327 ii:3.141592654, ii = ii + 1: 3.141592654 mm:1.570796327, mm = mm + 1: 2.570796327""" output = pyprepro.pyprepro(input) self.assert_(compare_lines(output, gold)) ################### # Test with depth and from CLI cmd = [ 'test_files/include_test2_0.inp', 'test_output/include_test2_0.out' ] pyprepro._pyprepro_cli(cmd) self.assert_(compare_lines(\ read('test_output/include_test2_0.out'), read('test_gold/include_test2.gold') ))