| 1 |
import test from 'ava'; |
| 2 |
import { formatCopyText, filterText } from "./copybutton_funcs"; |
| 3 |
|
| 4 |
const parameters = [ |
| 5 |
{ |
| 6 |
description: 'empty prompt', |
| 7 |
text: 'hallo', |
| 8 |
prompt: '', |
| 9 |
isRegexp: false, |
| 10 |
onlyCopyPromptLines: true, |
| 11 |
removePrompts: true, |
| 12 |
expected: 'hallo' |
| 13 |
}, |
| 14 |
{ |
| 15 |
description: 'no prompt in text', |
| 16 |
text: 'hallo', |
| 17 |
prompt: '>>> ', |
| 18 |
isRegexp: false, |
| 19 |
onlyCopyPromptLines: true, |
| 20 |
removePrompts: true, |
| 21 |
expected: 'hallo' |
| 22 |
}, |
| 23 |
{ |
| 24 |
description: 'with non-regexp python prompt', |
| 25 |
text: ` |
| 26 |
>>> first |
| 27 |
output |
| 28 |
>>> second`, |
| 29 |
prompt: '>>> ', |
| 30 |
isRegexp: false, |
| 31 |
onlyCopyPromptLines: true, |
| 32 |
removePrompts: true, |
| 33 |
expected: '\nfirst\nsecond' |
| 34 |
}, |
| 35 |
{ |
| 36 |
description: 'with non-regexp console prompt', |
| 37 |
text: ` |
| 38 |
$ first |
| 39 |
output |
| 40 |
$ second`, |
| 41 |
prompt: '$ ', |
| 42 |
isRegexp: false, |
| 43 |
onlyCopyPromptLines: true, |
| 44 |
removePrompts: true, |
| 45 |
expected: '\nfirst\nsecond' |
| 46 |
}, |
| 47 |
{ |
| 48 |
description: 'with non-regexp prompt, keep prompt', |
| 49 |
text: ` |
| 50 |
>>> first |
| 51 |
output |
| 52 |
>>> second`, |
| 53 |
prompt: '>>> ', |
| 54 |
isRegexp: false, |
| 55 |
onlyCopyPromptLines: true, |
| 56 |
removePrompts: false, |
| 57 |
expected: '\n>>> first\n>>> second' |
| 58 |
}, |
| 59 |
{ |
| 60 |
description: 'multiline with |, keep prompt', |
| 61 |
text: ` |
| 62 |
>>> first | |
| 63 |
output | |
| 64 |
is | |
| 65 |
fine |
| 66 |
is it? |
| 67 |
>>> second`, |
| 68 |
prompt: '>>> ', |
| 69 |
isRegexp: false, |
| 70 |
onlyCopyPromptLines: true, |
| 71 |
removePrompts: false, |
| 72 |
copyEmptyLines: false, |
| 73 |
lineContinuationChar: '|', |
| 74 |
expected: '>>> first |\noutput |\nis |\nfine\n>>> second' |
| 75 |
}, |
| 76 |
{ |
| 77 |
description: 'multiline with \\, remove prompt', |
| 78 |
text: ` |
| 79 |
$ datalad download-url http://www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf \\ |
| 80 |
--dataset . \\ |
| 81 |
-m "add beginners guide on bash" \\ |
| 82 |
-O books/bash_guide.pdf |
| 83 |
`, |
| 84 |
prompt: '$ ', |
| 85 |
isRegexp: false, |
| 86 |
onlyCopyPromptLines: true, |
| 87 |
removePrompts: true, |
| 88 |
copyEmptyLines: false, |
| 89 |
lineContinuationChar: '\\', |
| 90 |
expected: 'datalad download-url http://www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf \\\n--dataset . \\\n-m "add beginners guide on bash" \\\n-O books/bash_guide.pdf' |
| 91 |
}, |
| 92 |
{ |
| 93 |
description: 'multiline with "HERE-document", remove prompt', |
| 94 |
text: ` |
| 95 |
$ cat << EOT > notes.txt |
| 96 |
One can hear a joke. |
| 97 |
And laugh. |
| 98 |
|
| 99 |
EOT |
| 100 |
`, |
| 101 |
prompt: '$ ', |
| 102 |
isRegexp: false, |
| 103 |
onlyCopyPromptLines: true, |
| 104 |
removePrompts: true, |
| 105 |
copyEmptyLines: false, |
| 106 |
hereDocDelim: "EOT", |
| 107 |
expected: 'cat << EOT > notes.txt\nOne can hear a joke.\nAnd laugh.\n\nEOT' |
| 108 |
}, |
| 109 |
{ |
| 110 |
description: 'with non-regexp prompt, keep lines', |
| 111 |
text: ` |
| 112 |
>>> first |
| 113 |
output |
| 114 |
>>> second`, |
| 115 |
prompt: '>>> ', |
| 116 |
isRegexp: false, |
| 117 |
onlyCopyPromptLines: false, |
| 118 |
removePrompts: true, |
| 119 |
expected: '\nfirst\noutput\nsecond' |
| 120 |
}, |
| 121 |
{ |
| 122 |
description: 'with non-regexp prompt, keep all', |
| 123 |
text: ` |
| 124 |
>>> first |
| 125 |
output |
| 126 |
>>> second`, |
| 127 |
prompt: '>>> ', |
| 128 |
isRegexp: false, |
| 129 |
onlyCopyPromptLines: false, |
| 130 |
removePrompts: false, |
| 131 |
expected: '\n>>> first\noutput\n>>> second' |
| 132 |
}, |
| 133 |
{ |
| 134 |
description: 'with regexp python prompt', |
| 135 |
text: ` |
| 136 |
>>> first |
| 137 |
output |
| 138 |
>>> second`, |
| 139 |
prompt: '>>> ', |
| 140 |
isRegexp: true, |
| 141 |
onlyCopyPromptLines: true, |
| 142 |
removePrompts: true, |
| 143 |
expected: '\nfirst\nsecond' |
| 144 |
}, |
| 145 |
{ |
| 146 |
description: 'with regexp python prompt and empty lines', |
| 147 |
text: ` |
| 148 |
>>> first |
| 149 |
output |
| 150 |
|
| 151 |
>>> second`, |
| 152 |
prompt: '>>> ', |
| 153 |
isRegexp: true, |
| 154 |
onlyCopyPromptLines: true, |
| 155 |
removePrompts: true, |
| 156 |
copyEmptyLines: true, |
| 157 |
expected: '\nfirst\n\nsecond' |
| 158 |
}, |
| 159 |
{ |
| 160 |
description: 'with regexp python prompt and empty lines, ignore empty lines', |
| 161 |
text: ` |
| 162 |
>>> first |
| 163 |
output |
| 164 |
|
| 165 |
>>> second`, |
| 166 |
prompt: '>>> ', |
| 167 |
isRegexp: true, |
| 168 |
onlyCopyPromptLines: true, |
| 169 |
removePrompts: true, |
| 170 |
copyEmptyLines: false, |
| 171 |
expected: 'first\nsecond' |
| 172 |
}, |
| 173 |
{ |
| 174 |
description: 'with regexp console prompt', |
| 175 |
text: ` |
| 176 |
$ first |
| 177 |
output |
| 178 |
$ second`, |
| 179 |
prompt: '\\$ ', |
| 180 |
isRegexp: true, |
| 181 |
onlyCopyPromptLines: true, |
| 182 |
removePrompts: true, |
| 183 |
expected: '\nfirst\nsecond' |
| 184 |
}, |
| 185 |
{ |
| 186 |
description: 'with ipython prompt (old and new style) regexp', |
| 187 |
text: ` |
| 188 |
[1]: first |
| 189 |
...: continuation |
| 190 |
output |
| 191 |
[2]: second |
| 192 |
In [3]: jupyter`, |
| 193 |
prompt: '\\[\\d*\\]: |In \\[\\d*\\]: |\\.\\.\\.: ', |
| 194 |
isRegexp: true, |
| 195 |
onlyCopyPromptLines: true, |
| 196 |
removePrompts: true, |
| 197 |
expected: '\nfirst\ncontinuation\nsecond\njupyter', |
| 198 |
}, |
| 199 |
{ |
| 200 |
description: 'with ipython prompt (old and new style) regexp, keep prompts', |
| 201 |
text: ` |
| 202 |
[1]: first |
| 203 |
...: continuation |
| 204 |
output |
| 205 |
[2]: second |
| 206 |
In [3]: jupyter`, |
| 207 |
prompt: '\\[\\d*\\]: |In \\[\\d*\\]: |\\.\\.\\.: ', |
| 208 |
isRegexp: true, |
| 209 |
onlyCopyPromptLines: true, |
| 210 |
removePrompts: false, |
| 211 |
expected: '\n[1]: first\n...: continuation\n[2]: second\nIn [3]: jupyter', |
| 212 |
}, |
| 213 |
{ |
| 214 |
/* The following is included to demonstrate an example of a false positive regex test. |
| 215 |
As noted in https://github.com/executablebooks/sphinx-copybutton/issues/86, |
| 216 |
JS RegEx in some cases "fixes" incorrect regexes rather than failing on them. |
| 217 |
*/ |
| 218 |
description: 'with ipython prompt regexp (false positive)', |
| 219 |
text: ` |
| 220 |
[1]: first |
| 221 |
...: continuation |
| 222 |
output |
| 223 |
[2]: second`, |
| 224 |
prompt: '[\d*]: |\.\.\.: ', |
| 225 |
isRegexp: true, |
| 226 |
onlyCopyPromptLines: true, |
| 227 |
removePrompts: true, |
| 228 |
expected: '\nfirst\ncontinuation\nsecond' |
| 229 |
} |
| 230 |
] |
| 231 |
|
| 232 |
parameters.forEach((p) => { |
| 233 |
test(p.description, t => { |
| 234 |
const text = formatCopyText(p.text, p.prompt, p.isRegexp, p.onlyCopyPromptLines, p.removePrompts, p.copyEmptyLines, p.lineContinuationChar, p.hereDocDelim); |
| 235 |
t.is(text, p.expected) |
| 236 |
}); |
| 237 |
}) |
| 238 |
|
| 239 |
|
| 240 |
// |
| 241 |
|
| 242 |
/** |
| 243 |
* From https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518 |
| 244 |
* |
| 245 |
* @param {String} HTML representing a single element |
| 246 |
* @return {Element} |
| 247 |
*/ |
| 248 |
function htmlToElement(html) { |
| 249 |
var template = document.createElement('template'); |
| 250 |
html = html.trim(); // Never return a text node of whitespace as the result |
| 251 |
template.innerHTML = html; |
| 252 |
return template.content.firstChild; |
| 253 |
} |
| 254 |
|
| 255 |
const defaultTarget = htmlToElement(` |
| 256 |
<pre id="codecell0"> |
| 257 |
<span class="linenos"> 1</span><span class="c1"># import packages</span> |
| 258 |
<span class="linenos"> 2</span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span> |
| 259 |
</pre> |
| 260 |
`); |
| 261 |
|
| 262 |
const filterParameters = [ |
| 263 |
{ |
| 264 |
description: 'with default exclude text (.linenos)', |
| 265 |
target: defaultTarget, |
| 266 |
exclude: '.linenos', |
| 267 |
expected: ` |
| 268 |
<span class="c1"># import packages</span> |
| 269 |
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span> |
| 270 |
` |
| 271 |
}, |
| 272 |
{ |
| 273 |
description: 'with empty exclude text', |
| 274 |
target: defaultTarget, |
| 275 |
exclude: '', |
| 276 |
expected: ` |
| 277 |
<span class="linenos"> 1</span><span class="c1"># import packages</span> |
| 278 |
<span class="linenos"> 2</span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span> |
| 279 |
` |
| 280 |
}, |
| 281 |
{ |
| 282 |
description: 'with exclude comments (.c1) and line numbers (.linenos)', |
| 283 |
target: defaultTarget, |
| 284 |
exclude: '.linenos, .c1', |
| 285 |
expected: ` |
| 286 |
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span> |
| 287 |
` |
| 288 |
} |
| 289 |
] |
| 290 |
|
| 291 |
/** |
| 292 |
* Issues with tests because #filterText uses `.innerText` |
| 293 |
* See https://github.com/jsdom/jsdom/issues/1245. |
| 294 |
*/ |
| 295 |
/** |
| 296 |
filterParameters.forEach((p) => { |
| 297 |
test( p.description, t => { |
| 298 |
const text = filterText(p.target, p.exclude); |
| 299 |
t.is(text, p.expected.trim()) |
| 300 |
}); |
| 301 |
}) |
| 302 |
*/ |