def test_write_vocable_file (self):
		print('current working directory:', os.getcwd())
		
		vocable_file_path = self.create_test_vocables_file()
		print('--- using vocable file path:', vocable_file_path)
		
		list_of_words = ['你','好','吗','我','很']
		words_list_file_path = self.create_test_words_file(list_of_words)
		
		xsd_file_path = self.project_directory + 'res/xld-vocables-schema.xsd'
		
		attribute_name = 'chapters'
		attribute_value = 'HSK1-2012'
		words_attribute_name = 'secondLanguageTranslations'
		
		
		xml_parser = XMLParser()
		xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path)
		
		xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
		
		xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name)
		VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root)
		
		assert os.path.isfile(vocable_file_path), 'no file exists'
		
		# if the xml is not valid anymore when reading the file, the write function might have done something wrong
		try:
			xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
		except XMLParserException as exception:
			assert False
		else:
			assert True
    def test_write_vocable_file(self):
        print("current working directory:", os.getcwd())

        vocable_file_path = self.create_test_vocables_file()
        print("--- using vocable file path:", vocable_file_path)

        list_of_words = ["你", "好", "吗", "我", "很"]
        words_list_file_path = self.create_test_words_file(list_of_words)

        xsd_file_path = self.project_directory + "res/xld-vocables-schema.xsd"

        attribute_name = "chapters"
        attribute_value = "HSK1-2012"
        words_attribute_name = "secondLanguageTranslations"

        xml_parser = XMLParser()
        xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path)

        xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)

        xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name)
        VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root)

        assert os.path.isfile(vocable_file_path), "no file exists"

        # if the xml is not valid anymore when reading the file, the write function might have done something wrong
        try:
            xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
        except XMLParserException as exception:
            assert False
        else:
            assert True
    def test_add_values_to_attribute_of_vocables(self):
        xml_parser = XMLParser()

        list_of_words = ["你", "好", "吗", "我", "很"]

        test_word_list_file_path = self.create_test_words_file(list_of_words)
        test_vocable_file_path = self.create_test_vocables_file()
        xsd_file_path = self.project_directory + "res/xld-vocables-schema.xsd"

        # TODO: check if the chapter is not in the vocables already
        # attribute value hasn't been added yet, so it should not already be in the vocables
        # this assertion is necessary to check if the add_values_to_attribute_of_vocables really changes something
        xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path)

        for vocable in xml_root:
            vocable_word_attribute_text = vocable.find(self.words_attribute_name).text
            # is this vocable one of the world list?
            if WordListHelper.is_in_list(
                WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True
            ):
                print(vocable_word_attribute_text, "is in word list")
                attribute_text = vocable.find(self.attribute_name).text
                # does this vocable have the added attribute value?
                regex = "\s*" + self.attribute_value + "\s*$"
                print("regex:", regex)
                print("attribute_text:", attribute_text)
                assert re.search(regex, attribute_text) is None, (
                    "attribute value has already added to vocable " + vocable.find(self.words_attribute_name).text
                )

                # now add the attribute value
        xld_attribute_adder = XLDAttributeAdder(test_vocable_file_path, xsd_file_path, test_word_list_file_path)
        xld_attribute_adder.add_values_to_attribute_of_vocables(
            self.attribute_name, self.attribute_value, self.words_attribute_name
        )
        VocableFileWriter.write(VocableFileWriter, xsd_file_path, test_vocable_file_path, xld_attribute_adder.xml_root)

        # check the vocables again, to see if something has been added
        xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path)

        for vocable in xml_root:
            vocable_word_attribute_text = vocable.find(self.words_attribute_name).text
            # is this vocable one of the world list?
            if WordListHelper.is_in_list(
                WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True
            ):
                print(vocable_word_attribute_text, "is in word list")
                attribute_text = vocable.find(self.attribute_name).text
                # does this vocable have the added attribute value?
                regex = "\s*" + self.attribute_value + "\s*$"
                print("regex:", regex)
                print("attribute_text:", attribute_text)
                assert re.search(regex, attribute_text) is not None, (
                    "attribute value has not been added to vocable " + vocable.find(self.words_attribute_name).text
                )
	def write(self, xsd_file_path, vocable_file_path, xml_root):
		#print('writing xml to vocable file ...')
		xml_parser = XMLParser()
		if xml_parser.validate_tree(xsd_file_path, xml_root):
			try:
				with open(vocable_file_path, 'w') as file:
					file.write("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\n")
					file.write(etree.tostring(xml_root, xml_declaration=False, pretty_print=True, encoding="unicode"))
			except IOError:
				print("Error while writing in log file!")			
		else:
			print("Tree is invalid.")
			raise XMLInvalidException('Invalid XML!')
	def test_add_values_to_attribute_of_vocables (self):
		xml_parser = XMLParser()
		
		list_of_words = ['你','好','吗','我','很']
		
		test_word_list_file_path = self.create_test_words_file(list_of_words)
		test_vocable_file_path = self.create_test_vocables_file()
		xsd_file_path = self.project_directory + 'res/xld-vocables-schema.xsd'
		
		# TODO: check if the chapter is not in the vocables already
		# attribute value hasn't been added yet, so it should not already be in the vocables
		# this assertion is necessary to check if the add_values_to_attribute_of_vocables really changes something
		xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path)
		
		for vocable in xml_root:
			vocable_word_attribute_text = vocable.find(self.words_attribute_name).text
			# is this vocable one of the world list?
			if WordListHelper.is_in_list(WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True):
				print(vocable_word_attribute_text, 'is in word list')
				attribute_text = vocable.find(self.attribute_name).text
				# does this vocable have the added attribute value?
				regex = '\s*' + self.attribute_value + '\s*$'
				print('regex:', regex)
				print('attribute_text:', attribute_text)
				assert re.search(regex, attribute_text) is None, 'attribute value has already added to vocable ' + vocable.find(self.words_attribute_name).text
		
		
		# now add the attribute value
		xld_attribute_adder = XLDAttributeAdder(test_vocable_file_path, xsd_file_path, test_word_list_file_path)
		xld_attribute_adder.add_values_to_attribute_of_vocables(self.attribute_name, self.attribute_value, self.words_attribute_name)
		VocableFileWriter.write(VocableFileWriter, xsd_file_path, test_vocable_file_path, xld_attribute_adder.xml_root)
		
		
		# check the vocables again, to see if something has been added
		xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, test_vocable_file_path)
		
		for vocable in xml_root:
			vocable_word_attribute_text = vocable.find(self.words_attribute_name).text
			# is this vocable one of the world list?
			if WordListHelper.is_in_list(WordListHelper, vocable_word_attribute_text, list_of_words, allow_whitespace_characters=True):
				print(vocable_word_attribute_text, 'is in word list')
				attribute_text = vocable.find(self.attribute_name).text
				# does this vocable have the added attribute value?
				regex = '\s*' + self.attribute_value + '\s*$'
				print('regex:', regex)
				print('attribute_text:', attribute_text)
				assert re.search(regex, attribute_text) is not None, 'attribute value has not been added to vocable ' + vocable.find(self.words_attribute_name).text
	def test_write_vocable_file_failing (self):
		print('current working directory:', os.getcwd())
		
		vocable_file_path = self.create_test_vocables_file()
		print('--- using vocable file path:', vocable_file_path)
		
		list_of_words = ['你','好','吗','我','很']
		words_list_file_path = self.create_test_words_file(list_of_words)
		
		xsd_file_path = self.project_directory + 'res/xld-vocables-schema.xsd'
		
		attribute_name = 'chapters'
		attribute_value = 'HSK1-2012'
		words_attribute_name = 'secondLanguageTranslations'
		
		
		xml_parser = XMLParser()
		xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path)
		
		xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
		
		xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name)
		
		# adding invalid tags with texts
		invalid_vocable_element = etree.SubElement(xml_root, 'vocable')
		abc_element = etree.SubElement(invalid_vocable_element, 'abc')
		abc_element.text = 'abc'
		unknowntag_element = etree.SubElement(invalid_vocable_element, 'unknowntag')
		unknowntag_element.text = 'unknowntag'
		
		# the tree should be invalid at this point
		assert xml_parser.validate_tree(xsd_file_path, xml_root) == False, 'Your validation does not work properly.'
		
		# if the xml is not valid anymore when reading the file, the write function might have done something wrong
		try:
			VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root)
		except XMLInvalidException as exception:
			assert True
		else:
			assert False
		
		
		assert os.path.isfile(vocable_file_path), 'no file exists'
		
		xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)
 def write(self, xsd_file_path, vocable_file_path, xml_root):
     #print('writing xml to vocable file ...')
     xml_parser = XMLParser()
     if xml_parser.validate_tree(xsd_file_path, xml_root):
         try:
             with open(vocable_file_path, 'w') as file:
                 file.write(
                     "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\n")
                 file.write(
                     etree.tostring(xml_root,
                                    xml_declaration=False,
                                    pretty_print=True,
                                    encoding="unicode"))
         except IOError:
             print("Error while writing in log file!")
     else:
         print("Tree is invalid.")
         raise XMLInvalidException('Invalid XML!')
    def test_write_vocable_file_failing(self):
        print("current working directory:", os.getcwd())

        vocable_file_path = self.create_test_vocables_file()
        print("--- using vocable file path:", vocable_file_path)

        list_of_words = ["你", "好", "吗", "我", "很"]
        words_list_file_path = self.create_test_words_file(list_of_words)

        xsd_file_path = self.project_directory + "res/xld-vocables-schema.xsd"

        attribute_name = "chapters"
        attribute_value = "HSK1-2012"
        words_attribute_name = "secondLanguageTranslations"

        xml_parser = XMLParser()
        xld_attribute_adder = XLDAttributeAdder(vocable_file_path, xsd_file_path, words_list_file_path)

        xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)

        xld_attribute_adder.add_values_to_attribute_of_vocables(attribute_name, attribute_value, words_attribute_name)

        # adding invalid tags with texts
        invalid_vocable_element = etree.SubElement(xml_root, "vocable")
        abc_element = etree.SubElement(invalid_vocable_element, "abc")
        abc_element.text = "abc"
        unknowntag_element = etree.SubElement(invalid_vocable_element, "unknowntag")
        unknowntag_element.text = "unknowntag"

        # the tree should be invalid at this point
        assert xml_parser.validate_tree(xsd_file_path, xml_root) == False, "Your validation does not work properly."

        # if the xml is not valid anymore when reading the file, the write function might have done something wrong
        try:
            VocableFileWriter.write(VocableFileWriter, xsd_file_path, vocable_file_path, xml_root)
        except XMLInvalidException as exception:
            assert True
        else:
            assert False

        assert os.path.isfile(vocable_file_path), "no file exists"

        xml_root = xml_parser.get_xml_element_tree_root(xsd_file_path, vocable_file_path)