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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
// EndBASIC
// Copyright 2020 Julio Merino
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations
// under the License.

//! Stored program manipulation and interactive editor.

use crate::ast::{ArgSep, Expr, Value};
use crate::console::Console;
use crate::editor::Editor;
use crate::exec::{self, BuiltinCommand, Machine};
use async_trait::async_trait;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::io;
use std::path::PathBuf;
use std::rc::Rc;

/// Metadata of an entry in the store.
#[derive(Debug, Eq, PartialEq)]
pub struct Metadata {
    /// Last modification time of the entry.
    pub date: time::OffsetDateTime,

    /// Total size of the entry.
    pub length: u64,
}

/// Abstract operations to load and store programs on persistent storage.
pub trait Store {
    /// Deletes the program given by `name`.
    fn delete(&mut self, name: &str) -> io::Result<()>;

    /// Returns a sorted list of the entries in the store and their metadata.
    fn enumerate(&self) -> io::Result<BTreeMap<String, Metadata>>;

    /// Loads the contents of the program given by `name`.
    fn get(&self, name: &str) -> io::Result<String>;

    /// Saves the in-memory program given by `content` into `name`.
    fn put(&mut self, name: &str, content: &str) -> io::Result<()>;
}

/// Representation of the single program that we can keep in memory.
#[async_trait(?Send)]
pub trait Program {
    /// Edits the program interactively via the given `console`.
    async fn edit(&mut self, console: &mut dyn Console) -> io::Result<()>;

    /// Reloads the contents of the stored program with the given `text`.
    fn load(&mut self, text: &str);

    /// Gets the contents of the stored program as a single string.
    fn text(&self) -> String;
}

/// Computes the path to a source file given the `dir` where it lives and a `basename`.
fn to_filename<S: Into<PathBuf>>(basename: S) -> io::Result<String> {
    let mut basename = basename.into();

    if basename.components().fold(0, |count, _| count + 1) != 1 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "Filename must be a single path component",
        ));
    }

    if let Some(ext) = basename.extension() {
        if ext != "bas" && ext != "BAS" {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, "Invalid filename extension"));
        }
    } else {
        // Attempt to determine a sensible extension based on the case of the basename, assuming
        // that an all-uppercase basename wants an all-uppercase extension.  This is fragile on
        // case-sensitive file systems, but there is not a lot we can do.
        let mut ext = "BAS";
        for ch in basename.to_string_lossy().chars() {
            if ch.is_ascii_lowercase() {
                ext = "bas";
                break;
            }
        }
        basename.set_extension(ext);
    }
    Ok(basename.to_str().expect("Path came from a String").to_owned())
}

/// Shows the contents of directory `path`.
fn show_dir(store: &dyn Store, console: &mut dyn Console) -> io::Result<()> {
    let entries = store.enumerate()?;

    console.print("")?;
    console.print("    Modified              Size    Name")?;
    let mut total_files = 0;
    let mut total_bytes = 0;
    for (name, details) in entries {
        console.print(&format!(
            "    {}    {:6}    {}",
            details.date.format("%F %H:%M"),
            details.length,
            name,
        ))?;
        total_files += 1;
        total_bytes += details.length;
    }
    if total_files > 0 {
        console.print("")?;
    }
    console.print(&format!("    {} file(s), {} bytes", total_files, total_bytes))?;
    console.print("")?;
    Ok(())
}

/// The `DEL` command.
struct DelCommand {
    store: Rc<RefCell<dyn Store>>,
}

#[async_trait(?Send)]
impl BuiltinCommand for DelCommand {
    fn name(&self) -> &'static str {
        "DEL"
    }

    fn syntax(&self) -> &'static str {
        "filename"
    }

    fn description(&self) -> &'static str {
        "Deletes the given program.
The filename must be a string and must be a basename (no directory components).  The .BAS \
extension is optional, but if present, it must be .BAS."
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if args.len() != 1 {
            return exec::new_usage_error("DEL requires a filename");
        }
        let arg0 = args[0].0.as_ref().expect("Single argument must be present");
        match arg0.eval(machine.get_vars())? {
            Value::Text(t) => {
                let name = to_filename(t)?;
                self.store.borrow_mut().delete(&name)?;
            }
            _ => return exec::new_usage_error("DEL requires a string as the filename"),
        }
        Ok(())
    }
}

/// The `DIR` command.
struct DirCommand {
    console: Rc<RefCell<dyn Console>>,
    store: Rc<RefCell<dyn Store>>,
}

#[async_trait(?Send)]
impl BuiltinCommand for DirCommand {
    fn name(&self) -> &'static str {
        "DIR"
    }

    fn syntax(&self) -> &'static str {
        ""
    }

    fn description(&self) -> &'static str {
        "Displays the list of files on disk."
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        _machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("DIR takes no arguments");
        }
        show_dir(&*self.store.borrow(), &mut *self.console.borrow_mut())?;
        Ok(())
    }
}

/// The `EDIT` command.
struct EditCommand {
    console: Rc<RefCell<dyn Console>>,
    program: Rc<RefCell<dyn Program>>,
}

#[async_trait(?Send)]
impl BuiltinCommand for EditCommand {
    fn name(&self) -> &'static str {
        "EDIT"
    }

    fn syntax(&self) -> &'static str {
        "[lineno%]"
    }

    fn description(&self) -> &'static str {
        "Interactively edits the stored program."
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        _machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("EDIT takes no arguments");
        }

        let mut console = self.console.borrow_mut();
        let mut program = self.program.borrow_mut();
        program.edit(&mut *console).await?;
        Ok(())
    }
}

/// The `LOAD` command.
struct LoadCommand {
    store: Rc<RefCell<dyn Store>>,
    program: Rc<RefCell<dyn Program>>,
}

#[async_trait(?Send)]
impl BuiltinCommand for LoadCommand {
    fn name(&self) -> &'static str {
        "LOAD"
    }

    fn syntax(&self) -> &'static str {
        "filename"
    }

    fn description(&self) -> &'static str {
        "Loads the given program.
The filename must be a string and must be a basename (no directory components).  The .BAS \
extension is optional, but if present, it must be .BAS."
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if args.len() != 1 {
            return exec::new_usage_error("LOAD requires a filename");
        }
        let arg0 = args[0].0.as_ref().expect("Single argument must be present");
        match arg0.eval(machine.get_vars())? {
            Value::Text(t) => {
                let name = to_filename(t)?;
                let content = self.store.borrow().get(&name)?;
                self.program.borrow_mut().load(&content);
                machine.clear();
            }
            _ => return exec::new_usage_error("LOAD requires a string as the filename"),
        }
        Ok(())
    }
}

/// The `NEW` command.
struct NewCommand {
    program: Rc<RefCell<dyn Program>>,
}

#[async_trait(?Send)]
impl BuiltinCommand for NewCommand {
    fn name(&self) -> &'static str {
        "NEW"
    }

    fn syntax(&self) -> &'static str {
        ""
    }

    fn description(&self) -> &'static str {
        "Clears the stored program from memory."
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("NEW takes no arguments");
        }
        self.program.borrow_mut().load("");
        machine.clear();
        Ok(())
    }
}

/// The `RUN` command.
struct RunCommand {
    program: Rc<RefCell<dyn Program>>,
}

#[async_trait(?Send)]
impl BuiltinCommand for RunCommand {
    fn name(&self) -> &'static str {
        "RUN"
    }

    fn syntax(&self) -> &'static str {
        ""
    }

    fn description(&self) -> &'static str {
        "Runs the stored program.
Note that the program runs in the context of the interpreter so it will pick up any variables \
and other state that may already be set."
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if !args.is_empty() {
            return exec::new_usage_error("RUN takes no arguments");
        }
        let program = self.program.borrow().text();
        machine.exec(&mut program.as_bytes()).await
    }
}

/// The `SAVE` command.
struct SaveCommand {
    store: Rc<RefCell<dyn Store>>,
    program: Rc<RefCell<dyn Program>>,
}

#[async_trait(?Send)]
impl BuiltinCommand for SaveCommand {
    fn name(&self) -> &'static str {
        "SAVE"
    }

    fn syntax(&self) -> &'static str {
        "filename"
    }

    fn description(&self) -> &'static str {
        "Saves the current program in memory to the given filename.
The filename must be a string and must be a basename (no directory components).  The .BAS \
extension is optional, but if present, it must be .BAS."
    }

    async fn exec(
        &self,
        args: &[(Option<Expr>, ArgSep)],
        machine: &mut Machine,
    ) -> exec::Result<()> {
        if args.len() != 1 {
            return exec::new_usage_error("SAVE requires a filename");
        }
        let arg0 = args[0].0.as_ref().expect("Single argument must be present");
        match arg0.eval(machine.get_vars())? {
            Value::Text(t) => {
                let name = to_filename(t)?;
                let content = self.program.borrow().text();
                self.store.borrow_mut().put(&name, &content)?;
            }
            _ => return exec::new_usage_error("SAVE requires a string as the filename"),
        }
        Ok(())
    }
}

/// Instantiates all program editing commands against the stored `program`, using `console` for
/// interactive editing, and using `dir` as the on-disk storage for the programs.
fn all_commands_for(
    program: Rc<RefCell<dyn Program>>,
    console: Rc<RefCell<dyn Console>>,
    store: Rc<RefCell<dyn Store>>,
) -> Vec<Rc<dyn BuiltinCommand>> {
    vec![
        Rc::from(DelCommand { store: store.clone() }),
        Rc::from(DirCommand { console: console.clone(), store: store.clone() }),
        Rc::from(EditCommand { console: console.clone(), program: program.clone() }),
        Rc::from(LoadCommand { store: store.clone(), program: program.clone() }),
        Rc::from(NewCommand { program: program.clone() }),
        Rc::from(RunCommand { program: program.clone() }),
        Rc::from(SaveCommand { store: store.clone(), program }),
    ]
}

/// Instantiates all program editing commands against a new (empty) program, using `console` for
/// interactive editing, and using `dir` as the on-disk storage for the programs.
pub fn all_commands(
    console: Rc<RefCell<dyn Console>>,
    store: Rc<RefCell<dyn Store>>,
) -> Vec<Rc<dyn BuiltinCommand>> {
    all_commands_for(Rc::from(RefCell::from(Editor::new())), console, store)
}

#[cfg(test)]
mod testutils {
    use super::*;
    use crate::console;
    use std::collections::HashMap;

    #[derive(Default)]
    pub(crate) struct InMemoryStore {
        programs: HashMap<String, String>,
    }

    impl Store for InMemoryStore {
        fn delete(&mut self, name: &str) -> io::Result<()> {
            match self.programs.remove(name) {
                Some(_) => Ok(()),
                None => Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
            }
        }

        fn enumerate(&self) -> io::Result<BTreeMap<String, Metadata>> {
            let date = time::OffsetDateTime::from_unix_timestamp(1_588_757_875);

            let mut entries = BTreeMap::new();
            for (name, contents) in &self.programs {
                entries.insert(name.clone(), Metadata { date, length: contents.len() as u64 });
            }
            Ok(entries)
        }

        fn get(&self, name: &str) -> io::Result<String> {
            match self.programs.get(name) {
                Some(content) => Ok(content.to_owned()),
                None => Err(io::Error::new(io::ErrorKind::NotFound, "Entry not found")),
            }
        }

        fn put(&mut self, name: &str, content: &str) -> io::Result<()> {
            self.programs.insert(name.to_owned(), content.to_owned());
            Ok(())
        }
    }

    pub(crate) struct RecordedProgram {
        content: String,
    }

    impl RecordedProgram {
        pub fn new(content: &'static str) -> Self {
            Self { content: content.to_owned() }
        }
    }

    #[async_trait(?Send)]
    impl Program for RecordedProgram {
        async fn edit(&mut self, console: &mut dyn Console) -> io::Result<()> {
            let append = console::read_line(console, "", "").await?;
            self.content.push_str(&append);
            self.content.push('\n');
            Ok(())
        }

        fn load(&mut self, text: &str) {
            self.content = text.to_owned();
        }

        fn text(&self) -> String {
            self.content.clone()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::testutils::*;
    use super::*;
    use crate::console::testutils::*;
    use crate::exec::testutils::*;
    use crate::exec::MachineBuilder;
    use futures_lite::future::block_on;

    /// Runs the `input` code on a new machine that stores programs in `store` and verifies its
    /// output.
    ///
    /// `golden_in` is a sequence of keys to feed to the commands that request console input.
    ///
    /// `expected_out` is a sequence of expected calls to `PRINT`.
    ///
    /// `exp_program` is the expected state of `program` after execution.
    fn do_ok_test_with_store(
        program: Rc<RefCell<dyn Program>>,
        store: Rc<RefCell<dyn Store>>,
        input: &str,
        golden_in: &'static str,
        expected_out: &'static [&'static str],
        exp_program: &'static str,
    ) {
        let console =
            Rc::from(RefCell::from(MockConsoleBuilder::new().add_input_chars(golden_in).build()));
        let mut machine = MachineBuilder::default()
            .add_builtins(all_commands_for(program.clone(), console.clone(), store))
            .build();
        block_on(machine.exec(&mut input.as_bytes())).expect("Execution failed");
        let expected_out: Vec<CapturedOut> =
            expected_out.iter().map(|x| CapturedOut::Print((*x).to_owned())).collect();
        assert_eq!(expected_out, console.borrow().captured_out());
        assert_eq!(exp_program, program.borrow().text());
    }

    /// Same as `do_ok_test_with_store` but with an automatic `store`.
    fn do_ok_test(
        program: Rc<RefCell<dyn Program>>,
        input: &str,
        golden_in: &'static str,
        expected_out: &'static [&'static str],
        exp_program: &'static str,
    ) {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_ok_test_with_store(program, store, input, golden_in, expected_out, exp_program)
    }

    /// Runs the `input` code on a new machine and verifies that it fails with `expected_err`.
    ///
    /// Ensures that this does not touch the console.
    fn do_error_test_with_store(store: Rc<RefCell<dyn Store>>, input: &str, expected_err: &str) {
        let console = Rc::from(RefCell::from(MockConsoleBuilder::new().build()));
        let program = Rc::from(RefCell::from(RecordedProgram::new("")));
        let mut machine = MachineBuilder::default()
            .add_builtins(all_commands_for(program, console.clone(), store))
            .build();
        assert_eq!(
            expected_err,
            format!(
                "{}",
                block_on(machine.exec(&mut input.as_bytes())).expect_err("Execution did not fail")
            )
        );
        assert!(console.borrow().captured_out().is_empty());
    }

    /// Same as `do_error_test_with_store` but with an automatic (and inaccessible) `dir`.
    fn do_error_test(input: &str, expected_err: &str) {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_error_test_with_store(store, input, expected_err)
    }

    #[test]
    fn test_del_ok() {
        let mut store = InMemoryStore::default();
        store.put("bar.bas", "").unwrap();
        let store = Rc::from(RefCell::from(store));

        let program = Rc::from(RefCell::from(RecordedProgram::new("Leave me alone")));

        for p in &["foo", "foo.bas"] {
            store.borrow_mut().put("foo.bas", "line 1\n  line 2\n").unwrap();
            do_ok_test_with_store(
                program.clone(),
                store.clone(),
                &("DEL \"".to_owned() + p + "\""),
                "",
                &[],
                "Leave me alone",
            );
        }

        store.borrow().get("bar.bas").unwrap(); // Check that unrelated entries were not touched.
    }

    #[test]
    fn test_del_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        check_load_save_common_errors("DEL", store.clone());

        do_error_test_with_store(store.clone(), "DEL \"missing-file\"", "Entry not found");

        store.borrow_mut().put("mismatched-extension.bat", "").unwrap();
        do_error_test_with_store(store, "DEL \"mismatched-extension\"", "Entry not found");
    }

    #[test]
    fn test_dir_empty() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_ok_test_with_store(
            Rc::from(RefCell::from(RecordedProgram::new(""))),
            store,
            "DIR",
            "",
            &["", "    Modified              Size    Name", "    0 file(s), 0 bytes", ""],
            "",
        );
    }

    #[test]
    fn test_dir_entries_are_sorted() {
        let mut store = InMemoryStore::default();
        store.put("empty.bas", "").unwrap();
        store.put("some other file.bas", "not empty\n").unwrap();
        store.put("00AAA.BAS", "first\nfile\n").unwrap();
        store.put("not a bas.txt", "").unwrap();
        let store = Rc::from(RefCell::from(store));

        do_ok_test_with_store(
            Rc::from(RefCell::from(RecordedProgram::new(""))),
            store,
            "DIR",
            "",
            &[
                "",
                "    Modified              Size    Name",
                "    2020-05-06 09:37        11    00AAA.BAS",
                "    2020-05-06 09:37         0    empty.bas",
                "    2020-05-06 09:37         0    not a bas.txt",
                "    2020-05-06 09:37        10    some other file.bas",
                "",
                "    4 file(s), 21 bytes",
                "",
            ],
            "",
        );
    }

    #[test]
    fn test_dir_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        do_error_test_with_store(store, "DIR 2", "DIR takes no arguments");
    }

    #[test]
    fn test_edit_ok() {
        do_ok_test(
            Rc::from(RefCell::from(RecordedProgram::new("previous\n"))),
            "EDIT",
            "new line\n",
            &[],
            "previous\nnew line\n",
        );
    }

    #[test]
    fn test_edit_errors() {
        do_error_test("EDIT 1", "EDIT takes no arguments");
    }

    #[test]
    fn test_load_ok() {
        let mut store = InMemoryStore::default();
        store.put("foo.bas", "line 1\n\n  line 2\n").unwrap();
        store.put("foo.bak", "").unwrap();
        store.put("BAR.BAS", "line 1\n\n  line 2\n").unwrap();
        store.put("Baz.bas", "line 1\n\n  line 2\n").unwrap();
        let store = Rc::from(RefCell::from(store));

        for p in &["foo", "foo.bas", "BAR", "BAR.BAS", "Baz"] {
            do_ok_test_with_store(
                Rc::from(RefCell::from(RecordedProgram::new(""))),
                store.clone(),
                &("LOAD \"".to_owned() + p + "\""),
                "",
                &[],
                "line 1\n\n  line 2\n",
            );
        }
    }

    /// Checks errors that should be handled the same way by `LOAD` and `SAVE`.
    fn check_load_save_common_errors(cmd: &str, store: Rc<RefCell<dyn Store>>) {
        do_error_test_with_store(store.clone(), &cmd, &format!("{} requires a filename", cmd));
        do_error_test_with_store(
            store.clone(),
            &format!("{} 3", cmd),
            &format!("{} requires a string as the filename", cmd),
        );

        let mut non_basenames = vec!["./foo.bas", "a/b.bas", "a/b"];
        if cfg!(target_os = "windows") {
            non_basenames.push("c:foo.bas");
        }
        for p in non_basenames.as_slice() {
            do_error_test_with_store(
                store.clone(),
                &format!("{} \"{}\"", cmd, p),
                "Filename must be a single path component",
            );
        }

        for p in &["foo.bak", "foo.ba", "foo.basic"] {
            do_error_test_with_store(
                store.clone(),
                &format!("{} \"{}\"", cmd, p),
                "Invalid filename extension",
            );
        }
    }

    #[test]
    fn test_load_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        check_load_save_common_errors("LOAD", store.clone());

        do_error_test_with_store(store.clone(), "LOAD \"missing-file\"", "Entry not found");

        store.borrow_mut().put("mismatched-extension.bat", "").unwrap();
        do_error_test_with_store(store, "LOAD \"mismatched-extension\"", "Entry not found");
    }

    #[test]
    fn test_new_nothing() {
        do_ok_test(Rc::from(RefCell::from(RecordedProgram::new(""))), "NEW", "", &[], "");
    }

    #[test]
    fn test_new_clears_program_and_variables() {
        let program = Rc::from(RefCell::from(RecordedProgram::new("some stuff")));

        let mut machine = MachineBuilder::default()
            .add_builtin(Rc::from(NewCommand { program: program.clone() }))
            .build();

        block_on(machine.exec(&mut b"NEW".as_ref())).unwrap();
        assert!(program.borrow().text().is_empty());
        assert!(machine.get_vars().is_empty());
    }

    #[test]
    fn test_new_errors() {
        do_error_test("NEW 10", "NEW takes no arguments");
    }

    #[test]
    fn test_run_nothing() {
        do_ok_test(Rc::from(RefCell::from(RecordedProgram::new(""))), "RUN", "", &[], "");
    }

    #[test]
    fn test_run_something_that_shares_state() {
        let program = Rc::from(RefCell::from(RecordedProgram::new("OUT var\nvar = var + 1")));

        let captured_out = Rc::from(RefCell::from(vec![]));
        let out_cmd = OutCommand::from(captured_out.clone());
        let mut machine = MachineBuilder::default()
            .add_builtin(Rc::from(out_cmd))
            .add_builtin(Rc::from(RunCommand { program }))
            .build();

        block_on(machine.exec(&mut b"var = 7: RUN".as_ref())).unwrap();
        assert_eq!(&["7"], captured_out.borrow().as_slice());

        captured_out.borrow_mut().clear();
        block_on(machine.exec(&mut b"RUN".as_ref())).unwrap();
        assert_eq!(&["8"], captured_out.borrow().as_slice());
    }

    #[test]
    fn test_run_errors() {
        do_error_test("RUN 10", "RUN takes no arguments");
    }

    #[test]
    fn test_save_ok() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));

        let program = Rc::from(RefCell::from(RecordedProgram::new("line 1\n  line 2\n")));

        for p in &["first", "second.bas", "THIRD", "FOURTH.BAS", "Fifth"] {
            do_ok_test_with_store(
                program.clone(),
                store.clone(),
                &("SAVE \"".to_owned() + p + "\""),
                "",
                &[],
                "line 1\n  line 2\n",
            );
        }

        for p in &["first.bas", "second.bas", "THIRD.BAS", "FOURTH.BAS", "Fifth.bas"] {
            let content = store.borrow().get(p).unwrap();
            assert_eq!(content, "line 1\n  line 2\n");
        }
    }

    #[test]
    fn test_save_errors() {
        let store = Rc::from(RefCell::from(InMemoryStore::default()));
        check_load_save_common_errors("SAVE", store);
    }
}