class TestBackscatteredElectronEnergyDetectorXMLHandler(unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)

        self.h = BackscatteredElectronEnergyDetectorXMLHandler()

        self.obj = BackscatteredElectronEnergyDetector(1000, (12.34, 56.78))

        etree.register_namespace("mc", "http://pymontecarlo.sf.net")
        source = BytesIO(
            b'<mc:backscatteredElectronEnergyDetector xmlns:mc="http://pymontecarlo.sf.net"><channels>1000</channels><limits lower="12.34" upper="56.78" /></mc:backscatteredElectronEnergyDetector>'
        )
        self.element = etree.parse(source).getroot()

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def testcan_parse(self):
        self.assertTrue(self.h.can_parse(self.element))

    def testparse(self):
        obj = self.h.parse(self.element)

        self.assertAlmostEqual(12.34, obj.limits_eV[0], 4)
        self.assertAlmostEqual(56.78, obj.limits_eV[1], 4)
        self.assertEqual(1000, obj.channels)

    def testcan_convert(self):
        self.assertTrue(self.h.can_convert(self.obj))

    def testconvert(self):
        element = self.h.convert(self.obj)

        subelement = element.find("limits")
        self.assertAlmostEqual(12.34, float(subelement.get("lower")), 4)
        self.assertAlmostEqual(56.78, float(subelement.get("upper")), 4)

        subelement = element.find("channels")
        self.assertEqual(1000, int(subelement.text))