opal 0.3.18 → 0.3.19
Sign up to get free protection for your applications and to get access to all the features.
- 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
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,48 @@
|
|
1
|
+
describe "Enumerable#detect" do
|
2
|
+
before :each do
|
3
|
+
ScratchPad.record []
|
4
|
+
@elements = [2, 4, 6, 8, 10]
|
5
|
+
@numerous = EnumerableSpecs::Numerous.new(*@elements)
|
6
|
+
@empty = []
|
7
|
+
end
|
8
|
+
|
9
|
+
it "passes each entry in enum to block while block when block is false" do
|
10
|
+
visited_elements = []
|
11
|
+
@numerous.detect do |element|
|
12
|
+
visited_elements << element
|
13
|
+
false
|
14
|
+
end
|
15
|
+
visited_elements.should == @elements
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns nil when the block is false and there is no ifnone proc given" do
|
19
|
+
@numerous.detect {|e| false}.should == nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns the first element for which the block is not false" do
|
23
|
+
@elements.each do |element|
|
24
|
+
@numerous.detect {|e| e > element -1 }.should == element
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns the value of the ifnone proc if the block is false" do
|
29
|
+
fail_proc = lambda { "cheeseburgers" }
|
30
|
+
@numerous.detect(fail_proc) {|e| false }.should == "cheeseburgers"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "doesn't call the ifnone proc if an element is found" do
|
34
|
+
fail_proc = lambda { raise "This should't have been called" }
|
35
|
+
@numerous.detect(fail_proc) {|e| 2 == @elements.first }.should == 2
|
36
|
+
end
|
37
|
+
|
38
|
+
it "calls the ifnone proc only once when the block is false" do
|
39
|
+
times = 0
|
40
|
+
fail_proc = lambda { times += 1; raise if times > 1; "cheeseburgers" }
|
41
|
+
@numerous.detect(fail_proc) {|e| false }.should == "cheeseburgers"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "calls the ifnone proc when there are no elements" do
|
45
|
+
fail_proc = lambda { "yay" }
|
46
|
+
@empty.detect(fail_proc) {|e| true}.should == "yay"
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
describe "Enumerable#drop" do
|
2
|
+
before :each do
|
3
|
+
@enum = EnumerableSpecs::Numerous.new(3, 2, 1, :go)
|
4
|
+
end
|
5
|
+
|
6
|
+
describe "passed a number n as an argument" do
|
7
|
+
it "returns [] for empty enumerables" do
|
8
|
+
EnumerableSpecs::Empty.new.drop(0).should == []
|
9
|
+
EnumerableSpecs::Empty.new.drop(2).should == []
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns [] if dropping all" do
|
13
|
+
@enum.drop(5).should == []
|
14
|
+
EnumerableSpecs::Numerous.new(3, 2, 1, :go).drop(4).should == []
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
describe "Enumerable#drop_while" do
|
2
|
+
before :each do
|
3
|
+
@enum = EnumerableSpecs::Numerous.new(3, 2, 1, :go)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "returns an Enumerator if no block given" do
|
7
|
+
@enum.drop_while.should be_kind_of(Enumerator)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns no/all elements for {true/false} block" do
|
11
|
+
@enum.drop_while {true}.should == []
|
12
|
+
@enum.drop_while {false}.should == @enum.to_a
|
13
|
+
end
|
14
|
+
|
15
|
+
it "accepts returns other than true/false" do
|
16
|
+
@enum.drop_while{1}.should == []
|
17
|
+
@enum.drop_while{nil}.should == @enum.to_a
|
18
|
+
end
|
19
|
+
|
20
|
+
it "passed elements to the block until the first false" do
|
21
|
+
a = []
|
22
|
+
@enum.drop_while{|obj| (a << obj).size < 3}.should == [1, :go]
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
describe "Enumerable#each_with_index" do
|
2
|
+
before :each do
|
3
|
+
@b = EnumerableSpecs::Numerous.new(2, 5, 3, 6, 1, 4)
|
4
|
+
end
|
5
|
+
|
6
|
+
it "passes each element and its index to block" do
|
7
|
+
@a = []
|
8
|
+
@b.each_with_index { |o, i| @a << [o, i] }
|
9
|
+
@a.should == [[2, 0], [5, 1], [3, 2], [6, 3], [1, 4], [4, 5]]
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
describe "Enumerable#each_with_object" do
|
2
|
+
before :each do
|
3
|
+
@values = [2, 5, 3, 6, 1, 4]
|
4
|
+
@enum = EnumerableSpecs::Numerous.new(*@values)
|
5
|
+
@initial = "memo"
|
6
|
+
end
|
7
|
+
|
8
|
+
it "passes each element and its argument to the block" do
|
9
|
+
acc = []
|
10
|
+
@enum.each_with_object(@initial) do |elem, obj|
|
11
|
+
obj.should equal(@initial)
|
12
|
+
obj = 42
|
13
|
+
acc << elem
|
14
|
+
end.should equal(@initial)
|
15
|
+
acc.should == @values
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
describe "Enumerable#find_all" do
|
2
|
+
before :each do
|
3
|
+
ScratchPad.record []
|
4
|
+
@elements = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
5
|
+
@numerous = EnumerableSpecs::Numerous.new(*@elements)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns all elements for which the block is not false" do
|
9
|
+
@numerous.find_all {|i| i % 3 == 0 }.should == [3, 6, 9]
|
10
|
+
@numerous.find_all {|i| true }.should == @elements
|
11
|
+
@numerous.find_all {|i| false }.should == []
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
describe "Enumerable#find_index" do
|
2
|
+
before :each do
|
3
|
+
@elements = [2, 4, 6, 8, 10]
|
4
|
+
@numerous = EnumerableSpecs::Numerous.new(*@elements)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "passes each entry in enum to block while block when block is false" do
|
8
|
+
visited_elements = []
|
9
|
+
@numerous.find_index do |element|
|
10
|
+
visited_elements << element
|
11
|
+
false
|
12
|
+
end
|
13
|
+
visited_elements.should == @elements
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns nil when the block is false" do
|
17
|
+
@numerous.find_index {|e| false }.should == nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns the first index for which the block is not false" do
|
21
|
+
@elements.each_with_index do |element, index|
|
22
|
+
@numerous.find_index {|e| e > element - 1 }.should == index
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns the first found index" do
|
27
|
+
repeated = [10, 11, 11, 13, 11, 13, 10, 10, 13, 11]
|
28
|
+
numerous_repeat = EnumerableSpecs::Numerous.new(*repeated)
|
29
|
+
repeated.each do |element|
|
30
|
+
numerous_repeat.find_index(element).should == element - 10
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns nil when the element not found" do
|
35
|
+
@numerous.find_index(-1).should == nil
|
36
|
+
end
|
37
|
+
|
38
|
+
it "ignores the block if an argument is given" do
|
39
|
+
@numerous.find_index(-1) {|e| true }.should == nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns an Enumerator if no block given" do
|
43
|
+
@numerous.find_index.should be_kind_of(Enumerator)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
describe "Enumerable#find" do
|
2
|
+
before :each do
|
3
|
+
ScratchPad.record []
|
4
|
+
@elements = [2, 4, 6, 8, 10]
|
5
|
+
@numerous = EnumerableSpecs::Numerous.new(*@elements)
|
6
|
+
@empty = []
|
7
|
+
end
|
8
|
+
|
9
|
+
it "passes each entry in enum to block while block when block is false" do
|
10
|
+
visited_elements = []
|
11
|
+
@numerous.find do |element|
|
12
|
+
visited_elements << element
|
13
|
+
false
|
14
|
+
end
|
15
|
+
visited_elements.should == @elements
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns nil when the block is false and there is no ifnone proc given" do
|
19
|
+
@numerous.find {|e| false}.should == nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns the first element for which the block is not false" do
|
23
|
+
@elements.each do |element|
|
24
|
+
@numerous.find {|e| e > element -1 }.should == element
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns the value of the ifnone proc if the block is false" do
|
29
|
+
fail_proc = lambda { "cheeseburgers" }
|
30
|
+
@numerous.find(fail_proc) {|e| false }.should == "cheeseburgers"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "doesn't call the ifnone proc if an element is found" do
|
34
|
+
fail_proc = lambda { raise "This should't have been called" }
|
35
|
+
@numerous.find(fail_proc) {|e| 2 == @elements.first }.should == 2
|
36
|
+
end
|
37
|
+
|
38
|
+
it "calls the ifnone proc only once when the block is false" do
|
39
|
+
times = 0
|
40
|
+
fail_proc = lambda { times += 1; raise if times > 1; "cheeseburgers" }
|
41
|
+
@numerous.find(fail_proc) {|e| false }.should == "cheeseburgers"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "calls the ifnone proc when there are no elements" do
|
45
|
+
fail_proc = lambda { "yay" }
|
46
|
+
@empty.find(fail_proc) {|e| true}.should == "yay"
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
describe "Enumerable#first" do
|
2
|
+
before :each do
|
3
|
+
@values = [4,3,2,1,0,-1]
|
4
|
+
@enum = EnumerableSpecs::Numerous.new(*@values)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns the first element" do
|
8
|
+
EnumerableSpecs::Numerous.new.first.should == 2
|
9
|
+
EnumerableSpecs::Empty.new.first.should == nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns nil if self is empty" do
|
13
|
+
EnumerableSpecs::Empty.new.first.should == nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns the first count elements if given a count" do
|
17
|
+
@enum.first(2).should == [4, 3]
|
18
|
+
@enum.first(4).should == [4, 3, 2, 1]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns an empty array when passed a count on an empty array" do
|
22
|
+
empty = EnumerableSpecs::Empty.new
|
23
|
+
empty.first(0).should == []
|
24
|
+
empty.first(1).should == []
|
25
|
+
empty.first(2).should == []
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns an empty array when passed count == 0" do
|
29
|
+
@enum.first(0).should == []
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns an array containing the first element when passed count == 1" do
|
33
|
+
@enum.first(1).should == [4]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns the entire array when count > length" do
|
37
|
+
@enum.first(100).should == @values
|
38
|
+
@enum.first(8).should == @values
|
39
|
+
end
|
40
|
+
end
|
@@ -22,4 +22,23 @@ module EnumerableSpecs
|
|
22
22
|
raise "from each"
|
23
23
|
end
|
24
24
|
end
|
25
|
+
|
26
|
+
class EachDefiner
|
27
|
+
include Enumerable
|
28
|
+
|
29
|
+
attr_reader :arr
|
30
|
+
|
31
|
+
def initialize(*args)
|
32
|
+
@arr = arr
|
33
|
+
end
|
34
|
+
|
35
|
+
def each
|
36
|
+
i = 0
|
37
|
+
loop do
|
38
|
+
break if i == @arr.size
|
39
|
+
yield @arr[i]
|
40
|
+
i += 1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
25
44
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class EnumerableSpecGrep
|
2
|
+
def ===(obj); obj == '2'; end
|
3
|
+
end
|
4
|
+
|
5
|
+
class EnumerableSpecGrep2
|
6
|
+
def ===(obj); /^ca/ =~ obj; end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "Enumerable#grep" do
|
10
|
+
before(:each) do
|
11
|
+
@a = EnumerableSpecs::EachDefiner.new(2, 4, 6, 8, 10)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "grep without a block should return an array of all elements === pattern" do
|
15
|
+
EnumerableSpecs::Numerous.new('2', 'a', 'nil', '3', false).grep(EnumerableSpecGrep.new).should == ['2']
|
16
|
+
end
|
17
|
+
|
18
|
+
it "grep with a block should return an array of elements === pattern passed through block" do
|
19
|
+
EnumerableSpecs::Numerous.new("cat", "coat", "car", "cadr", "cost").grep(EnumerableSpecGrep2.new) { |i| i.upcase }.should == ["CAT", "CAR", "CADR"]
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
describe "Enumerable#take" do
|
2
|
+
before :each do
|
3
|
+
@values = [4,3,2,1,0,-1]
|
4
|
+
@enum = EnumerableSpecs::Numerous.new(*@values)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "returns the take element" do
|
8
|
+
EnumerableSpecs::Numerous.new.take.should == 2
|
9
|
+
EnumerableSpecs::Empty.new.take.should == nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns nil if self is empty" do
|
13
|
+
EnumerableSpecs::Empty.new.take.should == nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns the take count elements if given a count" do
|
17
|
+
@enum.take(2).should == [4, 3]
|
18
|
+
@enum.take(4).should == [4, 3, 2, 1]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns an empty array when passed a count on an empty array" do
|
22
|
+
empty = EnumerableSpecs::Empty.new
|
23
|
+
empty.take(0).should == []
|
24
|
+
empty.take(1).should == []
|
25
|
+
empty.take(2).should == []
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns an empty array when passed count == 0" do
|
29
|
+
@enum.take(0).should == []
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns an array containing the take element when passed count == 1" do
|
33
|
+
@enum.take(1).should == [4]
|
34
|
+
end
|
35
|
+
|
36
|
+
it "returns the entire array when count > length" do
|
37
|
+
@enum.take(100).should == @values
|
38
|
+
@enum.take(8).should == @values
|
39
|
+
end
|
40
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,20 @@
|
|
1
|
+
describe "File.expand_path" do
|
2
|
+
before :each do
|
3
|
+
@base = ""
|
4
|
+
@tmpdir = "tmp"
|
5
|
+
@rootdir = ""
|
6
|
+
end
|
7
|
+
|
8
|
+
it "converts a pathname to an absolute pathname" do
|
9
|
+
File.expand_path('').should == @base
|
10
|
+
File.expand_path('a').should == File.join(@base, "a")
|
11
|
+
File.expand_path('a', nil).should == File.join(@base, 'a')
|
12
|
+
end
|
13
|
+
|
14
|
+
it "converts a pathname to an absolute pathname, using a complete path" do
|
15
|
+
File.expand_path("", "#{@tmpdir}").should == "#{@tmpdir}"
|
16
|
+
File.expand_path("a", "#{@tmpdir}").should == "#{@tmpdir}/a"
|
17
|
+
File.expand_path("../a", "#{@tmpdir}/xxx").should == "#{@tmpdir}/a"
|
18
|
+
File.expand_path(".", "#{@rootdir}").should == "#{@rootdir}"
|
19
|
+
end
|
20
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,15 @@
|
|
1
|
+
describe "Hash#each_key" do
|
2
|
+
it "calls block once for each key, passing key" do
|
3
|
+
r = {}
|
4
|
+
h = {1 => -1, 2 => -2, 3 => -3, 4 => -4}
|
5
|
+
h.each_key { |k| r[k] = k }.should equal(h)
|
6
|
+
r.should == {1 => 1, 2 => 2, 3 => 3, 4 => 4}
|
7
|
+
end
|
8
|
+
|
9
|
+
it "processes keys in the same order as keys()" do
|
10
|
+
keys = []
|
11
|
+
h = {1 => -1, 2 => -2, 3 => -3, 4 => -4}
|
12
|
+
h.each_key { |k| keys << k }
|
13
|
+
keys.should == h.keys
|
14
|
+
end
|
15
|
+
end
|