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
use artichoke_core::debug::Debug;
use artichoke_core::value::Value as _;

use crate::types::Ruby;
use crate::value::Value;
use crate::Artichoke;

impl Debug for Artichoke {
    type Value = Value;

    fn inspect_type_name_for_value(&mut self, value: Self::Value) -> &str {
        match value.try_into(self) {
            Ok(Some(true)) => "true",
            Ok(Some(false)) => "false",
            Ok(None) => "nil",
            Err(_) if matches!(value.ruby_type(), Ruby::Data | Ruby::Object) => self.class_name_for_value(value),
            Err(_) => value.ruby_type().class_name(),
        }
    }

    fn class_name_for_value(&mut self, value: Self::Value) -> &str {
        if let Ok(class) = value.funcall(self, "class", &[], None) {
            if let Ok(class) = class.funcall(self, "name", &[], None) {
                if let Ok(class) = class.try_into_mut(self) {
                    return class;
                }
            }
        }
        ""
    }
}

#[cfg(test)]
mod tests {
    use crate::test::prelude::*;

    #[test]
    fn debug_true_value_as_classlike() {
        let mut interp = interpreter().unwrap();
        let value = interp.convert(true);
        assert_eq!(interp.inspect_type_name_for_value(value), "true");
    }

    #[test]
    fn debug_false_value_as_classlike() {
        let mut interp = interpreter().unwrap();
        let value = interp.convert(false);
        assert_eq!(interp.inspect_type_name_for_value(value), "false");
    }

    #[test]
    fn debug_nil_value_as_classlike() {
        let mut interp = interpreter().unwrap();
        let value = interp.convert(None::<Value>);
        assert_eq!(interp.inspect_type_name_for_value(value), "nil");
    }

    #[test]
    fn debug_fixnum_value_as_classlike() {
        let mut interp = interpreter().unwrap();
        let value = interp.convert(1_i64);
        assert_eq!(interp.inspect_type_name_for_value(value), "Integer");
    }

    #[test]
    fn debug_hash_value_as_classlike() {
        let mut interp = interpreter().unwrap();
        let value = interp.try_convert_mut(vec![(b"foo".to_vec(), vec![1, 2, 3])]).unwrap();
        assert_eq!(interp.inspect_type_name_for_value(value), "Hash");
    }

    #[test]
    fn debug_array_value_as_classlike() {
        let mut interp = interpreter().unwrap();
        let value = interp.try_convert_mut(vec![1_i64]).unwrap();
        assert_eq!(interp.inspect_type_name_for_value(value), "Array");
    }
}