|
| 1 | +/** |
| 2 | + * Run all unittest examples |
| 3 | + * |
| 4 | + * Copyright 2016 by D Language Foundation |
| 5 | + * |
| 6 | + * License: http://boost.org/LICENSE_1_0.txt, Boost License 1.0 |
| 7 | + */ |
| 8 | + |
| 9 | +// wraps a unittest into a runnable script |
| 10 | +function wrapIntoMain(code) { |
| 11 | + var currentPackage = $('body')[0].id; |
| 12 | + // BUMP mir-algorithm image here: https://github.com/dlang-tour/core-exec/blob/master/Dockerfile |
| 13 | + // run.dlang.io frontend: https://github.com/dlang-tour/core/blob/master/public/static/js/tour-controller.js#L398 |
| 14 | + var codeOut = '/+dub.sdl:\ndependency "mir-algorithm" version="~>0.6.14"\n+/\n'; |
| 15 | + |
| 16 | + // dynamically wrap into main if needed |
| 17 | + if (code.indexOf("void main") >= 0) { |
| 18 | + codeOut += "import " + currentPackage + "; "; |
| 19 | + codeOut += code; |
| 20 | + } |
| 21 | + else { |
| 22 | + codeOut += "void main()\n{\n"; |
| 23 | + codeOut += " import " + currentPackage + ";\n"; |
| 24 | + // writing to the stdout is probably often used |
| 25 | + codeOut += " import std.stdio: write, writeln, writef, writefln;\n "; |
| 26 | + codeOut += code.split("\n").join("\n "); |
| 27 | + codeOut += "\n}"; |
| 28 | + } |
| 29 | + return codeOut; |
| 30 | +} |
| 31 | + |
| 32 | +$(document).ready(function() |
| 33 | +{ |
| 34 | + if ($('body')[0].id == "Home") |
| 35 | + return; |
| 36 | + |
| 37 | + // only for std at the moment |
| 38 | + if (!$('body').hasClass("std")) |
| 39 | + return; |
| 40 | + |
| 41 | + // first selector is for ddoc - second for ddox |
| 42 | + var codeBlocks = $('pre[class~=d_code]').add('pre[class~=code]'); |
| 43 | + codeBlocks.each(function(index) |
| 44 | + { |
| 45 | + var currentExample = $(this); |
| 46 | + var orig = currentExample.html(); |
| 47 | + |
| 48 | + // check whether it is from a ddoced unittest |
| 49 | + // 1) check is for ddoc, 2) for ddox |
| 50 | + // manual created tests most likely can't be run without modifications |
| 51 | + if (!($(this).parent().parent().prev().hasClass("dlang_runnable") || |
| 52 | + $(this).prev().children(":last").hasClass("dlang_runnable"))) |
| 53 | + return; |
| 54 | + |
| 55 | + currentExample.replaceWith( |
| 56 | + '<div class="unittest_examples">' |
| 57 | + + '<div class="d_code">' |
| 58 | + + '<pre class="d_code">'+orig+'</pre>' |
| 59 | + + '</div>' |
| 60 | + + '<div class="d_run_code" style="display: block">' |
| 61 | + + '<textarea class="d_code" style="display: none;"></textarea>' |
| 62 | + + '</div>' |
| 63 | + + '<div class="d_example_buttons">' |
| 64 | + + '<div class="editButton"><i class="fa fa-edit" aria-hidden="true"></i> Edit</div>' |
| 65 | + + '<div class="runButton"><i class="fa fa-play" aria-hidden="true"></i> Run</div>' |
| 66 | + + '<div class="resetButton" style="display:none"><i class="fa fa-undo " aria-hidden="true"></i> Reset</div>' |
| 67 | + + '<div class="openInEditorButton" title="Open in an external editor"><i class="fa fa-external-link" aria-hidden="true"></i></div>' |
| 68 | + + '</div>' |
| 69 | + + '<div class="d_code_output"><span class="d_code_title">Application output</span><br><pre class="d_code_output" readonly>Running...</pre>' |
| 70 | + + '</div>' |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + $('textarea[class=d_code]').each(function(index) { |
| 75 | + var parent = $(this).parent(); |
| 76 | + var btnParent = parent.parent().children(".d_example_buttons"); |
| 77 | + var outputDiv = parent.parent().children(".d_code_output"); |
| 78 | + var editor = setupTextarea(this, { |
| 79 | + parent: btnParent, |
| 80 | + outputDiv: outputDiv, |
| 81 | + stdin: false, |
| 82 | + args: false, |
| 83 | + transformOutput: wrapIntoMain, |
| 84 | + defaultOutput: "All tests passed", |
| 85 | + keepCode: true, |
| 86 | + outputHeight: "auto", |
| 87 | + backend: "tour" |
| 88 | + }); |
| 89 | + }); |
| 90 | +}); |
0 commit comments