forked from spatten/ruby_koans
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0bbe77
commit b41d616
Showing
11 changed files
with
174 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
require File.expand_path(File.dirname(__FILE__) + '/edgecase') | ||
|
||
include Java | ||
|
||
# Concepts | ||
# * Pull in a java class | ||
# * calling a method, Camel vs snake | ||
# * Resovling module/class name conflicts | ||
# * Showing what gets returned | ||
# * Ruby Strings VS Java Strings | ||
# * Calling custom java class | ||
# * Calling Ruby from java??? | ||
|
||
class AboutJavaInterop < EdgeCase::Koan | ||
def test_using_a_java_library_class | ||
java_array = java.util.ArrayList.new | ||
assert_equal __, java_array.class | ||
end | ||
|
||
def test_java_class_can_be_referenced_using_both_ruby_and_java_like_syntax | ||
assert_equal __, Java::JavaUtil::ArrayList == java.util.ArrayList | ||
end | ||
|
||
def test_include_class_includes_class_in_module_scope | ||
assert_nil defined?(TreeSet) | ||
include_class "java.util.TreeSet" | ||
assert_equal __, defined?(TreeSet) | ||
end | ||
|
||
# THINK ABOUT IT: | ||
# | ||
# What if we use: | ||
# | ||
# include_class "java.lang.String" | ||
# | ||
# What would be the value of the String constant after this | ||
# include_class is run? Would it be useful to provide a way of | ||
# aliasing java classes to different names? | ||
|
||
JString = java.lang.String | ||
def test_also_java_class_can_be_given_ruby_aliases | ||
java_string = JString.new("A Java String") | ||
assert_equal __, java_string.class | ||
assert_equal __, JString | ||
end | ||
|
||
def test_can_directly_call_java_methods_on_java_objects | ||
java_string = JString.new("A Java String") | ||
assert_equal __, java_string.toLowerCase | ||
end | ||
|
||
def test_jruby_provides_snake_case_versions_of_java_methods | ||
java_string = JString.new("A Java String") | ||
assert_equal __, java_string.to_lower_case | ||
end | ||
|
||
def test_jruby_provides_question_mark_versions_of_boolean_methods | ||
java_string = JString.new("A Java String") | ||
assert_equal __, java_string.endsWith("String") | ||
assert_equal __, java_string.ends_with("String") | ||
assert_equal __, java_string.ends_with?("String") | ||
end | ||
|
||
def test_java_string_are_not_ruby_strings | ||
ruby_string = "A Java String" | ||
java_string = java.lang.String.new(ruby_string) | ||
assert_equal __, java_string.is_a?(java.lang.String) | ||
assert_equal __, java_string.is_a?(String) | ||
end | ||
|
||
def test_java_strings_can_be_compared_to_ruby_strings_maybe | ||
ruby_string = "A Java String" | ||
java_string = java.lang.String.new(ruby_string) | ||
assert_equal __, ruby_string == java_string | ||
assert_equal __, java_string == ruby_string | ||
|
||
# THINK ABOUT IT: | ||
# | ||
# Is there any possible way for this to be more wrong? | ||
# | ||
# SERIOUSLY, THINK ABOUT IT: | ||
# | ||
# Why do you suppose that Ruby and Java strings compare like that? | ||
# | ||
# ADVANCED THINK ABOUT IT: | ||
# | ||
# Is there a way to make Ruby/Java string comparisons commutative? | ||
# How would you do it? | ||
end | ||
|
||
def test_however_most_methods_returning_strings_return_ruby_strings | ||
java_array = java.util.ArrayList.new | ||
assert_equal __, java_array.toString | ||
assert_equal __, java_array.toString.is_a?(String) | ||
assert_equal __, java_array.toString.is_a?(java.lang.String) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters