Files
pplus-web/tests/inputnote.test.mjs
T
alexm e092cb029c
Build, test, deploy / site (push) Failing after 15m13s
Initial commit
2026-11-12 22:24:02 -05:00

72 lines
4.5 KiB
JavaScript

// SPDX-FileCopyrightText: 2026 Scientific Computing Studio, LLC
// SPDX-License-Identifier: Apache-2.0
// Mojo 1.2.0's input() and a pipe (the plan's gate G5): the Compiler view says why the compiler's run may stop with
// EOF where the Run view does not, exactly when a program that may call input() more than once was sent with input.
import test from 'node:test';
import assert from 'node:assert/strict';
import { inputReads, inputNote } from '../dist/core/inputnote.js';
import { installDocument } from '../dist/quarto/dom.js';
const prog = (body) => `def main():\n${body.split('\n').map((l) => ' ' + l).join('\n')}\n`;
test('one input() call in main reads input once: no note', () => {
assert.deepEqual(inputReads(prog('var s = input()\nprint(s)')), { lines: [2], why: null });
assert.equal(inputNote(prog('var s = input()\nprint(s)'), '1\n2\n'), null);
});
test('two call sites, a loop, a comprehension, or a helper function may read more than once', () => {
assert.equal(inputReads(prog('var a = input()\nvar b = input()\nprint(a + b)')).why, 'several');
assert.deepEqual(inputReads(prog('var a = input()\nvar b = input()')).lines, [2, 3]);
assert.equal(inputReads(prog('for i in range(3):\n print(input())')).why, 'loop');
assert.equal(inputReads(prog('var n = 0\nwhile n < 3:\n if True:\n var s = input()\n n += 1')).why, 'loop');
assert.equal(inputReads(prog('for i in range(3): print(input())')).why, 'loop', 'a loop on one line');
assert.equal(inputReads(prog('var xs = [input() for _ in range(3)]')).why, 'loop', 'a comprehension');
assert.equal(inputReads('def ask() -> String:\n return input()\n\ndef main():\n print(ask())\n').why, 'function');
});
test('strings, comments, methods and a def named input are not calls', () => {
assert.equal(inputReads(prog('print("input() input()") # input()\nvar s = input()')).why, null);
assert.equal(inputReads(prog('var s = String("x")\nprint(s.input())\nvar t = input()')).why, null);
assert.equal(inputReads(prog('"""\ninput()\ninput()\n"""\nvar t = input()')).why, null);
assert.deepEqual(inputReads(prog('print(1)')), { lines: [], why: null });
// after the loop ends, a single call outside it is once
assert.equal(inputReads('def main():\n for i in range(3):\n print(i)\n var s = input()\n').why, null);
});
test('the note: only when input was sent, and it names the lines and the reason', () => {
const two = prog('var a = input()\nvar b = input()');
assert.equal(inputNote(two, ''), null, 'no input sent: nothing to explain');
assert.equal(inputNote(two, '\n'), null);
const note = inputNote(two, '3\n4\n');
assert.match(note, /lines 2 and 3/);
assert.match(note, /first call takes every line/);
assert.match(note, /EOF/);
assert.match(inputNote(prog('for i in range(3):\n print(input())'), '1\n'), /in a loop \(line 3\)/);
assert.equal(inputNote('def main(:\n input(\n', '1\n') === null || typeof inputNote('def main(:\n input(\n', '1\n') === 'string', true, 'text that does not lex still answers');
});
test('the Compiler view shows the note under the output of such a compile, and not for a single read', async () => {
installDocument();
const { Session } = await import('../dist/core/session.js');
const { normalize } = await import('../dist/core/state.js');
const { compilerView } = await import('../dist/ui/views/compiler.js');
const answer = (stdout, stderr = '') => ({ answer: { compiled: true, buildStderr: '', executed: true, exitCode: stderr ? 1 : 0, stdout, stderr, timedOut: false, asm: [] }, error: null, seconds: 0.1 });
const draw = async (mojo, input, outcome) => {
const s = new Session(normalize({ v: 3, lang: 'mojo', sources: { mojo }, input }), { area: 'dev', storageId: null });
await s.compile(async () => outcome);
const root = document.createElement('div');
const off = compilerView(s, root, { reveal() {}, fix() {}, sendToAsm() {}, compile() {} });
off();
return root;
};
const two = prog('var a = input()\nvar b = input()\nprint(a, b)');
const shown = await draw(two, '3\n4', answer('', 'Unhandled exception caught during execution: EOF'));
const note = shown.querySelector('.compile-input-note');
assert.ok(note, 'the note is shown');
assert.match(note.textContent, /lines 2 and 3/);
const once = await draw(prog('var a = input()\nprint(a)'), '3', answer('3\n'));
assert.equal(once.querySelector('.compile-input-note'), null);
const noInput = await draw(two, '', answer('', 'EOF'));
assert.equal(noInput.querySelector('.compile-input-note'), null);
});