Skip to content

Instantly share code, notes, and snippets.

@JamesJinPark
Last active August 29, 2015 14:06
Show Gist options
  • Save JamesJinPark/0a32237e41c689ae628d to your computer and use it in GitHub Desktop.
Save JamesJinPark/0a32237e41c689ae628d to your computer and use it in GitHub Desktop.
tidyHTML.test
#Test file for Assignment 4 - tidyHTML
#by James Park and Yue Chen
import unittest
from tidyHTML import *
import os
import re
class TestTidyHTML(unittest.TestCase):
global bad_content, bad_content1, bad_content2, bad_content3, bad_content4, too_long_line
bad_content = "<h>RANDOM NAME</h>\n, <ol>\n, <li>TEST ME</ol>\n"
bad_content1 = "testing line <S>Hello</S> more testing"
bad_content2 = "testing line <B><S>Hello</S> more testing"
bad_content3 = "<head>TEST</head>"
bad_content4 = "<M>TEST</M>"
too_long_line ="<li>Read and process the input file, writing to the output file. You will probably find it simplest to read and process one line at a time.</li>"
def test_file_creation(self):
"""This function tests the file creation function."""
path = os.path.dirname(os.path.realpath(__file__))
random_name = generate_random_file_name(path)
self.assertFalse(file_exists(random_name))
create_test_file(random_name)
self.assertTrue(file_exists('unittest.html'))
delete_file('unittest.html')
def test_generate_random_file(self):
"""This function tests whether the program can build an output file with a random name."""
path = os.path.dirname(os.path.realpath(__file__))
random_name = generate_random_file_name(path)
self.assertFalse(file_exists(random_name))
content = "TEST"
create_output_file(random_name, content)
self.assertTrue(file_exists(random_name))
delete_file(random_name)
def test_read_entire_input_file(self):
"""This function tests whether the program can read the input file in its entirety."""
path = os.path.dirname(os.path.realpath(__file__))
random_name = generate_random_file_name(path)
content = "TEST"
create_output_file(random_name, content)
read_entire_input_file(random_name)
read_test_file = open(random_name, 'r')
new_content = read_test_file.read()
read_test_file.close()
self.assertTrue(file_exists(random_name))
self.assertTrue(compare_variable_with_string, (new_content, "TEST"))
delete_file(random_name)
def test_read_input_file_line_by_line(self):
"""This function tests whether the program can read the input file, line by line."""
path = os.path.dirname(os.path.realpath(__file__))
random_name = generate_random_file_name(path)
content = "TEST"
create_output_file(random_name, content)
read_input_file_line_by_line(random_name)
read_test_file = open(random_name, 'r')
new_content = read_test_file.read()
read_test_file.close()
self.assertTrue(file_exists(random_name))
self.assertTrue(compare_variable_with_string, (new_content, "TEST"))
delete_file(random_name)
def test_merge_list(self):
"""This function tests whether the program can take multiple lists of strings and merge it to create one list."""
a = [123]
b = [456]
c = [789]
list_of_elements = [a, b, c]
self.assertEqual = (merge_list(list_of_elements), ([1, 2, 3, 4, 5, 6, 7, 8, 9]))
self.assertNotEqual = (merge_list(list_of_elements), ([a, b, c]))
def test_find_next_tag(self):
"""This function tests whether the program can find tags and return the tag's location."""
self.assertEqual(find_next_tag(5, bad_content), ('/h', 14, 18))
self.assertEqual(find_next_tag(22, bad_content), ('li', 28, 32))
def test_tag_match(self):
"""This function tests whether the program can match start tags with end tags."""
self.assertFalse(tag_match('a', '/b'))
self.assertTrue(tag_match('a', '/a'))
def test_convert_to_end_tag(self):
"""This functions the programs ability to create end tags."""
self.assertEqual(convert_to_end_tag('tag'), '</tag>')
def test_insert_missing_tag(self):
"""This function tests the programs ability to insert missing tags."""
self.assertEqual(insert_missing_tag('li', 41, 48, bad_content), (46, 53, "<h>RANDOM NAME</h>\n, <ol>\n, <li>TEST ME</</li>ol>\n"))
def test_fix_missing_tags(self):
"""This function tests whether the program can find and fix start tags and also lower cases all tags."""
self.assertEqual(fix_missing_tags(bad_content), "<h>RANDOM NAME</h>\n, <ol>\n, <li>TEST ME</li></ol>\n")
self.assertNotEqual(fix_missing_tags(bad_content), "THIS IS WRONG")
self.assertEqual(fix_missing_tags(bad_content4), "<m>TEST</m>")
def test_check_if_line_has_both_start_and_end_tags_and_no_nested_start_tag(self):
"""This functions test whether the program can check whether a line has both a start tag and an end tag."""
self.assertTrue(check_if_line_has_both_start_and_end_tags_and_no_nested_start_tag(bad_content1))
self.assertFalse(check_if_line_has_both_start_and_end_tags_and_no_nested_start_tag(bad_content2))
self.assertTrue(check_if_line_has_both_start_and_end_tags_and_no_nested_start_tag(too_long_line))
def test_insert_space_in_long_line(self):
"""This functions tests whether the program can insert space at the 81st position in long lines."""
self.assertEqual(insert_space_in_long_line(too_long_line), "<li>Read and process the input file, writing to the output file. You will\nprobably find it simplest to read and process one line at a time.</li>")
def test_fix_length(self):
"""This function fixes the length of the lines of HTML if the line is over 80 characters long."""
self.assertEqual(fix_length(too_long_line), ["<li>Read and process the input file, writing to the output file. You will\nprobably find it simplest to read and process one line at a time.</li>"])
def test_insert_lines_for_special_tags(self):
"""This function tests whether the program can insert a line above special tags such as <head>."""
self.assertEqual(insert_lines_for_special_tags(bad_content3),"\n<head>TEST</head\n\n>")
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment