-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
179 lines (163 loc) · 5.23 KB
/
index.js
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
var fs = require('fs')
, path = require('path')
, docs = path.join(__dirname, 'docs')
, child_process = require('child_process')
, md = require('cli-md')
, exec = child_process.exec
;
function getOutput (program, cb) {
var child = exec(program, function (error, stdout, stderr) {
if (error) return cb(error)
cb(null, stdout)
})
return child
}
function verifyIntro (args, cb) {
var program = 'node ' + args[0]
getOutput(program, function (e, stdout) {
if (stdout === 'Hello World!\n') {
cb(true)
}
if (e) {
console.error('Your program did not run :(')
console.error(e)
return cb(false)
}
if (stdout !== 'Hello World!') {
console.log('Your program did not output the exact text "Hello World!". Please check your program, fix the error, and verify again.')
}
})
}
function verifyStrings (args, cb) {
var program = 'node ' + args[0]
getOutput(program, function (e, stdout) {
if (stdout === 'It\'s a stringy world.\n') {
cb(true)
}
if (e) {
console.error('Your program did not run :(')
console.error(e)
return cb(false)
}
if (stdout !== 'It\'s a stringy world.\n') {
console.log('Your program did not output the exact text "It\'s a stringy world.". Please check your program, fix the error, and verify again.')
}
})
}
function verifyVariables (args, cb) {
var program = 'node ' + args[0]
// TODO: use emscripten or some shit to validate they actually use variables
getOutput(program, function (e, stdout) {
if (stdout === 'It\'s a stringy world.\n') {
cb(true)
}
if (e) {
console.error('Your program did not run :(')
console.error(e)
return cb(false)
}
if (stdout !== 'It\'s a stringy world.\n') {
console.log('Your program did not output the exact text "It\'s a stringy world.". Please check your program, fix the error, and verify again.')
}
})
}
function verifyObjects1 (args, cb) {
var program = 'node ' + args[0]
// TODO: use emscripten or some shit to validate they actually use .toUpperCase()
getOutput(program, function (e, stdout) {
if (stdout === 'It\'s a stringy world.\n'.toUpperCase()) {
cb(true)
}
if (e) {
console.error('Your program did not run :(')
console.error(e)
return cb(false)
}
if (stdout !== 'It\'s a stringy world.\n'.toUpperCase()) {
console.log('Your program did not output the exact text "IT\'S A STRINGY WORLD.". Please check your program, fix the error, and verify again.')
}
})
}
function verifyObjects2 (args, cb) {
var program = 'node ' + args[0]
// TODO: use emsripten or some shit to valiate thay actually make an object w/ a property
getOutput(program, function (e, stdout) {
if (stdout === 'It\'s a stringy world.\n'.toUpperCase()) {
cb(true)
}
if (e) {
console.error('Your program did not run :(')
console.error(e)
return cb(false)
}
if (stdout !== 'It\'s a stringy world.\n'.toUpperCase()) {
console.log('Your program did not output the exact text "IT\'S A STRINGY WORLD.". Please check your program, fix the error, and verify again.')
}
})
}
function verifyFunction (args, cb) {
var program = 'node ' + args[0]
// TODO: use emsripten or some shit to valiate thay actually make a function to do this
getOutput(program, function (e, stdout) {
if (stdout === 'It\'s a stringy world.\n'.toUpperCase()+'It\'s a stringy world.\n'.toUpperCase()) {
cb(true)
}
if (e) {
console.error('Your program did not run :(')
console.error(e)
return cb(false)
}
if (stdout !== 'It\'s a stringy world.\n'.toUpperCase()+ 'It\'s a stringy world.\n'.toUpperCase()) {
console.log('Your program did not output the exact text "IT\'S A STRINGY WORLD.\\nIT\'S A STRINGY WORLD.". Please check your program, fix the error, and verify again.')
}
})
}
function verifyArrays (args, cb) {
var program = 'node ' + args[0]
// TODO: use emsripten or some shit to validate they are actually pushing to the Array prototype.
getOutput(program, function (e, stdout) {
console.error(JSON.stringify(stdout))
if (stdout === '1\n2\n') {
cb(true)
}
if (e) {
console.error('Your program did not run :(')
console.error(e)
return cb(false)
}
if (stdout !== '1\n2') {
console.log('Your program did not output the exact text "1\\n2\\n". Please check your program, fix the error, and verify again.')
}
})
}
function docText (name) {
return md(fs.readFileSync(path.join(docs, name)).toString()).replace(/'/g, "'").replace(/"/g, '"')
}
exports['Hello World!'] =
{ problem: docText('intro.mkd')
, verify: verifyIntro
}
exports['Strings'] =
{ problem: docText('strings.mkd')
, verify: verifyStrings
}
exports['Values and Variables'] =
{ problem: docText('variables.mkd')
, verify: verifyVariables
}
exports['Intro to Objects'] =
{ problem: docText('objects1.mkd')
, verify: verifyObjects1
}
exports['Making Objects'] =
{ problem: docText('objects2.mkd')
, verify: verifyObjects2
}
exports['Functions'] =
{ problem: docText('functions.mkd')
, verify: verifyFunction
}
exports['Arrays'] =
{ problem: docText('arrays.mkd')
, verify: verifyArrays
}