neovm-core 0.0.2

Core runtime structures for NeoVM
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
use super::*;
use crate::heap_types::LispString;

// -----------------------------------------------------------------------
// BookmarkManager unit tests
// -----------------------------------------------------------------------

fn bm_str(text: &str) -> LispString {
    runtime_string_to_bookmark_string(text)
}

fn bm_runtime(text: Option<&LispString>) -> Option<String> {
    text.map(bookmark_string_to_runtime)
}

#[test]
fn set_get_delete() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();

    let bm = Bookmark {
        name: bm_str("test"),
        filename: Some(bm_str("/tmp/test.txt")),
        position: 42,
        front_context: Some(bm_str("after")),
        rear_context: Some(bm_str("before")),
        annotation: None,
        handler: None,
    };

    mgr.set(bm_str("test"), bm);
    assert!(mgr.get(&bm_str("test")).is_some());
    assert_eq!(mgr.get(&bm_str("test")).unwrap().position, 42);
    assert_eq!(
        bm_runtime(mgr.get(&bm_str("test")).unwrap().filename.as_ref()).as_deref(),
        Some("/tmp/test.txt"),
    );

    assert!(mgr.delete(&bm_str("test")));
    assert!(mgr.get(&bm_str("test")).is_none());
    assert!(!mgr.delete(&bm_str("test"))); // already gone
}

#[test]
fn rename() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();

    let bm = Bookmark {
        name: bm_str("old"),
        filename: None,
        position: 10,
        front_context: None,
        rear_context: None,
        annotation: None,
        handler: None,
    };
    mgr.set(bm_str("old"), bm);

    assert!(mgr.rename(&bm_str("old"), bm_str("new")));
    assert!(mgr.get(&bm_str("old")).is_none());
    assert!(mgr.get(&bm_str("new")).is_some());
    assert_eq!(mgr.get(&bm_str("new")).unwrap().position, 10);
}

#[test]
fn rename_nonexistent() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();
    assert!(!mgr.rename(&bm_str("nope"), bm_str("whatever")));
}

#[test]
fn rename_collision() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();
    let bm1 = Bookmark {
        name: bm_str("a"),
        filename: None,
        position: 1,
        front_context: None,
        rear_context: None,
        annotation: None,
        handler: None,
    };
    let bm2 = Bookmark {
        name: bm_str("b"),
        filename: None,
        position: 2,
        front_context: None,
        rear_context: None,
        annotation: None,
        handler: None,
    };
    mgr.set(bm_str("a"), bm1);
    mgr.set(bm_str("b"), bm2);

    // Cannot rename a -> b when b already exists
    assert!(!mgr.rename(&bm_str("a"), bm_str("b")));

    // Renaming to self is fine
    assert!(mgr.rename(&bm_str("a"), bm_str("a")));
}

#[test]
fn all_names_sorted() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();

    for name in &["zebra", "alpha", "middle"] {
        let bm = Bookmark {
            name: bm_str(name),
            filename: None,
            position: 1,
            front_context: None,
            rear_context: None,
            annotation: None,
            handler: None,
        };
        mgr.set(bm_str(name), bm);
    }

    let names = mgr.all_names();
    assert_eq!(
        names,
        vec![bm_str("alpha"), bm_str("middle"), bm_str("zebra")]
    );
}

#[test]
fn most_recent_tracking() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();

    for name in &["first", "second", "third"] {
        let bm = Bookmark {
            name: bm_str(name),
            filename: None,
            position: 1,
            front_context: None,
            rear_context: None,
            annotation: None,
            handler: None,
        };
        mgr.set(bm_str(name), bm);
    }

    // Most recent should be "third"
    assert_eq!(mgr.recent_names()[0], bm_str("third"));
    assert_eq!(mgr.recent_names()[1], bm_str("second"));
    assert_eq!(mgr.recent_names()[2], bm_str("first"));

    // Re-set "first" -> moves to front
    let bm = Bookmark {
        name: bm_str("first"),
        filename: None,
        position: 99,
        front_context: None,
        rear_context: None,
        annotation: None,
        handler: None,
    };
    mgr.set(bm_str("first"), bm);
    assert_eq!(mgr.recent_names()[0], bm_str("first"));
}

#[test]
fn serialize_deserialize() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();

    let bm1 = Bookmark {
        name: bm_str("alpha"),
        filename: Some(bm_str("/home/test/file.el")),
        position: 100,
        front_context: Some(bm_str("(defun")),
        rear_context: Some(bm_str(";;")),
        annotation: Some(bm_str("Important function")),
        handler: None,
    };
    let bm2 = Bookmark {
        name: bm_str("beta"),
        filename: None,
        position: 1,
        front_context: None,
        rear_context: None,
        annotation: None,
        handler: Some(bm_str("my-handler")),
    };
    mgr.set(bm_str("alpha"), bm1);
    mgr.set(bm_str("beta"), bm2);

    let data = mgr.save_to_string();
    assert!(!data.is_empty());

    // Load into a fresh manager
    let mut mgr2 = BookmarkManager::new();
    mgr2.load_from_string(&data);

    let names = mgr2.all_names();
    assert_eq!(names, vec![bm_str("alpha"), bm_str("beta")]);

    let a = mgr2.get(&bm_str("alpha")).unwrap();
    assert_eq!(a.position, 100);
    assert_eq!(
        bm_runtime(a.filename.as_ref()).as_deref(),
        Some("/home/test/file.el")
    );
    assert_eq!(
        bm_runtime(a.front_context.as_ref()).as_deref(),
        Some("(defun")
    );
    assert_eq!(bm_runtime(a.rear_context.as_ref()).as_deref(), Some(";;"));
    assert_eq!(
        bm_runtime(a.annotation.as_ref()).as_deref(),
        Some("Important function")
    );
    assert!(a.handler.is_none());

    let b = mgr2.get(&bm_str("beta")).unwrap();
    assert_eq!(b.position, 1);
    assert!(b.filename.is_none());
    assert_eq!(
        bm_runtime(b.handler.as_ref()).as_deref(),
        Some("my-handler")
    );
}

#[test]
fn load_empty_string() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();
    let bm = Bookmark {
        name: bm_str("test"),
        filename: None,
        position: 1,
        front_context: None,
        rear_context: None,
        annotation: None,
        handler: None,
    };
    mgr.set(bm_str("test"), bm);

    mgr.load_from_string("");
    assert!(mgr.all_names().is_empty());
}

#[test]
fn modified_flag() {
    crate::test_utils::init_test_tracing();
    let mut mgr = BookmarkManager::new();
    assert!(!mgr.is_modified());

    let bm = Bookmark {
        name: bm_str("test"),
        filename: None,
        position: 1,
        front_context: None,
        rear_context: None,
        annotation: None,
        handler: None,
    };
    mgr.set(bm_str("test"), bm);
    assert!(mgr.is_modified());

    mgr.mark_saved();
    assert!(!mgr.is_modified());

    mgr.delete(&bm_str("test"));
    assert!(mgr.is_modified());
}

// -----------------------------------------------------------------------
// Builtin-level tests
// -----------------------------------------------------------------------

fn set_current_buffer_file(eval: &mut super::super::eval::Context, path: &str) {
    if let Some(buffer) = eval.buffers.current_buffer_mut() {
        buffer.set_file_name_value(Value::string(path));
    }
}

#[test]
fn test_builtin_bookmark_set_and_jump() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/test.el");

    // bookmark-set
    let result = builtin_bookmark_set(&mut eval, vec![Value::string("my-bookmark")]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());

    // bookmark-jump returns alist
    let result = builtin_bookmark_jump(&mut eval, vec![Value::string("my-bookmark")]);
    assert!(result.is_ok());
    let alist = result.unwrap();
    assert!(alist.is_list());

    // bookmark-jump on nonexistent -> error
    let result = builtin_bookmark_jump(&mut eval, vec![Value::string("nope")]);
    assert!(result.is_err());
}

#[test]
fn test_builtin_bookmark_jump_permissive_designators() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();

    // nil designator is a dedicated error in GNU Emacs.
    let nil_result = builtin_bookmark_jump(&mut eval, vec![Value::NIL]);
    assert!(nil_result.is_err());

    // Non-string designators are tolerated and return nil.
    let int_result = builtin_bookmark_jump(&mut eval, vec![Value::fixnum(1)]);
    assert!(int_result.unwrap().is_nil());

    let list_result =
        builtin_bookmark_jump(&mut eval, vec![Value::list(vec![Value::symbol("foo")])]);
    assert!(list_result.unwrap().is_nil());

    // Optional second argument is accepted.
    let missing_with_flag =
        builtin_bookmark_jump(&mut eval, vec![Value::string("missing"), Value::T]);
    assert!(missing_with_flag.is_err());
}

#[test]
fn test_builtin_bookmark_delete() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/delete.el");

    // Set a bookmark
    builtin_bookmark_set(&mut eval, vec![Value::string("del-me")]).unwrap();

    // Delete it (returns nil) and verify side effect.
    let result = builtin_bookmark_delete(&mut eval, vec![Value::string("del-me")]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());
    assert!(eval.bookmarks.get(&bm_str("del-me")).is_none());

    // Delete again -> nil (not found).
    let result = builtin_bookmark_delete(&mut eval, vec![Value::string("del-me")]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());

    // Non-string payloads are accepted and return nil.
    let result = builtin_bookmark_delete(&mut eval, vec![Value::fixnum(1)]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());

    // Optional second argument is accepted.
    let result = builtin_bookmark_delete(&mut eval, vec![Value::fixnum(1), Value::T]);
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());
}

#[test]
fn test_builtin_bookmark_delete_accepts_raw_unibyte_name() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/raw-bookmark.el");
    let raw_name = Value::heap_string(LispString::from_unibyte(vec![0xFF]));

    builtin_bookmark_set(&mut eval, vec![raw_name]).expect("set raw bookmark");
    assert_eq!(eval.bookmarks.all_names().len(), 1);

    builtin_bookmark_delete(&mut eval, vec![raw_name]).expect("delete raw bookmark");
    assert!(eval.bookmarks.all_names().is_empty());
}

#[test]
fn test_builtin_bookmark_rename() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/rename.el");

    builtin_bookmark_set(&mut eval, vec![Value::string("old-name")]).unwrap();

    // Rename
    let result = builtin_bookmark_rename(
        &mut eval,
        vec![Value::string("old-name"), Value::string("new-name")],
    );
    assert!(result.is_ok());

    // Old name gone, new name exists.
    assert!(eval.bookmarks.get(&bm_str("old-name")).is_none());
    assert!(eval.bookmarks.get(&bm_str("new-name")).is_some());
}

#[test]
fn test_builtin_bookmark_rename_permissive_designators() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/rename-permissive.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("old-name")]).unwrap();

    // One-arg calls fall back to prompt behavior in batch mode and error.
    let one_arg = builtin_bookmark_rename(&mut eval, vec![Value::string("old-name")]);
    assert!(one_arg.is_err());

    // Non-cons old payloads signal wrong-type in this compatibility path.
    let ints = builtin_bookmark_rename(&mut eval, vec![Value::fixnum(1), Value::fixnum(2)]);
    assert!(ints.is_err());

    // Cons/list old payloads with non-string NEW are tolerated and return nil.
    let list_ok = builtin_bookmark_rename(
        &mut eval,
        vec![
            Value::list(vec![Value::symbol("a")]),
            Value::list(vec![Value::symbol("b")]),
        ],
    );
    assert!(list_ok.unwrap().is_nil());

    // Cons/list old payloads with string NEW error on invalid bookmark designator.
    let list_str = builtin_bookmark_rename(
        &mut eval,
        vec![
            Value::list(vec![Value::symbol("a")]),
            Value::string("new-name"),
        ],
    );
    assert!(list_str.is_err());

    // String path still renames when the source bookmark exists.
    let rename_ok = builtin_bookmark_rename(
        &mut eval,
        vec![Value::string("old-name"), Value::string("new-name")],
    );
    assert!(rename_ok.is_ok());
    assert!(eval.bookmarks.get(&bm_str("old-name")).is_none());
    assert!(eval.bookmarks.get(&bm_str("new-name")).is_some());
}

#[test]
fn test_builtin_bookmark_all_names() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/all-names.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("z-bookmark")]).unwrap();
    builtin_bookmark_set(&mut eval, vec![Value::string("a-bookmark")]).unwrap();

    let result = builtin_bookmark_all_names(&mut eval, vec![]).unwrap();
    let names = super::super::value::list_to_vec(&result).unwrap();
    assert_eq!(names.len(), 2);
    assert_eq!(names[0].as_utf8_str(), Some("a-bookmark"));
    assert_eq!(names[1].as_utf8_str(), Some("z-bookmark"));
}

#[test]
fn test_builtin_bookmark_get_filename() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/file.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("with-file")]).unwrap();

    let found = builtin_bookmark_get_filename(&mut eval, vec![Value::string("with-file")]).unwrap();
    assert_eq!(found.as_utf8_str(), Some("/tmp/file.el"));

    let missing = builtin_bookmark_get_filename(&mut eval, vec![Value::string("missing")]).unwrap();
    assert!(missing.is_nil());
}

#[test]
fn test_builtin_bookmark_get_position() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/position.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("at-point")]).unwrap();

    let found = builtin_bookmark_get_position(&mut eval, vec![Value::string("at-point")]).unwrap();
    assert_eq!(found.as_int(), Some(0));

    let missing = builtin_bookmark_get_position(&mut eval, vec![Value::string("missing")]).unwrap();
    assert!(missing.is_nil());
}

#[test]
fn test_builtin_bookmark_get_annotation() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/annotation.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("with-note")]).unwrap();
    builtin_bookmark_set_annotation(
        &mut eval,
        vec![Value::string("with-note"), Value::string("note")],
    )
    .unwrap();

    let found =
        builtin_bookmark_get_annotation(&mut eval, vec![Value::string("with-note")]).unwrap();
    assert_eq!(found.as_utf8_str(), Some("note"));

    let missing =
        builtin_bookmark_get_annotation(&mut eval, vec![Value::string("missing")]).unwrap();
    assert!(missing.is_nil());
}

#[test]
fn test_builtin_bookmark_set_annotation() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    set_current_buffer_file(&mut eval, "/tmp/set-annotation.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("entry")]).unwrap();

    let set_result = builtin_bookmark_set_annotation(
        &mut eval,
        vec![Value::string("entry"), Value::string("note")],
    )
    .unwrap();
    assert_eq!(set_result.as_utf8_str(), Some("note"));

    let got = builtin_bookmark_get_annotation(&mut eval, vec![Value::string("entry")]).unwrap();
    assert_eq!(got.as_utf8_str(), Some("note"));

    let missing = builtin_bookmark_set_annotation(
        &mut eval,
        vec![Value::string("missing"), Value::string("note")],
    )
    .unwrap();
    assert!(missing.is_nil());
}

#[test]
fn test_builtin_bookmark_save_load() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();
    let save_file = "/tmp/neovm-bookmark-save-load.data";

    set_current_buffer_file(&mut eval, "/file1.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("bm1")]).unwrap();
    set_current_buffer_file(&mut eval, "/file2.el");
    builtin_bookmark_set(&mut eval, vec![Value::string("bm2")]).unwrap();

    // Save to an explicit file path.
    let result = builtin_bookmark_save(
        &mut eval,
        vec![Value::NIL, Value::string(save_file.to_string())],
    );
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());

    // Clear and load
    eval.bookmarks = BookmarkManager::new();
    let result = builtin_bookmark_load(&mut eval, vec![Value::string(save_file.to_string())]);
    assert!(result.is_ok());
    let load_message = result.unwrap();
    assert_eq!(
        load_message.as_utf8_str(),
        Some("Loading bookmarks from /tmp/neovm-bookmark-save-load.data...done")
    );

    // Verify restored bookmark payloads.
    let bm1 = eval.bookmarks.get(&bm_str("bm1")).expect("bm1 restored");
    assert_eq!(
        bm_runtime(bm1.filename.as_ref()).as_deref(),
        Some("/file1.el")
    );

    let bm2 = eval.bookmarks.get(&bm_str("bm2")).expect("bm2 restored");
    assert_eq!(
        bm_runtime(bm2.filename.as_ref()).as_deref(),
        Some("/file2.el")
    );

    // NO-MSG suppresses the loading message.
    let result = builtin_bookmark_load(
        &mut eval,
        vec![Value::string(save_file.to_string()), Value::NIL, Value::T],
    );
    assert!(result.is_ok());
    assert!(result.unwrap().is_nil());
}

#[test]
fn test_wrong_arg_count() {
    crate::test_utils::init_test_tracing();
    use super::super::eval::Context;

    let mut eval = Context::new();

    // bookmark-set needs between 1 and 2 args.
    let result = builtin_bookmark_set(&mut eval, vec![]);
    assert!(result.is_err());
    let result = builtin_bookmark_set(
        &mut eval,
        vec![Value::string("name"), Value::NIL, Value::NIL],
    );
    assert!(result.is_err());

    // bookmark-jump requires at least one argument.
    let result = builtin_bookmark_jump(&mut eval, vec![]);
    assert!(result.is_err());

    // bookmark-delete requires at least one argument.
    let result = builtin_bookmark_delete(&mut eval, vec![]);
    assert!(result.is_err());

    // bookmark-rename with one arg errors in batch mode.
    let result = builtin_bookmark_rename(&mut eval, vec![Value::string("x")]);
    assert!(result.is_err());
}