Sha256: 25b2f46f516f2700cbb24049ed52d075e6359defe1495ee4756c1d93f11d831b

Contents?: true

Size: 1.55 KB

Versions: 19

Compression:

Stored size: 1.55 KB

Contents

use magnus::{embed::init, method, prelude::*, rb_assert};

#[magnus::wrap(class = "Pleasantry")]
enum Pleasantry {
    #[magnus(class = "Greeting")]
    Greeting(String),
    #[magnus(class = "Farewell")]
    Farewell(String),
}

impl Pleasantry {
    fn to_s(&self) -> String {
        match self {
            Self::Greeting(subject) => format!("Hello, {}!", subject),
            Self::Farewell(subject) => format!("Goodbye, {}!", subject),
        }
    }
}

#[test]
fn it_wraps_rust_struct() {
    let ruby = unsafe { init() };

    let class = ruby
        .define_class("Pleasantry", ruby.class_object())
        .unwrap();
    ruby.define_class("Farewell", class).unwrap();
    ruby.define_class("Greeting", class).unwrap();

    class
        .define_method("to_s", method!(Pleasantry::to_s, 0))
        .unwrap();

    let greeting = Pleasantry::Greeting("World".to_owned());
    rb_assert!(ruby, "greeting.is_a?(Greeting)", greeting);

    let greeting = Pleasantry::Greeting("World".to_owned());
    rb_assert!(ruby, "greeting.is_a?(Pleasantry)", greeting);

    let greeting = Pleasantry::Greeting("World".to_owned());
    rb_assert!(ruby, r#"greeting.to_s == "Hello, World!""#, greeting);

    let farewell = Pleasantry::Farewell("World".to_owned());
    rb_assert!(ruby, "farewell.is_a?(Farewell)", farewell);

    let farewell = Pleasantry::Farewell("World".to_owned());
    rb_assert!(ruby, "farewell.is_a?(Pleasantry)", farewell);

    let farewell = Pleasantry::Farewell("World".to_owned());
    rb_assert!(ruby, r#"farewell.to_s == "Goodbye, World!""#, farewell);
}

Version data entries

19 entries across 19 versions & 1 rubygems

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