markdown_test.py 5.4 KB
Newer Older
M
Mars Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
import unittest

from parsec.state import BasicState

from skill_tree.excercises.market_math import processor
import skill_tree.excercises.markdown as mk


def math_processor(context):
    """ math(str)->str
对文本内容预处理,将公式标记为前端可展示的 html。
    """
    md = context
    new_md = []
    math_ctx = {
        "enter": False,
        "chars": []
    }

    count = len(md)
    i = 0
    while i < count:
        c = md[i]
        if c == '$':
            if math_ctx['enter']:
                j = 0
                chars = math_ctx['chars']
                length = len(chars)
                while j < length:
                    cc = chars[j]
                    if cc == '_':
                        next_c = chars[j + 1]
                        if next_c == '{':
                            subs = []
                            cursor = 2
                            next_c = chars[j + cursor]
                            while next_c != '}':
                                subs.append(next_c)
                                cursor += 1
                                next_c = chars[j + cursor]

                            sub = ''.join(subs)
                            new_md.append(f'<sub>{sub}</sub>')
                            j += cursor
                        else:
                            new_md.append(f'<sub>{next_c}</sub>')
                            j += 1
                    elif cc == '^':
                        next_c = chars[j + 1]
                        if next_c == '{':
                            subs = []
                            cursor = 2
                            next_c = chars[j + cursor]
                            while next_c != '}':
                                subs.append(next_c)
                                cursor += 1
                                next_c = chars[j + cursor]

                            sub = ''.join(subs)
                            new_md.append(f'<sup>{sub}</sup>')
                            j += cursor
                        else:
                            new_md.append(f'<sup>{next_c}</sup>')
                            j += 1
                    else:
                        new_md.append(cc)
                    j += 1

                math_ctx['enter'] = False
                math_ctx['chars'] = []
            else:
                math_ctx['enter'] = True
                math_ctx['chars'] = []
        else:
            if math_ctx['enter']:
                math_ctx['chars'].append(c)
            else:
                new_md.append(c)
        i += 1
    return "".join(new_md)

data = """
# Hello World

以下 `Hello World` 程序中,能够正确输出内容的是:

## 答案

```java
package app;

public class App {
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}
```

## 选项

### B

```java
package app;

public class App {
    public int main(){
        System.out.printf("Hello World");
        return 0;
    }
}
```

### C

```java
package app;

public class App {
    public static void main(String[] args){
        println("Hello World");
    }
}
```

### D

```java
package app;
import stdout

public class App {
    public int main(){
        print("Hello World\n");
        return 0;
    }
}

```

"""


class MarkdownTestCase(unittest.TestCase):
    def test_basic(self):
        state = BasicState(data.strip())
        doc = mk.parse(state)
        self.assertEqual(doc.title, "Hello World")
        self.assertEqual(len(doc.options), 3)
        self.assertEqual(doc.description[0].source.strip(), """以下 `Hello World` 程序中,能够正确输出内容的是:""")
        self.assertEqual(doc.answer[0].language, "java")
        self.assertEqual(doc.answer[0].source, """package app;

public class App {
    public static void main(String[] args){
        System.out.println("Hello World");
    }
}""")
        self.assertEqual(doc.options[0].paras[0].language, "java")
        self.assertEqual(doc.options[0].paras[0].source, """package app;

public class App {
    public int main(){
        System.out.printf("Hello World");
        return 0;
    }
}""")
        self.assertEqual(doc.options[1].paras[0].language, "java")
        self.assertEqual(doc.options[1].paras[0].source, """package app;

public class App {
    public static void main(String[] args){
        println("Hello World");
    }
}""")
        self.assertEqual(doc.options[2].paras[0].language, "java")
        self.assertEqual(doc.options[2].paras[0].source, """package app;
import stdout

public class App {
    public int main(){
        print("Hello World\n");
        return 0;
    }
}
""")


class MathTestCase(unittest.TestCase):

    def test_parse(self):
        data = "$e^{pi}$"
        result = processor(data)
        self.assertEqual(result, math_processor(data))

    def test_pack(self):
        data = "ploy is $e^{pi}$ in plain text"
        result = processor(data)
        self.assertEqual(result, math_processor(data))

    def test_simple(self):
        data = "ploy is $x_0$ in plain text"
        result = processor(data)
        self.assertEqual(result, math_processor(data))

    def test_left(self):
        data = "$x_0$ at start of plain text"
        result = processor(data)
        self.assertEqual(result, math_processor(data))

    def test_right(self):
        data = "ploy is $x_0$"
        result = processor(data)
        self.assertEqual(result, math_processor(data))