Sha256: d016a838e9cd821006168a1243cdc73d5371dd9dc6aabeeab81c3f677481201e

Contents?: true

Size: 1.03 KB

Versions: 19

Compression:

Stored size: 1.03 KB

Contents

use magnus::{block::Yield, eval, method, prelude::*, rb_assert, Ruby, Value};

fn count_to_3(ruby: &Ruby, rb_self: Value) -> Yield<impl Iterator<Item = u8>> {
    if ruby.block_given() {
        Yield::Iter((1..=3).into_iter())
    } else {
        Yield::Enumerator(rb_self.enumeratorize("count_to_3", ()))
    }
}

#[test]
fn it_converts_iterator_to_yields() {
    let ruby = unsafe { magnus::embed::init() };

    ruby.define_global_function("count_to_3", method!(count_to_3, 0));

    let a = ruby.ary_new();
    let _: Value = eval!(
        ruby,
        "
        count_to_3 do |i|
          a << i
        end
        ",
        a
    )
    .unwrap();
    rb_assert!(ruby, "a == [1,2,3]", a);

    let enumerator: Value = eval!(
        ruby,
        "
        def raises
          yield
          false
        rescue StopIteration
          true
        end
        count_to_3
        "
    )
    .unwrap();
    rb_assert!(ruby, "enumerator.next == 1 && enumerator.next == 2 && enumerator.next == 3 && raises { enumerator.next }", enumerator);
}

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/return_iter.rs
wasmtime-28.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/return_iter.rs
wasmtime-27.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/return_iter.rs
wasmtime-26.0.0 ./ext/cargo-vendor/magnus-0.7.1/tests/return_iter.rs
wasmtime-25.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-25.0.1 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-25.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-24.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-23.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-22.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-21.0.1 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-20.0.2 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-20.0.0 ./ext/cargo-vendor/magnus-0.6.4/tests/return_iter.rs
wasmtime-18.0.3 ./ext/cargo-vendor/magnus-0.6.2/tests/return_iter.rs
wasmtime-17.0.1 ./ext/cargo-vendor/magnus-0.6.2/tests/return_iter.rs
wasmtime-17.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/return_iter.rs
wasmtime-16.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/return_iter.rs
wasmtime-15.0.1 ./ext/cargo-vendor/magnus-0.6.2/tests/return_iter.rs
wasmtime-15.0.0 ./ext/cargo-vendor/magnus-0.6.2/tests/return_iter.rs