-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
230 lines (205 loc) Β· 6.35 KB
/
main.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import './style.css'
import { EditorView, basicSetup } from 'codemirror'
import { EditorState } from '@codemirror/state'
import { clojure } from "./src/clojure"
let editorState = EditorState.create({
doc: `(+ 1 1)`,
extensions: [basicSetup, clojure()]
})
let view = new EditorView({
state: editorState,
parent: document.querySelector('#app')
})
let testState = EditorState.create({
readOnly: true,
extensions: [
//EditorView.editable.of(false),
basicSetup, clojure()]
})
let testView = new EditorView({
state: testState,
parent: document.querySelector('#test')
})
let topLevelText = "Alt+Enter = Eval top-level form"
let keyBindings = "<strong>Key bindings:</strong>,Shift+Enter = Eval cell," +
topLevelText + ",Ctrl/Cmd+Enter = Eval at cursor";
keyBindings = keyBindings.split(',');
for (let i = 0; i < keyBindings.length; i++)
keyBindings[i] = "" + keyBindings[i] + "<br>";
keyBindings = keyBindings.join('');
document.getElementById("keymap").innerHTML = keyBindings;
for (const exercise of config.exercises.practice) {
const select = document.getElementById("exercise-select")
const opt = document.createElement('option')
opt.value = exercise.slug
opt.innerHTML = exercise.name
select.appendChild(opt)
}
const select = document.getElementById("exercise-select")
const opt = document.createElement('option')
select.addEventListener('change', function () {
const doc = view.state.doc.toString()
const end = doc.length
loadExercise(select.value)
});
let exercise = null
const results = document.getElementById("results")
function loadExercise(slug) {
results.innerHTML = ""
exercise = slug
const instructionsElement = document.getElementById("instructions")
const k = slug.replaceAll("-", "_")
const src = exercises[k].trim()
const testSuite = testSuites[k + "_test"].trim()
//clearTests()
//evalString("(do " + testSuite + ")")
//console.log("Deftests:", deftests)
const doc = view.state.doc.toString()
const testDoc = testView.state.doc.toString()
const end = doc.length
instructionsElement.innerHTML = instructions[k].substring(17).trim()
view.dispatch({
changes: { from: 0, to: end, insert: src},
selection: { anchor: 0, head: 0 }
})
testView.dispatch({
changes: { from: 0, to: testDoc.length, insert: testSuite},
selection: { anchor: 0, head: 0 }
})
}
const button = document.getElementById("button")
button.addEventListener('click', function () {
const k = exercise.replaceAll("-", "_")
const testSuite = testSuites[k + "_test"].trim()
clearTests()
//console.log("Running tests")
//let testEnv = new Env()
const doc = view.state.doc.toString()
//console.log("Doc:", doc)
evalString("(do " + doc + ")")
try {
evalString("(do " + testSuite + ")")
} catch (error) {
results.innerHTML = error
results.style.color = 'red';
return null
}
let fails = []
for (const test of deftests) {
if (!test.result) {
fails.push(test.test.value)
}
//console.log("fails:", fails)
}
const uniqueFails = [...new Set(fails)];
//console.log("uniqueFails: ", uniqueFails)
if (uniqueFails.length == 1) {
results.innerHTML = "1 fail: " + uniqueFails[0]
results.style.color = 'red';
} else if (uniqueFails.length > 1) {
results.innerHTML = uniqueFails.length + " fails: " + uniqueFails.join(", ")
results.style.color = 'red';
}
else {
results.innerHTML = "Passed π"
results.style.color = 'green';
}
})
function loadSolution(slug) {
loadExercise(slug)
const k = slug.replaceAll("-", "_")
const src = solutions[k].trim()
let doc = view.state.doc.toString()
const end = doc.length
view.dispatch({
changes: { from: 0, to: end, insert: src},
selection: { anchor: 0, head: 0 }
})
}
function testSolution(slug) {
loadExercise(slug)
const k = slug.replaceAll("-", "_")
const src = solutions[k].trim()
let doc = view.state.doc.toString()
const end = doc.length
view.dispatch({
changes: { from: 0, to: end, insert: src},
selection: { anchor: 0, head: 0 }
})
clearTests()
doc = view.state.doc.toString()
const testSuite = testSuites[k + "_test"].trim()
try {
evalString("(do " + doc + ")")
} catch (error) {
results.innerHTML = error
results.style.color = 'red';
return null
}
try {
evalString("(do " + testSuite + ")")
} catch (error) {
results.innerHTML = error
results.style.color = 'red';
return null
}
let fails = []
for (const test of deftests) {
if (!test.result) {
fails.push(test.test.value)
}
//console.log("fails:", fails)
}
const uniqueFails = [...new Set(fails)];
if (uniqueFails.length == 1) {
results.innerHTML = "1 fail: " + uniqueFails[0]
results.style.color = 'red';
} else if (uniqueFails.length > 1) {
results.innerHTML = uniqueFails.length + " fails: " + uniqueFails.join(", ")
results.style.color = 'red';
}
else {
results.innerHTML = "Passed π"
results.style.color = 'green';
}
}
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
//const exercisesToTest = ["hello-world", "two-fer", "reverse-string", "accumulate", "series"]
const exercisesToTest = shuffle(Object.keys(exercises))
function testExercises() {
let passes = []
let fails = []
for (let exercise = 0; exercise < exercisesToTest.length; exercise++) {
console.log("Testing ", exercisesToTest[exercise])
testSolution(exercisesToTest[exercise])
if (results.innerHTML === "Passed π") {
passes.push(exercisesToTest[exercise])
results.innerHTML = passes.length + " solutions passed π"
} else {
results.innerHTML = passes.length + " solutions passed, " + exercisesToTest[exercise] + " failed"
fails.push(exercisesToTest[exercise])
}
}
console.log("Passes:", passes)
console.log("Fails:", fails)
}
function randExercise() {
return exercisesToTest[Math.floor(Math.random() * exercisesToTest.length)]
}
evalString("(do " + core + ")")
loadExercise(randExercise())
//loadSolution(randExercise())
//loadSolution("two_fer")
//testExercises()