@@ -263,7 +263,7 @@ def test_draw_tree_basic(self):
263263 ef_file_path = ef_file .name
264264
265265 try :
266- result = draw_tree .draw_tree (ef_file_path )
266+ result = draw_tree .generate_tikz (ef_file_path )
267267
268268 # Verify the result contains expected components
269269 assert isinstance (result , str )
@@ -282,6 +282,69 @@ def test_draw_tree_basic(self):
282282 finally :
283283 os .unlink (ef_file_path )
284284
285+ def test_draw_tree_raises_when_no_ipython (self ):
286+ """When IPython is not available, draw_tree should raise EnvironmentError."""
287+ with patch ('draw_tree.core.get_ipython' , return_value = None ):
288+ with tempfile .NamedTemporaryFile (mode = 'w' , delete = False , suffix = '.ef' ) as ef_file :
289+ ef_file .write ("player 1\n " )
290+ ef_file .write ("level 0 node root player 1\n " )
291+ ef_file_path = ef_file .name
292+ try :
293+ with pytest .raises (EnvironmentError ):
294+ draw_tree .draw_tree (ef_file_path )
295+ finally :
296+ os .unlink (ef_file_path )
297+
298+ def test_draw_tree_calls_ipython_magic_when_available (self ):
299+ """When IPython is available, draw_tree should load the jupyter_tikz
300+ extension if needed and call the tikz cell magic with the generated code.
301+ """
302+ # Create a simple .ef file for testing
303+ with tempfile .NamedTemporaryFile (mode = 'w' , delete = False , suffix = '.ef' ) as ef_file :
304+ ef_file .write ("player 1\n " )
305+ ef_file .write ("level 0 node root player 1\n " )
306+ ef_file_path = ef_file .name
307+
308+ class DummyEM :
309+ def __init__ (self , loaded = None ):
310+ self .loaded = loaded or set ()
311+
312+ class DummyIP :
313+ def __init__ (self , em ):
314+ self .extension_manager = em
315+ self ._loaded_magics = []
316+ self ._run_cell_magic_calls = []
317+
318+ def run_line_magic (self , name , arg ):
319+ # record that load_ext was called
320+ self ._loaded_magics .append ((name , arg ))
321+
322+ def run_cell_magic (self , magic_name , args , code ):
323+ # record call and return a sentinel
324+ self ._run_cell_magic_calls .append ((magic_name , args , code ))
325+ return "MAGIC-RESULT"
326+
327+ try :
328+ # Case 1: extension already loaded
329+ em = DummyEM (loaded = {'jupyter_tikz' })
330+ ip = DummyIP (em )
331+ with patch ('draw_tree.core.get_ipython' , return_value = ip ):
332+ res = draw_tree .draw_tree (ef_file_path )
333+ # Should call run_cell_magic and return its value
334+ assert res == "MAGIC-RESULT"
335+
336+ # Case 2: extension not loaded -> run_line_magic should be called
337+ em2 = DummyEM (loaded = set ())
338+ ip2 = DummyIP (em2 )
339+ with patch ('draw_tree.core.get_ipython' , return_value = ip2 ):
340+ res2 = draw_tree .draw_tree (ef_file_path )
341+ assert res2 == "MAGIC-RESULT"
342+ # run_line_magic should have been called to load the extension
343+ assert ('load_ext' , 'jupyter_tikz' ) in ip2 ._loaded_magics
344+
345+ finally :
346+ os .unlink (ef_file_path )
347+
285348 def test_draw_tree_with_options (self ):
286349 """Test draw_tree with different options."""
287350 # Create a simple .ef file for testing
@@ -292,15 +355,15 @@ def test_draw_tree_with_options(self):
292355
293356 try :
294357 # Test with scale
295- result_scaled = draw_tree .draw_tree (ef_file_path , scale_factor = 2.0 )
358+ result_scaled = draw_tree .generate_tikz (ef_file_path , scale_factor = 2.0 )
296359 assert "scale=2" in result_scaled
297360
298361 # Test with grid
299- result_grid = draw_tree .draw_tree (ef_file_path , show_grid = True )
362+ result_grid = draw_tree .generate_tikz (ef_file_path , show_grid = True )
300363 assert "\\ draw [help lines, color=green]" in result_grid
301364
302365 # Test without grid (default)
303- result_no_grid = draw_tree .draw_tree (ef_file_path , show_grid = False )
366+ result_no_grid = draw_tree .generate_tikz (ef_file_path , show_grid = False )
304367 assert "% \\ draw [help lines, color=green]" in result_no_grid
305368
306369 finally :
@@ -310,15 +373,15 @@ def test_draw_tree_missing_files(self):
310373 """Test draw_tree with missing files."""
311374 # Test with missing .ef file
312375 with pytest .raises (FileNotFoundError ):
313- draw_tree .draw_tree ("nonexistent.ef" )
376+ draw_tree .generate_tikz ("nonexistent.ef" )
314377
315378 # Test with valid .ef file (should work with built-in macros)
316379 with tempfile .NamedTemporaryFile (mode = 'w' , delete = False , suffix = '.ef' ) as ef_file :
317380 ef_file .write ("player 1\n level 0 node root player 1\n " )
318381 ef_file_path = ef_file .name
319382
320383 try :
321- result = draw_tree .draw_tree (ef_file_path )
384+ result = draw_tree .generate_tikz (ef_file_path )
322385 # Should work with built-in macros
323386 assert "\\ begin{tikzpicture}" in result
324387 assert "\\ newcommand\\ chancecolor{red}" in result
0 commit comments