opal 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +40 -9
  3. data/CHANGELOG.md +349 -0
  4. data/Gemfile +7 -8
  5. data/README.md +25 -3
  6. data/Rakefile +4 -2
  7. data/bin/opal +1 -1
  8. data/examples/rack/Gemfile +3 -0
  9. data/examples/rack/app/application.rb +13 -0
  10. data/examples/rack/app/user.rb +21 -0
  11. data/examples/rack/config.ru +7 -0
  12. data/examples/rack/index.html.erb +10 -0
  13. data/examples/sinatra/Gemfile +4 -0
  14. data/examples/sinatra/app/application.rb +7 -0
  15. data/examples/sinatra/config.ru +21 -0
  16. data/lib/mspec/opal/rake_task.rb +29 -8
  17. data/lib/mspec/opal/runner.rb +5 -4
  18. data/lib/opal.rb +1 -0
  19. data/lib/opal/builder.rb +0 -28
  20. data/lib/opal/cli.rb +0 -14
  21. data/lib/opal/compiler.rb +12 -11
  22. data/lib/opal/fragment.rb +8 -1
  23. data/lib/opal/nodes/array.rb +1 -1
  24. data/lib/opal/nodes/base.rb +4 -0
  25. data/lib/opal/nodes/call.rb +6 -2
  26. data/lib/opal/nodes/call_special.rb +1 -1
  27. data/lib/opal/nodes/class.rb +2 -2
  28. data/lib/opal/nodes/constants.rb +3 -1
  29. data/lib/opal/nodes/helpers.rb +23 -14
  30. data/lib/opal/nodes/if.rb +16 -9
  31. data/lib/opal/nodes/literal.rb +37 -5
  32. data/lib/opal/nodes/logic.rb +7 -1
  33. data/lib/opal/nodes/module.rb +2 -2
  34. data/lib/opal/nodes/scope.rb +13 -2
  35. data/lib/opal/nodes/top.rb +9 -0
  36. data/lib/opal/nodes/variables.rb +5 -2
  37. data/lib/opal/parser.rb +306 -71
  38. data/lib/opal/parser/grammar.rb +2667 -2775
  39. data/lib/opal/parser/grammar.y +177 -233
  40. data/lib/opal/parser/lexer.rb +511 -427
  41. data/lib/opal/parser/sexp.rb +15 -3
  42. data/lib/opal/source_map.rb +8 -4
  43. data/lib/opal/sprockets.rb +4 -0
  44. data/lib/opal/sprockets/cache_key_fix.rb +17 -0
  45. data/lib/opal/sprockets/environment.rb +21 -0
  46. data/lib/opal/sprockets/erb.rb +30 -0
  47. data/lib/opal/sprockets/processor.rb +127 -0
  48. data/lib/opal/sprockets/server.rb +166 -0
  49. data/lib/opal/util.rb +29 -0
  50. data/lib/opal/version.rb +1 -1
  51. data/opal.gemspec +1 -1
  52. data/opal/corelib/array.rb +106 -187
  53. data/opal/corelib/array/inheritance.rb +113 -0
  54. data/opal/corelib/basic_object.rb +6 -2
  55. data/opal/corelib/boolean.rb +4 -0
  56. data/opal/corelib/class.rb +2 -0
  57. data/opal/corelib/complex.rb +3 -0
  58. data/opal/corelib/enumerable.rb +75 -8
  59. data/opal/corelib/enumerator.rb +2 -0
  60. data/opal/corelib/error.rb +23 -23
  61. data/opal/corelib/hash.rb +5 -5
  62. data/opal/corelib/helpers.rb +51 -16
  63. data/opal/corelib/io.rb +7 -24
  64. data/opal/corelib/kernel.rb +23 -11
  65. data/opal/corelib/module.rb +44 -47
  66. data/opal/corelib/nil_class.rb +4 -0
  67. data/opal/corelib/numeric.rb +101 -15
  68. data/opal/corelib/range.rb +2 -0
  69. data/opal/corelib/rational.rb +3 -0
  70. data/opal/corelib/regexp.rb +36 -17
  71. data/opal/corelib/runtime.js +22 -7
  72. data/opal/corelib/string.rb +213 -110
  73. data/opal/corelib/string/inheritance.rb +78 -0
  74. data/opal/corelib/struct.rb +8 -0
  75. data/opal/corelib/time.rb +54 -42
  76. data/opal/corelib/variables.rb +24 -0
  77. data/opal/opal.rb +5 -27
  78. data/spec/cli/compiler_spec.rb +136 -0
  79. data/spec/cli/dependency_resolver_spec.rb +40 -0
  80. data/spec/cli/lexer_spec.rb +110 -0
  81. data/spec/cli/parser/alias_spec.rb +26 -0
  82. data/spec/cli/parser/and_spec.rb +13 -0
  83. data/spec/cli/parser/attrasgn_spec.rb +28 -0
  84. data/spec/cli/parser/begin_spec.rb +42 -0
  85. data/spec/cli/parser/block_spec.rb +12 -0
  86. data/spec/cli/parser/break_spec.rb +17 -0
  87. data/spec/cli/parser/call_spec.rb +139 -0
  88. data/spec/cli/parser/class_spec.rb +35 -0
  89. data/spec/cli/parser/comments_spec.rb +11 -0
  90. data/spec/cli/parser/def_spec.rb +61 -0
  91. data/spec/cli/parser/if_spec.rb +26 -0
  92. data/spec/cli/parser/iter_spec.rb +59 -0
  93. data/spec/cli/parser/lambda_spec.rb +64 -0
  94. data/spec/cli/parser/literal_spec.rb +113 -0
  95. data/spec/cli/parser/masgn_spec.rb +37 -0
  96. data/spec/cli/parser/module_spec.rb +27 -0
  97. data/spec/cli/parser/not_spec.rb +27 -0
  98. data/spec/cli/parser/op_asgn1_spec.rb +23 -0
  99. data/spec/cli/parser/op_asgn2_spec.rb +23 -0
  100. data/spec/cli/parser/or_spec.rb +13 -0
  101. data/spec/cli/parser/return_spec.rb +17 -0
  102. data/spec/cli/parser/sclass_spec.rb +21 -0
  103. data/spec/cli/parser/string_spec.rb +269 -0
  104. data/spec/cli/parser/super_spec.rb +20 -0
  105. data/spec/cli/parser/undef_spec.rb +15 -0
  106. data/spec/cli/parser/unless_spec.rb +13 -0
  107. data/spec/cli/parser/variables_spec.rb +92 -0
  108. data/spec/cli/parser/while_spec.rb +15 -0
  109. data/spec/cli/parser/yield_spec.rb +20 -0
  110. data/spec/cli/spec_helper.rb +31 -11
  111. data/spec/opal/core/array/set_range_to_array_spec.rb +7 -0
  112. data/spec/opal/core/date_spec.rb +122 -0
  113. data/spec/opal/core/language/predefined_spec.rb +1 -1
  114. data/spec/opal/core/runtime/operator_call_spec.rb +13 -0
  115. data/spec/opal/core/runtime/truthy_spec.rb +23 -0
  116. data/spec/opal/filters/bugs/array.rb +96 -87
  117. data/spec/opal/filters/bugs/basic_object.rb +9 -0
  118. data/spec/opal/filters/bugs/class.rb +16 -0
  119. data/spec/opal/filters/bugs/enumerable.rb +54 -0
  120. data/spec/opal/filters/bugs/language.rb +37 -3
  121. data/spec/opal/filters/bugs/math.rb +93 -0
  122. data/spec/opal/filters/bugs/nil.rb +7 -0
  123. data/spec/opal/filters/bugs/numeric.rb +19 -0
  124. data/spec/opal/filters/bugs/opal.rb +12 -0
  125. data/spec/opal/filters/bugs/regexp.rb +0 -2
  126. data/spec/opal/filters/bugs/string.rb +317 -19
  127. data/spec/opal/filters/bugs/struct.rb +29 -0
  128. data/spec/opal/filters/bugs/time.rb +130 -9
  129. data/spec/opal/filters/unsupported/encoding.rb +52 -4
  130. data/spec/opal/filters/unsupported/enumerator.rb +0 -3
  131. data/spec/opal/filters/unsupported/integer_size.rb +7 -0
  132. data/spec/opal/filters/unsupported/method_added.rb +10 -0
  133. data/spec/opal/filters/unsupported/mutable_strings.rb +299 -1
  134. data/spec/opal/filters/unsupported/private_constants.rb +30 -0
  135. data/spec/opal/filters/unsupported/private_methods.rb +16 -0
  136. data/spec/opal/filters/unsupported/random.rb +4 -0
  137. data/spec/opal/filters/unsupported/tainted.rb +53 -0
  138. data/spec/opal/filters/unsupported/trusted.rb +5 -0
  139. data/spec/opal/rubyspecs +167 -234
  140. data/spec/opal/spec_helper.rb +3 -0
  141. data/spec/opal/stdlib/promise/error_spec.rb +15 -0
  142. data/spec/opal/stdlib/promise/rescue_spec.rb +35 -0
  143. data/spec/opal/stdlib/promise/then_spec.rb +54 -0
  144. data/spec/opal/stdlib/promise/trace_spec.rb +35 -0
  145. data/spec/opal/stdlib/promise/value_spec.rb +15 -0
  146. data/spec/opal/stdlib/promise/when_spec.rb +34 -0
  147. data/stdlib/base64.rb +152 -0
  148. data/stdlib/date.rb +82 -49
  149. data/{opal/corelib → stdlib}/encoding.rb +3 -1
  150. data/stdlib/erb.rb +0 -1
  151. data/stdlib/json.rb +10 -26
  152. data/stdlib/math.rb +370 -0
  153. data/stdlib/native.rb +40 -33
  154. data/stdlib/opal-parser.rb +7 -4
  155. data/stdlib/promise.rb +292 -0
  156. data/stdlib/strscan.rb +1 -1
  157. data/stdlib/template.rb +1 -3
  158. data/stdlib/time.rb +9 -0
  159. metadata +143 -204
  160. data/doc/compiler.md +0 -42
  161. data/doc/compiler_options.md +0 -5
  162. data/doc/examples/node_http_server.rb +0 -49
  163. data/doc/external_libraries.md +0 -9
  164. data/doc/generated_javascript.md +0 -272
  165. data/doc/home.md +0 -17
  166. data/doc/method_missing.md +0 -58
  167. data/doc/static_applications.md +0 -60
  168. data/doc/using_ruby_from_javascript.md +0 -18
  169. data/doc/using_sprockets.md +0 -65
  170. data/spec/opal/core/numeric/abs_spec.rb +0 -12
  171. data/spec/opal/core/numeric/downto_spec.rb +0 -19
  172. data/spec/opal/core/numeric/equal_value_spec.rb +0 -9
  173. data/spec/opal/core/numeric/even_spec.rb +0 -21
  174. data/spec/opal/core/numeric/magnitude_spec.rb +0 -12
  175. data/spec/opal/core/numeric/odd_spec.rb +0 -21
  176. data/spec/opal/core/string/chop_spec.rb +0 -10
  177. data/spec/opal/core/string/chr_spec.rb +0 -13
  178. data/spec/opal/core/string/clone_spec.rb +0 -8
  179. data/spec/opal/core/string/comparison_spec.rb +0 -13
  180. data/spec/opal/core/string/dup_spec.rb +0 -8
  181. data/spec/opal/core/string/element_reference_spec.rb +0 -96
  182. data/spec/opal/core/string/fixtures/classes.rb +0 -49
  183. data/spec/opal/core/string/format_spec.rb +0 -9
  184. data/spec/opal/core/string/freeze_spec.rb +0 -15
  185. data/spec/opal/core/string/gsub_spec.rb +0 -31
  186. data/spec/opal/core/string/lines_spec.rb +0 -9
  187. data/spec/opal/core/string/ljust_spec.rb +0 -32
  188. data/spec/opal/core/string/lstrip_spec.rb +0 -7
  189. data/spec/opal/core/string/match_spec.rb +0 -49
  190. data/spec/opal/core/string/next_spec.rb +0 -10
  191. data/spec/opal/core/string/ord_spec.rb +0 -9
  192. data/spec/opal/core/string/partition_spec.rb +0 -10
  193. data/spec/opal/core/string/rindex_spec.rb +0 -50
  194. data/spec/opal/core/string/rjust_spec.rb +0 -32
  195. data/spec/opal/core/string/rstrip_spec.rb +0 -7
  196. data/spec/opal/core/string/scan_spec.rb +0 -66
  197. data/spec/opal/core/string/slice_spec.rb +0 -74
  198. data/spec/opal/core/string/split_spec.rb +0 -5
  199. data/spec/opal/core/string/strip_spec.rb +0 -6
  200. data/spec/opal/core/string/sub_spec.rb +0 -38
  201. data/spec/opal/core/string/succ_spec.rb +0 -10
  202. data/spec/opal/core/string/sum_spec.rb +0 -5
  203. data/spec/opal/core/string/to_f_spec.rb +0 -14
  204. data/spec/opal/core/string/to_i_spec.rb +0 -25
  205. data/spec/opal/core/string/tr_s_spec.rb +0 -31
  206. data/spec/opal/core/string/tr_spec.rb +0 -31
  207. data/spec/opal/filters/bugs/parser.rb +0 -10
  208. data/spec/opal/filters/unsupported/immutable_strings.rb +0 -24
  209. data/spec/opal/filters/unsupported/string_subclasses.rb +0 -8
  210. data/spec/opal/parser/alias_spec.rb +0 -26
  211. data/spec/opal/parser/and_spec.rb +0 -13
  212. data/spec/opal/parser/array_spec.rb +0 -22
  213. data/spec/opal/parser/attrasgn_spec.rb +0 -28
  214. data/spec/opal/parser/begin_spec.rb +0 -42
  215. data/spec/opal/parser/block_spec.rb +0 -12
  216. data/spec/opal/parser/break_spec.rb +0 -17
  217. data/spec/opal/parser/call_spec.rb +0 -131
  218. data/spec/opal/parser/class_spec.rb +0 -35
  219. data/spec/opal/parser/const_spec.rb +0 -13
  220. data/spec/opal/parser/cvar_spec.rb +0 -11
  221. data/spec/opal/parser/def_spec.rb +0 -61
  222. data/spec/opal/parser/false_spec.rb +0 -17
  223. data/spec/opal/parser/file_spec.rb +0 -7
  224. data/spec/opal/parser/gvar_spec.rb +0 -13
  225. data/spec/opal/parser/hash_spec.rb +0 -17
  226. data/spec/opal/parser/heredoc_spec.rb +0 -30
  227. data/spec/opal/parser/iasgn_spec.rb +0 -9
  228. data/spec/opal/parser/if_spec.rb +0 -26
  229. data/spec/opal/parser/int_spec.rb +0 -13
  230. data/spec/opal/parser/iter_spec.rb +0 -59
  231. data/spec/opal/parser/ivar_spec.rb +0 -9
  232. data/spec/opal/parser/lambda_spec.rb +0 -64
  233. data/spec/opal/parser/lasgn_spec.rb +0 -8
  234. data/spec/opal/parser/line_spec.rb +0 -8
  235. data/spec/opal/parser/lvar_spec.rb +0 -38
  236. data/spec/opal/parser/masgn_spec.rb +0 -37
  237. data/spec/opal/parser/module_spec.rb +0 -27
  238. data/spec/opal/parser/nil_spec.rb +0 -17
  239. data/spec/opal/parser/not_spec.rb +0 -27
  240. data/spec/opal/parser/nth_ref_spec.rb +0 -13
  241. data/spec/opal/parser/op_asgn1_spec.rb +0 -23
  242. data/spec/opal/parser/op_asgn2_spec.rb +0 -23
  243. data/spec/opal/parser/or_spec.rb +0 -13
  244. data/spec/opal/parser/parse_spec.rb +0 -66
  245. data/spec/opal/parser/regexp_spec.rb +0 -16
  246. data/spec/opal/parser/return_spec.rb +0 -17
  247. data/spec/opal/parser/sclass_spec.rb +0 -21
  248. data/spec/opal/parser/self_spec.rb +0 -17
  249. data/spec/opal/parser/str_spec.rb +0 -107
  250. data/spec/opal/parser/string_spec.rb +0 -8
  251. data/spec/opal/parser/super_spec.rb +0 -20
  252. data/spec/opal/parser/true_spec.rb +0 -17
  253. data/spec/opal/parser/undef_spec.rb +0 -15
  254. data/spec/opal/parser/unless_spec.rb +0 -13
  255. data/spec/opal/parser/while_spec.rb +0 -15
  256. data/spec/opal/parser/xstr_spec.rb +0 -116
  257. data/spec/opal/parser/yield_spec.rb +0 -20
@@ -1,9 +0,0 @@
1
- describe "String#%" do
2
- it "returns formatted string as same as Kernel#format" do
3
- ("%-5d" % 123).should == "123 "
4
- end
5
-
6
- it "can accept multiple arguments by passing them in an array" do
7
- ("%d %s" % [456, "foo"]).should == "456 foo"
8
- end
9
- end
@@ -1,15 +0,0 @@
1
- # Just accepting reality of immutability
2
-
3
- describe "String#freeze" do
4
- it "is always frozen" do
5
- s = "a string"
6
- s.frozen?.should be_true
7
- s.freeze
8
- s.frozen?.should be_true
9
- end
10
-
11
- it "returns self" do
12
- s = "a string"
13
- s.freeze.should equal(s)
14
- end
15
- end
@@ -1,31 +0,0 @@
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
-
18
- it "should set the global match variable $~ inside block" do
19
- match_data = nil
20
- "hello".gsub(/(.)(.)(.)(.)(.)/) { match_data = $~; $~[1] }.should == "h"
21
- match_data.length.should == 6
22
- match_data.should == ["hello", "h", "e", "l", "l", "o"]
23
- end
24
-
25
- it "should replace match group with undefined value with nil in match array" do
26
- match_data = nil
27
- "def".gsub(/(a(b)c)?d(e)f/) { match_data = $~; $~[3] }.should == "e"
28
- match_data.length.should == 4
29
- match_data.should == ["def", nil, nil, "e"]
30
- end
31
- end
@@ -1,9 +0,0 @@
1
- describe "String#lines" do
2
- it "should split on the default record separator and return enumerator if not block is given" do
3
- "first\nsecond\nthird".lines.class.should == Enumerator
4
- "first\nsecond\nthird".lines.entries.class.should == Array
5
- "first\nsecond\nthird".lines.entries.size.should == 3
6
- "first\nsecond\nthird".lines.entries.should == ["first\n", "second\n", "third"]
7
- "first\nsecond\nthird\n".lines.entries.should == ["first\n", "second\n", "third\n"]
8
- end
9
- end
@@ -1,32 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../fixtures/classes.rb', __FILE__)
3
-
4
- describe "String#ljust" do
5
- it "returns a new string of specified length with self left justified and padded with padstr" do
6
- "hello".ljust(20, '1234').should == "hello123412341234123"
7
-
8
- "".ljust(1, "abcd").should == "a"
9
- "".ljust(2, "abcd").should == "ab"
10
- "".ljust(3, "abcd").should == "abc"
11
- "".ljust(4, "abcd").should == "abcd"
12
- "".ljust(6, "abcd").should == "abcdab"
13
-
14
- "OK".ljust(3, "abcd").should == "OKa"
15
- "OK".ljust(4, "abcd").should == "OKab"
16
- "OK".ljust(6, "abcd").should == "OKabcd"
17
- "OK".ljust(8, "abcd").should == "OKabcdab"
18
- end
19
-
20
- it "pads with whitespace if no padstr is given" do
21
- "hello".ljust(20).should == "hello "
22
- end
23
-
24
- it "returns self if it's longer than or as long as the specified length" do
25
- "".ljust(0).should == ""
26
- "".ljust(-1).should == ""
27
- "hello".ljust(4).should == "hello"
28
- "hello".ljust(-1).should == "hello"
29
- "this".ljust(3).should == "this"
30
- "radiology".ljust(8, '-').should == "radiology"
31
- end
32
- end
@@ -1,7 +0,0 @@
1
- describe "String#lstrip" do
2
- it "returns a copy of self with leading whitespace removed" do
3
- " hello ".lstrip.should == "hello "
4
- " hello world ".lstrip.should == "hello world "
5
- "hello".lstrip.should == "hello"
6
- end
7
- end
@@ -1,49 +0,0 @@
1
- describe "String#=~" do
2
- it "sets $~ to MatchData when there is a match and nil then there's none" do
3
- 'hello' =~ /(l)(l)/
4
- $~.should be_kind_of(MatchData)
5
- $~[0].should == "ll"
6
- $~.captures.should == ["l", "l"]
7
- $~.pre_match.should == "he"
8
- $`.should == "he"
9
- $~.post_match.should == "o"
10
- $'.should == "o"
11
-
12
- 'hello' =~ /not/
13
- $~.should == nil
14
- $`.should == nil
15
- $'.should == nil
16
- end
17
- end
18
-
19
- describe "String#match" do
20
- it "matches the pattern against self" do
21
- 'hello'.match(/(.)\1/)[0].should == "ll"
22
- end
23
-
24
- it "returns nil if there's no match" do
25
- 'hello'.match('xx').should == nil
26
- end
27
-
28
- it "sets $~ to MatchData of match or nil when there is none" do
29
- 'hello'.match(/./)
30
- $~[0].should == 'h'
31
-
32
- 'hello'.match(/X/)
33
- $~.should == nil
34
- end
35
-
36
- it "sets $` to pre_match and $' to post_match or nil when there is no match" do
37
- result = 'hello'.match(/ll/)
38
- $`.should == 'he'
39
- $'.should == 'o'
40
- result.pre_match.should == 'he'
41
- result.post_match.should == 'o'
42
-
43
- 'hello'.match(/X/)
44
- $`.should == nil
45
- $'.should == nil
46
- result.pre_match.should == 'he'
47
- result.post_match.should == 'o'
48
- end
49
- end
@@ -1,10 +0,0 @@
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
@@ -1,9 +0,0 @@
1
- describe "String#ord" do
2
- it "returns a Fixnum" do
3
- 'a'.ord.should be_kind_of(Numeric)
4
- end
5
-
6
- it "returns the codepoint of the first character in the String" do
7
- 'a'.ord.should == 97
8
- end
9
- end
@@ -1,10 +0,0 @@
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
@@ -1,50 +0,0 @@
1
- describe "String#rindex" do
2
- it "should return index of last occurrence of search string" do
3
- "camal".rindex("a").should == 3
4
- end
5
-
6
- it "should return index of last occurrence of RegExp match" do
7
- "camal".rindex(/a/).should == 3
8
- end
9
-
10
- it "should return nil if non-empty search is not found" do
11
- "camal".rindex("x").should be_nil
12
- end
13
-
14
- it "should return nil if RegExp is not matched" do
15
- "camal".rindex(/x/).should be_nil
16
- end
17
-
18
- it "should return 0 if string and search string are both empty" do
19
- "".rindex("").should == 0
20
- end
21
-
22
- it "should return index of last occurrence of search string from offset" do
23
- "camal".rindex("a", 1).should == 1
24
- "camal".rindex(/a/, 4).should == 3
25
- "camal".rindex("a", -4).should == 1
26
- "camal".rindex("a", -2).should == 3
27
- end
28
-
29
- it "should return index of last occurrence of RegExp match from offset" do
30
- "camal".rindex(/a/, 1).should == 1
31
- "camal".rindex(/a/, 4).should == 3
32
- "camal".rindex(/a/, -4).should == 1
33
- "camal".rindex(/a/, -2).should == 3
34
- end
35
-
36
- it "should return nil of last occurrence is after offset" do
37
- "camal".rindex("a", 0).should be_nil
38
- "camal".rindex("a", -5).should be_nil
39
- end
40
-
41
- it "should return nil of last occurrence of RegExp match is after offset" do
42
- "camal".rindex(/a/, 0).should be_nil
43
- "camal".rindex(/a/, -5).should be_nil
44
- end
45
-
46
- it "should raise TypeError if search is not String or RegExp" do
47
- lambda { "camal".rindex(nil) }.should raise_error(TypeError)
48
- lambda { "camal".rindex(1) }.should raise_error(TypeError)
49
- end
50
- end
@@ -1,32 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../fixtures/classes.rb', __FILE__)
3
-
4
- describe "String#rjust with length, padding" do
5
- it "returns a new string of specified length with self right justified and padded with padstr" do
6
- "hello".rjust(20, '1234').should == "123412341234123hello"
7
-
8
- "".rjust(1, "abcd").should == "a"
9
- "".rjust(2, "abcd").should == "ab"
10
- "".rjust(3, "abcd").should == "abc"
11
- "".rjust(4, "abcd").should == "abcd"
12
- "".rjust(6, "abcd").should == "abcdab"
13
-
14
- "OK".rjust(3, "abcd").should == "aOK"
15
- "OK".rjust(4, "abcd").should == "abOK"
16
- "OK".rjust(6, "abcd").should == "abcdOK"
17
- "OK".rjust(8, "abcd").should == "abcdabOK"
18
- end
19
-
20
- it "pads with whitespace if no padstr is given" do
21
- "hello".rjust(20).should == " hello"
22
- end
23
-
24
- it "returns self if it's longer than or as long as the specified length" do
25
- "".rjust(0).should == ""
26
- "".rjust(-1).should == ""
27
- "hello".rjust(4).should == "hello"
28
- "hello".rjust(-1).should == "hello"
29
- "this".rjust(3).should == "this"
30
- "radiology".rjust(8, '-').should == "radiology"
31
- end
32
- end
@@ -1,7 +0,0 @@
1
- describe "String#rstrip" do
2
- it "returns a copy of self with trailing whitespace removed" do
3
- " hello ".rstrip.should == " hello"
4
- " hello world ".rstrip.should == " hello world"
5
- "hello".rstrip.should == "hello"
6
- end
7
- end
@@ -1,66 +0,0 @@
1
- describe "String#scan" do
2
- VOWELS_RE_NO_CAPTURE = /[aeiou]/
3
- VOWELS_RE_CAPTURE = /([aeiou])/
4
- it "should create one-dimensional array of matches if pattern has no capture groups" do
5
- result = "string-a-long".scan(VOWELS_RE_NO_CAPTURE)
6
- result.should be_kind_of(Array)
7
- result.size.should == 3
8
- result.should == ["i", "a", "o"]
9
- end
10
-
11
- it "should create two-dimensional array of matches if pattern has capture groups" do
12
- result = "string-a-long".scan(VOWELS_RE_CAPTURE)
13
- result.should be_kind_of(Array)
14
- result.size.should == 3
15
- result.should == [["i"], ["a"], ["o"]]
16
- end
17
-
18
- it "should set implicit match variables to final match when no block" do
19
- result = "string-a-long".scan(VOWELS_RE_CAPTURE)
20
- $~.should be_kind_of(MatchData)
21
- $~.size.should == 2
22
- $~[0].should == "o"
23
- $~[1].should == "o"
24
- $`.should == "string-a-l"
25
- $~.pre_match.should == "string-a-l"
26
- $'.should == "ng"
27
- $~.post_match == "ng"
28
- result.should == [["i"], ["a"], ["o"]]
29
- end
30
-
31
- it "should set implicit match variables for each iteration of block" do
32
- iterations = 0
33
- vals = {
34
- :match_data => [],
35
- :capture1 => [],
36
- :pre_match => [],
37
- :post_match => []
38
- }
39
-
40
- result = "string-a-long".scan(VOWELS_RE_CAPTURE) do |capture1|
41
- iterations += 1
42
- vals[:match_data] << $~
43
- vals[:capture1] << capture1
44
- vals[:pre_match] << $`
45
- vals[:post_match] << $'
46
- end
47
-
48
- iterations.should == 3
49
- vals[:capture1].should == ["i", "a", "o"]
50
- vals[:pre_match].should == ["str", "string-", "string-a-l"]
51
- vals[:post_match].should == ["ng-a-long", "-long", "ng"]
52
- vals[:match_data][0][0].should == "i"
53
- vals[:match_data][1][0].should == "a"
54
- vals[:match_data][2][0].should == "o"
55
- result.should == "string-a-long"
56
- end
57
-
58
- # this verifies that the lastIndex is reset before using the regexp
59
- it "should get same matches on consecutive runs" do
60
- re = Regexp.new('[aeiou]', 'g')
61
- results1 = 'hello'.scan(re)
62
- results2 = 'hello'.scan(re)
63
- results1.should == ["e", "o"]
64
- results2.should == results1
65
- end
66
- end
@@ -1,74 +0,0 @@
1
- require File.expand_path('../fixtures/classes', __FILE__)
2
-
3
- describe "String#slice" do
4
- it "returns the character code of the character at the given index" do
5
- "hello".slice(0).should == "h"
6
- "hello".slice(-1).should == "o"
7
- end
8
-
9
- it "returns nil if index is outside of self" do
10
- "hello".slice(20).should == nil
11
- "hello".slice(-20).should == nil
12
-
13
- "".slice(0).should == nil
14
- "".slice(-1).should == nil
15
- end
16
- end
17
-
18
- describe "String#slice with index, length" do
19
- it "returns the substring starting at the given index with the given length" do
20
- "hello there".slice(0, 0).should == ""
21
- "hello there".slice(0, 1).should == "h"
22
- "hello there".slice(0, 3).should == "hel"
23
- "hello there".slice(0, 6).should == "hello "
24
- "hello there".slice(0, 9).should == "hello the"
25
- "hello there".slice(0, 12).should == "hello there"
26
-
27
- "hello there".slice(1, 0).should == ""
28
- "hello there".slice(1, 1).should == "e"
29
- "hello there".slice(1, 3).should == "ell"
30
- "hello there".slice(1, 6).should == "ello t"
31
- "hello there".slice(1, 9).should == "ello ther"
32
- "hello there".slice(1, 12).should == "ello there"
33
-
34
- "hello there".slice(3, 0).should == ""
35
- "hello there".slice(3, 1).should == "l"
36
- "hello there".slice(3, 3).should == "lo "
37
- "hello there".slice(3, 6).should == "lo the"
38
- "hello there".slice(3, 9).should == "lo there"
39
-
40
- "hello there".slice(4, 0).should == ""
41
- "hello there".slice(4, 3).should == "o t"
42
- "hello there".slice(4, 6).should == "o ther"
43
- "hello there".slice(4, 9).should == "o there"
44
-
45
- "foo".slice(2, 1).should == "o"
46
- "foo".slice(3, 0).should == ""
47
- "foo".slice(3, 1).should == ""
48
-
49
- "".slice(0, 0).should == ""
50
- "".slice(0, 1).should == ""
51
-
52
- "x".slice(0, 0).should == ""
53
- "x".slice(0, 1).should == "x"
54
- "x".slice(1, 0).should == ""
55
- "x".slice(-1, 1).should == "x"
56
- end
57
-
58
- it "returns nil if the offset falls outside of self" do
59
- "hello there".slice(20, 3).should == nil
60
- "hello there".slice(-20, 3).should == nil
61
-
62
- "".slice(1, 0).should == nil
63
- "".slice(1, 1).should == nil
64
-
65
- "".slice(2, 0).should == nil
66
- "".slice(2, 1).should == nil
67
-
68
- "x".slice(2, 0).should == nil
69
- "x".slice(2, 1).should == nil
70
-
71
- "x".slice(-2, 0).should == nil
72
- "x".slice(-2, 1).should == nil
73
- end
74
- end
@@ -1,5 +0,0 @@
1
- describe "String#split with String" do
2
- it "returns an array of substrings based on splitting on the given string" do
3
- "mellow yellow".split("ello").should == ["m", "w y", "w"]
4
- end
5
- end
@@ -1,6 +0,0 @@
1
- describe "String#strip" do
2
- it "returns a new string with leading and trailing whitespace removed" do
3
- " hello ".strip.should == "hello"
4
- " hello world ".strip.should == "hello world"
5
- end
6
- end