2727from matplotlib import pyplot as plt
2828import numpy as np
2929from numpy .random import random
30+ import pytest
3031
3132from mpldxf import backend_dxf
3233
3334
3435matplotlib .backend_bases .register_backend ("dxf" , backend_dxf .FigureCanvas )
3536
3637
37- class DxfBackendTestCase (unittest .TestCase ):
38+ class TestDxfBackendCase (unittest .TestCase ):
3839 """Tests for the dxf backend."""
3940
4041 def test_plot_line_with_no_axis (self ):
4142 """Test a simple line-plot command."""
4243 plt .gca ().patch .set_visible (False )
43- plt .plot (range (5 ), [1 , 2 , 3 , 2 , 4 ])
44+ plt .plot (range (7 ), [1 , 2 , 3 , 2 , 4 , 6 , 7 ])
4445 plt .axis ("off" )
46+ plt .savefig ("tests/files/test_plot_line_with_no_axis.png" )
4547
46- outfile = "tests/files/test_plot_line_with_no_axis.dxf"
47- plt .close ()
48+ try :
49+ outfile = "tests/files/test_plot_line_with_no_axis.dxf"
50+ plt .savefig (outfile , transparent = True )
51+ finally :
52+ plt .close ()
4853
4954 # Load the DXF file and inspect its content
5055 doc = ezdxf .readfile (outfile )
5156 modelspace = doc .modelspace ()
5257 entities = list (modelspace )
53- assert len (entities ) == 2 # 1 line and the bounding box of the plot
58+ assert len (entities ) == 1 # 1 line
5459
5560 def test_plot_line (self ):
5661 """Test a simple line-plot command."""
5762 plt .gca ().patch .set_visible (False )
5863 plt .plot (range (3 ), [1 , 2 , 3 ])
59- outfile = "tests/files/test_plot_line.dxf"
60- plt .savefig (outfile , transparent = True )
61- plt .close ()
64+ plt .savefig ("tests/files/test_plot_line.png" )
65+
66+ try :
67+ outfile = "tests/files/test_plot_line.dxf"
68+ plt .savefig (outfile , transparent = True )
69+ finally :
70+ plt .close ()
71+
72+ # Load the DXF file and inspect its content
73+ doc = ezdxf .readfile (outfile )
74+ modelspace = doc .modelspace ()
75+ entities = list (modelspace )
76+ entity_types = set ([entity .dxftype () for entity in entities ])
77+ assert entity_types == {"LWPOLYLINE" , "TEXT" }
78+
79+ def test_plot_with_data_outside_axes (self ):
80+ """Test a simple line-plot command with data outside the axes."""
81+ plt .plot (range (7 ), [1 , 2 , 3 , 1e5 , 5 , 6 , 7 ])
82+ plt .ylim (0 , 7 )
83+ plt .xlim (1 , 6 )
84+ plt .savefig ("tests/files/test_plot_with_data_outside_axes.png" )
85+
86+ try :
87+ plt .savefig ("tests/files/test_plot_with_data_outside_axes.png" )
88+ outfile = "tests/files/test_plot_with_data_outside_axes.dxf"
89+ plt .savefig (outfile , transparent = True )
90+ finally :
91+ plt .close ()
92+
93+ # Load the DXF file and inspect its content
94+ doc = ezdxf .readfile (outfile )
95+ modelspace = doc .modelspace ()
96+ entities = list (modelspace )
97+ entity_types = set ([entity .dxftype () for entity in entities ])
98+ assert entity_types == {"LWPOLYLINE" , "TEXT" }
99+
100+ def test_plot_with_twin_axis_and_data_outside_axes (self ):
101+ """Test a simple line-plot command with data outside the axes."""
102+ fig , ax1 = plt .subplots ()
103+ ax2 = ax1 .twinx ()
104+ ax1 .plot (range (7 ), [1 , 2 , 3 , 1e5 , 5 , 6 , 7 ])
105+ ax2 .plot (range (7 ), [1 , 2 , 3 , 1e5 , 5 , 6 , 7 ])
106+ ax1 .set_ylim (1 , 6 )
107+ ax2 .set_ylim (1 , 6 )
108+ plt .savefig ("tests/files/test_plot_with_twin_axis_and_data_outside_axes.png" )
109+
110+ try :
111+ plt .savefig (
112+ "tests/files/test_plot_with_twin_axis_and_data_outside_axes.png"
113+ )
114+ outfile = "tests/files/test_plot_with_twin_axis_and_data_outside_axes.dxf"
115+ plt .savefig (outfile , transparent = True )
116+ finally :
117+ plt .close ()
62118
63119 # Load the DXF file and inspect its content
64120 doc = ezdxf .readfile (outfile )
@@ -76,20 +132,29 @@ def test_boxplot(self):
76132 [3 , 5 , 6 , 7 , 9 , 10 , 12 , 13 ],
77133 ]
78134 plt .boxplot (data )
79- outfile = "tests/files/test_boxplot.dxf"
80- plt .savefig (outfile )
81- plt .close ()
135+ plt .savefig ("tests/files/test_boxplot.png" )
136+
137+ try :
138+ outfile = "tests/files/test_boxplot.dxf"
139+ plt .savefig (outfile )
140+ finally :
141+ plt .close ()
82142
83143 def test_contour (self ):
84144 """Test some contours."""
145+ print ("TEST CONTOUR" )
85146 x = np .linspace (- 5.0 , 5.0 , 30 )
86147 y = np .linspace (- 5.0 , 5.0 , 30 )
87148 X , Y = np .meshgrid (x , y )
88149 Z = np .sin (np .sqrt (X ** 2 + Y ** 2 ))
89150 plt .contour (X , Y , Z )
90- outfile = "tests/files/test_contour.dxf"
91- plt .savefig (outfile )
92- plt .close ()
151+ plt .savefig ("tests/files/test_contour.png" )
152+
153+ try :
154+ outfile = "tests/files/test_contour.dxf"
155+ plt .savefig (outfile )
156+ finally :
157+ plt .close ()
93158
94159 def test_contourf (self ):
95160 """Test some filled contours."""
@@ -98,6 +163,11 @@ def test_contourf(self):
98163 X , Y = np .meshgrid (x , y )
99164 Z = np .sin (np .sqrt (X ** 2 + Y ** 2 ))
100165 plt .contourf (X , Y , Z )
101- outfile = "tests/files/test_contourf.dxf"
102- plt .savefig (outfile )
103- plt .close ()
166+ plt .savefig ("tests/files/test_contourf.png" )
167+
168+ try :
169+ outfile = "tests/files/test_contourf.dxf"
170+ plt .savefig (outfile )
171+
172+ finally :
173+ plt .close ()
0 commit comments