1 #! /usr/bin/env python 2 # Copyright 2017 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 import os 7 import sys 8 import textwrap 9 import unittest 10 11 if __name__ == '__main__': 12 sys.path.append( 13 os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) 14 15 from devil.utils import markdown 16 17 18 class MarkdownTest(unittest.TestCase): 19 20 def testBold(self): 21 raw = 'foo' 22 self.assertEquals('**foo**', markdown.md_bold(raw)) 23 24 def testBoldContainsStars(self): 25 raw = '*foo*' 26 self.assertEquals('**\\*foo\\***', markdown.md_bold(raw)) 27 28 def testCode(self): 29 raw = textwrap.dedent("""\ 30 class MarkdownTest(unittest.TestCase): 31 def testCode(self): 32 pass""") 33 34 expected = textwrap.dedent("""\ 35 ```python 36 class MarkdownTest(unittest.TestCase): 37 def testCode(self): 38 pass 39 ``` 40 """) 41 actual = markdown.md_code(raw, language='python') 42 self.assertEquals(expected, actual) 43 44 def testCodeContainsTicks(self): 45 raw = textwrap.dedent("""\ 46 This is sample markdown. 47 ```c 48 // This is a sample code block. 49 int main(int argc, char** argv) { 50 return 0; 51 } 52 ```""") 53 54 expected = textwrap.dedent("""\ 55 ``` 56 This is sample markdown. 57 \\`\\`\\`c 58 // This is a sample code block. 59 int main(int argc, char** argv) { 60 return 0; 61 } 62 \\`\\`\\` 63 ``` 64 """) 65 actual = markdown.md_code(raw, language=None) 66 self.assertEquals(expected, actual) 67 68 def testEscape(self): 69 raw = 'text_with_underscores *and stars*' 70 expected = 'text\\_with\\_underscores \\*and stars\\*' 71 actual = markdown.md_escape(raw) 72 self.assertEquals(expected, actual) 73 74 def testHeading1(self): 75 raw = 'Heading 1' 76 self.assertEquals('# Heading 1', markdown.md_heading(raw, level=1)) 77 78 def testHeading5(self): 79 raw = 'Heading 5' 80 self.assertEquals('##### Heading 5', markdown.md_heading(raw, level=5)) 81 82 def testHeading10(self): 83 raw = 'Heading 10' 84 self.assertEquals('###### Heading 10', markdown.md_heading(raw, level=10)) 85 86 def testInlineCode(self): 87 raw = 'devil.utils.markdown_test' 88 self.assertEquals( 89 '`devil.utils.markdown_test`', markdown.md_inline_code(raw)) 90 91 def testInlineCodeContainsTicks(self): 92 raw = 'this contains `backticks`' 93 self.assertEquals( 94 '`this contains \\`backticks\\``', markdown.md_inline_code(raw)) 95 96 def testItalic(self): 97 raw = 'bar' 98 self.assertEquals('*bar*', markdown.md_italic(raw)) 99 100 def testItalicContainsStars(self): 101 raw = '*bar*' 102 self.assertEquals('*\\*bar\\**', markdown.md_italic(raw)) 103 104 def testLink(self): 105 link_text = 'Devil home' 106 link_target = ( 107 'https://github.com/catapult-project/catapult/tree/master/devil') 108 expected = ( 109 '[Devil home]' 110 '(https://github.com/catapult-project/catapult/tree/master/devil)') 111 self.assertEquals(expected, markdown.md_link(link_text, link_target)) 112 113 def testLinkTextContainsBracket(self): 114 link_text = 'foo [] bar' 115 link_target = 'https://www.google.com' 116 expected = '[foo [\\] bar](https://www.google.com)' 117 self.assertEquals(expected, markdown.md_link(link_text, link_target)) 118 119 120 if __name__ == '__main__': 121 unittest.main(verbosity=2) 122