endbasic-core 0.13.0

The EndBASIC programming language - core
Documentation
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// EndBASIC
// Copyright 2026 Julio Merino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Support functions to implement the integration tests.

use endbasic_core::*;
use std::cell::RefCell;
use std::collections::HashMap;
use std::env;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::{self, BufRead, BufReader, Seek, Write};
use std::path::{Path, PathBuf};
use std::process;
use std::rc::Rc;
use tempfile::NamedTempFile;

mod callables;

/// Computes the path to the directory where this test's binary lives.
fn self_dir() -> PathBuf {
    let self_exe = env::current_exe().expect("Cannot get self's executable path");
    let dir = self_exe.parent().expect("Cannot get self's directory");
    assert!(dir.ends_with("target/debug/deps") || dir.ends_with("target/release/deps"));
    dir.to_owned()
}

/// Computes the path to the source file `name`.
pub(super) fn src_path(name: &str) -> PathBuf {
    let test_dir = self_dir();
    let debug_or_release_dir = test_dir.parent().expect("Failed to get parent directory");
    let target_dir = debug_or_release_dir.parent().expect("Failed to get parent directory");
    let dir = target_dir.parent().expect("Failed to get parent directory");

    // Sanity-check that we landed in the right location.
    assert!(dir.join("Cargo.lock").exists());

    dir.join(name)
}

/// A parsed test case from a golden data file.
#[derive(Debug, Eq, PartialEq)]
struct Test {
    name: String,
    sources: Vec<String>,
}

/// A type describing the golden data of various tests in a file.
type Tests = Vec<Test>;

/// Returns true if the `line` corresponds to a source section.
fn is_source_header(line: &str) -> bool {
    line == "## Source" || line == "## Source (partial)"
}

/// Reads the source sections of a golden test description file.
fn read_sources(path: &Path) -> io::Result<Tests> {
    let file = File::open(path).expect("Failed to open golden data file");
    let reader = BufReader::new(file);

    fn add_test(tests: &mut Tests, name: String, sources: Vec<String>) -> io::Result<()> {
        if sources.is_empty() {
            Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Test case '{}' has no Source section", name),
            ))
        } else {
            tests.push(Test { name, sources });
            Ok(())
        }
    }

    fn finish_source(sources: &mut Vec<String>, source: &mut Option<String>) {
        if let Some(source) = source.take() {
            sources.push(source.trim_end().to_owned());
        }
    }

    #[derive(Clone, Copy, Eq, PartialEq)]
    enum Section {
        Other,
        Source,
    }

    let mut tests = vec![];
    let mut current_test = None;
    let mut current_section = Section::Other;
    let mut sources = vec![];
    let mut source: Option<String> = None;
    for line in reader.lines() {
        let line = line?;

        // Deal with CRLF.  I'd do this on Windows only, but keeping it unconditional helps
        // with cross-platform testing (per the unit tests below).
        let line = line.trim_end_matches('\r');

        if let Some(stripped) = line.strip_prefix("# Test: ") {
            finish_source(&mut sources, &mut source);
            if let Some(name) = current_test.take() {
                add_test(&mut tests, name, std::mem::take(&mut sources))?;
            }
            current_test = Some(stripped.to_owned());
            current_section = Section::Other;
            continue;
        } else if line.starts_with("# ") {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("Unexpected section header {}", line),
            ));
        } else if is_source_header(line) {
            current_section = Section::Source;
            continue;
        } else if line.starts_with("## ") {
            finish_source(&mut sources, &mut source);
            current_section = Section::Other;
            continue;
        } else if line == "```basic" {
            if current_section == Section::Source {
                if current_test.is_none() {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "Source section without test header",
                    ));
                }
                source = Some(String::new());
            }
            continue;
        } else if line == "```" {
            finish_source(&mut sources, &mut source);
            continue;
        }

        if let Some(source) = source.as_mut() {
            source.push_str(line);
            source.push('\n');
        }
    }

    finish_source(&mut sources, &mut source);
    if let Some(name) = current_test {
        add_test(&mut tests, name, std::mem::take(&mut sources))?;
    }

    if tests.is_empty() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!("Test file '{}' has no tests", path.display()),
        ));
    }

    Ok(tests)
}

#[test]
fn test_read_sources_one() -> io::Result<()> {
    let mut file = NamedTempFile::new()?;
    write!(
        file,
        "junk
# Test: first

## Source

```basic
First line

Second line
```

## Disassembly

```asm
foo bar
```
"
    )?;
    file.flush()?;

    assert_eq!(
        [Test { name: "first".to_owned(), sources: vec!["First line\n\nSecond line".to_owned()] }],
        read_sources(file.path())?.as_slice()
    );

    Ok(())
}

#[test]
fn test_read_sources_two() -> io::Result<()> {
    let mut file = NamedTempFile::new()?;
    write!(
        file,
        "junk
# Test: first

## Source

```basic
First line

Second line
```

## Disassembly

```asm
foo bar
```

# Test: second

## Source

```basic
The line
```
"
    )?;
    file.flush()?;

    assert_eq!(
        [
            Test {
                name: "first".to_owned(),
                sources: vec!["First line\n\nSecond line".to_owned()],
            },
            Test { name: "second".to_owned(), sources: vec!["The line".to_owned()] },
        ],
        read_sources(file.path())?.as_slice()
    );

    Ok(())
}

#[test]
fn test_read_sources_many_sources_per_test() -> io::Result<()> {
    let mut file = NamedTempFile::new()?;
    write!(
        file,
        "junk
# Test: first

## Source (partial)

```basic
First line
```

## Output

```plain
ignored
```

## Source (partial)

```basic
Second line

Third line
```
"
    )?;
    file.flush()?;

    assert_eq!(
        [Test {
            name: "first".to_owned(),
            sources: vec!["First line".to_owned(), "Second line\n\nThird line".to_owned()],
        }],
        read_sources(file.path())?.as_slice()
    );

    Ok(())
}

#[test]
fn test_read_sources_crlf() -> io::Result<()> {
    let mut file = NamedTempFile::new()?;
    write!(
        file,
        "junk\r\n# Test: first\r\n\r\n## Source\r\n\r\n```basic\r\nFirst line\r\n\r\nSecond line\r\n```\r\n"
    )?;
    file.flush()?;

    assert_eq!(
        [Test { name: "first".to_owned(), sources: vec!["First line\n\nSecond line".to_owned()] }],
        read_sources(file.path())?.as_slice()
    );

    Ok(())
}

#[test]
fn test_read_sources_crlf_many_tests() -> io::Result<()> {
    let mut file = NamedTempFile::new()?;
    write!(
        file,
        "junk\r\n# Test: first\r\n\r\n## Source\r\n\r\n```basic\r\nOne\r\n```\r\n\r\n# Test: second\r\n\r\n## Source\r\n\r\n```basic\r\nTwo\r\n```\r\n"
    )?;
    file.flush()?;

    assert_eq!(
        [
            Test { name: "first".to_owned(), sources: vec!["One".to_owned()] },
            Test { name: "second".to_owned(), sources: vec!["Two".to_owned()] },
        ],
        read_sources(file.path())?.as_slice()
    );

    Ok(())
}

/// Collection of section markers for a golden file.
struct Labels {
    source: &'static str,
    disassembly: &'static str,
    compiler_errors: &'static str,
    exit_code: &'static str,
    output: &'static str,
    runtime_errors: &'static str,
}

/// Obtains the section markers to use when writing out the data of `test`.
fn labels_for(test: &Test) -> Labels {
    if test.sources.len() > 1 {
        Labels {
            source: "## Source (partial)",
            disassembly: "## Disassembly (full)",
            compiler_errors: "## Compiler errors (partial)",
            exit_code: "## Exit code (partial)",
            output: "## Output (partial)",
            runtime_errors: "## Runtime errors (partial)",
        }
    } else {
        Labels {
            source: "## Source",
            disassembly: "## Disassembly",
            compiler_errors: "## Compilation errors",
            exit_code: "## Exit code",
            output: "## Output",
            runtime_errors: "## Runtime errors",
        }
    }
}

/// Generates a textual diff of `golden` and `generated`.  The output is meant to be useful for
/// human consumption when a test fails and is not guaranteed to be in patch format.
///
/// Returns the empty string when the two files match.
fn diff(golden: &Path, generated: &Path) -> io::Result<String> {
    match process::Command::new("diff")
        .args([OsStr::new("-u"), golden.as_os_str(), generated.as_os_str()])
        .output()
    {
        Ok(result) => {
            let Some(code) = result.status.code() else {
                return Err(io::Error::other("diff crashed"));
            };
            let Ok(stdout) = String::from_utf8(result.stdout) else {
                return Err(io::Error::other("diff printed non-UTF8 content to stdout"));
            };
            let Ok(stderr) = String::from_utf8(result.stderr) else {
                return Err(io::Error::other("diff printed non-UTF8 content to stderr"));
            };

            let mut diff = stdout;
            diff.push_str(&stderr);
            if code == 0 && !diff.is_empty() {
                return Err(io::Error::other("diff succeeded but output is not empty"));
            } else if code != 0 && diff.is_empty() {
                return Err(io::Error::other("diff succeeded but output is empty"));
            }

            Ok(diff)
        }

        Err(e) if e.kind() == io::ErrorKind::NotFound => {
            let left = fs::read_to_string(golden)?;
            let right = fs::read_to_string(generated)?;

            let mut diff = String::new();
            if left != right {
                diff.push_str("Golden\n");
                diff.push_str("======\n");
                diff.push_str(&left);
                diff.push_str("\n\nActual\n");
                diff.push_str("======\n");
                diff.push_str(&right);
            }
            Ok(diff)
        }

        Err(e) => Err(e),
    }
}

#[test]
fn test_diff_same() -> io::Result<()> {
    let mut f1 = NamedTempFile::new()?;
    let mut f2 = NamedTempFile::new()?;

    writeln!(f1, "Line 1")?;
    writeln!(f1, "Line 2")?;
    f1.flush()?;
    f1.seek(io::SeekFrom::Start(0))?;

    writeln!(f2, "Line 1")?;
    writeln!(f2, "Line 2")?;
    f2.flush()?;
    f2.seek(io::SeekFrom::Start(0))?;

    let diff = diff(f1.path(), f2.path())?;
    assert!(diff.is_empty());
    Ok(())
}

#[test]
fn test_diff_different() -> io::Result<()> {
    let mut f1 = NamedTempFile::new()?;
    let mut f2 = NamedTempFile::new()?;

    writeln!(f1, "Line 1")?;
    writeln!(f1, "Line 2")?;
    f1.flush()?;
    f1.seek(io::SeekFrom::Start(0))?;

    writeln!(f2, "Line 1")?;
    writeln!(f2, "Line2")?;
    f2.flush()?;
    f2.seek(io::SeekFrom::Start(0))?;

    let diff = diff(f1.path(), f2.path())?;
    assert!(!diff.is_empty());
    Ok(())
}

/// Obtains the line ending used in `golden` file.
fn line_ending_for(golden: &Path) -> io::Result<&'static str> {
    let text = fs::read_to_string(golden)?;
    if text.contains("\r\n") { Ok("\r\n") } else { Ok("\n") }
}

#[test]
fn test_line_ending_for_crlf() -> io::Result<()> {
    let mut file = NamedTempFile::new()?;
    write!(file, "Line 1\r\nLine 2\r\n")?;
    file.flush()?;

    assert_eq!("\r\n", line_ending_for(file.path())?);
    Ok(())
}

/// Rewrites `path` file with to use `line_ending`.
///
/// This is "inefficient" (not that it matters in this specific scenario) but it helps keep
/// `generate()` simple _and_ it also allows us to validate CRLF behavior outside of Windows.
fn rewrite_with_line_ending(path: &Path, line_ending: &str) -> io::Result<()> {
    if line_ending == "\n" {
        return Ok(());
    }

    let text = fs::read_to_string(path)?;
    let normalized = text.replace("\r\n", "\n");
    let rewritten = normalized.replace('\n', line_ending);
    fs::write(path, rewritten)
}

#[test]
fn test_rewrite_with_line_ending() -> io::Result<()> {
    let mut file = NamedTempFile::new()?;
    write!(file, "Line 1\nLine 2\n")?;
    file.flush()?;

    rewrite_with_line_ending(file.path(), "\r\n")?;
    let data = fs::read(file.path())?;
    assert!(data.windows(2).any(|w| w == b"\r\n"));
    for i in 0..data.len() {
        if data[i] == b'\n' {
            assert!(i > 0 && data[i - 1] == b'\r');
        }
    }
    Ok(())
}

/// Executes `image` through completion in `vm`, and converts the result into an exit code.
async fn run_image(vm: &mut Vm, image: &Image) -> Result<i32, String> {
    loop {
        match vm.exec(image) {
            StopReason::End(code) => return Ok(code.to_i32()),
            StopReason::Eof => return Ok(0),
            StopReason::UpcallAsync(handle) => {
                if let Err(e) = handle.invoke().await {
                    return Err(e.to_string());
                }
            }
            StopReason::Exception(pos, e) => return Err(format!("{}: {}", pos, e)),
            StopReason::Yield => (),
        }
    }
}

/// Given a `golden` test definition, executes its source part and writes the corresponding
/// `generated` file.  The test is expected to pass when both match, but the caller is responsible
/// for checking this condition.
#[allow(clippy::write_with_newline)]
async fn regenerate<W: Write>(golden: &Path, generated: &mut W) -> io::Result<()> {
    let tests = read_sources(golden)?;

    let mut first = true;
    for test in tests {
        if !first {
            write!(generated, "\n")?;
        }
        write!(generated, "# Test: {}\n", test.name)?;
        first = false;
        let labels = labels_for(&test);

        let console = Rc::from(RefCell::from(String::new()));
        let mut upcalls_by_name: HashMap<SymbolKey, Rc<dyn Callable>> = HashMap::default();
        callables::register_all(&mut upcalls_by_name, console.clone());
        let mut compiler = Compiler::new(&upcalls_by_name, &[]).expect("Cannot fail");
        let mut image = Image::default();
        let mut vm = Vm::new_with_limits(
            upcalls_by_name.clone(),
            Limits { max_call_stack: 4096, max_heap_entries: U24::from(128) },
        );

        for source in test.sources {
            write!(generated, "\n{}\n\n", labels.source)?;
            write!(generated, "```basic\n")?;
            if !source.is_empty() {
                write!(generated, "{}\n", source)?;
            }
            write!(generated, "```\n")?;

            if let Err(e) = compiler.compile_more(&mut image, &mut source.as_bytes()) {
                write!(generated, "\n{}\n\n", labels.compiler_errors)?;
                write!(generated, "```plain\n")?;
                write!(generated, "{}\n", e)?;
                write!(generated, "```\n")?;
                continue;
            }

            write!(generated, "\n{}\n\n", labels.disassembly)?;
            write!(generated, "```asm\n")?;
            for line in image.disasm() {
                write!(generated, "{}\n", line)?;
            }
            write!(generated, "```\n")?;

            console.borrow_mut().clear();
            match run_image(&mut vm, &image).await {
                Ok(0) => (),
                Ok(i) => {
                    write!(generated, "\n{}\n\n", labels.exit_code)?;
                    write!(generated, "```plain\n")?;
                    write!(generated, "{}\n", i)?;
                    write!(generated, "```\n")?;
                }
                Err(e) => {
                    write!(generated, "\n{}\n\n", labels.runtime_errors)?;
                    write!(generated, "```plain\n")?;
                    write!(generated, "{}\n", e)?;
                    write!(generated, "```\n")?;
                }
            }

            let console = console.borrow();
            if !console.is_empty() {
                write!(generated, "\n{}\n\n", labels.output)?;
                write!(generated, "```plain\n")?;
                write!(generated, "{}", console)?;
                write!(generated, "```\n")?;
            }
        }
    }

    Ok(())
}

/// Executes the test described in the `core/tests/<name>.md` file.
pub(super) async fn run_one_test(name: &'static str) -> io::Result<()> {
    let golden = src_path(&format!("core/tests/{}.md", name));
    let line_ending = line_ending_for(&golden)?;

    let mut generated = NamedTempFile::new()?;
    regenerate(&golden, &mut generated).await?;
    generated.flush()?;
    rewrite_with_line_ending(generated.path(), line_ending)?;

    let diff = diff(&golden, generated.path())?;
    if !diff.is_empty() {
        if matches!(env::var("REGEN").as_deref(), Ok("1") | Ok("true") | Ok("yes")) {
            {
                let mut output = File::create(golden)?;
                generated.as_file_mut().seek(io::SeekFrom::Start(0))?;
                io::copy(&mut generated, &mut output)?;
            }
            panic!("Golden data regenerated; flip REGEN back to false");
        } else {
            eprintln!("{}", diff);
            panic!("Test failed; see stderr for details");
        }
    }

    Ok(())
}