def test_DND2(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example5a.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.mkdir('%s/dnd' % tmdir) os.system('cp %s/quadratic.tex %s/dnd/' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() cfn = path(tmdir) / 'problem/p1.xml' assert (os.path.exists(cfn)) os.system('ls -sFC %s' % tmdir) os.system('ls -sFC %s/problem' % tmdir) self.assertTrue(os.path.exists(cfn)) data = open(cfn).read() assert ( '<drag_and_drop_input img="/static/images/quadratic/quadratic_dnd.png" target_outline="false" one_per_target="true"' in data) assert ( '<draggable id="term1" icon="/static/images/quadratic/quadratic_dnd_label1.png" can_reuse="true"/>' in data)
def test_sh1(self): ''' Test the output of `latex2edx example14_showhide.tex` for proper formation of the showhide element. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example14_showhide.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() jsfn = '%s/static/latex2edx.js' % tmdir cssfn = '%s/static/latex2edx.css' % tmdir self.assertTrue(os.path.exists(jsfn)) self.assertTrue(os.path.exists(cssfn)) tfn = '%s/html/Text.xml' % tmdir xml = etree.parse(tfn).getroot() sh = xml.find('.//div[@class="hideshowbox"]') self.assertEqual(sh[0].tag, 'h4') self.assertEqual(sh[0].get('onclick'), 'hideshow(this);') self.assertEqual(sh[0].text, 'Secret text') self.assertEqual(sh[1].get('class'), 'hideshowcontent') self.assertEqual(sh[1].text.strip(), 'Hidden text') self.assertEqual(sh[2].get('class'), 'hideshowbottom') lk = xml.find('.//LINK') self.assertTrue('latex2edx.css' in lk.get('href')) sc = xml.find('.//SCRIPT') self.assertTrue('latex2edx.js' in sc.get('src'))
def test_custom_html1(self): tex = ('\\begin{edXcourse}{1.00x}{1.00x Fall 2013}[url_name=2013_Fall]\n' '\\begin{edXchapter}{Unit 1}[start="2013-11-22"]\n' '\\begin{edXsection}{Introduction}\n' '\\begin{edXtext}{My Name}[url_name=text_url_name]\n' 'Hello world!\n\n' '\\begin{html}{span}[style="display:none;color:red;border-style:solid" data-x=3]\n' 'this is red text with a border\n' '\end{html}\n\n' '\\end{edXtext}\n' '\\end{edXsection}\n' '\\end{edXchapter}\n' '\\end{edXcourse}\n' ) with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(tex).fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) l2e.xhtml2xbundle() print "xbundle = " print str(l2e.xb) print # self.assertIn(r'<html display_name="My Name" url_name="text_url_name">', str(l2e.xb)) xml = etree.fromstring(str(l2e.xb)) html = xml.find('.//html') self.assertTrue(html.get('display_name') == 'My Name') self.assertIn('<span style="display:none;color:red;border-style:solid" data-x="3">this is red text with a border </span>', str(l2e.xb))
def test_hints1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example3.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) p1fn = path(tmdir) / 'problem/p1.xml' self.assertTrue(os.path.exists(p1fn)) dat = open(p1fn).read() self.assertIn('# General hint system for edX', dat) p2fn = path(tmdir) / 'problem/p2.xml' self.assertTrue(os.path.exists(p2fn)) dat = open(p2fn).read() self.assertIn('# General hint system for edX', dat) cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(os.path.exists(cfn))
def test_text1(self): tex = ('\\begin{edXcourse}{1.00x}{1.00x Fall 2013}[url_name=2013_Fall]\n' '\n' '\\begin{edXchapter}{Unit 1}[start="2013-11-22"]\n' '\n' '\\begin{edXsection}{Introduction}\n' '\n' '\\begin{edXtext}{My Name}[url_name=text_url_name]\n' 'Hello world!\n\n' '\\end{edXtext}\n' '\\end{edXsection}\n' '\\end{edXchapter}\n' '\\end{edXcourse}\n' ) with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(tex).fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) l2e.xhtml2xbundle() # print "xbundle = " # print str(l2e.xb) # print # self.assertIn(r'<html display_name="My Name" url_name="text_url_name">', str(l2e.xb)) xml = etree.fromstring(str(l2e.xb)) html = xml.find('.//html') self.assertTrue(html.get('display_name') == 'My Name') self.assertTrue(html.get('url_name') == 'text_url_name')
def test_timestamp2(self): tex = ('\\begin{edXcourse}{1.00x}{1.00x Fall 2013}[url_name=2013_Fall]\n' '\\begin{edXchapter}{Unit 1}[start="2013-11-22"]\n' '\\begin{edXsection}{Introduction}\n' '\\begin{edXtext}{My Name}[url_name=text_url_name]\n' 'Hello world!\n\n' '{\\bf this is bold}\n\n' '\\end{edXtext}\n' '\\end{edXsection}\n' '\\end{edXchapter}\n' '\\end{edXcourse}\n' ) with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(tex).fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir, add_timestamp=False, timestamp_revision="91234", timestamp_threshold=0, ) l2e.xhtml2xbundle() print "xbundle = " print str(l2e.xb) # print # self.assertIn(r'<html display_name="My Name" url_name="text_url_name">', str(l2e.xb)) xml = etree.fromstring(str(l2e.xb)) html = xml.find('.//html') self.assertTrue(html.get('display_name') == 'My Name') self.assertTrue(html.get('url_name') == 'text_url_name') self.assertFalse("This page was last updated on" in str(l2e.xb))
def test_example1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example1.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() # self.assertIn('<chapter display_name="Unit 1" start="2013-11-22" url_name="Unit_1">', xb) xml = etree.parse(xbfn).getroot() chapter = xml.find('.//chapter') self.assertTrue(chapter.get('display_name') == 'Unit 1') self.assertTrue(chapter.get('start') == '2013-11-22') self.assertTrue(chapter.get('url_name') == 'Unit_1') cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'chapter/Unit_1.xml' self.assertTrue(os.path.exists(cfn)) # self.assertIn('<sequential display_name="Introduction" due="2013-11-22" url_name="Introduction"', open(cfn).read()) xml = etree.parse(cfn).getroot() seq = xml.find('.//sequential') self.assertTrue(seq.get('display_name') == 'Introduction') self.assertTrue(seq.get('due') == '2013-11-22') self.assertTrue(seq.get('url_name') == 'Introduction') self.assertIn('<problem url_name="p1"/>', open(cfn).read())
def test_toc1(self): ''' Test the output of `latex2edx example11_toc_test.tex` for proper label and reference tags, as well as figure numbering. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example11_toc_test.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() xhfn = nfn[:-4] + '.xhtml' xml = etree.fromstring(open(xhfn).read()) toclabels = xml.findall('.//toclabel') self.assertEqual(toclabels[0].text, 'chap:intro') self.assertEqual(toclabels[1].text, 'mo:explore') tocref = xml.find('.//tocref') self.assertEqual(tocref.text, 'mo:explore') labels = xml.findall('.//label') self.assertEqual(labels[0].text, 'fig:single') self.assertEqual(labels[1].text, 'fig:multi') captions = xml.findall('.//div[@class="caption"]/b') self.assertEqual(captions[0].text, 'Figure 1') self.assertEqual(captions[1].text, 'Figure 2') chaps = xml.findall('.//chapter') self.assertIsNone(chaps[0].get('refnum')) self.assertEqual(chaps[1].get('refnum'), '1') equation = xml.find('.//td[@class="equation"]') self.assertEqual( re.findall(r'\\label\{(.*?)\}', equation.text, re.S)[0], 'eq:pythagorean')
def test_output_fmts4(self): ''' units only (problems) ''' testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example1.tex' print("file %s" % fn) with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir, units_only=True) l2e.convert() err = "" except Exception as err: print(err) xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(not os.path.exists(cfn)) cfn = path(tmdir) / 'sequential' self.assertTrue(not os.path.exists(cfn)) cfn = path(tmdir) / 'problem/Problem_2.xml' self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'vertical' self.assertTrue(not os.path.exists(cfn))
def test_toc2(self): ''' Test the output of `latex2edx --popups example11_toc_test.tex` for proper equation numbering and button links to the static tocindex.html file. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example11_toc_test.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir, popup_flag=True) l2eout.convert() cfn = path(tmdir) / 'html/text-L1.xml' data = open(cfn).read() xml = etree.fromstring(data) href = xml.findall('.//a[@href]') # Check the reference link text self.assertEqual(href[0].text, '0') # Non-numbered chapter self.assertEqual(href[1].text, '(1.2)') # Numbered equation 2 self.assertEqual(href[2].text, '(1.3)') # Numbered equation 3 # Check for popup format self.assertEqual(href[1].get('href'), 'javascript: void(0)') self.assertEqual(href[2].get('href'), 'javascript: void(0)') # Check for taglist in problem cfn = path(tmdir) / 'problem/p0.xml' data = open(cfn).read() xml = etree.fromstring(data) taglist = xml.find('.//p[@id="taglist"]') self.assertEqual(taglist.get('tags'), 'mo:explore,mo:problem')
def test_edXinclude1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example7.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() cfn = path(tmdir) / 'chapter/Unit_2.xml' assert(os.path.exists(cfn)) self.assertTrue(os.path.exists(cfn)) data = open(cfn).read() expect = r"""<vertical url_name="A_second_section_vertical1"> <staffgrading url_name="project_paper"/> </vertical>""" assert(expect in data) cfn = path(tmdir) / 'staffgrading/project_paper.xml' assert(os.path.exists(cfn))
def test_output_fmts3(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example9_section_only.tex' print("file %s" % fn) with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir, section_only=True, suppress_verticals=True) l2e.convert() err = "" except Exception as err: print(err) xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(not os.path.exists(cfn)) cfn = path(tmdir) / 'sequential/A_second_section.xml' os.system('/bin/ls -R %s' % (path(tmdir))) self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'problem/p1.xml' self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'vertical/A_second_section_vertical.xml' self.assertTrue(not os.path.exists(cfn))
def test_toc3(self): ''' Test the output of `latex2edx example17_toc_vert.tex` for proper generation of a tocindex.html file based on the contents of a vertical. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example17_toc_vert.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() tocfn = '%s/tabs/tocindex.html' % tmdir toc = etree.fromstring(open(tocfn).read().replace('<br>', '<br/>')) self.assertEqual(toc.findtext('body/h1'), 'Table of Contents') # Check the measurable outcome table headers tocmos = toc.findall('.//table/tbody/tr/th/a') self.assertEqual(tocmos[0][0].text, 'MO1.1') self.assertEqual(tocmos[1][0].text, 'MO1.2') self.assertIn('Follow a Lesson', tocmos[0].getnext().text) self.assertIn('Answer a problem set', tocmos[1].getnext().text) # check measurable outcomes subheadings tocsubs = toc.find('.//table/tbody/tr/td') self.assertEqual(tocsubs[0].text, 'Learn') self.assertEqual(tocsubs[2].text, 'Assess') # check reference to measurable outcomes tocref1 = toc.find('.//table/tbody/tr[@id="indmo1p1"]/td/ul/li/a') self.assertIn('Example text', tocref1.text) tocref2 = toc.find('.//table/tbody/tr[@id="indmo1p2"]/td/ul/li/a') self.assertIn('Problem Set 1', tocref2.text)
def test_latex2edx_cutset5(self): with make_temp_directory() as tmdir: os.chdir(tmdir) ofn = "testcuts.yaml" tex = r'''\begin{edXproblem}{A problem}{url_name="a_problem"} \edXabox{type="option" options="red","green","blue" expect="red"} \edXinline{What is $n$ for $C^\perp$?~} \edXabox{type=symbolic size=10 expect="3" inline="1"} \edXinline{$d(C) = $} \edXabox{type=numerical size=10 expect="5" inline="1"} \edXinline{$\Delta t = $} \edXabox{type="custom" expect="pi" cfn=sympy_formula_check inline="1"} \edXinline{$\Delta t = $} \edXabox{type="formula" expect="pi" inline="1" samples='pi@1:10#10'} \end{edXproblem}''' l2e = latex2edx(tmdir + '/test.tex', latex_string=tex, add_wrap=True, do_images=False, output_dir=tmdir, output_cutset=ofn) xmlstr = l2e.xml cutset = CourseUnitTestSet(fn=ofn) self.assertEqual(len(cutset.tests), 1) self.assertEqual(cutset.tests[0].url_name, "a_problem") self.assertEqual(len(cutset.tests[0].responses), 5)
def test_askta1(self): tex = ( '\\begin{edXcourse}{1.00x}{1.00x Fall 2013}[url_name=2013_Fall]\n' '\n' '\\begin{edXchapter}{Unit 1}[start="2013-11-22"]\n' '\n' '\\begin{edXsection}{Introduction}\n' '\n' '\\begin{edXproblem}{test problem}\n' '\n' '\\edXaskta{settings=1 to="*****@*****.**"}\n\n' '\\edXaskta{cc="*****@*****.**"}\n\n' '\\end{edXproblem}\n' '\\end{edXsection}\n' '\\end{edXchapter}\n' '\\end{edXcourse}\n') # make sure edXaskta buttons work properly with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(tex).fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) l2e.xhtml2xbundle() # print "xbundle = " # print str(l2e.xb) # print self.assertIn( r'<a style="display:none" href="/course/jump_to_id" id="aturl_1"/>', str(l2e.xb))
def test_edxxml1(self): """ Test the output of `latex2edx example13_edxxml.tex` for proper rendering of the complete XML with multiple calls in `edXxml` in a given line. """ testdir = path(l2e.__file__).parent / "testtex" tfn = testdir / "example13_edxxml.tex" print "file %s" % tfn with make_temp_directory() as tmdir: nfn = "%s/%s" % (tmdir, tfn.basename()) os.system("cp %s/* %s" % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() cfn = path(tmdir) / "html/Code_Text.xml" data = open(cfn).read().split("\n") expected = ( "A matrix <code>A</code> and column vector named " "<code>b</code> can be multiplied in the form " "<code>A b</code> only if the number of columns " "of <code>b</code> match the number of rows in " "<code>A</code>. </p>" ) self.assertEqual(data[2], expected)
def test_latex2edx_cutset2(self): with make_temp_directory() as tmdir: os.chdir(tmdir) ofn = "testcuts.yaml" tex = r''' \begin{edXproblem}{A problem}{url_name="a_problem"} \edXabox{type="option" options="red","green","blue" expect="red"} \end{edXproblem} \begin{edXproblem}{A problem 2}{url_name="a_problem2"} \edXabox{type="custom" expect="red" cfn="mytest"} \end{edXproblem} ''' l2e = latex2edx(tmdir + '/test.tex', latex_string=tex, add_wrap=True, do_images=False, output_dir=tmdir, output_cutset=ofn) xhtml = l2e.xhtml assert ('<optionresponse' in xhtml) xmlstr = l2e.xml cutset = CourseUnitTestSet(fn=ofn) self.assertEqual(len(cutset.tests), 2) self.assertEqual(cutset.tests[1].url_name, "a_problem2") self.assertEqual(cutset.tests[1].responses, ["red"]) self.assertEqual(cutset.tests[1].expected, ["correct"])
def test_policy1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example3.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir, update_policy=True, suppress_policy=True) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) pfn = path(tmdir) / 'policies/2013_Fall/policy.json' self.assertTrue(os.path.exists(pfn)) dat = open(pfn).read() policy = json.loads(dat) self.assertTrue('course/2013_Fall' in policy) self.assertTrue(policy['course/2013_Fall']['start'] == "2014-05-11T12:00") self.assertTrue(policy['course/2013_Fall']['end'] == "2012-08-12T00:00") self.assertTrue(policy['course/2013_Fall']['showanswer'] == "always") self.assertTrue('chapter/Unit_2' in policy) self.assertTrue(policy['sequential/A_second_section']['graded'] == "true") self.assertTrue(policy['sequential/A_second_section']['due'] == "2016-11-22T00:00")
def test_latex2edx_cutset_wrap1(self): with make_temp_directory() as tmdir: os.chdir(tmdir) ofn = "testcuts.yaml" tex = r''' \begin{edXproblem}{A problem with wrapped abox}{url_name="a_problem"} \edXinline{$C(ZX) =$} \edXabox{type="custom" size=60 expect="[H(1),CNOT(2,1)]" options="nqubits=3" cfn=check_clifford_circuit_eq inline="1" wrapclass=subtext2.Subtext(debug=True,sanitize_allow_lists=False) import=subtext2 } \end{edXproblem} ''' l2e = latex2edx(tmdir + '/test.tex', latex_string=tex, add_wrap=True, do_images=False, output_dir=tmdir, output_cutset=ofn) xmlstr = l2e.xml cutset = CourseUnitTestSet(fn=ofn) self.assertEqual(len(cutset.tests), 1) self.assertEqual(cutset.tests[0].url_name, "a_problem") self.assertEqual(cutset.tests[0].responses, ["[H(1),CNOT(2,1)]"]) self.assertEqual(cutset.tests[0].expected, ["correct"])
def test_output_fmts4(self): ''' units only (problems) ''' testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example1.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir, units_only=True) l2e.convert() err = "" except Exception as err: print err xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(not os.path.exists(cfn)) cfn = path(tmdir) / 'sequential' self.assertTrue(not os.path.exists(cfn)) cfn = path(tmdir) / 'problem/Problem_2.xml' self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'vertical' self.assertTrue(not os.path.exists(cfn))
def test_key1(self): ''' Test the output of `latex2edx example12_index.tex` for proper formation of the key_map.json file. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example12_index.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() jsfn = '%s/static/key_map.json' % tmdir keymap = json.load(open(jsfn)) self.assertEqual(set(keymap.keys()), set(['topic', 'numerical'])) self.assertEqual(keymap['topic'][0], [ 'Module_1/A_lecture_section/1', 'Module_1/Another_lecture_section/1' ]) self.assertEqual( keymap['numerical'], [['Module_0/A_problem_section/2'], ['Example problem']])
def test_edXmath1(self): testdir = path(l2emod.__file__).parent / "testtex" fn = testdir / "example6.tex" print "file %s" % fn with make_temp_directory() as tmdir: nfn = "%s/%s" % (tmdir, fn.basename()) os.system("cp %s/* %s" % (testdir, tmdir)) os.mkdir("%s/dnd" % tmdir) os.system("cp %s/quadratic.tex %s/dnd/" % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + ".xbundle" self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() cfn = path(tmdir) / "problem/p1.xml" assert os.path.exists(cfn) os.system("ls -sFC %s" % tmdir) os.system("ls -sFC %s/problem" % tmdir) self.assertTrue(os.path.exists(cfn)) data = open(cfn).read() expect = r"""[mathjax]\begin{eqnarray} S(\rho) &=& -\lambda_{1} \log \lambda_{1} -\lambda_{2} \log \lambda_{2} \\ &=& H((1+r)/2) \end{eqnarray}[/mathjax]""" assert expect in data
def test_image1(self): # make sure includegraphics works testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example-html-text.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) xb = open(xbfn).read() self.assertIn( '<img src="/static/images/example-image.png" width="660"/>', xb) self.assertTrue( os.path.exists( path(tmdir) / 'static/images/example-image.png')) cfn = path(tmdir) / 'course/2015_Spring.xml' self.assertTrue(os.path.exists(cfn)) self.assertIn( '<course number="8.01x" display_name="Mechanics" start="2015-04-21" org="MITx" semester="2015_Spring">', open(cfn).read())
def test_merge(self): testdir = path(l2emod.__file__).parent / 'testtex' with make_temp_directory() as tmdir: fn = testdir / 'example1.tex' print "file %s" % fn nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() fn = testdir / 'example2.tex' print "file %s" % fn nfn = '%s/%s' % (tmdir, fn.basename()) l2e = latex2edx(nfn, output_dir=tmdir, do_merge=True) l2e.convert() cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(os.path.exists(cfn)) self.assertIn('<chapter url_name="Unit_1"', open(cfn).read()) self.assertIn('<chapter url_name="Unit_2"', open(cfn).read()) cfn = path(tmdir) / 'chapter/Unit_1.xml' self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'chapter/Unit_2.xml' self.assertTrue(os.path.exists(cfn))
def test_toc1(self): ''' Test the output of `latex2edx example11_toc_test.tex` for proper label and reference tags, as well as figure numbering. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example11_toc_test.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() xhfn = nfn[:-4] + '.xhtml' xml = etree.fromstring(open(xhfn).read()) toclabels = xml.findall('.//toclabel') self.assertEqual(toclabels[0].text, 'chap:intro') self.assertEqual(toclabels[1].text, 'mo:explore') tocref = xml.find('.//tocref') self.assertEqual(tocref.text, 'mo:explore') labels = xml.findall('.//label') self.assertEqual(labels[0].text, 'fig:single') self.assertEqual(labels[1].text, 'fig:multi') captions = xml.findall('.//div[@class="caption"]/b') self.assertEqual(captions[0].text, 'Figure 1') self.assertEqual(captions[1].text, 'Figure 2') chaps = xml.findall('.//chapter') self.assertIsNone(chaps[0].get('refnum')) self.assertEqual(chaps[1].get('refnum'), '1') equation = xml.find('.//td[@class="equation"]') self.assertEqual(re.findall(r'\\label\{(.*?)\}', equation.text, re.S)[0], 'eq:pythagorean')
def test_hints1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example3.tex' print("file %s" % fn) with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) p1fn = path(tmdir) / 'problem/p1.xml' self.assertTrue(os.path.exists(p1fn)) dat = open(p1fn).read() self.assertIn('# General hint system for edX', dat) p2fn = path(tmdir) / 'problem/p2.xml' self.assertTrue(os.path.exists(p2fn)) dat = open(p2fn).read() self.assertIn('# General hint system for edX', dat) cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(os.path.exists(cfn))
def test_policy2(self): ''' Check for good error message, with filename and lineno ''' testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example10_badpolicy.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir, update_policy=True, suppress_policy=True) l2e.convert() except Exception as err: pass print "Error = %s" % str(err) self.assertTrue( re.search( 'Error processing element sequential in file .*example10_badpolicy.tex line 18', str(err)))
def test_edXmath1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example6.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.mkdir('%s/dnd' % tmdir) os.system('cp %s/quadratic.tex %s/dnd/' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() cfn = path(tmdir) / 'problem/p1.xml' assert (os.path.exists(cfn)) os.system('ls -sFC %s' % tmdir) os.system('ls -sFC %s/problem' % tmdir) self.assertTrue(os.path.exists(cfn)) data = open(cfn).read() expect = r"""[mathjax]\begin{eqnarray} S(\rho) &=& -\lambda_{1} \log \lambda_{1} -\lambda_{2} \log \lambda_{2} \\ &=& H((1+r)/2) \end{eqnarray}[/mathjax]""" assert (expect in data)
def test_output_fmts3(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example9_section_only.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir, section_only=True, suppress_verticals=True) l2e.convert() err = "" except Exception as err: print err xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(not os.path.exists(cfn)) cfn = path(tmdir) / 'sequential/A_second_section.xml' os.system('/bin/ls -R %s' % (path(tmdir))) self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'problem/p1.xml' self.assertTrue(os.path.exists(cfn)) cfn = path(tmdir) / 'vertical/A_second_section_vertical.xml' self.assertTrue(not os.path.exists(cfn))
def test_math1(self): # make sure latex math expressions are turned into mathjax with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(r'$\frac{\alpha}{\sqrt{1+\beta}}$').fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) self.assertIn(r'<div>[mathjaxinline]\frac{\alpha}{\sqrt{1+\beta}}[/mathjaxinline]</div>', l2e.xhtml.replace(' ', ''))
def test_toc1(self): ''' Test the output of `latex2edx example11_toc_test.tex` for proper label and reference tags, as well as figure numbering. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example11_toc_test.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() xhfn = nfn[:-4] + '.xhtml' xml = etree.fromstring(open(xhfn).read()) toclabels = xml.findall('.//toclabel') self.assertEqual(toclabels[0].text, 'chap:intro') self.assertEqual(toclabels[1].text, 'mo:explore') tocref = xml.find('.//tocref') self.assertEqual(tocref.text, 'mo:explore') labels = xml.findall('.//label') self.assertEqual(labels[0].text, 'fig:single') self.assertEqual(labels[1].text, 'fig:multi') captions = xml.findall('.//div[@class="caption"]/b') self.assertEqual(captions[0].text, 'Figure 1') self.assertEqual(captions[1].text, 'Figure 2') chaps = xml.findall('.//chapter') self.assertIsNone(chaps[0].get('refnum')) self.assertEqual(chaps[1].get('refnum'), '1') equation = xml.find('.//td[@class="equation"]') self.assertEqual(re.findall(r'\\label\{(.*?)\}', equation.text, re.S)[0], 'eq:pythagorean') tocfn = '%s/tabs/tocindex.html' % tmdir toc = etree.fromstring(open(tocfn).read().replace('<br>', '<br/>')) self.assertEqual(toc.findtext('body/h1'), 'Table of Contents') # Check the measurable outcome table headers tocmos = toc.findall('.//table/tbody/tr/th/a') self.assertEqual(tocmos[0][0].text, '0') self.assertEqual(tocmos[1][0].text, 'MO1') self.assertEqual(tocmos[2][0].text, 'MO2') self.assertIn('Explore the edX platform', tocmos[1].getnext().text) self.assertIn('Answer an edX question', tocmos[2].getnext().text) # check measurable outcomes subheadings tocsubs = toc.find('.//table/tbody/tr/td') self.assertEqual(tocsubs[0].text, 'Learn') self.assertEqual(tocsubs[2].text, 'Assess') # check reference to measurable outcomes tocrefs = toc.findall('.//table/tbody/tr/td/ul/li/a') self.assertIn('Example problem', tocrefs[0].text) self.assertIn('Example text', tocrefs[1].text) self.assertIn('Example problem', tocrefs[2].text) self.assertIn('Example problem', tocrefs[3].text) self.assertEqual(toc[1][8].tag, 'br') self.assertIn('Module 1', toc[1][9][0].text)
def test_math2(self): with make_temp_directory() as tmdir: os.chdir(tmdir) tex = r'\begin{eqnarray}\dot{Q} = \frac{A}{R_{\rm thermal}} \Delta T\end{eqnarray}' expect = '[mathjaxinline]\\displaystyle \\dot{Q} = \\frac{A}{R_{\\rm thermal}} \\Delta T[/mathjaxinline]' l2e = latex2edx(tmdir + '/test.tex', latex_string=tex, add_wrap=True, do_images=False, output_dir=tmdir) # print l2e.p2x.renderer.__dict__ self.assertIn(expect, l2e.xhtml)
def test_toc1(self): ''' Test the output of `latex2edx example11_toc_test.tex` for proper label and reference tags, as well as figure numbering. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example11_toc_test.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() xhfn = nfn[:-4] + '.xhtml' xml = etree.fromstring(open(xhfn).read()) toclabels = xml.findall('.//toclabel') self.assertEqual(toclabels[0].text, 'chap:intro') self.assertEqual(toclabels[1].text, 'mo:explore') tocref = xml.find('.//tocref') self.assertEqual(tocref.text, 'mo:explore') labels = xml.findall('.//label') self.assertEqual(labels[0].text, 'fig:single') self.assertEqual(labels[1].text, 'fig:multi') captions = xml.findall('.//div[@class="caption"]/b') self.assertIn(captions[0].text, ['Figure 1', " 1"]) self.assertIn(captions[1].text, ['Figure 2', " 2"]) chaps = xml.findall('.//chapter') self.assertIsNone(chaps[0].get('refnum')) self.assertEqual(chaps[1].get('refnum'), '1') equation = xml.find('.//td[@class="equation"]') self.assertEqual( re.findall(r'\\label\{(.*?)\}', equation.text, re.S)[0], 'eq:pythagorean') tocfn = '%s/tabs/tocindex.html' % tmdir toc = etree.fromstring(open(tocfn).read().replace('<br>', '<br/>')) self.assertEqual(toc.findtext('body/h1'), 'Table of Contents') # Check the measurable outcome table headers tocmos = toc.findall('.//table/tbody/tr/th/a') self.assertEqual(tocmos[0][0].text, '0') self.assertEqual(tocmos[1][0].text, 'MO1') self.assertEqual(tocmos[2][0].text, 'MO2') self.assertIn('Explore the edX platform', tocmos[1].getnext().text) self.assertIn('Answer an edX question', tocmos[2].getnext().text) # check measurable outcomes subheadings tocsubs = toc.find('.//table/tbody/tr/td') self.assertEqual(tocsubs[0].text, 'Learn') self.assertEqual(tocsubs[2].text, 'Assess') # check reference to measurable outcomes tocrefs = toc.findall('.//table/tbody/tr/td/ul/li/a') self.assertIn('Example problem', tocrefs[0].text) self.assertIn('Example text', tocrefs[1].text) self.assertIn('Example problem', tocrefs[2].text) self.assertIn('Example problem', tocrefs[3].text) self.assertEqual(toc[1][8].tag, 'br') self.assertIn('Module 1', toc[1][9][0].text)
def test_math1(self): # make sure latex math expressions are turned into mathjax with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(r'$\frac{\alpha}{\sqrt{1+\beta}}$').fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) self.assertIn( r'<div>[mathjaxinline]\frac{\alpha}{\sqrt{1+\beta}}[/mathjaxinline]</div>', l2e.xhtml.replace(' ', ''))
def test_bad_abox1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example8_badscript.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() err = "" except Exception as err: print err assert('abox located: linenum="43"' in str(err)) xbfn = nfn[:-4] + '.xbundle' self.assertTrue(not os.path.exists(xbfn))
def test_edXinclude2(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example7_bad.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() except Exception as err: pass print "Error = %s" % str(err) self.assertTrue(re.search('Error processing element edxincludepy in file .*example.*\.tex line 25', str(err)))
def test_bad_abox1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example8_badscript.tex' print("file %s" % fn) with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() err = "" except Exception as err: print(err) assert ('abox located: linenum="43"' in str(err)) xbfn = nfn[:-4] + '.xbundle' self.assertTrue(not os.path.exists(xbfn))
def test_bad_script1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example1_bad_script.tex' print("file %s" % fn) with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() except Exception as err: print("Error = %s" % str(err)) self.assertTrue( re.search( 'Error processing element script in file .*\.tex line 82', str(err)))
def test_image1(self): # make sure includegraphics works testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example-html-text.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) xb = open(xbfn).read() self.assertIn('<img src="/static/images/example-image.png" width="660"/>', xb) self.assertTrue(os.path.exists(path(tmdir) / 'static/images/example-image.png')) cfn = path(tmdir) / 'course/2015_Spring.xml' self.assertTrue(os.path.exists(cfn)) self.assertIn('<course number="8.01x" display_name="Mechanics" start="2015-04-21" org="MITx" semester="2015_Spring">', open(cfn).read())
def test_margin_note1(self): tex = ( '\\begin{edXcourse}{1.00x}{1.00x Fall 2013}[url_name=2013_Fall]\n' '\n' '\\begin{edXchapter}{Unit 1}[start="2013-11-22"]\n' '\n' '\\begin{edXsection}{Introduction}\n' '\n' '\\begin{edXtext}{My Name}[url_name=text_url_name]\n' 'Hello world!\n\n' '\\marginote{this is a note}{this is the anchor text}\n\n' '\\end{edXtext}\n' '\\end{edXsection}\n' '\\end{edXchapter}\n' '\\end{edXcourse}\n') with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(tex).fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) l2e.xhtml2xbundle() # print "xbundle = " # print str(l2e.xb) # print # self.assertIn(r'<html display_name="My Name" url_name="text_url_name">', str(l2e.xb)) xml = etree.fromstring(str(l2e.xb)) html = xml.find('.//html') mn = html.find('.//span[@class="marginote"]') print(("marginote xml = %s" % etree.tostring(mn))) self.assertTrue(mn is not None) mnspan = mn.findall(".//span")[1] print(("mnspan.text=%s" % mnspan.text)) self.assertTrue(mnspan.text == "this is the anchor text") desc = mn.find('.//span[@class="marginote_desc"]') self.assertTrue(desc is not None) self.assertTrue(desc.text == "this is a note")
def test_video1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example4.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() cfn = path(tmdir) / 'chapter/Unit_2.xml' self.assertTrue(os.path.exists(cfn)) xml = etree.parse(cfn).getroot() assert (xml.tag == 'chapter') assert (xml.get('display_name') == "Unit 2") assert (xml.get('start') == "2013-11-22") assert (xml[0].tag == 'sequential') assert (xml[0][0].tag == 'vertical') assert (xml[0][0][0].tag == 'video') assert (xml[0][0][0].get('url_name') == 'A_sample_video') assert (not xml.findall('.//p')) assert (xml[0][1][0].tag == 'problem') assert (xml[0][2][0].tag == 'discussion') assert (xml[0][2][0].get('url_name') == 'Discuss_this_question') assert (len(xml[0]) == 3) cfn = path(tmdir) / 'video/A_sample_video.xml' self.assertTrue(os.path.exists(cfn)) self.assertTrue( '<video display_name="A sample video" youtube_id_1_0="u23ZUSu7-HY" source="test"/>' in open(cfn).read()) cfn = path(tmdir) / 'discussion/Discuss_this_question.xml' self.assertTrue(os.path.exists(cfn)) self.assertTrue( '<discussion display_name="Discuss this question" forumid="discuss2"/>' in open(cfn).read())
def test_policy2(self): ''' Check for good error message, with filename and lineno ''' testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example10_badpolicy.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir, update_policy=True, suppress_policy=True) l2e.convert() except Exception as err: pass print "Error = %s" % str(err) self.assertTrue(re.search('Error processing element sequential in file .*example10_badpolicy.tex line 18', str(err)))
def test_output_fmts1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example6.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) try: l2e = latex2edx(nfn, output_dir=tmdir, xml_only=True) l2e.convert() err = "" except Exception as err: print err xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) cfn = path(tmdir) / 'course/2013_Fall.xml' self.assertTrue(not os.path.exists(cfn))
def test_latex2edx_cutset1b(self): with make_temp_directory() as tmdir: os.chdir(tmdir) ofn = "testcuts.yaml" tex = r'''\begin{edXproblem}{A problem}{url_name="a_problem"} \edXabox{type="option" options="<red>","green","blue" expect="<red>" test_pass="******" test_fail="<green>"} \end{edXproblem}''' l2e = latex2edx(tmdir + '/test.tex', latex_string=tex, add_wrap=True, do_images=False, output_dir=tmdir, output_cutset=ofn) xmlstr = l2e.xml cutset = CourseUnitTestSet(fn=ofn) self.assertEqual(len(cutset.tests), 2) self.assertEqual(cutset.tests[0].url_name, "a_problem") self.assertEqual(cutset.tests[0].responses, ["<ruby>"]) self.assertEqual(cutset.tests[0].expected, "correct")
def test_allow_dirs2(self): ''' Test that the output problem is properly nested when allow-directories is specified ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example15_directories.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir, allow_dirs=True) l2eout.convert() opfn = path(tmdir) / 'problem/test/subdirprob.xml' self.assertTrue(os.path.exists(opfn)) cfn = path(tmdir) / 'chapter/One.xml' xml = etree.fromstring(open(cfn).read()) prob = xml.find('.//problem') self.assertEqual(prob.get('url_name'), 'test:subdirprob')
def testRoundTrip(self): print "Testing XBundle round trip import -> export" xb = XBundle() cxmls = ''' <course semester="2013_Spring" course="mitx.01"> <chapter display_name="Intro"> <sequential display_name="Overview"> <html display_name="Overview text"> hello world </html> </sequential> <!-- a comment --> </chapter> </course> ''' pxmls = """ <policies semester='2013_Spring'> <policy>x:1</policy> </policies> """ xb.set_course(etree.XML(cxmls)) xb.add_policies(etree.XML(pxmls)) xb.add_about_file("overview.html", "hello overview") xbin = str(xb) # test round trip with make_temp_directory() as tdir: xb.export_to_directory(tdir) xb2 = XBundle() xb2.import_from_directory(tdir + '/mitx.01') xbreloaded = str(xb2) self.assertEqual(xbin, xbreloaded)
def test_video1(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example4.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() cfn = path(tmdir) / 'chapter/Unit_2.xml' self.assertTrue(os.path.exists(cfn)) xml = etree.parse(cfn).getroot() assert(xml.tag == 'chapter') assert(xml.get('display_name') == "Unit 2") assert(xml.get('start') == "2013-11-22") assert(xml[0].tag == 'sequential') assert(xml[0][0].tag == 'vertical') assert(xml[0][0][0].tag == 'video') assert(xml[0][0][0].get('url_name') == 'A_sample_video') assert(not xml.findall('.//p')) assert(xml[0][1][0].tag == 'problem') assert(xml[0][2][0].tag == 'discussion') assert(xml[0][2][0].get('url_name') == 'Discuss_this_question') assert(len(xml[0]) == 3) cfn = path(tmdir) / 'video/A_sample_video.xml' self.assertTrue(os.path.exists(cfn)) self.assertTrue('<video display_name="A sample video" youtube_id_1_0="u23ZUSu7-HY" source="test"/>' in open(cfn).read()) cfn = path(tmdir) / 'discussion/Discuss_this_question.xml' self.assertTrue(os.path.exists(cfn)) self.assertTrue('<discussion display_name="Discuss this question" forumid="discuss2"/>' in open(cfn).read())
def test_askta1(self): tex = ('\\begin{edXcourse}{1.00x}{1.00x Fall 2013}[url_name=2013_Fall]\n' '\\begin{edXchapter}{Unit 1}[start="2013-11-22"]\n' '\\begin{edXsection}{Introduction}\n' '\\begin{edXproblem}{test problem}\n' '\\edXaskta{settings=1 to="*****@*****.**"}\n\n' '\\edXaskta{cc="*****@*****.**"}\n\n' '\\end{edXproblem}\n' '\\end{edXsection}\n' '\\end{edXchapter}\n' '\\end{edXcourse}\n' ) # make sure edXaskta buttons work properly with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(tex).fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) l2e.xhtml2xbundle() # print "xbundle = " # print str(l2e.xb) # print self.assertIn(r'<a style="display:none" href="/course/jump_to_id" id="aturl_1"/>', str(l2e.xb))
def test_DND2(self): testdir = path(l2emod.__file__).parent / 'testtex' fn = testdir / 'example5a.tex' print "file %s" % fn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, fn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.mkdir('%s/dnd' % tmdir) os.system('cp %s/quadratic.tex %s/dnd/' % (testdir, tmdir)) os.chdir(tmdir) l2e = latex2edx(nfn, output_dir=tmdir) l2e.convert() xbfn = nfn[:-4] + '.xbundle' self.assertTrue(os.path.exists(xbfn)) # xb = open(xbfn).read() cfn = path(tmdir) / 'problem/p1.xml' assert(os.path.exists(cfn)) os.system('ls -sFC %s' % tmdir) os.system('ls -sFC %s/problem' % tmdir) self.assertTrue(os.path.exists(cfn)) data = open(cfn).read() assert('<drag_and_drop_input img="/static/images/quadratic/quadratic_dnd.png" target_outline="false" one_per_target="true"' in data) assert('<draggable id="term1" icon="/static/images/quadratic/quadratic_dnd_label1.png" can_reuse="true"/>' in data)
def test_margin_note1(self): tex = ('\\begin{edXcourse}{1.00x}{1.00x Fall 2013}[url_name=2013_Fall]\n' '\\begin{edXchapter}{Unit 1}[start="2013-11-22"]\n' '\\begin{edXsection}{Introduction}\n' '\\begin{edXtext}{My Name}[url_name=text_url_name]\n' 'Hello world!\n\n' '\\marginote{this is a note}{this is the anchor text}\n\n' '\\end{edXtext}\n' '\\end{edXsection}\n' '\\end{edXchapter}\n' '\\end{edXcourse}\n' ) with make_temp_directory() as tmdir: os.chdir(tmdir) fp = MakeTeX(tex).fp l2e = latex2edx(tmdir + '/test.tex', fp=fp, do_images=False, output_dir=tmdir) l2e.xhtml2xbundle() # print "xbundle = " # print str(l2e.xb) # print # self.assertIn(r'<html display_name="My Name" url_name="text_url_name">', str(l2e.xb)) xml = etree.fromstring(str(l2e.xb)) html = xml.find('.//html') mn = html.find('.//span[@class="marginote"]') print("marginote xml = %s" % etree.tostring(mn)) self.assertTrue(mn is not None) mnspan = mn.findall(".//span")[1] print("mnspan.text=%s" % mnspan.text) self.assertTrue(mnspan.text == "this is the anchor text") desc = mn.find('.//span[@class="marginote_desc"]') self.assertTrue(desc is not None) self.assertTrue(desc.text == "this is a note")
def test_key1(self): ''' Test the output of `latex2edx example12_index.tex` for proper formation of the key_map.json file. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example12_index.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() jsfn = '%s/static/key_map.json' % tmdir keymap = json.load(open(jsfn)) self.assertEqual(set(keymap.keys()), set(['topic', 'numerical'])) self.assertEqual(keymap['topic'][0], ['Module_1/A_lecture_section/1', 'Module_1/Another_lecture_section/1']) self.assertEqual(keymap['numerical'], [['Module_0/A_problem_section/2'], ['Example problem']])
def test_edxxml1(self): ''' Test the output of `latex2edx example13_edxxml.tex` for proper rendering of the complete XML with multiple calls in `edXxml` in a given line. ''' testdir = path(l2e.__file__).parent / 'testtex' tfn = testdir / 'example13_edxxml.tex' print "file %s" % tfn with make_temp_directory() as tmdir: nfn = '%s/%s' % (tmdir, tfn.basename()) os.system('cp %s/* %s' % (testdir, tmdir)) os.chdir(tmdir) l2eout = latex2edx(nfn, output_dir=tmdir) l2eout.convert() cfn = path(tmdir) / 'html/Code_Text.xml' data = open(cfn).read().split('\n') expected = ('A matrix <code>A</code> and column vector named ' '<code>b</code> can be multiplied in the form ' '<code>A b</code> only if the number of columns ' 'of <code>b</code> match the number of rows in ' '<code>A</code>. </p>') self.assertEqual(data[2], expected)