Permalink
Browse files

inspect/to_s do the right thing now for clojure objects. Usable from

irb.
  • Loading branch information...
daveray committed Oct 15, 2011
1 parent 76d9088 commit 9df8acd19e6cf50fc99a7d4b3e9ec8f0bcbaa38e
Showing with 51 additions and 1 deletion.
  1. +23 −1 lib/familiar.rb
  2. +28 −0 test/test_familiar.rb
View
@@ -23,7 +23,6 @@ def self.method_missing(meth, *args, &block)
end
end
-
# Provides access to Clojure vars for when you need to use a Clojure
# var without invoking it.
#
@@ -47,6 +46,29 @@ def self.method_missing(meth, *args, &block)
end
end
+ # Make inspect and to_s look right in irb
+ [Java::ClojureLang::LazySeq,
+ Java::ClojureLang::PersistentVector,
+ Java::ClojureLang::PersistentList,
+ Java::ClojureLang::PersistentArrayMap,
+ Java::ClojureLang::PersistentHashMap,
+ Java::ClojureLang::PersistentHashSet,
+ Java::ClojureLang::Symbol,
+ Java::ClojureLang::Keyword,
+ Java::ClojureLang::Atom,
+ Java::ClojureLang::Ref,
+ Java::ClojureLang::Agent,
+ Java::ClojureLang::Var
+ ].each do |x|
+ x.class_eval do
+ def to_s
+ self.to_string
+ end
+ def inspect
+ Familiar.pr_str self
+ end
+ end
+ end
# Run a block of code without having to qualify everything in this module:
#
View
@@ -13,6 +13,34 @@
class FamiliarTest < Test::Unit::TestCase
+ def test_to_s_looks_right
+ assert_equal '(1 2 3 4 5)', Familiar.list(1, 2, 3, 4, 5).to_s
+ assert_equal '[1 2 3 4 5]', Familiar.vector(1, 2, 3, 4, 5).to_s
+ assert_equal '{1 2, 3 4, 5 6}', Familiar.hash_map(1, 2, 3, 4, 5, 6).to_s
+ assert_equal '#{1 2 3 4 5}', Familiar.hash_set(1, 2, 3, 4, 5).to_s
+ assert_equal 'hi', Familiar.symbol("hi").to_s
+ assert_equal ':hi', Familiar.keyword("hi").to_s
+ assert Familiar.range(4).to_s =~ /^clojure\.lang\.LazySeq@\h+$/
+ assert Familiar.atom('hi').to_s =~ /^clojure\.lang\.Atom@\h+$/
+ assert Familiar.ref('hi').to_s =~ /^clojure\.lang\.Ref@\h+$/
+ assert Familiar.agent('hi').to_s =~ /^clojure\.lang\.Agent@\h+$/
+ assert Familiar.vars.identity.to_s =~ /^#'clojure\.core\/identity$/
+ end
+
+ def test_inspect_looks_right
+ assert_equal '(1 2 3 4 5)', Familiar.list(1, 2, 3, 4, 5).inspect
+ assert_equal '[1 2 3 4 5]', Familiar.vector(1, 2, 3, 4, 5).inspect
+ assert_equal '{1 2, 3 4, 5 6}', Familiar.hash_map(1, 2, 3, 4, 5, 6).inspect
+ assert_equal '#{1 2 3 4 5}', Familiar.hash_set(1, 2, 3, 4, 5).inspect
+ assert_equal 'hi', Familiar.symbol("hi").inspect
+ assert_equal ':hi', Familiar.keyword("hi").inspect
+ assert_equal '(0 1 2 3)', Familiar.range(4).inspect
+ assert Familiar.atom('hi').inspect =~ /^#<Atom@\h+: "hi">$/
+ assert Familiar.ref('hi').inspect =~ /^#<Ref@\h+: "hi">$/
+ assert Familiar.agent('hi').inspect =~ /^#<Agent@\h+: "hi">$/
+ assert Familiar.vars.identity.inspect =~ /^#'clojure\.core\/identity$/
+ end
+
def test_can_create_a_function_from_a_lambda
f = Familiar.fn(lambda {|x| x * 2 })
assert f.is_a? Java::clojure.lang.IFn

0 comments on commit 9df8acd

Please sign in to comment.