1616import numpy as np
1717import pandas as pd
1818
19- from layoutparser .elements import (
20- Interval ,
21- Rectangle ,
22- Quadrilateral ,
23- TextBlock ,
24- Layout
25- )
19+ from layoutparser .elements import Interval , Rectangle , Quadrilateral , TextBlock , Layout
2620from layoutparser .elements .errors import InvalidShapeError , NotSupportedShapeError
2721
2822
@@ -242,12 +236,25 @@ def test_layout():
242236 r = Rectangle (3 , 3 , 5 , 6 )
243237 t = TextBlock (i , id = 1 , type = 2 , text = "12" )
244238
239+ # Test Initializations
240+ l = Layout ([i , q , r ])
241+ l = Layout ((i ,q ))
242+ Layout ([l ])
243+ with pytest .raises (ValueError ):
244+ Layout (l )
245+
246+ # Test tuple-like inputs
247+ l = Layout ((i , q , r ))
248+ assert l ._blocks == [i , q , r ]
249+ l .append (i )
250+
251+ # Test apply functions
245252 l = Layout ([i , q , r ])
246253 l .get_texts ()
247- l . condition_on ( i )
248- l . relative_to ( q )
249- l . filter_by ( t )
250- l .is_in (r )
254+ assert l . filter_by ( t ) == Layout ([ i ] )
255+ assert l . condition_on ( i ) == Layout ([ block . condition_on ( i ) for block in [ i , q , r ]] )
256+ assert l . relative_to ( q ) == Layout ([ block . relative_to ( q ) for block in [ i , q , r ]] )
257+ assert l .is_in (r ) == Layout ([ block . is_in ( r ) for block in [ i , q , r ]] )
251258 assert l .get_homogeneous_blocks () == [i .to_quadrilateral (), q , r .to_quadrilateral ()]
252259
253260 i2 = TextBlock (i , id = 1 , type = 2 , text = "12" )
@@ -286,17 +293,39 @@ def test_layout():
286293 l + l2
287294
288295 # Test sort
296+ ## When sorting inplace, it should return None
297+ l = Layout ([i ])
298+ assert l .sort (key = lambda x : x .coordinates [1 ], inplace = True ) is None
299+
300+ ## Make sure only sorting inplace works
289301 l = Layout ([i , i .shift (2 )])
290302 l .sort (key = lambda x : x .coordinates [1 ], reverse = True )
303+ assert l != Layout ([i .shift (2 ), i ])
304+ l .sort (key = lambda x : x .coordinates [1 ], reverse = True , inplace = True )
291305 assert l == Layout ([i .shift (2 ), i ])
292306
293307 l = Layout ([q , r , i ], page_data = {"width" : 200 , "height" : 400 })
294- assert l .sort (key = lambda x : x .coordinates [0 ], inplace = False ) == Layout (
308+ assert l .sort (key = lambda x : x .coordinates [0 ]) == Layout (
295309 [i , q , r ], page_data = {"width" : 200 , "height" : 400 }
296310 )
297311
298312 l = Layout ([q , t ])
299- assert l .sort (key = lambda x : x .coordinates [0 ], inplace = False ) == Layout ([q , t ])
313+ assert l .sort (key = lambda x : x .coordinates [0 ]) == Layout ([t , q ])
314+
315+
316+ def test_layout_comp ():
317+ a = Layout ([Rectangle (1 , 2 , 3 , 4 )])
318+ b = Layout ([Rectangle (1 , 2 , 3 , 4 )])
319+
320+ assert a == b
321+
322+ a .append (Rectangle (1 , 2 , 3 , 5 ))
323+ assert a != b
324+ b .append (Rectangle (1 , 2 , 3 , 5 ))
325+ assert a == b
326+
327+ a = Layout ([TextBlock (Rectangle (1 , 2 , 3 , 4 ))])
328+ assert a != b
300329
301330
302331def test_shape_operations ():
@@ -428,4 +457,4 @@ def test_dict():
428457
429458 l2 = Layout ([i2 , r2 , q2 ])
430459 l2_dict = {"page_data" : {}, "blocks" : [i_dict , r_dict , q_dict ]}
431- assert l2 .to_dict () == l2_dict
460+ assert l2 .to_dict () == l2_dict
0 commit comments