generated from bobbicodes/web-editor-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
176 lines (162 loc) Β· 5 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
import './style.css'
import { EditorView, basicSetup } from 'codemirror'
import { EditorState } from '@codemirror/state'
import { clojure } from "./src/clojure"
import solutions from './test/exercises.json';
import testSuites from './test/tests.json';
import { evalString, deftests, clearTests } from "./src/interpreter"
let editorState = EditorState.create({
doc: `(=
{1/2 1
2/3 1}
{1/2 1
2/3 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 = "Shift+Enter = Eval top-level form"
let keyBindings = "<strong>Key bindings:</strong>,Alt+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;
let exercise
const results = document.getElementById("results")
function loadExercise(slug) {
results.innerHTML = ""
exercise = slug
const k = slug.replaceAll("-", "_")
const src = solutions[k].trim()
//console.log("loading test suite", testSuites[k + "_test"])
const testSuite = testSuites[k + "_test"].trim()
const doc = view.state.doc.toString()
const testDoc = testView.state.doc.toString()
const end = doc.length
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 }
})
}
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 }
})
doc = view.state.doc.toString()
clearTests()
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.includes(false)) {
fails.push(test.test.value)
}
}
//console.log("deftests:", deftests)
//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 (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
const exercisesToTest = shuffle(Object.keys(solutions))
function randExercise() {
return exercisesToTest[Math.floor(Math.random() * exercisesToTest.length)]
}
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 π") {
console.log("exercise passed")
passes.push(exercisesToTest[exercise])
results.innerHTML = passes.length + " tests passed π"
} else {
results.innerHTML = passes.length + " tests passed, " + exercisesToTest[exercise] + " failed"
fails.push(exercisesToTest[exercise])
}
}
console.log("Passes:", passes)
console.log("Fails:", fails)
}
function testExercisesUntilFail() {
let passes = []
var fails = []
for (let exercise = 0; exercise < exercisesToTest.length; exercise++) {
console.log("Testing ", exercisesToTest[exercise])
testSolution(exercisesToTest[exercise])
if (results.innerHTML === "Passed π") {
console.log("exercise passed")
passes.push(exercisesToTest[exercise])
results.innerHTML = passes.length + " tests passed π"
} else {
results.innerHTML = passes.length + " tests passed, " + exercisesToTest[exercise] + " failed"
fails.push(exercisesToTest[exercise])
break
}
}
console.log("Passes:", passes)
console.log("Fails:", fails)
}
//testSolution(randExercise())
testSolution("spiral_matrix")
//loadExercise("go_counting")
//testExercisesUntilFail()
//testExercises()