Skip to content

Commit 77dc25d

Browse files
committed
Finalized migration to latest workshopper infrastructure. Pfew.
1 parent bb8dfb7 commit 77dc25d

File tree

8 files changed

+139
-95
lines changed

8 files changed

+139
-95
lines changed
Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,8 @@
11
"use strict"
22

3-
var input = require('../../input')
4-
5-
6-
7-
module.exports = input().wrap(function(input, repeat) {
8-
9-
var count = 0
10-
var CYCLES = 100000
11-
12-
function operation() {
13-
for (var i = 0; i < 1000000; i++) {} // burn some CPU cycles
14-
count++ // count how many times this function was called
15-
}
16-
17-
console.log()
18-
console.log('the operation:')
19-
console.log(operation.toString())
20-
console.log()
21-
console.log('Trying to repeat the operation %d times...', CYCLES)
22-
console.log('Press control+c to kill.')
23-
console.log()
24-
25-
var start = Date.now()
26-
repeat(operation, CYCLES)
3+
"use strict"
274

28-
setTimeout(function() {
29-
var end = Date.now()
30-
console.error('Performed %d operations.', count)
31-
if (count === CYCLES) console.log('Fail! Should not have completed all operations!')
32-
// TODO calculate ideal ops per second?
33-
//if (count < 1000) console.log('Fail! Should have performed more operations!')
34-
if (end - start < 1500) console.log('Interrupted in approximately 1 second!')
35-
else console.log('Fail! Interrupted in %d milliseconds', end - start)
36-
process.exit()
37-
}, 1000)
38-
})
5+
var path = require('path')
6+
var runner = require('../runner')
397

8+
module.exports = runner.wrapWith(path.join(__dirname, 'wrapper.js'))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
var path = require('path')
4+
5+
var repeat = require(path.resolve(process.cwd(), process.argv[2]))
6+
7+
var count = 0
8+
var CYCLES = 100000
9+
10+
function operation() {
11+
for (var i = 0; i < 1000000; i++) {} // burn some CPU cycles
12+
count++ // count how many times this function was called
13+
}
14+
15+
console.log()
16+
console.log('the operation:')
17+
console.log(operation.toString())
18+
console.log()
19+
console.log('Trying to repeat the operation %d times...', CYCLES)
20+
console.log('Press control+c to kill.')
21+
console.log()
22+
23+
var start = Date.now()
24+
repeat(operation, CYCLES)
25+
26+
setTimeout(function() {
27+
var end = Date.now()
28+
console.error('Performed %d operations.', count)
29+
if (count === CYCLES) console.log('Fail! Should not have completed all operations!')
30+
// TODO calculate ideal ops per second?
31+
//if (count < 1000) console.log('Fail! Should have performed more operations!')
32+
if (end - start < 1500) console.log('Interrupted in approximately 1 second!')
33+
else console.log('Fail! Interrupted in %d milliseconds', end - start)
34+
process.exit()
35+
}, 1000)
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
"use strict"
22

3-
var input = require('../../input')
4-
var lorem = require('lorem-ipsum')
3+
var deepEqual = require('deep-eql')
4+
var random = require('../randomizer')
5+
var runner = require('../runner')
6+
var loremIpsum = require('lorem-ipsum')
7+
var util = require('util')
58

6-
module.exports = input(Array.apply(null, {length: Math.random() * 20}).map(function() {
7-
return lorem().split(' ')
8-
})).wrap(function(input, Spy) {
9-
input = input[0]
10-
var assert = require('assert')
9+
var input = random.arrayOf(20, function() { return loremIpsum().split(' ') })
1110

12-
console.log.bind = function () {
13-
throw new Error('Try implementing this without bind!')
14-
}
15-
16-
var count = 0
11+
var exercise = module.exports = runner.custom(function(Spy, input) {
12+
var count = 0, slice = Array.prototype.slice
1713
var parent = {
1814
test: function() {
19-
assert.deepEqual([].slice.call(arguments), input[count], "Check you are passing ALL the arguments! Hint: Function#apply")
20-
assert.strictEqual(this, parent, "Check the function's this! Hint: Function#apply")
15+
if (!deepEqual(slice.call(arguments), input[count])) {
16+
exercise.emit('fail', "Check you are passing ALL the arguments! Hint: Function#apply")
17+
}
18+
if (this !== parent) {
19+
exercise.emit('fail', "Check the function's this! Hint: Function#apply")
20+
}
2121
return arguments
2222
}
2323
}
2424
var originalFn = parent.test.bind(parent)
2525
var spy = Spy(parent, 'test')
2626

27+
var result = []
2728
input.forEach(function(args, i) {
28-
console.log.apply(console, args)
29+
result.push(util.format.apply(util, args))
2930
count = i
30-
assert.deepEqual(originalFn.apply(parent, args), parent.test.apply(parent, args), "Check your function's return value!")
31+
if (!deepEqual(originalFn.apply(parent, args), parent.test.apply(parent, args))) {
32+
exercise.emit('fail', "Check your function's return value!")
33+
}
3134
})
3235

33-
console.log('Method called %d times. ', spy.count)
34-
})
36+
result.push(util.format('Method called %d times. ', spy.count))
37+
return result
38+
}).quiet(input)
Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
"use strict"
22

3-
var input = require('../../input')
4-
var lorem = require('lorem-ipsum')
3+
var path = require('path')
4+
var random = require('../randomizer')
5+
var runner = require('../runner')
56

6-
module.exports = input(Array.apply(null, {length: Math.random() * 20 + 1}).map(function() {
7-
return lorem()
8-
})).wrap(function(input, logger) {
9-
var info = logger('INFO:')
10-
var warn = logger('WARN:')
11-
input[0].forEach(function(message, i) {
12-
var args = message.split(' ')
13-
if (i % 2 === 0) info.apply(null, args)
14-
else warn.apply(null, args)
15-
})
16-
})
7+
var input = random.arrayOfLorems(1, 20)
8+
9+
module.exports = runner.wrapWith(path.join(__dirname, 'wrapper.js'), input)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
var fs = require('fs')
4+
var path = require('path')
5+
6+
var fx = require(path.resolve(process.cwd(), process.argv[2]))
7+
var input = JSON.parse(fs.readFileSync(process.argv[3], { encoding: 'utf-8' }))[0]
8+
9+
var info = fx('INFO:')
10+
var warn = fx('WARN:')
11+
input.forEach(function(message, i) {
12+
if (i % 2 === 0) info.apply(null, message.split(' '))
13+
else warn.apply(null, message.split(' '))
14+
})
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
"use strict"
22

3-
var loremIpsum = require('lorem-ipsum')
3+
var path = require('path')
44
var random = require('../randomizer')
55
var runner = require('../runner')
66

77
var input = random.arrayOfLorems(1, 20)
88

9-
// FIXME: THIS DOES NOT CAPTURE console.log OUTPUT PROPERLY (as it happens
10-
// inside the runner, not in child processes). WE NEED TO CHANGE THIS INTO
11-
// NON-runner, RAW EXERCISE CODE WITH A WRAPPER MODULE FOR SUBMISSION/SOLUTION CMDS
12-
13-
module.exports = runner.custom(function(fx, input) {
14-
console.log.bind = function() {
15-
throw new Error('Try implementing this without bind!')
16-
}
17-
18-
var info = fx('INFO:')
19-
var warn = fx('WARN:')
20-
input.forEach(function(message, i) {
21-
if (i % 2 === 0) info.apply(null, message.split(' '))
22-
else warn.apply(null, message.split(' '))
23-
})
24-
})(input)
9+
module.exports = runner.wrapWith(path.join(__dirname, 'wrapper.js'), input)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
var fs = require('fs')
4+
var path = require('path')
5+
6+
var fx = require(path.resolve(process.cwd(), process.argv[2]))
7+
var input = JSON.parse(fs.readFileSync(process.argv[3], { encoding: 'utf-8' }))[0]
8+
9+
console.log.bind = function() {
10+
throw new Error('Try implementing this without bind!')
11+
}
12+
13+
var info = fx('INFO:')
14+
var warn = fx('WARN:')
15+
input.forEach(function(message, i) {
16+
if (i % 2 === 0) info.apply(null, message.split(' '))
17+
else warn.apply(null, message.split(' '))
18+
})

exercises/runner.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ var deepEqual = require('deep-eql')
33
var execute = require('workshopper-exercise/execute')
44
var exerciser = require('workshopper-exercise')
55
var filecheck = require('workshopper-exercise/filecheck')
6+
var fs = require('fs')
7+
var os = require('os')
68
var path = require('path')
79

8-
var verbose = true, showInput = true, compareDisplays = false, initFx, wrapUpFx, customFx
10+
var verbose = true, showInput = true, initFx, wrapUpFx, customFx, wrapperModulePath
911

1012
function runner() {
11-
var exercise = filecheck(exerciser())
13+
var exercise = execute(filecheck(exerciser()))
1214
var input = Array.prototype.slice.call(arguments)
13-
if (compareDisplays) {
14-
exercise = compareStdOut(exercise)
15-
}
15+
var submittedFx, __
1616

1717
exercise.addProcessor(function(mode, callback) {
18-
var submittedFx, __ = exercise.__.bind(exercise)
18+
__ = exercise.__.bind(exercise)
1919
try {
2020
submittedFx = require(path.resolve(process.cwd(), this.args[0]));
2121
} catch (e) {
@@ -31,6 +31,31 @@ function runner() {
3131
this.emit('fail', __('fail.must_export_function'))
3232
return callback(null, false)
3333
}
34+
35+
callback(null, true)
36+
});
37+
38+
if (wrapperModulePath) {
39+
exercise.addSetup(function setupWrapperModule(mode, callback) {
40+
this.solutionCommand = [ wrapperModulePath, this.solution ].concat(this.solutionArgs)
41+
this.submissionCommand = [ wrapperModulePath, this.submission ].concat(this.submissionArgs)
42+
43+
if (input.length > 0) {
44+
var file = path.join(os.tmpdir(), path.basename(this.solution)) + '.input.json'
45+
fs.writeFileSync(file, JSON.stringify(input), { encoding: 'utf-8' })
46+
exercise.addCleanup(function(mode, pass, callback) {
47+
fs.unlink(file, callback)
48+
})
49+
this.solutionCommand.splice(2, 0, file)
50+
this.submissionCommand.splice(2, 0, file)
51+
}
52+
process.nextTick(callback)
53+
})
54+
55+
return compareStdOut(exercise)
56+
}
57+
58+
exercise.addProcessor(function(mode, callback) {
3459
if (initFx) { initFx(); }
3560
var submittedResult = obtainResult(submittedFx, input)
3661
if (verbose) {
@@ -59,7 +84,7 @@ function runner() {
5984
exercise.addVerifyProcessor(wrapUpFx)
6085
}
6186

62-
return execute(exercise)
87+
return exercise
6388
}
6489

6590
function obtainResult(fx, input) {
@@ -70,11 +95,6 @@ function obtainResult(fx, input) {
7095
return fx.apply(null, input)
7196
}
7297

73-
runner.compareDisplay = function compareDisplay() {
74-
compareDisplays = true
75-
return this.quiet.apply(this, arguments)
76-
}
77-
7898
runner.custom = function custom(fx) {
7999
customFx = fx
80100
return runner
@@ -95,6 +115,12 @@ runner.quiet = function quiet() {
95115
return runner.apply(null, arguments)
96116
}
97117

118+
runner.wrapWith = function wrapWith(modulePath) {
119+
verbose = false
120+
wrapperModulePath = modulePath
121+
return runner.apply(null, Array.prototype.slice.call(arguments, 1))
122+
}
123+
98124
runner.wrapUp = function wrapUp(fx) {
99125
wrapUpFx = fx
100126
return runner

0 commit comments

Comments
 (0)