Skip to content

Commit 4c667e7

Browse files
authored
Print succesfully parsed results from readNumber family of functions (#139)
1 parent 114aa57 commit 4c667e7

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/console/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class Console {
160160
* @private
161161
*/
162162
readNumber(str, parseFn, errorMsgType, asynchronous) {
163-
const DEFAULT = 0; // If we get into an infinite recursion, return DEFAULT.
163+
const DEFAULT = Symbol(); // If we get into an infinite recursion, return DEFAULT.
164164
const MAX_RECURSION_DEPTH = 100;
165165
// a special indicator that th program should be exiting
166166
const ABORT = Symbol('ABORT');
@@ -215,7 +215,17 @@ class Console {
215215
}
216216
}
217217
};
218-
return attemptInput(promptString, 0, asynchronous);
218+
const result = attemptInput(promptString, 0, asynchronous);
219+
if (result === DEFAULT) {
220+
return 0;
221+
}
222+
if (result === null) {
223+
return null;
224+
}
225+
// success
226+
this.print(str);
227+
this.println(result);
228+
return result;
219229
}
220230

221231
/**

test/console.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ describe('Console', () => {
133133
expect(promptSpy).toHaveBeenCalledOnceWith('give me a line!');
134134
expect(windowPromptSpy).toHaveBeenCalledOnceWith('modified prompt');
135135
});
136+
it('Prints a parsed result', () => {
137+
const promptSpy = spyOn(window, 'prompt').and.returnValue('hiya');
138+
const outputSpy = jasmine.createSpy();
139+
const c = new Console({ output: outputSpy });
140+
c.readLine('give me a line!');
141+
expect(outputSpy.calls.allArgs()).toEqual([['give me a line!'], ['hiya', '\n']]);
142+
});
136143
});
137144
describe('readFloat', () => {
138145
it('Errors for >1 argument', () => {
@@ -211,6 +218,20 @@ describe('Console', () => {
211218
expect(promptSpy).toHaveBeenCalledOnceWith('give me an int!');
212219
expect(windowPromptSpy).toHaveBeenCalledOnceWith('modified prompt');
213220
});
221+
it('Prints a parsed result', () => {
222+
const promptSpy = spyOn(window, 'prompt').and.returnValue(1);
223+
const outputSpy = jasmine.createSpy();
224+
const c = new Console({ output: outputSpy });
225+
c.readInt('give me an int!');
226+
expect(outputSpy.calls.allArgs()).toEqual([['give me an int!'], [1, '\n']]);
227+
});
228+
it('Doesnt print a default value', () => {
229+
const promptSpy = spyOn(window, 'prompt').and.returnValue(null);
230+
const outputSpy = jasmine.createSpy();
231+
const c = new Console({ output: outputSpy });
232+
c.readInt('give me an int!');
233+
expect(outputSpy.calls.allArgs()).toEqual([]);
234+
});
214235
});
215236
describe('readBoolean', () => {
216237
it('Errors for >1 argument', () => {

0 commit comments

Comments
 (0)