Sha256: 60fe97ee8396e880ab46f5818a1bf76fa983d03155a651e653fa8fcbd9141810

Contents?: true

Size: 1.94 KB

Versions: 19

Compression:

Stored size: 1.94 KB

Contents

use magnus::{
    eval, function, gc, method, prelude::*, typed_data, value::Opaque, DataTypeFunctions,
    TypedData, Value,
};

#[derive(TypedData, Clone)]
#[magnus(class = "Pair", free_immediately, mark)]
struct Pair {
    #[magnus(opaque_attr_reader)]
    a: Opaque<Value>,
    #[magnus(opaque_attr_reader)]
    b: Opaque<Value>,
}

impl Pair {
    fn new(a: Value, b: Value) -> Self {
        Self {
            a: a.into(),
            b: b.into(),
        }
    }
}

impl DataTypeFunctions for Pair {
    fn mark(&self, marker: &gc::Marker) {
        marker.mark(self.a);
        marker.mark(self.b);
    }
}

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

    let class = ruby.define_class("Pair", ruby.class_object()).unwrap();
    class
        .define_singleton_method("new", function!(Pair::new, 2))
        .unwrap();
    class
        .define_method("dup", method!(<Pair as typed_data::Dup>::dup, 0))
        .unwrap();
    class
        .define_method("clone", method!(<Pair as typed_data::Dup>::clone, -1))
        .unwrap();

    let res: bool = eval!(
        ruby,
        r#"
        a = Pair.new("foo", 1)
        raise "shouldn't be frozen without freeze:" if a.clone.frozen?
        raise "shouldn't be frozen with freeze: nil" if a.clone(freeze: nil).frozen?
        raise "shouldn't be frozen with freeze: false" if a.clone(freeze: false).frozen?
        raise "should be frozen with freeze: true" unless a.clone(freeze: true).frozen?

        a.freeze
        raise "should be frozen without freeze:" unless a.clone.frozen?
        raise "should be frozen with freeze: nil" unless a.clone(freeze: nil).frozen?
        raise "shouldn't be frozen with #dup" if a.dup.frozen?

        b = Pair.new("bar", 2)
        def b.foobar
          "test"
        end

        raise "dup shouldn't copy singleton class" if b.dup.respond_to?(:foobar)
        b.clone.foobar == "test"
    "#
    )
    .unwrap();
    assert!(res);
}

Version data entries

19 entries across 19 versions & 1 rubygems

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