New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Changeset 4126 for branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo/test_nml.py – NEMO

Ignore:
Timestamp:
2013-10-24T18:49:17+02:00 (11 years ago)
Author:
andrewryan
Message:

added latest version of OOO namelist editor and test suite

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2013/dev_r3987_UKMO4_OBS/NEMOGCM/TOOLS/OBSTOOLS/OOO/ooo/test_nml.py

    r4122 r4126  
    55def version(vn): 
    66    # Converts module version string to int tuple as PEP-440 
    7     return tuple(map(int, nml.__version__.split("."))) 
     7    return tuple(map(int, vn.split("."))) 
    88 
    99HEADER = """ 
     
    1616LOGIC = " ln_test = .TRUE. ! Comment\n" 
    1717NUMERIC = " nn_off_idx = 1 2 3\n" 
    18 FORD = " nn_off_idx=1 2 3\n" 
     18NOSPACE = " nn_off_idx=1 2 3\n" 
    1919NEGATIVE = " nn_off_idx = -1 -2 -3\n" 
    2020LIST = " off_files = 'a.nc' 'b.nc' 'c.nc' ! Comment\n" 
     
    2828        self.logic_text = "\n".join([HEADER, LOGIC, FOOTER]) 
    2929        self.numeric_text = "\n".join([HEADER, NUMERIC, FOOTER]) 
    30         self.ford_text = "\n".join([HEADER, FORD, FOOTER]) 
     30        self.nospace_text = "\n".join([HEADER, NOSPACE, FOOTER]) 
    3131        self.neg_numeric_text = "\n".join([HEADER, NEGATIVE, FOOTER]) 
    3232        self.list_text = "\n".join([HEADER, LIST, FOOTER]) 
     
    6363        self.assertDictEqual(data, truth) 
    6464 
    65     def test_ford_data(self): 
    66         data = nml.variables(self.ford_text) 
     65    def test_nospace_data(self): 
     66        data = nml.variables(self.nospace_text) 
    6767        truth = {"nn_off_idx": "1 2 3"} 
    6868        self.assertDictEqual(data, truth) 
     
    8484 
    8585    def test_replace_variable_comment(self): 
    86         FIXTURE = " x = y ! comment \n" 
    87         RESULT = " x = z ! comment \n" 
     86        FIXTURE = " x = 'y' ! comment \n" 
     87        RESULT = " x = 'z' ! comment \n" 
    8888        text = nml.replace(FIXTURE, {"x": "z"}) 
    8989        self.assertEqual(text, RESULT) 
    9090 
    9191    def test_replace_variable_no_comment(self): 
    92         FIXTURE = " x = y \n" 
    93         RESULT = " x = z \n" 
     92        FIXTURE = " x = 'y' \n" 
     93        RESULT = " x = 'z' \n" 
     94        text = nml.replace(FIXTURE, {"x": "z"}) 
     95        self.assertEqual(text, RESULT) 
     96 
     97    def test_replace_variable_no_space(self): 
     98        FIXTURE = " x='y' \n" 
     99        RESULT = " x='z' \n" 
     100        text = nml.replace(FIXTURE, {"x": "z"}) 
     101        self.assertEqual(text, RESULT) 
     102 
     103    def test_replace_variable_no_space_comment(self): 
     104        FIXTURE = " x='y' ! comment \n" 
     105        RESULT = " x='z' ! comment \n" 
    94106        text = nml.replace(FIXTURE, {"x": "z"}) 
    95107        self.assertEqual(text, RESULT) 
     
    160172        self.char = "foo@bar.com" 
    161173        self.num = 10 
     174        self.mixed_list = ["foo.nc", -1, True] 
     175 
     176    def test_should_format_mixed_list(self): 
     177        data = nml.tostring(self.mixed_list) 
     178        result = "'foo.nc', -1, .TRUE." 
     179        self.assertEqual(data, result) 
    162180 
    163181    def test_should_format_numeric_list(self): 
     
    173191    def test_should_format_strings(self): 
    174192        data = nml.tostring(self.char) 
    175         result = self.char 
     193        result = "'%s'" % (self.char,) 
    176194        self.assertEqual(data, result) 
    177195 
     
    180198        result = str(self.num) 
    181199        self.assertEqual(data, result) 
     200 
     201    def test_should_not_format_numeric_string(self): 
     202        input = "3.14159" 
     203        self.assertEqual(nml.tostring(input), input) 
    182204 
    183205    def test_should_format_logicals(self): 
     
    186208        self.assertEqual(data.upper(), result) 
    187209 
     210    def test_should_not_format_string_of_list_data(self): 
     211        for input in ["1 2 3", "1, 2, 3", ".TRUE. .FALSE."]: 
     212            case = nml.tostring(input) 
     213            self.assertEqual(case, input) 
     214 
    188215class TestUpdateNamelist(unittest.TestCase): 
    189216    def setUp(self): 
     
    194221        self.single = """ 
    195222&namone 
    196    x = y 
     223   x = 'y' 
    197224/ 
    198225""" 
    199226        self.single_update = """ 
    200227&namone 
    201    x = z 
     228   x = 'z' 
    202229/ 
    203230""" 
    204231 
    205232    def test_should_append_new_variable_to_namelist(self): 
    206         trial = nml.update("namone", self.empty, {"x": "y"}) 
     233        trial = nml.update("namone", self.empty, {"x": "'y'"}) 
    207234        self.assertEqual(trial, self.single) 
    208235 
    209236    def test_should_update_existing_variables(self): 
    210         trial = nml.update("namone", self.single, {"x": "z"}) 
     237        trial = nml.update("namone", self.single, {"x": "'z'"}) 
    211238        self.assertEqual(trial, self.single_update) 
    212239 
Note: See TracChangeset for help on using the changeset viewer.