More steps towards integrating the bonfire challenge framework. Importing challnge seed, public tests, private tests, entry point and entry point negator

This commit is contained in:
Nathan Leniz
2015-01-22 15:49:16 -05:00
parent 378277f0e1
commit 0e9523ab72
6 changed files with 118 additions and 78 deletions

View File

@ -51,6 +51,7 @@ exports.returnBonfire = function(req, res, next) {
if (bonfireNumber > highestBonfireNumber) { bonfireNumber = 0; }
Bonfire.find({}, null, { sort: { bonfireNumber: 1 } }, function(err, bonfire) {
debug(bonfire[bonfireNumber].challengeEntryPoint);
if (err) {
debug('bonfire err: ', err);
next(err);
@ -65,9 +66,7 @@ exports.returnBonfire = function(req, res, next) {
privateTests: bonfire[bonfireNumber].privateTests,
challengeSeed: bonfire[bonfireNumber].challengeSeed,
challengeEntryPoint: bonfire[bonfireNumber].challengeEntryPoint,
//title: bonfire.name,
//name: bonfire.name,
//description: bonfire.description,
challengeEntryPointNegate: bonfire[bonfireNumber].challengeEntryPointNegate,
//cc: req.user ? req.user.bonfiresHash : undefined,
//points: req.user ? req.user.points : undefined,

View File

@ -18,7 +18,10 @@ var bonfireSchema = new mongoose.Schema({
publicTests: Array,
privateTests: Array,
challengeSeed: String,
bonfireNumber: Number
bonfireNumber: Number,
challengeEntryPoint: String,
challengeEntryPointNegate: String
});
module.exports = mongoose.model('Bonfire', bonfireSchema);

View File

@ -20,19 +20,37 @@ var myCodeMirror = CodeMirror.fromTextArea(document.getElementById("codeEditor")
}
});
var editor = myCodeMirror;
myCodeMirror.setValue('/*Welcome to Bonfire, Free Code Camp\'s future CoderByte replacement.\n' +
'Please feel free to use Bonfire as an in-browser playground and linting tool.\n' +
'Note that you can also write tests using Chai.js\n' +
' by using the keywords assert and expect */\n\n' +
'function test() {\n' +
' assert(2 !== 3, "2 is not equal to 3");\n' +
' return [1,2,3].map(function(elem) {\n' +
' return elem * elem;\n' +
' });\n' +
'}\n' +
'expect(test()).to.be.a("array");\n\n' +
'assert.deepEqual(test(), [1,4,9]);\n\n' +
'test();');
// Default value for editor if one isn't provided in (i.e. a challenge)
var nonChallengeValue = '/*Welcome to Bonfire, Free Code Camp\'s future CoderByte replacement.\n' +
'Please feel free to use Bonfire as an in-browser playground and linting tool.\n' +
'Note that you can also write tests using Chai.js\n' +
' by using the keywords assert and expect */\n\n' +
'function test() {\n' +
' assert(2 !== 3, "2 is not equal to 3");\n' +
' return [1,2,3].map(function(elem) {\n' +
' return elem * elem;\n' +
' });\n' +
'}\n' +
'expect(test()).to.be.a("array");\n\n' +
'assert.deepEqual(test(), [1,4,9]);';
// Default seed for editor if one isn't provided
var nonChallengeSeed = 'test();';
var editorValue;
if (challengeSeed) {
editorValue = challengeSeed;
} else {
editorValue = nonChallengeValue;
}
myCodeMirror.setValue(editorValue);
var codeOutput = CodeMirror.fromTextArea(document.getElementById("codeOutput"), {
lineNumbers: false,
@ -83,6 +101,17 @@ var tests;
var testSalt = Math.random();
var scrapeTests = function(userJavaScript) {
console.log(challengeEntryPointNegate);
var checkIfUserSuppliedEntry = new RegExp(challengeEntryPointNegate, 'g');
console.log(checkIfUserSuppliedEntry);
console.log(checkIfUserSuppliedEntry.test(userJavaScript));
if (!(checkIfUserSuppliedEntry.test(userJavaScript))) {
userJavaScript += '\n' + challengeEntryPoint;
}
for (var i = 0; i < publicTests.length; i++) {
userJavaScript += '\n' + publicTests[i];
}
var counter = 0;
var regex = new RegExp(/(expect(\s+)?\(.*\;)|(assert(\s+)?\(.*\;)|(assert\.\w.*\;)|(.*\.should\..*\;)/);
var match = regex.exec(userJavaScript);
@ -97,6 +126,11 @@ var scrapeTests = function(userJavaScript) {
counter++;
match = regex.exec(userJavaScript);
}
//for (publicTest in publicTests) {
// console.log(publicTest);
// tests.push(publicTest);
//}
if (tests) replaceQuotesInTests();
return userJavaScript;
};
@ -170,7 +204,7 @@ var reassembleTest = function(test, data) {
var runTests = function(err, data) {
pushed = false;
$('#testSuite').children().remove();
if (err && tests) {
if (err && tests.length > 0) {
tests = [{text:"Program Execution Failure", err: "No tests were run."}];
createTestDisplay();
} else if (tests) {

View File

@ -9,7 +9,7 @@
"Return true if the string is a palindrome, otherwise false"
],
"publicTests": [
"expect(palindrome(\"eye\")).to.be.a.(\"boolean\");",
"expect(palindrome(\"eye\")).to.be.a(\"boolean\");",
"assert.deepEqual(palindrome(\"eye\"), true);",
"assert.deepEqual(palindrome(\"race car\"), true);",
"assert.deepEqual(palindrome(\"not a palindrome\"), false);"
@ -20,8 +20,48 @@
"assert.deepEqual(palindrome(\"nope\"), false);"
],
"challengeSeed": "function palindrome(str) {\n // Good luck!\n return true;\n}\n\n",
"challengeEntryPoint": "palindrome(!!!)",
"bonfireNumber": 1
"challengeEntryPoint": "palindrome(\"eye\")",
"bonfireNumber": 1,
"challengeEntryPointNegate" : "palindrome\\([^str].*\\;",
},
{
"name": "Validate US Telephone Numbers",
"difficulty": 3,
"description": [
"You are in charge of a campground's website signup form. Specifically, you have been asked to validate US phone numbers before forms are submitted.",
"But consider yourself forewarned: the camp leader is quick to anger. So don't try to pull something like forcing the user to enter a phone number in a predefined format. He can't stand it when coders are lazy. Don't be lazy, or he might throw you in the fire pit.",
"The user cmay fill out the form field any way they choose as long as it is a valid US number. The following are all valid formats for US numbers:",
" 555-555-5555",
" (555)555-5555",
" (555) 555-5555",
" 555 555 5555",
" 5555555555",
" 1 555 555 5555",
"For this challenge you will be presented with a string such as \"800-692-7753\" or \"8oo-six427676;laskdjf\". Your job is to validate or reject the US phone number based on any combination of the formats provided above. The area code is required. If the country code code is provided, you must confirm that the country code is \"1\". Return true if the string is a valid US phone number; otherwise false."
],
"publicTests": [
"expect(telephoneCheck(\"555-555-5555\")).to.be.a(\"boolean\");",
"assert.deepEqual(telephoneCheck(\"555-555-5555\"), true);",
"assert.deepEqual(telephoneCheck(\"1 456 789 4444\"), true);",
"assert.deepEqual(telephoneCheck(\"123**&!!asdf#\"), false);"
],
"privateTests": [
"assert.deepEqual(telephoneCheck(\"55555555\"), false);",
"assert.deepEqual(telephoneCheck(\"(6505552368)\"), false);",
"assert.deepEqual(telephoneCheck(\"2 (757) 622-7382\"), false);",
"assert.deepEqual(telephoneCheck(\"0 (757) 622-7382\"), false);",
"assert.deepEqual(telephoneCheck(\"-1 (757) 622-7382\"), false);",
"assert.deepEqual(telephoneCheck(\"2 757 622-7382\"), false);",
"assert.deepEqual(telephoneCheck(\"10 (757) 622-7382\"), false);",
"assert.deepEqual(telephoneCheck(\"27576227382\"), false);",
"assert.deepEqual(telephoneCheck(\"(275)76227382\"), false);",
"assert.deepEqual(telephoneCheck(\"2(757)6227382\"), false);",
"assert.deepEqual(telephoneCheck(\"2(757)622-7382\"), false);"
],
"challengeSeed": "function telephoneCheck(str) {\n // Good luck!\n return true;\n}\n\n",
"challengeEntryPoint": "telephoneCheck(\"555-555-5555\")",
"bonfireNumber": 2,
"challengeEntryPointNegate" : "palindrome\\([^str].*\\;",
}
]

View File

@ -15,19 +15,21 @@ block content
script(src='/js/lib/codemirror/mode/javascript/javascript.js')
script(src='/js/lib/jailed/jailed.js')
script(src='/js/lib/bonfire/bonfireInit.js')
.row
#mainEditorPanel.col-sm-12.col-md-7.col-xs-12
.panel.panel-primary.panel-bonfire
.panel-heading.text-center #{name} (Level #{difficulty} bonfire)
.panel.panel-body
form.code
h3 Details
h5
ol
for sentence in description
li!= sentence
.form-group.codeMirrorView
textarea#codeEditor(autofocus=true)
.panel.panel-primary.panel-bonfire
.panel-heading.text-center #{name} (Level #{difficulty} bonfire)
.panel.panel-body
form.code
h3 Details
h5
ol
for sentence in description
li!= sentence
.form-group.codeMirrorView
textarea#codeEditor(autofocus=true)
#testCreatePanel.col-sm-12.col-md-5.col-xs-12
.panel.panel-primary.panel-bonfire
.panel-heading.text-center Output
@ -40,4 +42,10 @@ block content
br
ul#testSuite.list-group
br
script(src='/js/lib/bonfire/bonfireFramework.js')
script(type="text/javascript").
var publicTests = !{JSON.stringify(publicTests)};
var privateTests = !{JSON.stringify(privateTests)};
var challengeSeed = !{JSON.stringify(challengeSeed)};
var challengeEntryPoint = !{JSON.stringify(challengeEntryPoint)};
var challengeEntryPointNegate = !{JSON.stringify(challengeEntryPointNegate)};
script(src='/js/lib/bonfire/bonfireFramework.js')

View File

@ -1,45 +1 @@
h3
ol(start='0')
for bonfire in bonfires
li
a(href="/bonfire/#{bonfire.bonfireNumber}") #{bonfire.name}
| &nbsp; (Level #{bonfire.difficulty})
block content
script(src='/js/lib/codemirror/lib/codemirror.js')
script(src='/js/lib/codemirror/addon/edit/closebrackets.js')
script(src='/js/lib/codemirror/addon/edit/matchbrackets.js')
script(src='/js/lib/codemirror/addon/lint/lint.js')
script(src='/js/lib/codemirror/addon/lint/javascript-lint.js')
script(src='//ajax.aspnetcdn.com/ajax/jshint/r07/jshint.js')
script(src='/js/lib/chai/chai.js')
link(rel='stylesheet', href='/js/lib/codemirror/lib/codemirror.css')
link(rel='stylesheet', href='/js/lib/codemirror/addon/lint/lint.css')
link(rel='stylesheet', href='/js/lib/codemirror/theme/monokai.css')
link(rel="stylesheet", href="http://fonts.googleapis.com/css?family=Ubuntu+Mono")
script(src='/js/lib/codemirror/mode/javascript/javascript.js')
script(src='/js/lib/jailed/jailed.js')
script(src='/js/lib/bonfire/bonfireInit.js')
.row
#mainEditorPanel.col-sm-12.col-md-7.col-xs-12
.panel.panel-primary.panel-bonfire
.panel-heading.text-center Bonfire Playground
.panel.panel-body
form.code
.form-group.codeMirrorView
textarea#codeEditor(autofocus=true)
#testCreatePanel.col-sm-12.col-md-5.col-xs-12
.panel.panel-primary.panel-bonfire
.panel-heading.text-center Output
.panel.panel-body
#submitButton.btn.btn-primary.btn-big.btn-block Run code (ctrl + enter)
br
form.code
.form-group.codeMirrorView
textarea#codeOutput
br
ul#testSuite.list-group
br
script(src='/js/lib/bonfire/bonfireFramework.js')
//TODO: STUFF