CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

84 lines
2.7 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Tests for CAT-SOOP's markdown math extension
  18. """
  19. import unittest
  20. from .. import loader
  21. from .. import language
  22. from ..test import CATSOOPTest
  23. # -----------------------------------------------------------------------------
  24. def math(x):
  25. return "<math>%s</math>" % (x,)
  26. def dmath(x):
  27. return "<displaymath>%s</displaymath>" % (x,)
  28. class TestMarkdownMath(CATSOOPTest):
  29. def setUp(self):
  30. CATSOOPTest.setUp(self)
  31. context = {}
  32. loader.load_global_data(context)
  33. assert "cs_unit_test_course" in context
  34. self.cname = context["cs_unit_test_course"]
  35. self.ctx = loader.generate_context([self.cname])
  36. def test_inline_math(self):
  37. pairs = [
  38. (r"If $x$ is 2", r"If %s is 2" % (math("x"))),
  39. (
  40. r"If $x$ is $\frac{2}{3}$",
  41. r"If %s is %s" % (math("x"), math(r"\frac{2}{3}")),
  42. ),
  43. (r"If $x$ is $\frac{2}{3}", r"If %s is $\frac{2}{3}" % (math("x"),)),
  44. (r"If \$2.38 is $x$", r"If $2.38 is %s" % (math("x"),)),
  45. (
  46. r"If $x$ is \$2.38, but $y$ is $\$3.47$",
  47. r"If %s is $2.38, but %s is %s"
  48. % (math("x"), math("y"), math(r"\$3.47")),
  49. ),
  50. ]
  51. for i, o in pairs:
  52. self.assertEqual(language._md_format_string(self.ctx, i, False), o)
  53. def test_display_math(self):
  54. self.maxDiff = 10000
  55. _ft = r"x_5[n]= \cases{\left({1\over2}\right)^{n/2}&$n=0, 2, 4, 6, 8, \dots, \infty$\cr0&otherwise}"
  56. pairs = [
  57. (r"If $$x$$ is 2", r"If %s is 2" % (dmath("x"))),
  58. (
  59. r"If $$A$$ is $\frac{2}{3}$ and $x_5[n]$ is given by: $$%s$$" % _ft,
  60. r"If %s is %s and %s is given by: %s"
  61. % (dmath("A"), math(r"\frac{2}{3}"), math("x_5[n]"), dmath(_ft)),
  62. ),
  63. ]
  64. for i, o in pairs:
  65. self.assertEqual(language._md_format_string(self.ctx, i, False), o)
  66. if __name__ == "__main__":
  67. unittest.main()