bop-lang 0.3.0

A small, embeddable, dynamically-typed programming language with zero dependencies
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
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
//! Smoke tests for the bundled stdlib modules. Each module is
//! resolved through `bop::stdlib::resolve` and executed against the
//! tree-walker with a minimal host that:
//!
//! - captures `print` output so tests can assert on it
//! - routes `use std.*` back through `bop::stdlib::resolve` so
//!   stdlib modules importing each other work the same way
//!   `StandardHost` handles them.
//!
//! Keeping these smoke tests in `bop-lang` itself means refactors to the
//! `.bop` sources break cleanly inside this crate rather than
//! trickling into `bop-cli` or embedder tests downstream.

use std::cell::RefCell;

use bop::{BopError, BopHost, BopLimits, Value};

struct BufHost {
    prints: RefCell<Vec<String>>,
}

impl BufHost {
    fn new() -> Self {
        Self {
            prints: RefCell::new(Vec::new()),
        }
    }

    fn last(&self) -> String {
        self.prints
            .borrow()
            .last()
            .cloned()
            .expect("expected at least one print")
    }
}

impl BopHost for BufHost {
    fn call(&mut self, _name: &str, _args: &[Value], _line: u32) -> Option<Result<Value, BopError>> {
        None
    }

    fn on_print(&mut self, msg: &str) {
        self.prints.borrow_mut().push(msg.to_string());
    }

    fn resolve_module(&mut self, name: &str) -> Option<Result<String, BopError>> {
        bop::stdlib::resolve(name).map(|src| Ok(src.to_string()))
    }
}

fn run(code: &str) -> BufHost {
    let mut host = BufHost::new();
    bop::run(code, &mut host, &BopLimits::standard())
        .unwrap_or_else(|e| panic!("program errored: {}\n\n{}", e, code));
    host
}

// ─── Result methods ───────────────────────────────────────────
//
// `Result` is a built-in enum; its combinators are engine-level
// methods dispatched via `methods::result_method` and the per-
// engine callable helpers. No `use std.result` required.

#[test]
fn result_is_ok_and_is_err() {
    let host = run(
        r#"print(Result::Ok(1).is_ok())
print(Result::Err("boom").is_err())
print(Result::Err("x").is_ok())"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["true", "true", "false"]);
}

#[test]
fn result_unwrap_or_returns_default_on_err() {
    let host = run(
        r#"print(Result::Ok(42).unwrap_or(0))
print(Result::Err("boom").unwrap_or(99))"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["42", "99"]);
}

#[test]
fn result_map_applies_only_to_ok() {
    let host = run(
        r#"let doubled = Result::Ok(5).map(fn(x) { return x * 2 })
print(match doubled {
    Result::Ok(v) => v,
    Result::Err(_) => -1,
})
let passed = Result::Err("stop").map(fn(x) { return x * 2 })
print(match passed {
    Result::Ok(_) => "ok?",
    Result::Err(e) => e,
})"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["10", "stop"]);
}

#[test]
fn result_map_err_applies_only_to_err() {
    let host = run(
        r#"let tagged = Result::Err("bad").map_err(fn(e) { return e + "!" })
print(match tagged {
    Result::Ok(_)  => "ok?",
    Result::Err(e) => e,
})
let passed = Result::Ok(5).map_err(fn(e) { return e + "!" })
print(match passed {
    Result::Ok(v)  => v,
    Result::Err(_) => -1,
})"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["bad!", "5"]);
}

#[test]
fn result_and_then_chains_fallible_steps() {
    let host = run(
        r#"fn halve(x) {
    if x % 2 == 0 { return Result::Ok((x / 2).to_int()) }
    return Result::Err("odd")
}
let r = Result::Ok(8).and_then(halve).and_then(halve)
print(match r { Result::Ok(v) => v, Result::Err(e) => e })
let bad = Result::Ok(7).and_then(halve)
print(match bad { Result::Ok(_) => "ok?", Result::Err(e) => e })"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["2", "odd"]);
}

#[test]
fn result_unwrap_on_err_raises_with_message() {
    // `unwrap` / `expect` raise a runtime error carrying the
    // inspected payload (or caller-supplied message). `try_call`
    // catches it so the test can observe `e.message` directly.
    let host = run(
        r#"let r = try_call(fn() { return Result::Err("boom").unwrap() })
print(match r {
    Result::Ok(_)  => "unexpected ok",
    Result::Err(e) => e.message,
})
let r2 = try_call(fn() { return Result::Err("bad").expect("custom message") })
print(match r2 {
    Result::Ok(_)  => "unexpected ok",
    Result::Err(e) => e.message,
})"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["unwrap on Err: \"boom\"", "custom message"],
    );
}

#[test]
fn result_methods_work_without_any_use() {
    // The whole point of moving these off `std.result` was to
    // make them always available — no imports, no host resolver
    // involvement, just method calls on the built-in `Result`.
    let host = run(
        r#"let mapped = Result::Ok(10).map(fn(v) { return v + 1 })
print(mapped)
print(Result::Err("x").is_err())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["Result::Ok(11)", "true"],
    );
}

// ─── Dict missing-key lookup ──────────────────────────────────
//
// `d[key]` for an absent key returns `none` rather than raising.
// Matches Python / JS / Lua — composes naturally with the
// `.is_none()` / `.is_some()` check, and aligns with the
// language's "any variable can be `none`" story. Callers who
// need strict presence checks use `d.has(key)` explicitly.

#[test]
fn dict_missing_key_returns_none() {
    let host = run(
        r#"let d = {"hp": 10}
print(d["hp"])
print(d["missing"])
print(d["missing"].is_none())
print(d["missing"].is_some())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["10", "none", "true", "false"],
    );
}

#[test]
fn dict_missing_key_distinct_from_explicit_none_value() {
    // A key mapped to an actual `none` value is still "present"
    // in the sense that `d.has(k)` reports `true`; the soft
    // lookup can't distinguish the two (both yield `none`),
    // which matches Python's default behaviour and keeps the
    // primitive simple. Callers who care reach for `d.has(k)`.
    let host = run(
        r#"let d = {"set": none}
print(d["set"])
print(d["set"].is_none())
print(d["absent"].is_none())
print(d.has("set"))
print(d.has("absent"))"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["none", "true", "true", "true", "false"],
    );
}

#[test]
fn dict_assign_to_missing_key_creates_entry() {
    // Regression: the soft-read behaviour must not affect
    // writes. Assigning to an absent key still inserts it.
    let host = run(
        r#"let d = {}
d["new"] = 42
print(d["new"])
print(d.has("new"))"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["42", "true"],
    );
}

// ─── is_none / is_some universal methods ──────────────────────
//
// Any value can be checked for `none`-ness via `.is_none()` /
// `.is_some()`. They're common methods (alongside `.type()` /
// `.to_str()` / `.inspect()`) because every variable, regardless
// of declared shape, can hold `none` in a dynamically-typed
// language — not just `Option`-shaped receivers.

#[test]
fn is_none_detects_only_the_none_value() {
    // Truthy / falsy values *other* than `none` are not none.
    // `is_none()` is specifically "is this the Value::None
    // variant?", not "is this falsy?".
    let host = run(
        r#"print(none.is_none())
print((42).is_none())
print("".is_none())
print([].is_none())
print({}.is_none())
print(false.is_none())
print((0).is_none())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["true", "false", "false", "false", "false", "false", "false"],
    );
}

#[test]
fn is_some_is_the_inverse_of_is_none() {
    let host = run(
        r#"print(none.is_some())
print((0).is_some())
print("".is_some())
print(false.is_some())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["false", "true", "true", "true"],
    );
}

#[test]
fn is_none_works_on_optional_return_values() {
    // Motivating use case: a helper that returns `none` for
    // "nothing found" composes cleanly with `.is_none()` at
    // the call site without a `== none` detour.
    let host = run(
        r#"fn first_or_none(arr) {
    if arr.len() == 0 { return none }
    return arr[0]
}
let a = first_or_none([])
let b = first_or_none([10, 20])
if a.is_none() { print("empty") }
if b.is_some() { print("got: " + b.to_str()) }"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["empty", "got: 10"],
    );
}

#[test]
fn is_none_equivalent_to_equality_check() {
    // `.is_none()` and `== none` must agree on every value.
    let host = run(
        r#"let vs = [none, 0, "", [], false, 42]
for v in vs { print(v.is_none() == (v == none)) }"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["true", "true", "true", "true", "true", "true"],
    );
}

// ─── Ok / Err shorthand ────────────────────────────────────────
//
// `Ok(x)` / `Err(e)` are parser-level sugar for
// `Result::Ok(x)` / `Result::Err(e)`. Both the expression form
// and the pattern form get the desugar so match arms stay
// readable and returns stay short.

#[test]
fn ok_err_expression_sugar_produces_result_variants() {
    let host = run(
        r#"print(Ok(42))
print(Err("boom"))
print(Ok("nested").is_ok())
print(Err(123).is_err())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec![
            "Result::Ok(42)",
            "Result::Err(\"boom\")",
            "true",
            "true",
        ],
    );
}

#[test]
fn ok_err_pattern_sugar_matches_result_variants() {
    // Pattern side — `match r { Ok(v) => ..., Err(e) => ... }`
    // desugars to `Result::Ok(v)` / `Result::Err(e)`.
    let host = run(
        r#"fn describe(r) {
    return match r {
        Ok(v) => "ok: " + v.to_str(),
        Err(e) => "err: " + e,
    }
}
print(describe(Ok(5)))
print(describe(Err("stop")))"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["ok: 5", "err: stop"],
    );
}

#[test]
fn ok_err_shorthand_chains_through_try_call() {
    // The shorthand plays nicely with `try_call` since the
    // desugared value is an actual `Result::Err(RuntimeError
    // { ... })` — but `Err("x")` produces a string payload, so
    // match arms decide per payload shape.
    let host = run(
        r#"fn fail() { return Err("explicit") }
let r = try_call(fail)
print(match r {
    Ok(inner) => match inner {
        Ok(_)  => "double ok?",
        Err(e) => "inner err: " + e,
    },
    Err(_) => "caught",
})"#,
    );
    // `fail()` returns `Err("explicit")` — a successful return,
    // so `try_call` wraps it in `Result::Ok(Result::Err(...))`.
    // Inner match picks it up as `Err(e)`.
    assert_eq!(host.prints.borrow().clone(), vec!["inner err: explicit"]);
}

// ─── Iterator protocol ─────────────────────────────────────────
//
// `.iter()` on arrays / strings / dicts yields a `Value::Iter`
// whose `.next()` returns `Iter::Next(v)` / `Iter::Done`. User
// types that implement `.iter()` participate in the same
// protocol so `for x in my_container` works out of the box.

#[test]
fn iter_array_next_returns_iter_next_until_done() {
    let host = run(
        r#"let it = [10, 20, 30].iter()
print(it.type())
print(it.next())
print(it.next())
print(it.next())
print(it.next())
print(it.next())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec![
            "iter",
            "Iter::Next(10)",
            "Iter::Next(20)",
            "Iter::Next(30)",
            "Iter::Done",
            "Iter::Done",
        ],
    );
}

#[test]
fn iter_string_yields_code_points() {
    // Strings iterate by Unicode code point — each yielded item
    // is a 1-char string, matching `for c in s` semantics.
    let host = run(
        r#"let it = "ab".iter()
print(it.next())
print(it.next())
print(it.next())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["Iter::Next(\"a\")", "Iter::Next(\"b\")", "Iter::Done"],
    );
}

#[test]
fn iter_dict_yields_keys_in_declaration_order() {
    let host = run(
        r#"let it = {"a": 1, "b": 2}.iter()
print(it.next())
print(it.next())
print(it.next())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["Iter::Next(\"a\")", "Iter::Next(\"b\")", "Iter::Done"],
    );
}

#[test]
fn iter_is_its_own_iterator() {
    // `iterator.iter()` returns the same iterator — matches the
    // Python / Rust convention so `for x in arr.iter()` doesn't
    // need a special case in the for-loop dispatcher.
    let host = run(
        r#"let a = [1, 2].iter()
let b = a.iter()
print(a.next())
print(b.next())
print(a.next())"#,
    );
    // `b` shares state with `a` — cloning `Value::Iter` bumps an
    // Rc, it doesn't fork the cursor.
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["Iter::Next(1)", "Iter::Next(2)", "Iter::Done"],
    );
}

#[test]
fn for_in_works_on_value_iter() {
    // `for x in arr.iter()` uses the protocol path, not the
    // eager one. Must produce the same output as `for x in arr`.
    let host = run(
        r#"for x in [1, 2, 3].iter() { print(x) }"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["1", "2", "3"],
    );
}

#[test]
fn for_in_supports_user_container_via_iter_method() {
    // A user container that delegates `.iter()` to a backing
    // array is the motivating use case for the protocol:
    // structural typing without a trait system. `for v in b`
    // calls `Bag.iter(b)` → backing array iterator → loops.
    let host = run(
        r#"struct Bag { items }
fn bag_of(arr) { return Bag { items: arr } }
fn Bag.iter(self) { return self.items.iter() }

let b = bag_of(["x", "y", "z"])
for v in b { print(v) }"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["x", "y", "z"],
    );
}

#[test]
fn for_in_on_dict_iterates_keys() {
    // Dict for-loops now materialise the keys up front; the
    // eager fast path stays equivalent to `for k in d.iter()`.
    let host = run(
        r#"for k in {"alpha": 1, "beta": 2} { print(k) }"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["alpha", "beta"],
    );
}

#[test]
fn for_in_on_primitive_raises_cant_iterate_error() {
    let mut host = BufHost::new();
    let err = bop::run("for x in 42 { print(x) }", &mut host, &BopLimits::standard())
        .expect_err("expected for-loop over int to fail");
    let msg = err.to_string();
    assert!(
        msg.contains("Can't iterate over int"),
        "got: {}",
        msg
    );
}

// ─── panic builtin ─────────────────────────────────────────────

#[test]
fn panic_raises_non_fatal_runtime_error() {
    // Bare `panic(msg)` raises with the message verbatim, and the
    // resulting error is non-fatal so `try_call` catches it.
    let host = run(
        r#"let r = try_call(fn() { panic("deliberate") })
print(match r {
    Result::Ok(_)  => "unexpected ok",
    Result::Err(e) => e.message,
})"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["deliberate"]);
}

#[test]
fn panic_propagates_when_not_caught() {
    // Outside of `try_call`, a `panic` should bubble up as a
    // top-level program error. The `run` helper's `unwrap_or_else`
    // would blow up on success, so we reach for `bop::run`
    // directly and assert on the error surface.
    let mut host = BufHost::new();
    let err = bop::run("panic(\"top-level\")", &mut host, &BopLimits::standard())
        .expect_err("expected panic to surface as an error");
    assert!(
        err.to_string().contains("top-level"),
        "panic message should appear verbatim in the error: {}",
        err
    );
}

// ─── std.test — exercises the assertion surface directly ──────

#[test]
fn test_assert_eq_failure_carries_inspected_values() {
    // `assert_eq` (and siblings) now route through `panic` so
    // the user sees a readable message in `Err(e).message`
    // rather than "Can't index none with string".
    let host = run(
        r#"use std.test
let r = try_call(fn() { assert_eq(1, 2) })
print(match r {
    Result::Ok(_)  => "unexpected ok",
    Result::Err(e) => e.message,
})"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["assert_eq failed: 1 != 2"],
    );
}

// ─── std.math ──────────────────────────────────────────────────

#[test]
fn math_constants_have_expected_precision() {
    let host = run(
        r#"use std.math
print(PI)
print(E)
print(TAU)"#,
    );
    let prints = host.prints.borrow();
    assert!(prints[0].starts_with("3.14159"));
    assert!(prints[1].starts_with("2.71828"));
    assert!(prints[2].starts_with("6.28318"));
}

#[test]
fn math_clamp_bounds_correctly() {
    let host = run(
        r#"use std.math
print(clamp(5, 0, 10))
print(clamp(-3, 0, 10))
print(clamp(20, 0, 10))"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["5", "0", "10"]);
}

#[test]
fn math_sign_handles_zero_and_negatives() {
    let host = run(
        r#"use std.math
print(sign(5))
print(sign(-3))
print(sign(0))"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["1", "-1", "0"]);
}

#[test]
fn math_factorial_and_gcd_and_lcm() {
    let host = run(
        r#"use std.math
print(factorial(5))
print(gcd(12, 18))
print(lcm(4, 6))"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["120", "6", "12"]);
}

#[test]
fn math_mean_empty_check() {
    // `mean` lives in `std.math`. `sum` used to live here
    // too; it was a duplicate of `std.iter.sum`, so it moved
    // out and `mean` now inlines its own running total.
    let host = run(
        r#"use std.math
print(mean([2, 4, 6]))
print(mean([10, 20, 30, 40, 50]))"#,
    );
    assert_eq!(host.prints.borrow().clone(), vec!["4", "30"]);
}

// ─── std.iter ──────────────────────────────────────────────────

#[test]
fn iter_map_filter_reduce() {
    let host = run(
        r#"use std.iter
let nums = [1, 2, 3, 4, 5]
let doubled = map(nums, fn(x) { return x * 2 })
print(doubled)
let evens = filter(nums, fn(x) { return x % 2 == 0 })
print(evens)
let total = reduce(nums, 0, fn(a, b) { return a + b })
print(total)"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "[2, 4, 6, 8, 10]");
    assert_eq!(prints[1], "[2, 4]");
    assert_eq!(prints[2], "15");
}

#[test]
fn iter_take_and_drop() {
    let host = run(
        r#"use std.iter
print(take([10, 20, 30, 40], 2))
print(drop([10, 20, 30, 40], 2))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "[10, 20]");
    assert_eq!(prints[1], "[30, 40]");
}

#[test]
fn iter_zip_and_enumerate() {
    let host = run(
        r#"use std.iter
print(zip([1, 2, 3], ["a", "b", "c"]))
print(enumerate(["x", "y"]))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "[[1, \"a\"], [2, \"b\"], [3, \"c\"]]");
    assert_eq!(prints[1], "[[0, \"x\"], [1, \"y\"]]");
}

#[test]
fn iter_any_all_count() {
    let host = run(
        r#"use std.iter
let is_pos = fn(x) { return x > 0 }
print(all([1, 2, 3], is_pos))
print(all([1, -2, 3], is_pos))
print(any([-1, -2, 3], is_pos))
print(count([1, 2, 3, 4], fn(x) { return x % 2 == 0 }))"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["true", "false", "true", "2"]
    );
}

#[test]
fn iter_find_and_find_index() {
    let host = run(
        r#"use std.iter
print(find([1, 2, 3], fn(x) { return x > 1 }))
print(find_index([1, 2, 3], fn(x) { return x > 1 }))
print(find([1, 2, 3], fn(x) { return x > 99 }))
print(find_index([1, 2, 3], fn(x) { return x > 99 }))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "2");
    assert_eq!(prints[1], "1");
    assert_eq!(prints[2], "none");
    assert_eq!(prints[3], "-1");
}

#[test]
fn iter_flatten_sum_product() {
    let host = run(
        r#"use std.iter
print(flatten([[1, 2], [3, 4], [5]]))
print(sum([1, 2, 3, 4]))
print(product([2, 3, 4]))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "[1, 2, 3, 4, 5]");
    assert_eq!(prints[1], "10");
    assert_eq!(prints[2], "24");
}

#[test]
fn iter_min_and_max() {
    let host = run(
        r#"use std.iter
print(min_array([5, 3, 8, 1, 4]))
print(max_array([5, 3, 8, 1, 4]))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "1");
    assert_eq!(prints[1], "8");
}

// ─── std.string ────────────────────────────────────────────────

#[test]
fn string_pad_left_right_and_center() {
    let host = run(
        r#"use std.string
print(pad_left("42", 5, " "))
print(pad_right("42", 5, "0"))
print(center("hi", 6, "-"))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "   42");
    assert_eq!(prints[1], "42000");
    assert_eq!(prints[2], "--hi--");
}

#[test]
fn string_chars_reverse_palindrome() {
    let host = run(
        r#"use std.string
print(chars("abc"))
print(reverse("hello"))
print(is_palindrome("racecar"))
print(is_palindrome("hello"))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "[\"a\", \"b\", \"c\"]");
    assert_eq!(prints[1], "olleh");
    assert_eq!(prints[2], "true");
    assert_eq!(prints[3], "false");
}

#[test]
fn string_count_substrings() {
    let host = run(
        r#"use std.string
print(count("banana", "na"))
print(count("aaaa", "aa"))
print(count("empty", ""))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "2");
    // Non-overlapping: "aaaa" → "aa" matches at 0 and 2, count = 2.
    assert_eq!(prints[1], "2");
    assert_eq!(prints[2], "0");
}

// ─── std.test ──────────────────────────────────────────────────

#[test]
fn test_asserts_pass_silently() {
    // Successful asserts shouldn't produce any output or
    // error out.
    let host = run(
        r#"use std.test
assert(true, "should pass")
assert_eq(1 + 1, 2)
assert_near(0.1 + 0.2, 0.3, 0.01)
print("all good")"#,
    );
    assert_eq!(host.last(), "all good");
}

#[test]
fn test_assert_eq_failure_raises() {
    let mut host = BufHost::new();
    let err = bop::run(
        r#"use std.test
assert_eq(1, 2)"#,
        &mut host,
        &BopLimits::standard(),
    )
    .expect_err("assert_eq should raise");
    assert!(err.message.len() > 0, "got empty error message");
}

#[test]
fn test_assert_raises_catches_expected_failure() {
    let host = run(
        r#"use std.test
assert_raises(fn() { return 1 / 0 })
print("caught")"#,
    );
    assert_eq!(host.last(), "caught");
}

// ─── Core math builtins (no use needed) ────────────────────

#[test]
fn core_math_builtins_available_without_import() {
    let host = run(
        r#"print(16.sqrt())
print(3.7.floor())
print(3.2.ceil())
print(3.5.round())
print(2.pow(10))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "4");
    assert_eq!(prints[1], "3");
    assert_eq!(prints[2], "4");
    assert_eq!(prints[3], "4");
    assert_eq!(prints[4], "1024");
}

#[test]
fn core_math_floor_ceil_round_return_int_when_possible() {
    let host = run(
        r#"print(3.7.floor().type())
print(3.2.ceil().type())
print(3.5.round().type())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["int", "int", "int"]
    );
}

// ─── std.collections ──────────────────────────────────────────

#[test]
fn collections_stack_push_top_pop() {
    let host = run(
        r#"use std.collections
let s = stack()
s = s.push(1)
s = s.push(2)
s = s.push(3)
print(s.top())
print(s.size())
s = s.pop()
print(s.top())
print(s.size())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["3", "3", "2", "2"]
    );
}

#[test]
fn collections_stack_pop_empty_is_noop() {
    let host = run(
        r#"use std.collections
let s = stack()
s = s.pop()
print(s.is_empty())
print(s.top())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["true", "none"]
    );
}

#[test]
fn collections_queue_fifo_order() {
    let host = run(
        r#"use std.collections
let q = queue()
q = q.enqueue("a")
q = q.enqueue("b")
q = q.enqueue("c")
print(q.front())
print(q.size())
q = q.dequeue()
print(q.front())
q = q.dequeue()
print(q.front())
q = q.dequeue()
print(q.is_empty())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["a", "3", "b", "c", "true"]
    );
}

#[test]
fn collections_set_add_remove_has() {
    let host = run(
        r#"use std.collections
let s = set()
s = s.add(1)
s = s.add(2)
s = s.add(2)  // duplicate, no-op
s = s.add(3)
print(s.size())
print(s.has(2))
print(s.has(99))
s = s.remove(2)
print(s.has(2))
print(s.size())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["3", "true", "false", "false", "2"]
    );
}

#[test]
fn collections_set_of_handles_duplicates() {
    let host = run(
        r#"use std.collections
let s = set_of([1, 2, 1, 3, 2])
print(s.size())
print(s.values())"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "3");
    assert_eq!(prints[1], "[1, 2, 3]");
}

#[test]
fn collections_set_union_intersect_difference() {
    let host = run(
        r#"use std.collections
let a = set_of([1, 2, 3])
let b = set_of([2, 3, 4])
print(a.union(b).values())
print(a.intersect(b).values())
print(a.difference(b).values())"#,
    );
    let prints = host.prints.borrow();
    // Union: [1,2,3,4]. intersect: [2,3]. difference: [1].
    assert_eq!(prints[0], "[1, 2, 3, 4]");
    assert_eq!(prints[1], "[2, 3]");
    assert_eq!(prints[2], "[1]");
}

#[test]
fn collections_remove_preserves_order() {
    let host = run(
        r#"use std.collections
let s = set_of([1, 2, 3, 4, 5])
s = s.remove(3)
print(s.values())"#,
    );
    assert_eq!(host.prints.borrow()[0], "[1, 2, 4, 5]");
}

// ─── std.json ──────────────────────────────────────────────────

#[test]
fn json_stringify_scalars_and_null() {
    let host = run(
        r#"use std.json
print(stringify(none))
print(stringify(true))
print(stringify(false))
print(stringify(42))
print(stringify(3.14))
print(stringify("hello"))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "null");
    assert_eq!(prints[1], "true");
    assert_eq!(prints[2], "false");
    assert_eq!(prints[3], "42");
    assert_eq!(prints[4], "3.14");
    assert_eq!(prints[5], "\"hello\"");
}

#[test]
fn json_stringify_escapes_special_chars() {
    // Source has a literal newline inside a quoted string in
    // Bop — the `\n` escape is recognised at lex time.
    let host = run(
        r#"use std.json
print(stringify("a\"b"))
print(stringify("a\\b"))
print(stringify("a\nb"))
print(stringify("a\tb"))"#,
    );
    let prints = host.prints.borrow();
    // Each assertion compares against the literal JSON bytes
    // the stringify helper emits.
    assert_eq!(prints[0], "\"a\\\"b\"");
    assert_eq!(prints[1], "\"a\\\\b\"");
    assert_eq!(prints[2], "\"a\\nb\"");
    assert_eq!(prints[3], "\"a\\tb\"");
}

#[test]
fn json_stringify_array() {
    let host = run(
        r#"use std.json
print(stringify([1, 2, 3]))
print(stringify([]))
print(stringify(["a", 1, true, none]))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "[1,2,3]");
    assert_eq!(prints[1], "[]");
    assert_eq!(prints[2], "[\"a\",1,true,null]");
}

#[test]
fn json_stringify_dict() {
    let host = run(
        r#"use std.json
let d = {"name": "bop", "version": 1}
print(stringify(d))"#,
    );
    // Dict iteration order is insertion order in Bop.
    assert_eq!(
        host.prints.borrow()[0],
        "{\"name\":\"bop\",\"version\":1}"
    );
}

#[test]
fn json_parse_scalars() {
    let host = run(
        r#"use std.json
print(parse("null"))
print(parse("true"))
print(parse("false"))
print(parse("42"))
print(parse("3.14"))
print(parse("-7"))
print(parse("\"hello\""))"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "none");
    assert_eq!(prints[1], "true");
    assert_eq!(prints[2], "false");
    assert_eq!(prints[3], "42");
    assert_eq!(prints[4], "3.14");
    assert_eq!(prints[5], "-7");
    assert_eq!(prints[6], "hello");
}

#[test]
fn json_parse_number_types() {
    // `42` is int; `42.0` / `1e2` are numbers.
    let host = run(
        r#"use std.json
print(parse("42").type())
print(parse("42.0").type())
print(parse("1e2").type())
print(parse("-3").type())"#,
    );
    assert_eq!(
        host.prints.borrow().clone(),
        vec!["int", "number", "number", "int"]
    );
}

#[test]
fn json_parse_array() {
    let host = run(
        r#"use std.json
let arr = parse("[1, 2, 3]")
print(arr)
print(arr.len())
let empty = parse("[]")
print(empty.len())"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "[1, 2, 3]");
    assert_eq!(prints[1], "3");
    assert_eq!(prints[2], "0");
}

#[test]
fn json_parse_object_indexes_by_key() {
    let host = run(
        r#"use std.json
let o = parse("{\"name\": \"bop\", \"n\": 42}")
print(o["name"])
print(o["n"])"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "bop");
    assert_eq!(prints[1], "42");
}

#[test]
fn json_parse_nested_structures() {
    let host = run(
        r#"use std.json
let txt = "{\"users\": [{\"name\": \"a\"}, {\"name\": \"b\"}]}"
let data = parse(txt)
print(data["users"].len())
print(data["users"][0]["name"])
print(data["users"][1]["name"])"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "2");
    assert_eq!(prints[1], "a");
    assert_eq!(prints[2], "b");
}

#[test]
fn json_parse_handles_whitespace() {
    let host = run(
        r#"use std.json
let txt = "  [  1 ,  2 , 3  ]  "
let arr = parse(txt)
print(arr)"#,
    );
    assert_eq!(host.prints.borrow()[0], "[1, 2, 3]");
}

#[test]
fn json_parse_string_escapes() {
    let host = run(
        r#"use std.json
let s = parse("\"a\\\"b\\nc\"")
print(s)
print(s.len())"#,
    );
    let prints = host.prints.borrow();
    // a, ", b, \n, c — 5 chars.
    assert_eq!(prints[1], "5");
    assert!(prints[0].contains("\"b"));
}

#[test]
fn json_parse_roundtrip_via_stringify() {
    // stringify(parse(x)) should produce semantically the
    // same data; whitespace may differ.
    let host = run(
        r#"use std.json
let original = {"name": "bop", "ok": true, "vals": [1, 2, 3]}
let dumped = stringify(original)
print(dumped)
let roundtrip = parse(dumped)
print(roundtrip["name"])
print(roundtrip["ok"])
print(roundtrip["vals"])"#,
    );
    let prints = host.prints.borrow();
    assert_eq!(prints[0], "{\"name\":\"bop\",\"ok\":true,\"vals\":[1,2,3]}");
    assert_eq!(prints[1], "bop");
    assert_eq!(prints[2], "true");
    assert_eq!(prints[3], "[1, 2, 3]");
}

#[test]
fn json_parse_error_raises_and_try_call_catches() {
    let host = run(
        r#"use std.json
let r = try_call(fn() { return parse("[1, 2,") })
print(match r {
    Result::Ok(_) => "ok?",
    Result::Err(e) => "caught",
})"#,
    );
    assert_eq!(host.prints.borrow()[0], "caught");
}

#[test]
fn json_parse_empty_input_errors() {
    let host = run(
        r#"use std.json
let r = try_call(fn() { return parse("   ") })
print(match r {
    Result::Ok(_) => "ok?",
    Result::Err(_) => "err",
})"#,
    );
    assert_eq!(host.prints.borrow()[0], "err");
}

#[test]
fn collections_composes_with_std_iter() {
    // Collections + iter helpers interoperate because a Set's
    // .values() / a Queue's items are ordinary arrays.
    let host = run(
        r#"use std.collections
use std.iter
let s = set_of([1, 2, 3, 4, 5])
let evens = filter(s.values(), fn(x) { return x % 2 == 0 })
print(evens)"#,
    );
    assert_eq!(host.prints.borrow()[0], "[2, 4]");
}