opal 0.3.11 → 0.3.15
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/Gemfile +10 -0
- data/LICENSE +20 -0
- data/README.md +11 -116
- data/Rakefile +126 -0
- data/bin/opal +1 -2
- data/docs/spec_runner.html +16 -0
- data/index.html +434 -0
- data/lib/opal.rb +14 -15
- data/lib/opal/builder.rb +46 -148
- data/lib/opal/command.rb +45 -115
- data/lib/opal/context.rb +139 -78
- data/lib/opal/dependency_builder.rb +34 -0
- data/lib/opal/environment.rb +92 -0
- data/lib/opal/parser/grammar.rb +4915 -0
- data/lib/opal/{parser.y → parser/grammar.y} +430 -284
- data/lib/opal/parser/lexer.rb +1329 -0
- data/lib/opal/parser/parser.rb +1460 -0
- data/lib/opal/parser/scope.rb +140 -0
- data/lib/opal/parser/sexp.rb +17 -0
- data/lib/opal/version.rb +2 -1
- data/opal.gemspec +23 -0
- data/opal.js +3149 -4162
- data/runtime/README.md +25 -0
- data/runtime/corelib/alpha.rb +10 -0
- data/runtime/corelib/array.rb +962 -0
- data/runtime/corelib/basic_object.rb +66 -0
- data/runtime/corelib/boolean.rb +44 -0
- data/runtime/corelib/class.rb +43 -0
- data/runtime/corelib/comparable.rb +25 -0
- data/runtime/corelib/complex.rb +2 -0
- data/runtime/corelib/dir.rb +29 -0
- data/runtime/corelib/enumerable.rb +316 -0
- data/runtime/corelib/enumerator.rb +80 -0
- data/runtime/corelib/error.rb +25 -0
- data/runtime/corelib/file.rb +80 -0
- data/runtime/corelib/hash.rb +503 -0
- data/runtime/corelib/io.rb +44 -0
- data/runtime/corelib/kernel.rb +237 -0
- data/runtime/corelib/load_order +29 -0
- data/runtime/corelib/match_data.rb +37 -0
- data/runtime/corelib/module.rb +171 -0
- data/runtime/corelib/native.rb +50 -0
- data/runtime/corelib/nil_class.rb +47 -0
- data/runtime/corelib/numeric.rb +219 -0
- data/runtime/corelib/object.rb +21 -0
- data/runtime/corelib/proc.rb +42 -0
- data/runtime/corelib/range.rb +38 -0
- data/runtime/corelib/rational.rb +16 -0
- data/runtime/corelib/regexp.rb +63 -0
- data/runtime/corelib/string.rb +185 -0
- data/runtime/corelib/struct.rb +97 -0
- data/runtime/corelib/time.rb +196 -0
- data/runtime/corelib/top_self.rb +7 -0
- data/runtime/gemlib/alpha.rb +5 -0
- data/runtime/gemlib/kernel.rb +17 -0
- data/runtime/gemlib/load_order +2 -0
- data/runtime/kernel/class.js +256 -0
- data/runtime/kernel/debug.js +42 -0
- data/runtime/kernel/init.js +114 -0
- data/runtime/kernel/load_order +5 -0
- data/runtime/kernel/loader.js +151 -0
- data/runtime/kernel/runtime.js +414 -0
- data/runtime/spec/README.md +34 -0
- data/runtime/spec/core/array/allocate_spec.rb +15 -0
- data/runtime/spec/core/array/append_spec.rb +31 -0
- data/runtime/spec/core/array/assoc_spec.rb +29 -0
- data/runtime/spec/core/array/at_spec.rb +38 -0
- data/runtime/spec/core/array/clear_spec.rb +22 -0
- data/runtime/spec/core/array/collect_spec.rb +3 -0
- data/runtime/spec/core/array/compact_spec.rb +42 -0
- data/runtime/spec/core/array/concat_spec.rb +21 -0
- data/runtime/spec/core/array/constructor_spec.rb +24 -0
- data/runtime/spec/core/array/count_spec.rb +11 -0
- data/runtime/spec/core/array/delete_at_spec.rb +31 -0
- data/runtime/spec/core/array/delete_if_spec.rb +24 -0
- data/runtime/spec/core/array/delete_spec.rb +26 -0
- data/runtime/spec/core/array/each_index_spec.rb +33 -0
- data/runtime/spec/core/array/each_spec.rb +11 -0
- data/runtime/spec/core/array/element_reference_spec.rb +136 -0
- data/runtime/spec/core/array/element_set_spec.rb +7 -0
- data/runtime/spec/core/array/empty_spec.rb +10 -0
- data/runtime/spec/core/array/eql_spec.rb +3 -0
- data/runtime/spec/core/array/equal_value_spec.rb +3 -0
- data/runtime/spec/core/array/fetch_spec.rb +26 -0
- data/runtime/spec/core/array/first_spec.rb +54 -0
- data/runtime/spec/core/array/fixtures/classes.rb +8 -0
- data/runtime/spec/core/array/flatten_spec.rb +41 -0
- data/runtime/spec/core/array/include_spec.rb +20 -0
- data/runtime/spec/core/array/insert_spec.rb +59 -0
- data/runtime/spec/core/array/last_spec.rb +57 -0
- data/runtime/spec/core/array/length_spec.rb +3 -0
- data/runtime/spec/core/array/map_spec.rb +3 -0
- data/runtime/spec/core/array/plus_spec.rb +16 -0
- data/runtime/spec/core/array/pop_spec.rb +79 -0
- data/runtime/spec/core/array/push_spec.rb +19 -0
- data/runtime/spec/core/array/rassoc_spec.rb +12 -0
- data/runtime/spec/core/array/reject_spec.rb +54 -0
- data/runtime/spec/core/array/replace_spec.rb +3 -0
- data/runtime/spec/core/array/reverse_each_spec.rb +18 -0
- data/runtime/spec/core/array/reverse_spec.rb +9 -0
- data/runtime/spec/core/array/shared/collect.rb +53 -0
- data/runtime/spec/core/array/shared/eql.rb +19 -0
- data/runtime/spec/core/array/shared/length.rb +6 -0
- data/runtime/spec/core/array/shared/replace.rb +31 -0
- data/runtime/spec/core/class/new_spec.rb +19 -0
- data/runtime/spec/core/enumerable/all_spec.rb +102 -0
- data/runtime/spec/core/enumerable/any_spec.rb +115 -0
- data/runtime/spec/core/enumerable/collect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/count_spec.rb +29 -0
- data/runtime/spec/core/enumerable/detect_spec.rb +3 -0
- data/runtime/spec/core/enumerable/find_spec.rb +3 -0
- data/runtime/spec/core/enumerable/fixtures/classes.rb +26 -0
- data/runtime/spec/core/enumerable/shared/collect.rb +12 -0
- data/runtime/spec/core/enumerable/shared/entries.rb +7 -0
- data/runtime/spec/core/enumerable/shared/find.rb +49 -0
- data/runtime/spec/core/enumerable/to_a_spec.rb +7 -0
- data/runtime/spec/core/false/and_spec.rb +11 -0
- data/runtime/spec/core/false/inspect_spec.rb +7 -0
- data/runtime/spec/core/false/or_spec.rb +11 -0
- data/runtime/spec/core/false/to_s_spec.rb +7 -0
- data/runtime/spec/core/false/xor_spec.rb +11 -0
- data/runtime/spec/core/hash/allocate_spec.rb +15 -0
- data/runtime/spec/core/hash/assoc_spec.rb +29 -0
- data/runtime/spec/core/hash/clear_spec.rb +21 -0
- data/runtime/spec/core/hash/clone_spec.rb +12 -0
- data/runtime/spec/core/hash/default_spec.rb +6 -0
- data/runtime/spec/core/hash/delete_if_spec.rb +15 -0
- data/runtime/spec/core/hash/element_reference_spec.rb +16 -0
- data/runtime/spec/core/hash/element_set_spec.rb +8 -0
- data/runtime/spec/core/hash/new_spec.rb +13 -0
- data/runtime/spec/core/matchdata/to_a_spec.rb +7 -0
- data/runtime/spec/core/nil/and_spec.rb +12 -0
- data/runtime/spec/core/nil/inspect_spec.rb +8 -0
- data/runtime/spec/core/nil/nil_spec.rb +8 -0
- data/runtime/spec/core/nil/or_spec.rb +12 -0
- data/runtime/spec/core/nil/to_a_spec.rb +8 -0
- data/runtime/spec/core/nil/to_f_spec.rb +12 -0
- data/runtime/spec/core/nil/to_i_spec.rb +12 -0
- data/runtime/spec/core/nil/to_s_spec.rb +8 -0
- data/runtime/spec/core/nil/xor_spec.rb +12 -0
- data/runtime/spec/core/numeric/equal_value_spec.rb +11 -0
- data/runtime/spec/core/object/is_a_spec.rb +2 -0
- data/runtime/spec/core/object/shared/kind_of.rb +0 -0
- data/runtime/spec/core/regexp/match_spec.rb +23 -0
- data/runtime/spec/core/regexp/shared/match.rb +11 -0
- data/runtime/spec/core/symbol/to_proc_spec.rb +8 -0
- data/runtime/spec/core/true/and_spec.rb +11 -0
- data/runtime/spec/core/true/inspect_spec.rb +7 -0
- data/runtime/spec/core/true/or_spec.rb +11 -0
- data/runtime/spec/core/true/to_s_spec.rb +7 -0
- data/runtime/spec/core/true/xor_spec.rb +11 -0
- data/runtime/spec/language/alias_spec.rb +25 -0
- data/runtime/spec/language/and_spec.rb +62 -0
- data/runtime/spec/language/array_spec.rb +68 -0
- data/runtime/spec/language/block_spec.rb +105 -0
- data/runtime/spec/language/break_spec.rb +49 -0
- data/runtime/spec/language/case_spec.rb +165 -0
- data/runtime/spec/language/defined_spec.rb +80 -0
- data/runtime/spec/language/ensure_spec.rb +82 -0
- data/runtime/spec/language/fixtures/block.rb +19 -0
- data/runtime/spec/language/fixtures/break.rb +39 -0
- data/runtime/spec/language/fixtures/defined.rb +9 -0
- data/runtime/spec/language/fixtures/ensure.rb +37 -0
- data/runtime/spec/language/fixtures/next.rb +46 -0
- data/runtime/spec/language/fixtures/send.rb +36 -0
- data/runtime/spec/language/fixtures/super.rb +43 -0
- data/runtime/spec/language/hash_spec.rb +43 -0
- data/runtime/spec/language/if_spec.rb +278 -0
- data/runtime/spec/language/loop_spec.rb +32 -0
- data/runtime/spec/language/next_spec.rb +128 -0
- data/runtime/spec/language/or_spec.rb +65 -0
- data/runtime/spec/language/predefined_spec.rb +21 -0
- data/runtime/spec/language/regexp/interpolation_spec.rb +9 -0
- data/runtime/spec/language/regexp_spec.rb +7 -0
- data/runtime/spec/language/send_spec.rb +105 -0
- data/runtime/spec/language/string_spec.rb +4 -0
- data/runtime/spec/language/super_spec.rb +18 -0
- data/runtime/spec/language/symbol_spec.rb +41 -0
- data/runtime/spec/language/undef_spec.rb +16 -0
- data/runtime/spec/language/unless_spec.rb +44 -0
- data/runtime/spec/language/until_spec.rb +137 -0
- data/runtime/spec/language/variables_spec.rb +28 -0
- data/runtime/spec/language/versions/hash_1.9.rb +20 -0
- data/runtime/spec/language/while_spec.rb +175 -0
- data/runtime/spec/library/stringscanner/scan_spec.rb +36 -0
- data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +49 -0
- data/runtime/spec/opal/opal/defined_spec.rb +15 -0
- data/runtime/spec/opal/opal/function_spec.rb +11 -0
- data/runtime/spec/opal/opal/native_spec.rb +16 -0
- data/runtime/spec/opal/opal/null_spec.rb +10 -0
- data/runtime/spec/opal/opal/number_spec.rb +11 -0
- data/runtime/spec/opal/opal/object_spec.rb +16 -0
- data/runtime/spec/opal/opal/string_spec.rb +11 -0
- data/runtime/spec/opal/opal/typeof_spec.rb +9 -0
- data/runtime/spec/opal/opal/undefined_spec.rb +10 -0
- data/runtime/spec/opal/true/case_compare_spec.rb +12 -0
- data/runtime/spec/opal/true/class_spec.rb +10 -0
- data/runtime/spec/spec_helper.rb +25 -0
- data/runtime/stdlib/base64.rb +91 -0
- data/runtime/stdlib/date.rb +4 -0
- data/{stdlib → runtime/stdlib}/dev.rb +0 -0
- data/runtime/stdlib/forwardable.rb +33 -0
- data/runtime/stdlib/optparse.rb +0 -0
- data/runtime/stdlib/pp.rb +6 -0
- data/{stdlib → runtime/stdlib}/racc/parser.rb +0 -0
- data/runtime/stdlib/rbconfig.rb +0 -0
- data/runtime/stdlib/si.rb +17 -0
- data/runtime/stdlib/strscan.rb +53 -0
- data/runtime/stdlib/uri.rb +111 -0
- data/runtime/stdlib/uri/common.rb +1014 -0
- data/runtime/stdlib/uri/ftp.rb +261 -0
- data/runtime/stdlib/uri/generic.rb +1599 -0
- data/runtime/stdlib/uri/http.rb +106 -0
- data/runtime/stdlib/uri/https.rb +22 -0
- data/runtime/stdlib/uri/ldap.rb +260 -0
- data/runtime/stdlib/uri/ldaps.rb +20 -0
- data/runtime/stdlib/uri/mailto.rb +280 -0
- data/spec/builder/build_source_spec.rb +52 -0
- data/spec/builder/fixtures/build_source/adam.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/a.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/b.rb +0 -0
- data/spec/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
- data/spec/builder/fixtures/build_source/beynon.rb +0 -0
- data/spec/builder/fixtures/build_source/charles.js +0 -0
- data/spec/builder/fixtures/build_source/foo/a.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/b.rb +0 -0
- data/spec/builder/fixtures/build_source/foo/x.js +0 -0
- data/spec/builder/fixtures/build_source/foo/y.js +0 -0
- data/spec/builder/output_path_spec.rb +50 -0
- data/spec/grammar/alias_spec.rb +26 -0
- data/spec/grammar/and_spec.rb +13 -0
- data/spec/grammar/array_spec.rb +22 -0
- data/spec/grammar/attrasgn_spec.rb +28 -0
- data/spec/grammar/begin_spec.rb +38 -0
- data/spec/grammar/block_spec.rb +12 -0
- data/spec/grammar/break_spec.rb +17 -0
- data/spec/grammar/call_spec.rb +58 -0
- data/spec/grammar/class_spec.rb +35 -0
- data/spec/grammar/const_spec.rb +13 -0
- data/spec/grammar/cvar_spec.rb +11 -0
- data/spec/grammar/def_spec.rb +60 -0
- data/spec/grammar/false_spec.rb +17 -0
- data/spec/grammar/file_spec.rb +7 -0
- data/spec/grammar/gvar_spec.rb +13 -0
- data/spec/grammar/hash_spec.rb +17 -0
- data/spec/grammar/iasgn_spec.rb +9 -0
- data/spec/grammar/if_spec.rb +26 -0
- data/spec/grammar/iter_spec.rb +59 -0
- data/spec/grammar/ivar_spec.rb +9 -0
- data/spec/grammar/lasgn_spec.rb +8 -0
- data/spec/grammar/line_spec.rb +8 -0
- data/spec/grammar/lvar_spec.rb +38 -0
- data/spec/grammar/module_spec.rb +27 -0
- data/spec/grammar/nil_spec.rb +17 -0
- data/spec/grammar/not_spec.rb +27 -0
- data/spec/grammar/op_asgn1_spec.rb +23 -0
- data/spec/grammar/op_asgn2_spec.rb +23 -0
- data/spec/grammar/or_spec.rb +13 -0
- data/spec/grammar/return_spec.rb +17 -0
- data/spec/grammar/sclass_spec.rb +20 -0
- data/spec/grammar/self_spec.rb +17 -0
- data/spec/grammar/str_spec.rb +96 -0
- data/spec/grammar/super_spec.rb +20 -0
- data/spec/grammar/true_spec.rb +17 -0
- data/spec/grammar/undef_spec.rb +15 -0
- data/spec/grammar/unless_spec.rb +13 -0
- data/spec/grammar/while_spec.rb +15 -0
- data/spec/grammar/xstr_spec.rb +116 -0
- data/spec/grammar/yield_spec.rb +20 -0
- data/spec/spec_helper.rb +9 -0
- metadata +346 -21
- data/lib/opal/bundle.rb +0 -34
- data/lib/opal/lexer.rb +0 -902
- data/lib/opal/nodes.rb +0 -2150
- data/lib/opal/parser.rb +0 -4894
- data/lib/opal/rake/bundle_task.rb +0 -63
- data/opal-parser.js +0 -8343
- data/stdlib/strscan.rb +0 -52
- data/templates/init/Rakefile +0 -7
- data/templates/init/index.html +0 -17
- data/templates/init/lib/__NAME__.rb +0 -2
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "NilClass#^" do
|
4
|
+
it "returns false if other is nil or false, otherwise true" do
|
5
|
+
(nil ^ nil).should == false
|
6
|
+
(nil ^ true).should == true
|
7
|
+
(nil ^ false).should == false
|
8
|
+
(nil ^ "").should == true
|
9
|
+
(nil ^ 'x').should == true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "Numeric#==" do
|
4
|
+
it "returns true if self has the same value as other" do
|
5
|
+
(1 == 1).should == true
|
6
|
+
(9 == 5).should == false
|
7
|
+
|
8
|
+
(9 == 9.0).should == true
|
9
|
+
(9 == 9.01).should == false
|
10
|
+
end
|
11
|
+
end
|
File without changes
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../shared/match', __FILE__)
|
3
|
+
|
4
|
+
describe "Regexp#=~ on a successful match" do
|
5
|
+
it "returns the index of the first character of the matching region" do
|
6
|
+
(/(.)(.)(.)/ =~ "abc").should == 0
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "Regexp#match on a successful match" do
|
11
|
+
it "returns a MatchData object" do
|
12
|
+
/(.)(.)(.)/.match("abc").should be_kind_of(MatchData)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "resets $~ if passed nil" do
|
16
|
+
# set $~
|
17
|
+
/./.match("a")
|
18
|
+
$~.should be_kind_of(MatchData)
|
19
|
+
|
20
|
+
/1/.match(nil)
|
21
|
+
$~.should be_nil
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path('../../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe :regexp_match do
|
4
|
+
it "returns nil if there is no match" do
|
5
|
+
/xyz/.match("abxyc").should be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns nil if the object is nil" do
|
9
|
+
/xyz/.match(nil).should be_nil
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "TrueClass#&" do
|
4
|
+
it "returns false if other is nil or false, otherwise true" do
|
5
|
+
(true & true).should == true
|
6
|
+
(true & false).should == false
|
7
|
+
(true & nil).should == false
|
8
|
+
(true & "").should == true
|
9
|
+
(true & mock('x')).should == true
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "TrueClass#|" do
|
4
|
+
it "returns true" do
|
5
|
+
(true | true).should == true
|
6
|
+
(true | false).should == true
|
7
|
+
(true | nil).should == true
|
8
|
+
(true | "").should == true
|
9
|
+
(true | mock('x')).should == true
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "TrueClass#^" do
|
4
|
+
it "returns true if other is nil or false, otherwise false" do
|
5
|
+
(true ^ true).should == false
|
6
|
+
(true ^ false).should == true
|
7
|
+
(true ^ nil).should == true
|
8
|
+
(true ^ "").should == false
|
9
|
+
(true ^ mock('x')).should == false
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
class AliasObject
|
4
|
+
attr :foo
|
5
|
+
attr_reader :baz
|
6
|
+
attr_accessor :baz
|
7
|
+
|
8
|
+
def prep; @foo = 3; @bar = 4; end
|
9
|
+
def value; 5; end
|
10
|
+
def false_value; 6; end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "The alias keyword" do
|
14
|
+
before(:each) do
|
15
|
+
@obj = AliasObject.new
|
16
|
+
@meta = class << @obj;self;end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "creates a new name for an existing method" do
|
20
|
+
@meta.class_eval do
|
21
|
+
alias __value value
|
22
|
+
end
|
23
|
+
@obj.__value.should == 5
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "The '&&' statement" do
|
4
|
+
it "short-circuits evaluation at the first condition to be false" do
|
5
|
+
x = nil
|
6
|
+
true && false && x = 1
|
7
|
+
x.should be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "evaluates to the first condition not to be true" do
|
11
|
+
("yes" && 1 && nil && true).should == nil
|
12
|
+
("yes" && 1 && false && true).should == false
|
13
|
+
end
|
14
|
+
|
15
|
+
it "evaluates to the last condition if all are true" do
|
16
|
+
("yes" && 1).should == 1
|
17
|
+
(1 && "yes").should == "yes"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "evaluates the full set of chained conditions during assignment" do
|
21
|
+
x = y = nil
|
22
|
+
x = 1 && y = 2
|
23
|
+
x.should == 2
|
24
|
+
end
|
25
|
+
|
26
|
+
it "treats empty expressions as nil" do
|
27
|
+
(() && true).should be_nil
|
28
|
+
(true && ()).should be_nil
|
29
|
+
(() && ()).should be_nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "The 'and' statement" do
|
34
|
+
it "short-circuits evaluation at the first condition to be false" do
|
35
|
+
x = nil
|
36
|
+
true and false and x = 1
|
37
|
+
x.should be_nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it "evaluates to the first condition not to be true" do
|
41
|
+
("yes" and 1 and nil and true).should == nil
|
42
|
+
("yes" and 1 and false and true).should == false
|
43
|
+
end
|
44
|
+
|
45
|
+
it "evaluates to the last condition if all are true" do
|
46
|
+
("yes" and 1).should == 1
|
47
|
+
(1 and "yes").should == "yes"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "when used in assignment, evaluates and assigns expressions individually" do
|
51
|
+
x = y = nil
|
52
|
+
x = 1 and y = 2
|
53
|
+
x.should == 1
|
54
|
+
end
|
55
|
+
|
56
|
+
it "treats empty expressions as nil" do
|
57
|
+
(() and true).should be_nil
|
58
|
+
(true and ()).should be_nil
|
59
|
+
(() and ()).should be_nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "Array literals" do
|
4
|
+
it "[] should return a new array populated with the given elements" do
|
5
|
+
array = [1, 'a', nil]
|
6
|
+
array.should be_kind_of(Array)
|
7
|
+
array[0].should == 1
|
8
|
+
array[1].should == 'a'
|
9
|
+
array[2].should == nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "[] treats empty expressions as nil elements" do
|
13
|
+
array = [0, (), 2, (), 4]
|
14
|
+
array.should be_kind_of(Array)
|
15
|
+
array[0].should == 0
|
16
|
+
array[1].should == nil
|
17
|
+
array[2].should == 2
|
18
|
+
array[3].should == nil
|
19
|
+
array[4].should == 4
|
20
|
+
end
|
21
|
+
|
22
|
+
it "[] accepts a literal hash without curly braces as its only parameter" do
|
23
|
+
["foo" => :bar, :baz => 42].should == [{"foo" => :bar, :baz => 42}]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "Barewood array literal" do
|
28
|
+
it "%w() transforms unquoted barewords into an array" do
|
29
|
+
a = 3
|
30
|
+
%w(a #{3+a} 3).should == ["a", '#{3+a}', "3"]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "%W() transforms unquoted barewords into an array, supporting interpolation" do
|
34
|
+
a = 3
|
35
|
+
%W(a #{3+a} 3).should == ["a", '6', "3"]
|
36
|
+
end
|
37
|
+
|
38
|
+
it "%W() always treats interpolated expressions as a single word" do
|
39
|
+
a = "hello world"
|
40
|
+
%W(a b c #{a} d e).should == ["a", "b", "c", "hello world", "d", "e"]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "treats consecutive whitespace characters the same as one" do
|
44
|
+
%w(a b c d).should == ["a", "b", "c", "d"]
|
45
|
+
%w(hello
|
46
|
+
world).should == ["hello", "world"]
|
47
|
+
end
|
48
|
+
|
49
|
+
it "treats whitespace as literals characters when escaped by a backslash" do
|
50
|
+
%w(a b\ c d e).should == ["a", "b c", "d", "e"]
|
51
|
+
%w(a b\
|
52
|
+
c d).should == ["a", "b\nc", "d"]
|
53
|
+
%W(a\ b\tc).should == ["a ", "b\tc"]
|
54
|
+
%W(white\ \ \ \ \ space).should == ["white ", " ", " ", " space"]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "The unpacking splat operator (*)" do
|
59
|
+
it "when applied to a literal nested array, unpacks its elements into then containing array" do
|
60
|
+
[1, 2, *[3, 4, 5]].should == [1, 2, 3, 4, 5]
|
61
|
+
end
|
62
|
+
|
63
|
+
it "when applied to a nested references array, unpacks its elements into the containing array" do
|
64
|
+
splatted_array = [3, 4, 5]
|
65
|
+
[1, 2, *splatted_array].should == [1, 2, 3, 4, 5]
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,105 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/block', __FILE__)
|
3
|
+
|
4
|
+
describe "A block" do
|
5
|
+
before :each do
|
6
|
+
@y = BlockSpecs::Yielder.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "captures locals from the surrounding scope" do
|
10
|
+
var = 1
|
11
|
+
@y.z { var }.should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it "does not capture a local when an argument has the same name" do
|
15
|
+
var = 1
|
16
|
+
@y.s(2) { |var| var }.should == 2
|
17
|
+
var.should == 1
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "taking zero arguments" do
|
21
|
+
before :each do
|
22
|
+
@y = BlockSpecs::Yielder.new
|
23
|
+
end
|
24
|
+
|
25
|
+
it "does not raise an exception when no values are yielded" do
|
26
|
+
@y.z { 1 }.should == 1
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does not raise an exception when values are yielded" do
|
30
|
+
@y.s(0) { 1 }.should == 1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "taking || arguments" do
|
35
|
+
before :each do
|
36
|
+
@y = BlockSpecs::Yielder.new
|
37
|
+
end
|
38
|
+
|
39
|
+
it "does not raise an exception when no values are yielded" do
|
40
|
+
@y.z { || 1 }.should == 1
|
41
|
+
end
|
42
|
+
|
43
|
+
it "does not raise an exception when values are yielded" do
|
44
|
+
@y.s(0) { || 1 }.should == 1
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "taking |a| arguments" do
|
49
|
+
before :each do
|
50
|
+
@y = BlockSpecs::Yielder.new
|
51
|
+
end
|
52
|
+
|
53
|
+
it "assigns nil to the argument when no values are yielded" do
|
54
|
+
@y.z { |a| a }.should be_nil
|
55
|
+
end
|
56
|
+
|
57
|
+
it "assigns the value yielded to the argument" do
|
58
|
+
@y.s(1) { |a| a }.should == 1
|
59
|
+
end
|
60
|
+
|
61
|
+
it "assigns the first value yielded to the argument" do
|
62
|
+
@y.m(1, 2) { |a| a }.should == 1
|
63
|
+
end
|
64
|
+
|
65
|
+
it "does not deconstruct a single Array value" do
|
66
|
+
@y.s([1, 2]) { |a| a }.should == [1, 2]
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe "taking |a, b| arguments" do
|
71
|
+
before :each do
|
72
|
+
@y = BlockSpecs::Yielder.new
|
73
|
+
end
|
74
|
+
|
75
|
+
it "assigns nil to the arguments when no values are yielded" do
|
76
|
+
@y.z { |a, b| [a, b] }.should == [nil, nil]
|
77
|
+
end
|
78
|
+
|
79
|
+
it "assigns one value yielded to the first argument" do
|
80
|
+
@y.s(1) { |a, b| [a, b] }.should == [1, nil]
|
81
|
+
end
|
82
|
+
|
83
|
+
it "assigns the first two values yielded to the arguments" do
|
84
|
+
@y.m(1, 2, 3) { |a, b| [a, b] }.should == [1, 2]
|
85
|
+
end
|
86
|
+
|
87
|
+
it "does not destructive an Array value as one of the several values yielded" do
|
88
|
+
@y.m([1, 2], 3, 4) { |a, b| [a, b] }.should == [[1, 2], 3]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "taking |a, *b| arguments" do
|
93
|
+
before :each do
|
94
|
+
@y = BlockSpecs::Yielder.new
|
95
|
+
end
|
96
|
+
|
97
|
+
it "assigns 'nil' and '[]' to the arguments when no values are yielded" do
|
98
|
+
@y.z { |a, *b| [a, b] }.should == [nil, []]
|
99
|
+
end
|
100
|
+
|
101
|
+
it "assigns all yielded values after the first to the rest arguments" do
|
102
|
+
@y.m(1, 2, 3) { |a, *b| [a, b] }.should == [1, [2, 3]]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/break', __FILE__)
|
3
|
+
|
4
|
+
describe "The break statement in a block" do
|
5
|
+
before :each do
|
6
|
+
ScratchPad.record []
|
7
|
+
@program = BreakSpecs::Block.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns nil to method invoking the method yielding to the block when not passed an argument" do
|
11
|
+
@program.break_nil
|
12
|
+
ScratchPad.recorded.should == [:a, :aa, :b, nil, :d]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns a value to the method invoking the method yielding to the block" do
|
16
|
+
@program.break_value
|
17
|
+
ScratchPad.recorded.should == [:a, :aa, :b, :break, :d]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "Break inside a while loop" do
|
22
|
+
describe "with a value" do
|
23
|
+
it "exists the loop and returns the value" do
|
24
|
+
a = while true; break; end; a.should == nil
|
25
|
+
a = while true; break nil; end; a.should == nil
|
26
|
+
a = while true; break 1; end; a.should == 1
|
27
|
+
a = while true; break []; end; a.should == []
|
28
|
+
a = while true; break [1]; end; a.should == [1]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "stops a while loop when run" do
|
33
|
+
i = 0
|
34
|
+
while true
|
35
|
+
break if i == 2
|
36
|
+
i+=1
|
37
|
+
end
|
38
|
+
i.should == 2
|
39
|
+
end
|
40
|
+
|
41
|
+
it "causes a call with a block to return when run" do
|
42
|
+
at = 0
|
43
|
+
0.upto(5) do |i|
|
44
|
+
at = i
|
45
|
+
break i if i == 2
|
46
|
+
end.should == 2
|
47
|
+
at.should == 2
|
48
|
+
end
|
49
|
+
end
|