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,30 @@
|
|
1
|
+
describe "Hash#each_pair" do
|
2
|
+
it "yields the key and value of each pair to a block expecting |key, value|" do
|
3
|
+
r = {}
|
4
|
+
h = {:a => 1, :b => 2, :c => 3, :d => 5}
|
5
|
+
h.each_pair { |k,v| r[k.to_s] = v.to_s }.should equal(h)
|
6
|
+
r.should == {"a" => "1", "b" => "2", "c" => "3", "d" => "5"}
|
7
|
+
end
|
8
|
+
|
9
|
+
# FIXME: should be: h.each { |k,| ary << k }
|
10
|
+
it "yields the key only to a block expecting |key,|" do
|
11
|
+
ary = []
|
12
|
+
h = {"a" => 1, "b" => 2, "c" => 3}
|
13
|
+
h.each_pair { |k| ary << k }
|
14
|
+
ary.should == ["a", "b", "c"]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "uses the same order as keys() and values()" do
|
18
|
+
h = {:a => 1, :b => 2, :c => 3, :d => 5}
|
19
|
+
keys = []
|
20
|
+
values = []
|
21
|
+
|
22
|
+
h.each_pair do |k, v|
|
23
|
+
keys << k
|
24
|
+
values << v
|
25
|
+
end
|
26
|
+
|
27
|
+
keys.should == h.keys
|
28
|
+
values.should == h.values
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
describe "Hash#each" do
|
2
|
+
it "yields the key and value of each pair to a block expecting |key, value|" do
|
3
|
+
r = {}
|
4
|
+
h = {:a => 1, :b => 2, :c => 3, :d => 5}
|
5
|
+
h.each { |k,v| r[k.to_s] = v.to_s }.should equal(h)
|
6
|
+
r.should == {"a" => "1", "b" => "2", "c" => "3", "d" => "5"}
|
7
|
+
end
|
8
|
+
|
9
|
+
# FIXME: should be: h.each { |k,| ary << k }
|
10
|
+
it "yields the key only to a block expecting |key,|" do
|
11
|
+
ary = []
|
12
|
+
h = {"a" => 1, "b" => 2, "c" => 3}
|
13
|
+
h.each { |k| ary << k }
|
14
|
+
ary.should == ["a", "b", "c"]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "uses the same order as keys() and values()" do
|
18
|
+
h = {:a => 1, :b => 2, :c => 3, :d => 5}
|
19
|
+
keys = []
|
20
|
+
values = []
|
21
|
+
|
22
|
+
h.each do |k, v|
|
23
|
+
keys << k
|
24
|
+
values << v
|
25
|
+
end
|
26
|
+
|
27
|
+
keys.should == h.keys
|
28
|
+
values.should == h.values
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
describe "Hash#each_value" do
|
2
|
+
it "calls block once for each key, passing value" do
|
3
|
+
r = []
|
4
|
+
h = {:a => -5, :b => -3, :c => -2, :d => -1, :e => -1}
|
5
|
+
h.each_value { |v| r << v }.should equal(h)
|
6
|
+
r.should == [-5, -3, -2, -1, -1]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "processes values in the same order as values()" do
|
10
|
+
values = []
|
11
|
+
h = {:a => -5, :b => -3, :c => -2, :d => -1, :e => -1}
|
12
|
+
h.each_value { |v| values << v }
|
13
|
+
values.should == h.values
|
14
|
+
end
|
15
|
+
end
|
@@ -11,4 +11,18 @@ describe "Hash#[]" do
|
|
11
11
|
it "returns nil as default default value" do
|
12
12
|
{0 => 0}[5].should == nil
|
13
13
|
end
|
14
|
+
|
15
|
+
it "returns the default (imediate) value for missing keys" do
|
16
|
+
h = Hash.new 5
|
17
|
+
h[:a].should == 5
|
18
|
+
h[:a] = 0
|
19
|
+
h[:a].should == 0
|
20
|
+
h[:b].should == 5
|
21
|
+
end
|
22
|
+
|
23
|
+
it "does not return default values for keys with nil values" do
|
24
|
+
h = Hash.new 5
|
25
|
+
h[:a] = nil
|
26
|
+
h[:a].should == nil
|
27
|
+
end
|
14
28
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "Hash#empty?" do
|
2
|
+
it "returns true if the hash has no entries" do
|
3
|
+
{}.empty?.should == true
|
4
|
+
{1 => 1}.empty?.should == false
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns true if the hash has no entires and has a default value" do
|
8
|
+
Hash.new(5).empty?.should == true
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe "Hash#fetch" do
|
2
|
+
it "returns the value for key" do
|
3
|
+
{:a => 1, :b => -1}.fetch(:b).should == -1
|
4
|
+
end
|
5
|
+
|
6
|
+
it "raises and KeyError if key is not found" do
|
7
|
+
lambda { Hash.new.fetch(:a) }.should raise_error(KeyError)
|
8
|
+
lambda { Hash.new(5).fetch(:a) }.should raise_error(KeyError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns default if key is not found when passed a default" do
|
12
|
+
Hash.new.fetch(:a, nil).should == nil
|
13
|
+
Hash.new.fetch(:a, 'not here!').should == "not here!"
|
14
|
+
{:a => nil}.fetch(:a, 'not here!').should == nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns value of block if key is not found when passed a block" do
|
18
|
+
{}.fetch('a') { |k| k + '!' }.should == "a!"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "gives precedence to the default block over the default argument when passed both" do
|
22
|
+
{}.fetch(9, :foo) { |i| i * i }.should == 81
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
describe "Hash#flatten" do
|
2
|
+
before(:each) do
|
3
|
+
@h = {:plato => :greek,
|
4
|
+
:witgenstein => [:austrian, :british],
|
5
|
+
:russell => :welsh}
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns an Array" do
|
9
|
+
{}.flatten.should be_kind_of(Array)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns an empty Array for an empty Hash" do
|
13
|
+
{}.flatten.should == []
|
14
|
+
end
|
15
|
+
|
16
|
+
it "sets each even index of the Array to a key of the hash" do
|
17
|
+
a = @h.flatten
|
18
|
+
a[0].should == :plato
|
19
|
+
a[2].should == :witgenstein
|
20
|
+
a[4].should == :russell
|
21
|
+
end
|
22
|
+
|
23
|
+
it "sets each odd index of the Array to the value corresponding to the previous element" do
|
24
|
+
a = @h.flatten
|
25
|
+
a[1].should == :greek
|
26
|
+
a[3].should == [:austrian, :british]
|
27
|
+
a[5].should == :welsh
|
28
|
+
end
|
29
|
+
|
30
|
+
it "does not recursively flatten Array values when called without arguments" do
|
31
|
+
a = @h.flatten
|
32
|
+
a[3].should == [:austrian, :british]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "does not recursivley flatten Hash values when called without arguments" do
|
36
|
+
@h[:russell] = {:born => :wales, :influenced_by => :mill}
|
37
|
+
a = @h.flatten
|
38
|
+
a[5].should_not == {:born => :wales, :influenced_by => :mill}.flatten
|
39
|
+
end
|
40
|
+
|
41
|
+
it "recursively flattens Array values when called with an argument >= 2" do
|
42
|
+
a = @h.flatten(2)
|
43
|
+
a[3].should == :austrian
|
44
|
+
a[4].should == :british
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe "Hash#has_key?" do
|
2
|
+
it "returns true if the argument is a key" do
|
3
|
+
h = {:a => 1, :b => 2, :c => 3, 4 => 0}
|
4
|
+
h.has_key?(:a).should == true
|
5
|
+
h.has_key?(:b).should == true
|
6
|
+
h.has_key?(:B).should == false
|
7
|
+
h.has_key?(2).should == false
|
8
|
+
h.has_key?(4).should == true
|
9
|
+
h.has_key?(42).should == false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns true if the key's matching value was nil" do
|
13
|
+
{:xyz => nil}.has_key?(:xyz).should == true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns true if the key's matching value was false" do
|
17
|
+
{:xyz => false}.has_key?(:xyz).should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns true if the key is nil" do
|
21
|
+
{nil => 'b'}.has_key?(nil).should == true
|
22
|
+
{nil => nil}.has_key?(nil).should == true
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe "Hash#has_value?" do
|
2
|
+
it "returns true if the value exists in the hash" do
|
3
|
+
{:a => :b}.has_value?(:a).should == false
|
4
|
+
{1 => 2}.has_value?(2).should == true
|
5
|
+
h = Hash.new 5
|
6
|
+
h.has_value?(5).should == false
|
7
|
+
end
|
8
|
+
|
9
|
+
it "uses == semantics for comparing values" do
|
10
|
+
{5 => 2.0}.has_value?(2).should == true
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe "Hash#include?" do
|
2
|
+
it "returns true if the argument is a key" do
|
3
|
+
h = {:a => 1, :b => 2, :c => 3, 4 => 0}
|
4
|
+
h.include?(:a).should == true
|
5
|
+
h.include?(:b).should == true
|
6
|
+
h.include?(:B).should == false
|
7
|
+
h.include?(2).should == false
|
8
|
+
h.include?(4).should == true
|
9
|
+
h.include?(42).should == false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns true if the key's matching value was nil" do
|
13
|
+
{:xyz => nil}.include?(:xyz).should == true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns true if the key's matching value was false" do
|
17
|
+
{:xyz => false}.include?(:xyz).should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns true if the key is nil" do
|
21
|
+
{nil => 'b'}.include?(nil).should == true
|
22
|
+
{nil => nil}.include?(nil).should == true
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
describe "Hash#index" do
|
2
|
+
it "returns the corresponding key for value" do
|
3
|
+
{2 => 'a', 1 => 'b'}.index('b').should == 1
|
4
|
+
end
|
5
|
+
|
6
|
+
it "returns nil if the value is not found" do
|
7
|
+
{:a => -1, :b => 3.14, :c => 2.718}.index(1).should be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "doesn't return default value if the value isn't found" do
|
11
|
+
Hash.new(5).index(5).should be_nil
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
describe "Hash#indexes" do
|
2
|
+
it "returns an array of values for the given keys" do
|
3
|
+
h = {:a => 9, :b => 'a', :c => -10, :d => nil}
|
4
|
+
h.indexes.should be_kind_of(Array)
|
5
|
+
h.indexes.should == []
|
6
|
+
h.indexes(:a, :d, :b).should be_kind_of(Array)
|
7
|
+
h.indexes(:a, :d, :b).should == [9, nil, 'a']
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
describe "Hash#indices" do
|
2
|
+
it "returns an array of values for the given keys" do
|
3
|
+
h = {:a => 9, :b => 'a', :c => -10, :d => nil}
|
4
|
+
h.indices.should be_kind_of(Array)
|
5
|
+
h.indices.should == []
|
6
|
+
h.indices(:a, :d, :b).should be_kind_of(Array)
|
7
|
+
h.indices(:a, :d, :b).should == [9, nil, 'a']
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
describe "Hash#invert" do
|
2
|
+
it "returns a new hash where keys are values and vice versa" do
|
3
|
+
{1 => 'a', 2 => 'b', 3 => 'c'}.invert.should ==
|
4
|
+
{'a' => 1, 'b' => 2, 'c' => 3}
|
5
|
+
end
|
6
|
+
|
7
|
+
it "handles collisions by overriding with the key coming later in keys()" do
|
8
|
+
h = {:a => 1, :b => 1}
|
9
|
+
override_key = h.keys.last
|
10
|
+
h.invert[1].should == override_key
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
describe "Hash#keep_if" do
|
2
|
+
it "yields two arguments: key and value" do
|
3
|
+
all_args = []
|
4
|
+
{1 => 2, 3 => 4}.keep_if { |*args| all_args << args }
|
5
|
+
all_args.should == [[1, 2], [3, 4]]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "keeps every entry for which block is true and returns self" do
|
9
|
+
h = {:a => 1, :b => 2, :c => 3, :d => 4}
|
10
|
+
h.keep_if { |k,v| v % 2 == 0 }.should equal(h)
|
11
|
+
h.should == {:b => 2, :d => 4}
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns self even if unmodified" do
|
15
|
+
h = {1 => 2, 3 => 4}
|
16
|
+
h.keep_if { true }.should equal(h)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe "Hash#key?" do
|
2
|
+
it "returns true if the argument is a key" do
|
3
|
+
h = {:a => 1, :b => 2, :c => 3, 4 => 0}
|
4
|
+
h.key?(:a).should == true
|
5
|
+
h.key?(:b).should == true
|
6
|
+
h.key?(:B).should == false
|
7
|
+
h.key?(2).should == false
|
8
|
+
h.key?(4).should == true
|
9
|
+
h.key?(42).should == false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns true if the key's matching value was nil" do
|
13
|
+
{:xyz => nil}.key?(:xyz).should == true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns true if the key's matching value was false" do
|
17
|
+
{:xyz => false}.key?(:xyz).should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns true if the key is nil" do
|
21
|
+
{nil => 'b'}.key?(nil).should == true
|
22
|
+
{nil => nil}.key?(nil).should == true
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "Hash#keys" do
|
2
|
+
it "returns an array with the keys in the order they were inserted" do
|
3
|
+
{}.keys.should == []
|
4
|
+
{}.keys.should be_kind_of(Array)
|
5
|
+
Hash.new(5).keys.should == []
|
6
|
+
Hash.new { 5 }.keys.should == []
|
7
|
+
# {1 => 2, 2 => 8, 4 => 4}.keys.should == [1, 4, 2]
|
8
|
+
{nil => nil}.keys.should == [nil]
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
describe "Hash#length" do
|
2
|
+
it "returns the number of entries" do
|
3
|
+
{:a => 1, :b => 'c'}.length.should == 2
|
4
|
+
{:a => 1, :b => 2, :a => 2}.length.should == 2
|
5
|
+
{:a => 1, :b => 1, :c => 1}.length.should == 3
|
6
|
+
Hash.new.length.should == 0
|
7
|
+
Hash.new(5).length.should == 0
|
8
|
+
Hash.new { 5 }.length.should == 0
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe "Hash#member?" do
|
2
|
+
it "returns true if the argument is a key" do
|
3
|
+
h = {:a => 1, :b => 2, :c => 3, 4 => 0}
|
4
|
+
h.member?(:a).should == true
|
5
|
+
h.member?(:b).should == true
|
6
|
+
h.member?(:B).should == false
|
7
|
+
h.member?(2).should == false
|
8
|
+
h.member?(4).should == true
|
9
|
+
h.member?(42).should == false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns true if the key's matching value was nil" do
|
13
|
+
{:xyz => nil}.member?(:xyz).should == true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns true if the key's matching value was false" do
|
17
|
+
{:xyz => false}.member?(:xyz).should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns true if the key is nil" do
|
21
|
+
{nil => 'b'}.member?(nil).should == true
|
22
|
+
{nil => nil}.member?(nil).should == true
|
23
|
+
end
|
24
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,34 @@
|
|
1
|
+
describe "Hash#rassoc" do
|
2
|
+
before :each do
|
3
|
+
@h = {:apple => :green, :orange => :orange, :grape => :green, :banana => :yellow}
|
4
|
+
end
|
5
|
+
|
6
|
+
it "returns an Array if the argument is a value of the Hash" do
|
7
|
+
@h.rassoc(:green).should be_kind_of(Array)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns a 2-element Array if the argument is a valud of the Hash" do
|
11
|
+
@h.rassoc(:orange).size.should == 2
|
12
|
+
end
|
13
|
+
|
14
|
+
it "sets the first element of the Array to the key of the located value" do
|
15
|
+
@h.rassoc(:yellow).first.should == :banana
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sets the last element of the Array to the located value" do
|
19
|
+
@h.rassoc(:yellow).last.should == :yellow
|
20
|
+
end
|
21
|
+
|
22
|
+
it "only returns the first matching key-value pair" do
|
23
|
+
@h.rassoc(:green).should == [:apple, :green]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns nil if the argument is not a value of the Hash" do
|
27
|
+
@h.rassoc(:banana).should be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "returns nil if the argument is not a value of the Hash even when there is a default" do
|
31
|
+
Hash.new(42).merge!( :foo => :bar ).rassoc(42).should be_nil
|
32
|
+
Hash.new{|h, k| h[k] = 42}.merge!( :foo => :bar ).rassoc(42).should be_nil
|
33
|
+
end
|
34
|
+
end
|