Skip to content

Commit ae6a4a0

Browse files
committed
def adds docstring to meta (again), with test
1 parent 4321d53 commit ae6a4a0

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

src/value.rs

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -349,24 +349,17 @@ impl Value {
349349
match &**defname {
350350
Value::Symbol(sym) => {
351351
println!("Def: meta on sym is {}",sym.meta());
352-
// TODO: environment.insert with meta?
353-
let s = if doc_string != Value::Nil {
354-
let ss = Symbol::intern_with_ns(
355-
&sym.ns,
356-
&sym.name
357-
// conj!(
358-
// meta::base_meta(&sym.ns, &sym.name),
359-
// map_entry!("doc", doc_string)
360-
// ),
361-
);
362-
ss.clone()
363-
} else {
364-
sym.clone()
365-
};
366-
environment.insert(s.to_owned(), defval);
367-
// @TODO return var. For now, however, we only have symbols
368-
// @TODO intern from environment, don't make new sym ?
369-
Some(s.to_rc_value())
352+
353+
let mut meta = sym.meta();
354+
355+
if doc_string != Value::Nil {
356+
meta = conj!(meta,map_entry!("doc",doc_string));
357+
}
358+
359+
let sym = sym.with_meta(meta);
360+
environment.insert(sym.clone(), defval);
361+
// @TODO return var
362+
Some(sym.to_rc_value())
370363
}
371364
_ => Some(Rc::new(Value::Condition(std::string::String::from(
372365
"First argument to def must be a symbol",
@@ -778,3 +771,43 @@ impl Evaluable for Value {
778771
self.to_rc_value().eval_to_rc(environment)
779772
}
780773
}
774+
mod tests {
775+
use crate::keyword::Keyword;
776+
use crate::symbol::Symbol;
777+
use crate::protocols;
778+
use crate::value::Value;
779+
use crate::value::ToValue;
780+
use crate::traits::IMeta;
781+
use std::rc::Rc;
782+
use crate::environment::Environment;
783+
use crate::persistent_list_map::PersistentListMap;
784+
use crate::persistent_list_map::IPersistentMap;
785+
use crate::maps::MapEntry;
786+
use crate::protocol::ProtocolCastable;
787+
// (def ^{:cat 1 :dog 2} a "Docstring" 1)
788+
// ==>
789+
// a with meta of {:cat 1 :dog 2 :doc "Docstring"} ?
790+
#[test]
791+
fn def_with_docstring() {
792+
let sym_meta = persistent_list_map!{
793+
"cat" => 1,
794+
"dog" => 2
795+
};
796+
let a = sym!("a").with_meta(sym_meta);
797+
let result = Value::DefMacro.apply_to_persistent_list(
798+
&Rc::new(Environment::new_main_environment()),
799+
&Rc::new(list!(a "Docstring" 1))
800+
);
801+
802+
let final_sym_meta =
803+
result
804+
.unwrap()
805+
.as_protocol::<protocols::IMeta>()
806+
.meta();
807+
808+
assert_eq!(Value::I32(1),*final_sym_meta.get(&Keyword::intern("cat").to_rc_value()));
809+
assert_eq!(Value::I32(2),*final_sym_meta.get(&Keyword::intern("dog").to_rc_value()));
810+
assert_eq!(Value::String("Docstring".to_string()),*final_sym_meta.get(&Keyword::intern("doc").to_rc_value()));
811+
812+
}
813+
}

0 commit comments

Comments
 (0)