Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
reMarkAble/test/test_parser.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
355 lines (254 sloc)
9.88 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
import remarkable.parser as parser | |
import remarkable.section as section | |
class parserTest(unittest.TestCase): | |
""" | |
Test cases for our parser object | |
""" | |
def setUp(self): | |
""" | |
Run at the start of each set of test cases. | |
Create a new parser each time | |
""" | |
self.theParser = parser.MarkdownParser("test/inputs/input.md") | |
def testParse(self): | |
""" | |
Does the parser run without errors. | |
TODO: We may want to consider adding this to Init | |
""" | |
hasParsed = self.theParser.parseFile() | |
self.assertTrue(hasParsed) | |
def testSections(self): | |
""" | |
Do we get the correct list of sections. | |
These are stored internally as a dictionary | |
{[Name] : Section Object} Pairs | |
""" | |
self.theParser.parseFile() | |
sections = self.theParser.sections | |
#Do the sections exist as we expect | |
for item in ["Introduction", | |
"Lit_Review", | |
"Experimentation", | |
"Summary"]: | |
self.assertTrue(sections[item]) | |
def testSection_Introduction(self): | |
""" | |
Is the content of the Sections correct | |
Lets see if the Introduction matches the expedted text. | |
""" | |
self.theParser.parseFile() | |
sections = self.theParser.sections | |
#Remember New Lines | |
expectedIntro = ["", "Some Text can Go here", ""] | |
theSection = sections["Introduction"] | |
self.assertIsInstance(theSection, section.Section) | |
self.assertEqual(expectedIntro, theSection.text) | |
def testSection_Experementation(self): | |
"""Is the content of the Sections correct | |
Lets see if the Experementation is correct. This is a bit | |
more interesting as its multiple lines | |
""" | |
self.theParser.parseFile() | |
sections = self.theParser.sections | |
expectedExp = ["", | |
"Bleh", | |
"It spans a couple of Lines", | |
""] | |
theSection = sections["Experimentation"] | |
self.assertIsInstance(theSection, section.Section) | |
self.assertEqual(expectedExp, theSection.text) | |
class parserTestMarks(unittest.TestCase): | |
""" Test Cases with Marks Included. | |
We can also add marks to the section. So lets make sure they come out OK. | |
NOTE: Basic functinality should be covered in parserTest | |
""" | |
def setUp(self): | |
""" | |
Run at the start of each set of test cases. | |
Create a new parser each time | |
""" | |
self.theParser = parser.MarkdownParser("test/inputs/inputMarks.md") | |
self.theParser.parseFile() | |
def testMarks_Total(self): | |
""" | |
Do marks in the form [N] work? | |
""" | |
theSection = self.theParser.sections["Introduction"] | |
self.assertEqual(theSection.marks, 10) | |
def testMarks_TotalSpaced(self): | |
"""Marks of the form [ N ]""" | |
theSection = self.theParser.sections["Experimentation"] | |
self.assertEqual(theSection.marks, 30) | |
def testMarks_Of(self): | |
"""Marks in the forms [X/Y]""" | |
theSection = self.theParser.sections["Lit_Review"] | |
self.assertEqual(theSection.marks, 20) | |
def testMarks_OfSpaced(self): | |
"""Marks in the forms [X/Y]""" | |
theSection = self.theParser.sections["Summary"] | |
self.assertEqual(theSection.marks, 40) | |
class parserTestHeader(unittest.TestCase): | |
""" | |
Test the parser with header information. | |
Uses the same standard functionality as above. | |
""" | |
def setUp(self): | |
""" | |
Run at the start of each set of test cases. | |
Create a new parser each time | |
""" | |
self.theParser = parser.MarkdownParser("test/inputs/inputHeader.md") | |
#self.theParser.parseFile() | |
def testSplitFile(self): | |
""" | |
Split file into header and body | |
""" | |
header, body = self.theParser._splitHeader() | |
#Expected Header | |
headerStr = """title: Marking for Whoever | |
author: Dan Goldsmith | |
sid: 14242""" | |
self.assertEqual(header, headerStr) | |
bodyStr = """# Introduction [10] | |
This time the input is Markdown | |
# Lit Review [20/50] | |
Its a bugger to test everything, so let worry about basic formatting | |
For example new lines. | |
With a new paragraph after it. | |
# Experimentation [ 30 ] | |
Other forms of formatting can include bullets: | |
- Like This | |
- And This | |
- And This | |
# Summary [ 40 / 50 ] | |
And More Spacing to be dealt with""" | |
self.assertEqual("\n".join(body), bodyStr) | |
def testSplitNoHeader(self): | |
""" | |
Does the split function work without a header | |
""" | |
theParser = parser.MarkdownParser("test/inputs/inputMarkdown.md") | |
#theParser.parseFile() | |
header, body = theParser._splitHeader() | |
#Header is None | |
self.assertFalse(header) | |
#Body is there | |
bodyStr = """# Introduction [10] | |
This time the input is Markdown | |
# Lit Review [20/50] | |
Its a bugger to test everything, so let worry about basic formatting | |
For example new lines. | |
With a new paragraph after it. | |
# Experimentation [ 30 ] | |
Other forms of formatting can include bullets: | |
- Like This | |
- And This | |
- And This | |
# Summary [ 40 / 50 ] | |
And More Spacing to be dealt with""" | |
self.assertEqual("\n".join(body), bodyStr) | |
def testHeaderTemplate(self): | |
""" | |
Test if the header has a Template attached | |
""" | |
theParser = parser.MarkdownParser("test/inputs/inputController.md") | |
hasParsed = theParser.parseFile() | |
#theParser.parseFile() | |
#header, body = theParser._splitHeader() | |
self.assertEqual(theParser.header["template"], "controller.jinja2") | |
def testHeaderMarks(self): | |
""" | |
If marks are given in the header, do we summarise them correctly | |
""" | |
theParser = parser.MarkdownParser("test/inputs/inputHeaderMarks.md") | |
theParser.parseFile() | |
#First do we have the marks we expected | |
self.assertEqual(theParser.header["marks"]["secOne"], 5) | |
self.assertEqual(theParser.header["marks"]["secTwo"], 10) | |
self.assertEqual(theParser.header["marks"]["total"], 15) | |
def testHeaderTotalMarks(self): | |
""" | |
If marks are given in the header, do we summarise them correctly | |
""" | |
theParser = parser.MarkdownParser("test/inputs/inputHeaderMarksTotal.md") | |
hasParsed = theParser.parseFile() | |
#First do we have the marks we expected | |
self.assertEqual(theParser.header["marks"]["secOne"], 50) | |
self.assertEqual(theParser.header["marks"]["secTwo"], 10) | |
self.assertEqual(theParser.header["marks"]["total"], 60) | |
def testHeaderTotalMismatch(self): | |
""" | |
If marks are given in the header, do we summarise them correctly | |
""" | |
theParser = parser.MarkdownParser("test/inputs/inputHeaderMarksTotalMismatch.md") | |
with self.assertLogs(level="WARNING") as cm: | |
hasParsed = theParser.parseFile() | |
#First do we have the marks we expected | |
self.assertEqual(theParser.header["marks"]["secOne"], 20) | |
self.assertEqual(theParser.header["marks"]["secTwo"], 30) | |
self.assertEqual(theParser.header["marks"]["total"], 50) | |
self.assertEqual(cm.records[0].msg, "Marks Mismatch. Provided 60 Calculated 50") | |
def testHeaderVals(self): | |
""" | |
Are the values in our header correct | |
title: Marking for Whoever | |
author: Dan Goldsmith | |
sid: 14242 | |
""" | |
headerStr = """ | |
title: Marking for Whoever | |
author: Dan Goldsmith | |
sid: 14242 | |
""" | |
self.theParser._parseHeader(headerStr) | |
self.assertEqual(self.theParser.header["title"], "Marking for Whoever") | |
self.assertEqual(self.theParser.header["author"], "Dan Goldsmith") | |
self.assertEqual(self.theParser.header["sid"], 14242) | |
def testParseAll(self): | |
""" | |
Do we break the Parse Everything function | |
""" | |
hasParsed = self.theParser.parseFile() | |
self.assertTrue(hasParsed) | |
self.assertEqual(self.theParser.header["title"], "Marking for Whoever") | |
self.assertEqual(self.theParser.header["author"], "Dan Goldsmith") | |
self.assertEqual(self.theParser.header["sid"], 14242) | |
def testSection_Introduction(self): | |
""" | |
Is the content of the Introduction Correct | |
Its important to do this. As the stuff before the header could | |
Mangle the output of the sections. As introduction is first. | |
""" | |
self.theParser.parseFile() | |
sections = self.theParser.sections | |
#Remember New Lines | |
expectedIntro = ["", "This time the input is Markdown", ""] | |
theSection = sections["Introduction"] | |
self.assertIsInstance(theSection, section.Section) | |
self.assertEqual(expectedIntro, theSection.text) | |
# --- Quick test of other functionlality. In case I have broken anything | |
def testMarks_Total(self): | |
""" | |
Do marks in the form [N] work? | |
""" | |
self.theParser.parseFile() | |
theSection = self.theParser.sections["Introduction"] | |
self.assertEqual(theSection.marks, 10) | |
def testSection_Experementation(self): | |
"""Is the content of the Sections correct | |
Lets see if the Experementation is correct. This is a bit | |
more interesting as its multiple lines | |
""" | |
self.theParser.parseFile() | |
sections = self.theParser.sections | |
expectedExp = ["", | |
"Other forms of formatting can include bullets:", | |
"", | |
" - Like This", | |
" - And This", | |
" - And This", | |
""] | |
theSection = sections["Experimentation"] | |
self.assertIsInstance(theSection, section.Section) | |
self.assertEqual(expectedExp, theSection.text) | |