1+ import re
2+ from datetime import timedelta
13from typing import Any
24
35import numpy as np
46
5- from parcels import Grid , TimeConverter
7+ import parcels
8+ from parcels import Grid , ParticleFile , TimeConverter , Variable
69from parcels .grid import RectilinearGrid
10+ from tests .utils import create_fieldset_unit_mesh , create_simple_pset
711
812
9- def validate_simple_repr (class_ : type , kwargs : dict [str , Any ]):
13+ def assert_simple_repr (class_ : type , kwargs : dict [str , Any ]):
1014 """Test that the repr of an object contains all the arguments. This only works for objects where the repr matches the calling signature."""
1115 obj = class_ (** kwargs )
1216 obj_repr = repr (obj )
@@ -17,12 +21,48 @@ def validate_simple_repr(class_: type, kwargs: dict[str, Any]):
1721 assert class_ .__name__ in obj_repr
1822
1923
24+ def valid_indentation (s : str ) -> bool :
25+ """Make sure that all lines in string is indented with a multiple of 4 spaces."""
26+ if s .startswith (" " ):
27+ return False
28+
29+ lines = s .split ("\n " )
30+ for line in lines :
31+ line = re .sub ("^( {4})+" , "" , line )
32+ if line .startswith (" " ):
33+ return False
34+ return True
35+
36+
37+ def test_check_indentation ():
38+ valid = """
39+ test
40+ test
41+ test
42+ test
43+ test
44+ test"""
45+ assert valid_indentation (valid )
46+ invalid = """
47+ test
48+ test
49+ invalid!
50+ """
51+ assert not valid_indentation (invalid )
52+
53+
2054def test_grid_repr ():
2155 """Test arguments are in the repr of a Grid object"""
2256 kwargs = dict (
2357 lon = np .array ([1 , 2 , 3 ]), lat = np .array ([4 , 5 , 6 ]), time = None , time_origin = TimeConverter (), mesh = "spherical"
2458 )
25- validate_simple_repr (Grid , kwargs )
59+ assert_simple_repr (Grid , kwargs )
60+
61+
62+ def test_variable_repr ():
63+ """Test arguments are in the repr of the Variable object."""
64+ kwargs = dict (name = "test" , dtype = np .float32 , initial = 0 , to_write = False )
65+ assert_simple_repr (Variable , kwargs )
2666
2767
2868def test_rectilineargrid_repr ():
@@ -34,4 +74,41 @@ def test_rectilineargrid_repr():
3474 kwargs = dict (
3575 lon = np .array ([1 , 2 , 3 ]), lat = np .array ([4 , 5 , 6 ]), time = None , time_origin = TimeConverter (), mesh = "spherical"
3676 )
37- validate_simple_repr (RectilinearGrid , kwargs )
77+ assert_simple_repr (RectilinearGrid , kwargs )
78+
79+
80+ def test_particlefile_repr ():
81+ pset = create_simple_pset ()
82+ kwargs = dict (
83+ name = "file.zarr" , particleset = pset , outputdt = timedelta (hours = 1 ), chunks = None , create_new_zarrfile = False
84+ )
85+ assert_simple_repr (ParticleFile , kwargs )
86+
87+
88+ def test_field_repr ():
89+ field = create_fieldset_unit_mesh ().U
90+ assert valid_indentation (repr (field ))
91+
92+
93+ def test_vectorfield_repr ():
94+ field = create_fieldset_unit_mesh ().UV
95+ assert isinstance (field , parcels .VectorField )
96+ assert valid_indentation (repr (field ))
97+
98+
99+ def test_fieldset_repr ():
100+ fieldset = create_fieldset_unit_mesh ()
101+ assert valid_indentation (repr (fieldset ))
102+
103+
104+ def test_particleset_repr ():
105+ pset = create_simple_pset ()
106+ valid_indentation (repr (pset ))
107+
108+ pset = create_simple_pset (n = 15 )
109+ valid_indentation (repr (pset ))
110+
111+
112+ def capture (s ):
113+ with open ("file.txt" , "a" ) as f :
114+ f .write (s )
0 commit comments