opal 0.3.18 → 0.3.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -1
- data/Gemfile +1 -3
- data/README.md +472 -10
- data/Rakefile +10 -52
- data/core/array.rb +9 -14
- data/core/basic_object.rb +7 -10
- data/core/boolean.rb +5 -1
- data/core/class.rb +15 -38
- data/core/dir.rb +89 -0
- data/core/enumerable.rb +133 -57
- data/core/error.rb +15 -1
- data/core/file.rb +85 -0
- data/core/hash.rb +186 -32
- data/core/kernel.rb +30 -31
- data/core/load_order +4 -2
- data/core/module.rb +42 -62
- data/core/numeric.rb +7 -1
- data/core/object.rb +1 -1
- data/core/proc.rb +6 -2
- data/core/range.rb +16 -28
- data/core/regexp.rb +3 -3
- data/core/runtime.js +281 -350
- data/core/string.rb +100 -110
- data/docs/CNAME +1 -0
- data/docs/Rakefile +55 -0
- data/docs/css/styles.css +50 -0
- data/docs/css/syntax.css +63 -0
- data/docs/layout/post.html +3 -0
- data/docs/layout/pre.html +11 -0
- data/examples/dependencies/app.rb +3 -0
- data/lib/opal.rb +2 -1
- data/lib/opal/builder.rb +36 -10
- data/lib/opal/builder_task.rb +51 -24
- data/lib/opal/grammar.rb +2509 -2439
- data/lib/opal/grammar.y +38 -5
- data/lib/opal/lexer.rb +18 -2
- data/lib/opal/parser.rb +375 -349
- data/lib/opal/scope.rb +24 -2
- data/lib/opal/version.rb +1 -1
- data/spec/builder/build_order_spec.rb +20 -0
- data/spec/builder/lib_name_for_spec.rb +24 -0
- data/spec/grammar/call_spec.rb +9 -6
- data/spec/grammar/lambda_spec.rb +64 -0
- data/spec/grammar/sclass_spec.rb +5 -3
- data/{core/spec → test}/core/array/allocate_spec.rb +0 -0
- data/{core/spec → test}/core/array/append_spec.rb +0 -0
- data/{core/spec → test}/core/array/assoc_spec.rb +0 -0
- data/{core/spec → test}/core/array/at_spec.rb +0 -0
- data/{core/spec → test}/core/array/clear_spec.rb +0 -0
- data/{core/spec → test}/core/array/clone_spec.rb +0 -0
- data/{core/spec → test}/core/array/collect_spec.rb +0 -0
- data/{core/spec → test}/core/array/compact_spec.rb +0 -0
- data/{core/spec → test}/core/array/concat_spec.rb +0 -0
- data/{core/spec → test}/core/array/constructor_spec.rb +0 -0
- data/{core/spec → test}/core/array/count_spec.rb +0 -0
- data/{core/spec → test}/core/array/delete_at_spec.rb +0 -0
- data/{core/spec → test}/core/array/delete_if_spec.rb +0 -0
- data/{core/spec → test}/core/array/delete_spec.rb +0 -0
- data/{core/spec → test}/core/array/each_index_spec.rb +0 -0
- data/{core/spec → test}/core/array/each_spec.rb +0 -0
- data/{core/spec → test}/core/array/element_reference_spec.rb +0 -0
- data/{core/spec → test}/core/array/empty_spec.rb +0 -0
- data/{core/spec → test}/core/array/eql_spec.rb +0 -0
- data/{core/spec → test}/core/array/fetch_spec.rb +0 -0
- data/{core/spec → test}/core/array/first_spec.rb +0 -0
- data/{core/spec → test}/core/array/flatten_spec.rb +0 -0
- data/{core/spec → test}/core/array/include_spec.rb +0 -0
- data/{core/spec → test}/core/array/insert_spec.rb +0 -0
- data/{core/spec → test}/core/array/last_spec.rb +0 -0
- data/{core/spec → test}/core/array/length_spec.rb +0 -0
- data/{core/spec → test}/core/array/map_spec.rb +0 -0
- data/{core/spec → test}/core/array/plus_spec.rb +0 -0
- data/{core/spec → test}/core/array/pop_spec.rb +0 -0
- data/{core/spec → test}/core/array/push_spec.rb +0 -0
- data/{core/spec → test}/core/array/rassoc_spec.rb +0 -0
- data/{core/spec → test}/core/array/reject_spec.rb +0 -0
- data/{core/spec → test}/core/array/replace_spec.rb +0 -0
- data/{core/spec → test}/core/array/reverse_each_spec.rb +0 -0
- data/{core/spec → test}/core/array/reverse_spec.rb +0 -0
- data/{core/spec → test}/core/array/size_spec.rb +0 -0
- data/{core/spec → test}/core/array/to_ary_spec.rb +0 -0
- data/{core/spec → test}/core/array/uniq_spec.rb +0 -0
- data/{core/spec → test}/core/array/zip_spec.rb +0 -0
- data/test/core/class/fixtures/classes.rb +9 -0
- data/test/core/class/new_spec.rb +108 -0
- data/{core/spec → test}/core/enumerable/all_spec.rb +0 -0
- data/{core/spec → test}/core/enumerable/any_spec.rb +0 -0
- data/{core/spec → test}/core/enumerable/collect_spec.rb +0 -0
- data/{core/spec → test}/core/enumerable/count_spec.rb +0 -0
- data/test/core/enumerable/detect_spec.rb +48 -0
- data/test/core/enumerable/drop_spec.rb +17 -0
- data/test/core/enumerable/drop_while_spec.rb +24 -0
- data/test/core/enumerable/each_with_index_spec.rb +11 -0
- data/test/core/enumerable/each_with_object_spec.rb +17 -0
- data/test/core/enumerable/entries_spec.rb +6 -0
- data/test/core/enumerable/find_all_spec.rb +13 -0
- data/test/core/enumerable/find_index_spec.rb +45 -0
- data/test/core/enumerable/find_spec.rb +48 -0
- data/test/core/enumerable/first_spec.rb +40 -0
- data/{core/spec → test}/core/enumerable/fixtures/classes.rb +19 -0
- data/test/core/enumerable/grep_spec.rb +21 -0
- data/test/core/enumerable/take_spec.rb +40 -0
- data/test/core/enumerable/to_a_spec.rb +6 -0
- data/{core/spec → test}/core/false/and_spec.rb +0 -0
- data/{core/spec → test}/core/false/inspect_spec.rb +0 -0
- data/{core/spec → test}/core/false/or_spec.rb +0 -0
- data/{core/spec → test}/core/false/to_s_spec.rb +0 -0
- data/{core/spec → test}/core/false/xor_spec.rb +0 -0
- data/test/core/file/expand_path_spec.rb +20 -0
- data/{core/spec → test}/core/hash/allocate_spec.rb +0 -0
- data/{core/spec → test}/core/hash/assoc_spec.rb +0 -0
- data/{core/spec → test}/core/hash/clear_spec.rb +0 -0
- data/{core/spec → test}/core/hash/clone_spec.rb +0 -0
- data/test/core/hash/default_spec.rb +9 -0
- data/{core/spec → test}/core/hash/delete_if_spec.rb +0 -0
- data/test/core/hash/each_key_spec.rb +15 -0
- data/test/core/hash/each_pair_spec.rb +30 -0
- data/test/core/hash/each_spec.rb +30 -0
- data/test/core/hash/each_value_spec.rb +15 -0
- data/{core/spec → test}/core/hash/element_reference_spec.rb +14 -0
- data/{core/spec → test}/core/hash/element_set_spec.rb +1 -0
- data/test/core/hash/empty_spec.rb +10 -0
- data/test/core/hash/fetch_spec.rb +24 -0
- data/test/core/hash/flatten_spec.rb +46 -0
- data/test/core/hash/has_key_spec.rb +24 -0
- data/test/core/hash/has_value_spec.rb +12 -0
- data/test/core/hash/include_spec.rb +24 -0
- data/test/core/hash/index_spec.rb +13 -0
- data/test/core/hash/indexes_spec.rb +9 -0
- data/test/core/hash/indices_spec.rb +9 -0
- data/test/core/hash/invert_spec.rb +12 -0
- data/test/core/hash/keep_if_spec.rb +18 -0
- data/test/core/hash/key_spec.rb +24 -0
- data/test/core/hash/keys_spec.rb +10 -0
- data/test/core/hash/length_spec.rb +10 -0
- data/test/core/hash/member_spec.rb +24 -0
- data/{core/spec → test}/core/hash/merge_spec.rb +0 -0
- data/{core/spec → test}/core/hash/new_spec.rb +0 -0
- data/test/core/hash/rassoc_spec.rb +34 -0
- data/test/core/hash/replace_spec.rb +7 -0
- data/test/core/hash/select_spec.rb +52 -0
- data/test/core/hash/shift_spec.rb +19 -0
- data/test/core/hash/size_spec.rb +10 -0
- data/test/core/hash/update_spec.rb +17 -0
- data/test/core/hash/value_spec.rb +12 -0
- data/test/core/hash/values_at_spec.rb +9 -0
- data/test/core/hash/values_spec.rb +7 -0
- data/test/core/kernel/eql_spec.rb +15 -0
- data/test/core/kernel/equal_value_spec.rb +12 -0
- data/test/core/kernel/loop_spec.rb +23 -0
- data/test/core/kernel/nil_spec.rb +7 -0
- data/test/core/kernel/proc_spec.rb +9 -0
- data/test/core/kernel/rand_spec.rb +14 -0
- data/test/core/kernel/respond_to_spec.rb +24 -0
- data/test/core/kernel/send_spec.rb +56 -0
- data/test/core/kernel/tap_spec.rb +10 -0
- data/test/core/kernel/to_s_spec.rb +5 -0
- data/{core/spec → test}/core/matchdata/to_a_spec.rb +0 -0
- data/{core/spec → test}/core/nil/and_spec.rb +0 -0
- data/{core/spec → test}/core/nil/inspect_spec.rb +0 -0
- data/{core/spec → test}/core/nil/nil_spec.rb +0 -0
- data/{core/spec → test}/core/nil/or_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_a_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_f_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_i_spec.rb +0 -0
- data/{core/spec → test}/core/nil/to_s_spec.rb +0 -0
- data/{core/spec → test}/core/nil/xor_spec.rb +0 -0
- data/{core/spec → test}/core/numeric/equal_value_spec.rb +0 -0
- data/test/core/range/begin_spec.rb +9 -0
- data/test/core/range/case_compare_spec.rb +16 -0
- data/test/core/range/end_spec.rb +9 -0
- data/{core/spec → test}/core/regexp/match_spec.rb +0 -0
- data/test/core/string/capitalize_spec.rb +10 -0
- data/test/core/string/casecmp_spec.rb +16 -0
- data/test/core/string/chomp_spec.rb +43 -0
- data/test/core/string/chop_spec.rb +10 -0
- data/test/core/string/chr_spec.rb +13 -0
- data/test/core/string/comparison_spec.rb +13 -0
- data/test/core/string/downcase_spec.rb +6 -0
- data/test/core/string/element_reference_spec.rb +72 -0
- data/test/core/string/empty_spec.rb +8 -0
- data/test/core/string/end_with_spec.rb +12 -0
- data/test/core/string/fixtures/classes.rb +3 -0
- data/test/core/string/gsub_spec.rb +17 -0
- data/test/core/string/include_spec.rb +12 -0
- data/test/core/string/intern_spec.rb +9 -0
- data/test/core/string/length_spec.rb +9 -0
- data/test/core/string/lstrip_spec.rb +7 -0
- data/test/core/string/match_spec.rb +27 -0
- data/test/core/string/next_spec.rb +10 -0
- data/test/core/string/ord_spec.rb +9 -0
- data/test/core/string/partition_spec.rb +10 -0
- data/test/core/string/reverse_spec.rb +7 -0
- data/test/core/string/rstrip_spec.rb +7 -0
- data/test/core/string/size_spec.rb +9 -0
- data/test/core/string/slice_spec.rb +72 -0
- data/test/core/string/split_spec.rb +5 -0
- data/test/core/string/start_with_spec.rb +12 -0
- data/test/core/string/strip_spec.rb +6 -0
- data/test/core/string/sub_spec.rb +22 -0
- data/test/core/string/succ_spec.rb +10 -0
- data/test/core/string/sum_spec.rb +5 -0
- data/test/core/string/swapcase_spec.rb +18 -0
- data/test/core/string/to_a_spec.rb +9 -0
- data/test/core/string/to_f_spec.rb +14 -0
- data/test/core/string/to_i_spec.rb +25 -0
- data/test/core/string/to_s_spec.rb +13 -0
- data/test/core/string/to_str_spec.rb +13 -0
- data/test/core/string/to_sym_spec.rb +9 -0
- data/test/core/string/upcase_spec.rb +6 -0
- data/test/core/symbol/to_proc_spec.rb +12 -0
- data/{core/spec → test}/core/true/and_spec.rb +0 -0
- data/{core/spec → test}/core/true/inspect_spec.rb +0 -0
- data/{core/spec → test}/core/true/or_spec.rb +0 -0
- data/{core/spec → test}/core/true/to_s_spec.rb +0 -0
- data/{core/spec → test}/core/true/xor_spec.rb +0 -0
- data/test/index.html +11 -0
- data/{core/spec → test}/language/alias_spec.rb +4 -0
- data/{core/spec → test}/language/and_spec.rb +0 -0
- data/{core/spec → test}/language/array_spec.rb +0 -0
- data/{core/spec → test}/language/block_spec.rb +0 -0
- data/{core/spec → test}/language/break_spec.rb +0 -0
- data/{core/spec → test}/language/case_spec.rb +0 -0
- data/{core/spec → test}/language/defined_spec.rb +0 -0
- data/{core/spec → test}/language/ensure_spec.rb +0 -0
- data/test/language/fixtures/yield.rb +23 -0
- data/{core/spec → test}/language/hash_spec.rb +0 -0
- data/{core/spec → test}/language/if_spec.rb +0 -0
- data/test/language/literal_lambda_spec.rb +47 -0
- data/{core/spec → test}/language/loop_spec.rb +0 -0
- data/{core/spec → test}/language/metaclass_spec.rb +0 -0
- data/{core/spec → test}/language/next_spec.rb +0 -0
- data/{core/spec → test}/language/or_spec.rb +0 -0
- data/{core/spec → test}/language/predefined_spec.rb +0 -0
- data/{core/spec → test}/language/regexp_spec.rb +0 -0
- data/{core/spec → test}/language/send_spec.rb +0 -0
- data/{core/spec → test}/language/singleton_class_spec.rb +0 -0
- data/{core/spec → test}/language/super_spec.rb +0 -0
- data/{core/spec → test}/language/symbol_spec.rb +0 -0
- data/{core/spec → test}/language/undef_spec.rb +0 -0
- data/{core/spec → test}/language/unless_spec.rb +0 -0
- data/{core/spec → test}/language/until_spec.rb +0 -0
- data/{core/spec → test}/language/variables_spec.rb +0 -0
- data/{core/spec → test}/language/while_spec.rb +0 -0
- data/test/language/yield_spec.rb +100 -0
- data/test/opal/array/subclassing_spec.rb +32 -0
- data/test/opal/class/bridge_class_spec.rb +37 -0
- data/test/opal/exception/subclassing_spec.rb +17 -0
- data/test/opal/runtime/_methods_spec.rb +48 -0
- data/test/opal/runtime/class_hierarchy_spec.rb +22 -0
- data/test/opal/runtime/def_spec.rb +23 -0
- data/test/opal/string/subclassing_spec.rb +26 -0
- data/test/spec_helper.rb +3 -0
- metadata +437 -111
- data/core/spec/core/class/new_spec.rb +0 -16
- data/core/spec/core/hash/default_spec.rb +0 -4
- data/core/spec/core/symbol/to_proc_spec.rb +0 -6
- data/core/spec/index.html +0 -11
- data/spec/builder/build_source_spec.rb +0 -52
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "String#chop" do
|
2
|
+
it "returns a new string with the last character removed" do
|
3
|
+
"hello\n".chop.should == "hello"
|
4
|
+
"hello".chop.should == "hell"
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns an empty string when applied to an empty string" do
|
8
|
+
"".chop.should == ""
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
describe "String#chr" do
|
2
|
+
it "returns an empty String if self is an empty String" do
|
3
|
+
"".chr.should == ""
|
4
|
+
end
|
5
|
+
|
6
|
+
it "returns a 1-character String" do
|
7
|
+
"glark".chr.size.should == 1
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns the character at the start of the String" do
|
11
|
+
"Goodbye, world".chr.should == "G"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
describe "String#<=>" do
|
2
|
+
it "returns -1 when self is less than other" do
|
3
|
+
("this" <=> "those").should == -1
|
4
|
+
end
|
5
|
+
|
6
|
+
it "returns 0 when self is equal to other" do
|
7
|
+
("yep" <=> "yep").should == 0
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns 1 when self is greater than other" do
|
11
|
+
("yoddle" <=> "griddle").should == 1
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
describe "String#[]" do
|
2
|
+
it "returns the character code of the character at the given index" do
|
3
|
+
"hello"[0].should == "h"
|
4
|
+
"hello"[-1].should == "o"
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns nil if index is outside of self" do
|
8
|
+
"hello"[20].should == nil
|
9
|
+
"hello"[-20].should == nil
|
10
|
+
|
11
|
+
""[0].should == nil
|
12
|
+
""[-1].should == nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "String#slice with index, length" do
|
17
|
+
it "returns the substring starting at the given index with the given length" do
|
18
|
+
"hello there"[0, 0].should == ""
|
19
|
+
"hello there"[0, 1].should == "h"
|
20
|
+
"hello there"[0, 3].should == "hel"
|
21
|
+
"hello there"[0, 6].should == "hello "
|
22
|
+
"hello there"[0, 9].should == "hello the"
|
23
|
+
"hello there"[0, 12].should == "hello there"
|
24
|
+
|
25
|
+
"hello there"[1, 0].should == ""
|
26
|
+
"hello there"[1, 1].should == "e"
|
27
|
+
"hello there"[1, 3].should == "ell"
|
28
|
+
"hello there"[1, 6].should == "ello t"
|
29
|
+
"hello there"[1, 9].should == "ello ther"
|
30
|
+
"hello there"[1, 12].should == "ello there"
|
31
|
+
|
32
|
+
"hello there"[3, 0].should == ""
|
33
|
+
"hello there"[3, 1].should == "l"
|
34
|
+
"hello there"[3, 3].should == "lo "
|
35
|
+
"hello there"[3, 6].should == "lo the"
|
36
|
+
"hello there"[3, 9].should == "lo there"
|
37
|
+
|
38
|
+
"hello there"[4, 0].should == ""
|
39
|
+
"hello there"[4, 3].should == "o t"
|
40
|
+
"hello there"[4, 6].should == "o ther"
|
41
|
+
"hello there"[4, 9].should == "o there"
|
42
|
+
|
43
|
+
"foo"[2, 1].should == "o"
|
44
|
+
"foo"[3, 0].should == ""
|
45
|
+
"foo"[3, 1].should == ""
|
46
|
+
|
47
|
+
""[0, 0].should == ""
|
48
|
+
""[0, 1].should == ""
|
49
|
+
|
50
|
+
"x"[0, 0].should == ""
|
51
|
+
"x"[0, 1].should == "x"
|
52
|
+
"x"[1, 0].should == ""
|
53
|
+
"x"[-1, 1].should == ""
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns nil if the offset falls outside of self" do
|
57
|
+
"hello there"[20, 3].should == nil
|
58
|
+
"hello there"[-20, 3].should == nil
|
59
|
+
|
60
|
+
""[1, 0].should == nil
|
61
|
+
""[1, 1].should == nil
|
62
|
+
|
63
|
+
""[2, 0].should == nil
|
64
|
+
""[2, 1].should == nil
|
65
|
+
|
66
|
+
"x"[2, 0].should == nil
|
67
|
+
"x"[2, 1].should == nil
|
68
|
+
|
69
|
+
"x"[-2, 0].should == nil
|
70
|
+
"x"[-2, 1].should == nil
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe "String#end_with?" do
|
2
|
+
it "returns true only if ends with match" do
|
3
|
+
s = "hello"
|
4
|
+
s.end_with?('o').should be_true
|
5
|
+
s.end_with?('llo').should be_true
|
6
|
+
s.end_with?('ll').should be_false
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns true only if any ending match" do
|
10
|
+
"hello".end_with?('x', 'y', 'llo', 'z').should be_true
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
describe "String#gsub with pattern and replacement" do
|
2
|
+
it "returns a copy of self with all occurrences of pattern replaced with replacement" do
|
3
|
+
"hello".gsub(/[aeiou]/, '*').should == "h*ll*"
|
4
|
+
end
|
5
|
+
|
6
|
+
it "ignores a block if supplied" do
|
7
|
+
"food".gsub(/f/, "g") { "w" }.should == "good"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "String#gsub with pattern and block" do
|
12
|
+
it "returns a copy of self with all occurrences of pattern replaced with the block's return value" do
|
13
|
+
"hello".gsub(/./) { |s| s.succ + ' ' }.should == "i f m m p "
|
14
|
+
"hello!".gsub(/(.)(.)/) { |*a| a.inspect }.should == '["he"]["ll"]["o!"]'
|
15
|
+
"hello".gsub('l') { 'x'}.should == 'hexxo'
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe "String#include?" do
|
2
|
+
it "returns true if self contains other_str" do
|
3
|
+
"hello".include?("lo").should == true
|
4
|
+
"hello".include?("ol").should == false
|
5
|
+
end
|
6
|
+
|
7
|
+
it "ignores subclass differences" do
|
8
|
+
"hello".include?(StringSpecs::MyString.new("lo")).should == true
|
9
|
+
StringSpecs::MyString.new("hello").include?("lo").should == true
|
10
|
+
StringSpecs::MyString.new("hello").include?(StringSpecs::MyString.new("lo")).should == true
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
describe "String#intern" do
|
2
|
+
it "returns the symbol corresponding to self" do
|
3
|
+
"Koala".intern.should == :Koala
|
4
|
+
'cat'.intern.should == :cat
|
5
|
+
'@cat'.intern.should == :@cat
|
6
|
+
'cat and dog'.intern.should == :"cat and dog"
|
7
|
+
"abc=".intern.should == :abc=
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
describe "String#=~" do
|
2
|
+
it "sets $~ to MatchData when there is a match and nil then there's none" do
|
3
|
+
'hello' =~ /./
|
4
|
+
$~[0].should == 'h'
|
5
|
+
|
6
|
+
'hello' =~ /not/
|
7
|
+
$~.should == nil
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "String#match" do
|
12
|
+
it "matches the pattern against self" do
|
13
|
+
'hello'.match(/(.)\1/)[0].should == "ll"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns nil if there's no match" do
|
17
|
+
'hello'.match('xx').should == nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "sets $~ to MatchData of match or nil when there is none" do
|
21
|
+
'hello'.match(/./)
|
22
|
+
$~[0].should == 'h'
|
23
|
+
|
24
|
+
'hello'.match(/X/)
|
25
|
+
$~.should == nil
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "String#next" do
|
2
|
+
it "returns an empty string for empty strings" do
|
3
|
+
"".next.should == ""
|
4
|
+
end
|
5
|
+
|
6
|
+
it "returns the successor by increasing the rightmost alphanumeric" do
|
7
|
+
"abcd".next.should == "abce"
|
8
|
+
"THX1138".next.should == "THX1139"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "String#partition" do
|
2
|
+
it "returns an array of substrings based on splitting on the given string" do
|
3
|
+
"hello world".partition("o").should == ["hell", "o", " world"]
|
4
|
+
end
|
5
|
+
|
6
|
+
it "always returns 3 elements" do
|
7
|
+
"hello".partition("x").should == ["hello", "", ""]
|
8
|
+
"hello".partition("hello").should == ["", "hello", ""]
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
describe "String#slice" do
|
2
|
+
it "returns the character code of the character at the given index" do
|
3
|
+
"hello".slice(0).should == "h"
|
4
|
+
"hello".slice(-1).should == "o"
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns nil if index is outside of self" do
|
8
|
+
"hello".slice(20).should == nil
|
9
|
+
"hello".slice(-20).should == nil
|
10
|
+
|
11
|
+
"".slice(0).should == nil
|
12
|
+
"".slice(-1).should == nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "String#slice with index, length" do
|
17
|
+
it "returns the substring starting at the given index with the given length" do
|
18
|
+
"hello there".slice(0, 0).should == ""
|
19
|
+
"hello there".slice(0, 1).should == "h"
|
20
|
+
"hello there".slice(0, 3).should == "hel"
|
21
|
+
"hello there".slice(0, 6).should == "hello "
|
22
|
+
"hello there".slice(0, 9).should == "hello the"
|
23
|
+
"hello there".slice(0, 12).should == "hello there"
|
24
|
+
|
25
|
+
"hello there".slice(1, 0).should == ""
|
26
|
+
"hello there".slice(1, 1).should == "e"
|
27
|
+
"hello there".slice(1, 3).should == "ell"
|
28
|
+
"hello there".slice(1, 6).should == "ello t"
|
29
|
+
"hello there".slice(1, 9).should == "ello ther"
|
30
|
+
"hello there".slice(1, 12).should == "ello there"
|
31
|
+
|
32
|
+
"hello there".slice(3, 0).should == ""
|
33
|
+
"hello there".slice(3, 1).should == "l"
|
34
|
+
"hello there".slice(3, 3).should == "lo "
|
35
|
+
"hello there".slice(3, 6).should == "lo the"
|
36
|
+
"hello there".slice(3, 9).should == "lo there"
|
37
|
+
|
38
|
+
"hello there".slice(4, 0).should == ""
|
39
|
+
"hello there".slice(4, 3).should == "o t"
|
40
|
+
"hello there".slice(4, 6).should == "o ther"
|
41
|
+
"hello there".slice(4, 9).should == "o there"
|
42
|
+
|
43
|
+
"foo".slice(2, 1).should == "o"
|
44
|
+
"foo".slice(3, 0).should == ""
|
45
|
+
"foo".slice(3, 1).should == ""
|
46
|
+
|
47
|
+
"".slice(0, 0).should == ""
|
48
|
+
"".slice(0, 1).should == ""
|
49
|
+
|
50
|
+
"x".slice(0, 0).should == ""
|
51
|
+
"x".slice(0, 1).should == "x"
|
52
|
+
"x".slice(1, 0).should == ""
|
53
|
+
"x".slice(-1, 1).should == ""
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns nil if the offset falls outside of self" do
|
57
|
+
"hello there".slice(20, 3).should == nil
|
58
|
+
"hello there".slice(-20, 3).should == nil
|
59
|
+
|
60
|
+
"".slice(1, 0).should == nil
|
61
|
+
"".slice(1, 1).should == nil
|
62
|
+
|
63
|
+
"".slice(2, 0).should == nil
|
64
|
+
"".slice(2, 1).should == nil
|
65
|
+
|
66
|
+
"x".slice(2, 0).should == nil
|
67
|
+
"x".slice(2, 1).should == nil
|
68
|
+
|
69
|
+
"x".slice(-2, 0).should == nil
|
70
|
+
"x".slice(-2, 1).should == nil
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe "String#start_with?" do
|
2
|
+
it "returns true only if beginning match" do
|
3
|
+
s = "hello"
|
4
|
+
s.start_with?('h').should be_true
|
5
|
+
s.start_with?('hel').should be_true
|
6
|
+
s.start_with?('el').should be_false
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns true only if any beginning match" do
|
10
|
+
"hello".start_with?('x', 'y', 'he', 'z').should be_true
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
describe "String#sub with pattern, replacement" do
|
2
|
+
it "returns a copy of self with all occurrences of pattern replaced with replacement" do
|
3
|
+
"hello".sub(/[aeiou]/, '*').should == "h*llo"
|
4
|
+
"hello".sub(//, ".").should == ".hello"
|
5
|
+
end
|
6
|
+
|
7
|
+
it "ignores a block if supplied" do
|
8
|
+
"food".sub(/f/, "g") { "w" }.should == "good"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "supports /i for ignoring case" do
|
12
|
+
"Hello".sub(/h/i, "j").should == "jello"
|
13
|
+
"hello".sub(/H/i, "j").should == "jello"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "String#sub with pattern and block" do
|
18
|
+
it "returns a copy of self with the first occurences of pattern replaces with block's return value" do
|
19
|
+
"hi".sub(/./) { |s| s + ' ' }.should == "h i"
|
20
|
+
"hi!".sub(/(.)(.)/) { |*a| a.inspect }.should == '["hi"]!'
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "String#succ" do
|
2
|
+
it "returns an empty string for empty strings" do
|
3
|
+
"".succ.should == ""
|
4
|
+
end
|
5
|
+
|
6
|
+
it "returns the successor by increasing the rightmost alphanumeric" do
|
7
|
+
"abcd".succ.should == "abce"
|
8
|
+
"THX1138".succ.should == "THX1139"
|
9
|
+
end
|
10
|
+
end
|