MCPcopy Index your code
hub / github.com/RustPython/RustPython / str_maketrans_and_translate

Function str_maketrans_and_translate

crates/vm/src/builtins/str.rs:2669–2688  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2667
2668 #[test]
2669 fn str_maketrans_and_translate() {
2670 Interpreter::without_stdlib(Default::default()).enter(|vm| {
2671 let table = vm.ctx.new_dict();
2672 table
2673 .set_item("a", vm.ctx.new_str("🎅").into(), vm)
2674 .unwrap();
2675 table.set_item("b", vm.ctx.none(), vm).unwrap();
2676 table
2677 .set_item("c", vm.ctx.new_str(ascii!("xda")).into(), vm)
2678 .unwrap();
2679 let translated =
2680 PyStr::maketrans(table.into(), OptionalArg::Missing, OptionalArg::Missing, vm)
2681 .unwrap();
2682 let text = PyStr::from("abc");
2683 let translated = text.translate(translated, vm).unwrap();
2684 assert_eq!(translated, Wtf8Buf::from("🎅xda"));
2685 let translated = text.translate(vm.ctx.new_int(3).into(), vm);
2686 assert_eq!("TypeError", &*translated.unwrap_err().class().name(),);
2687 })
2688 }
2689}

Callers

nothing calls this directly

Calls 8

new_dictMethod · 0.80
noneMethod · 0.80
enterMethod · 0.45
unwrapMethod · 0.45
set_itemMethod · 0.45
new_strMethod · 0.45
translateMethod · 0.45
new_intMethod · 0.45

Tested by

no test coverage detected