opal 0.3.29 → 0.3.30

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.
@@ -0,0 +1,122 @@
1
+ describe "Kernel#format" do
2
+ it "should prepend '+' or '-' for numbers when flag '+' is specified" do
3
+ format("%+d", 1).should == "+1"
4
+ format("%+x", -1).should == "-1"
5
+ end
6
+
7
+ it "should prepend ' ' or '-' for numbers when flag ' ' is specified" do
8
+ format("% d", 1).should == " 1"
9
+ format("% x", 1).should == " 1"
10
+ format("% x", -1).should == "-1"
11
+ end
12
+
13
+ it "should align left when flag '-' is specified" do
14
+ format("%-5d", 123).should == "123 "
15
+ end
16
+
17
+ it "should prepend '0's when flag '0' is specified" do
18
+ format("%010d", 10).should == "0000000010"
19
+ end
20
+
21
+ it "should output at least specified number of characters when width is specified" do
22
+ format("%5d", 123).should == " 123"
23
+ format("%+5d", 11).should == " +11"
24
+ format("%+-5d", 11).should == "+11 "
25
+ format("%+05d", 11).should == "+0011"
26
+
27
+ format("%0*x", 5, 10).should == "0000a"
28
+ end
29
+
30
+ describe "with precision" do
31
+ it "should output specified number of digits for integers" do
32
+ format("%10.5d", 1).should == " 00001"
33
+ format("%+10.5x", 1).should == " +00001"
34
+ end
35
+
36
+ it "should output specified number of significant digits for floats" do
37
+ format("%10.5f", 1).should == " 1.00000"
38
+ format("%10.5f", 10).should == " 10.00000"
39
+
40
+ (format("%10.5e", 1) =~ /^1\.00000e\+0*0$/).should_not == nil
41
+ (format("%10.5e", 10) =~ /^1\.00000e\+0*1$/).should_not == nil
42
+ end
43
+
44
+ it "should output at most specified number of characters for strings" do
45
+ format("%10.2s", "foo").should == " fo"
46
+
47
+ format("%5.5s", "foo").should == " foo"
48
+ format("%5.5s", "foobar").should == "fooba"
49
+
50
+ format("%.5s", "foobar").should == "fooba"
51
+ format("%.*s", 5, "foobar").should == "fooba"
52
+ end
53
+ end
54
+
55
+ it "should format a character with specifier 'c'" do
56
+ format("%c", 97).should == "a"
57
+ format("%c", 'a').should == "a"
58
+ end
59
+
60
+ it "should format a string with specifier 's'" do
61
+ format("%s", "foo").should == "foo"
62
+ end
63
+
64
+ it "should format an object with specifier 'p'" do
65
+ format("%p", "foo").should == "\"foo\""
66
+ end
67
+
68
+ it "should format an integer with specifier 'd' or 'i'" do
69
+ format("%d", -1).should == "-1"
70
+ format("%d", 3.1).should == "3"
71
+ end
72
+
73
+ it "should format an integer with specifier 'u'" do
74
+ format("%u", -1).should == "-1"
75
+ end
76
+
77
+ it "should format an integer with specifier 'b', 'B', 'o', 'x' or 'X'" do
78
+ format("%b", 10).should == "1010"
79
+ format("%B", 10).should == "1010"
80
+ format("%o", 10).should == "12"
81
+ format("%x", 10).should == "a"
82
+ format("%X", 10).should == "A"
83
+
84
+ format("%x", -1).should == "-1" # incompatible
85
+ end
86
+
87
+ it "should format a floating number with specifier 'f', 'e', 'E', 'g' or 'G'" do
88
+ format("%f", 1.0).should == "1.000000"
89
+ (format("%e", 1.0) =~ /^1\.000000e\+0*0$/).should_not == nil
90
+ format("%g", 1.0).should == "1.00000" # incompatible
91
+
92
+ format("%f", 10.1).should == "10.100000"
93
+ (format("%E", 10.1) =~ /^1\.010000E\+0*1$/).should_not == nil
94
+ format("%g", 10.1).should == "10.1000" # incompatible
95
+
96
+ (format("%g", 1000000) =~ /^1.00000e\+0*6$/).should_not == nil
97
+ (format("%G", 0.0000001) =~ /^1.00000E-0*7$/).should_not == nil
98
+ end
99
+
100
+ it "should format special floating number special values" do
101
+ format("%f", 1.0/0).should == "Infinity"
102
+ format("%f", -1.0/0).should == "-Infinity"
103
+ format("%f", 0.0/0).should == "NaN"
104
+
105
+ format("%E", 1.0/0).should == "INFINITY"
106
+ format("%E", -1.0/0).should == "-INFINITY"
107
+ format("%E", 0.0/0).should == "NAN"
108
+ end
109
+
110
+ it "should take specified index of argument if '$' is specified" do
111
+ format("%d, %x, %o", 1, 2, 3).should == "1, 2, 3"
112
+ format("%3$d, %2$x, %1$o", 1, 2, 3).should == "3, 2, 1"
113
+
114
+ format("%1$d, %1$x, %1$o", 10).should == "10, a, 12"
115
+
116
+ format("%1$*2$.*3$f", 1, 5, 2).should == " 1.00"
117
+ end
118
+
119
+ it "can output % by escaping it" do
120
+ format("%%").should == "%"
121
+ end
122
+ end
@@ -0,0 +1,31 @@
1
+ module KernelSpecs
2
+ class Foo
3
+ def bar
4
+ 'done'
5
+ end
6
+
7
+ def self.baz
8
+ 'class done'
9
+ end
10
+ end
11
+ end
12
+
13
+ describe "Kernel#method" do
14
+ it "returns a method object for a valid method" do
15
+ m = KernelSpecs::Foo.new.method(:bar)
16
+ m.should be_kind_of(Method)
17
+ m.call.should == 'done'
18
+ end
19
+
20
+ it "returns a method object for a valid singleton method" do
21
+ m = KernelSpecs::Foo.method(:baz)
22
+ m.should be_kind_of Method
23
+ m.call.should == 'class done'
24
+ end
25
+
26
+ it "raises a NameError for an invalid method name" do
27
+ lambda {
28
+ KernelSpecs::Foo.new.method(:invalid_and_silly_method_name)
29
+ }.should raise_error(NameError)
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ describe "Kernel#printf" do
2
+ it "returns nil if called with no arguments" do
3
+ printf.should == nil
4
+ end
5
+
6
+ it "returns nil if called with arguments" do
7
+ printf("%d", 123).should == nil
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ describe "Kernel#sprintf" do
2
+ it "returns formatted string as same as Kernel#format" do
3
+ sprintf("%5d", 123).should == " 123"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
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
@@ -28,6 +28,10 @@ describe "The begin keyword" do
28
28
  opal_parse('begin 1; rescue => @a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:iasgn, :@a, [:gvar, :$!]]], [:lit, 2]]]
29
29
  opal_parse('begin 1; rescue Klass => @a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:const, :Klass],[:iasgn, :@a, [:gvar, :$!]]], [:lit, 2]]]
30
30
  end
31
+
32
+ it "should parse newline right after rescue" do
33
+ opal_parse("begin; 1; rescue\n 2; end").should == [:rescue, [:lit, 1], [:resbody, [:array], [:lit, 2]]]
34
+ end
31
35
  end
32
36
 
33
37
  describe "with an 'ensure' block" do
@@ -80,4 +80,25 @@ describe "Optional paren calls" do
80
80
  opal_parse("x (1).y").should == [:call, nil, :x, [:arglist, [:call, [:lit, 1], :y, [:arglist]]]]
81
81
  opal_parse("x(1).y").should == [:call, [:call, nil, :x, [:arglist, [:lit, 1]]], :y, [:arglist]]
82
82
  end
83
- end
83
+ end
84
+
85
+ describe "Operator precedence" do
86
+ it "should be raised with parentheses" do
87
+ opal_parse("(1 + 2) + (3 - 4)").should == [:operator, :+,
88
+ [:operator, :+, [:lit, 1], [:lit, 2]],
89
+ [:operator, :-, [:lit, 3], [:lit, 4]],
90
+ ]
91
+ opal_parse("(1 + 2) - (3 - 4)").should == [:operator, :-,
92
+ [:operator, :+, [:lit, 1], [:lit, 2]],
93
+ [:operator, :-, [:lit, 3], [:lit, 4]],
94
+ ]
95
+ opal_parse("(1 + 2) * (3 - 4)").should == [:operator, :*,
96
+ [:operator, :+, [:lit, 1], [:lit, 2]],
97
+ [:operator, :-, [:lit, 3], [:lit, 4]],
98
+ ]
99
+ opal_parse("(1 + 2) / (3 - 4)").should == [:operator, :/,
100
+ [:operator, :+, [:lit, 1], [:lit, 2]],
101
+ [:operator, :-, [:lit, 3], [:lit, 4]],
102
+ ]
103
+ end
104
+ end
@@ -0,0 +1,32 @@
1
+ describe "Opal.parse_erb" do
2
+ before do
3
+ @simple = opal_eval_compiled(Opal.parse_erb("<div><%= @some_data %></div>", "simple_test"))
4
+ @quoted = opal_eval_compiled(Opal.parse_erb('<div class="foo">hello <%= "there " + @name %></div>', "quoted_test"))
5
+ end
6
+
7
+ it "should be an instance of Template" do
8
+ @simple.should be_kind_of(Template)
9
+ end
10
+
11
+ it "calling the block with a context should render the block" do
12
+ @some_data = "hello"
13
+ @simple.render(self).should == "<div>hello</div>"
14
+ end
15
+
16
+ it "should accept quotes in strings" do
17
+ @name = "adam"
18
+ @quoted.render(self).should == "<div class=\"foo\">hello there adam</div>"
19
+ end
20
+
21
+ it 'stores created templates in Template[] by name' do
22
+ Template['simple_test'].should == @simple
23
+ Template['quoted_test'].should == @quoted
24
+ end
25
+
26
+ describe '.parse' do
27
+ it 'parses erb content by running it through compiler' do
28
+ opal_eval_compiled Opal.parse_erb("hi there", 'test_parse')
29
+ Template['test_parse'].should be_kind_of(Template)
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,9 @@
1
1
  describe "Opal::Parser" do
2
2
  it "should parse simple ruby values" do
3
3
  opal_eval('3.142').should == 3.142
4
+ opal_eval('123e1').should == 1230.0
5
+ opal_eval('123E+10').should == 1230000000000.0
6
+ opal_eval('123e-9').should == 0.000000123
4
7
  opal_eval('false').should == false
5
8
  opal_eval('true').should == true
6
9
  opal_eval('nil').should == nil
@@ -41,4 +44,4 @@ describe "Opal::Parser" do
41
44
  ParserClassDefinition.bar.should == 42
42
45
  ParserClassDefinition.new.foo.should == 500
43
46
  end
44
- end
47
+ end
data/spec/spec_helper.rb CHANGED
@@ -9,6 +9,10 @@ module Kernel
9
9
  def opal_parse(str, file='(string)')
10
10
  Opal::Grammar.new.parse str, file
11
11
  end
12
+
13
+ def opal_eval_compiled(javascript)
14
+ `eval(javascript)`
15
+ end
12
16
  end
13
17
 
14
18
  OpalSpec::Runner.autorun
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.29
4
+ version: 0.3.30
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-28 00:00:00.000000000Z
12
+ date: 2012-12-02 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby runtime and core library for javascript.
15
15
  email: adam.beynon@gmail.com
@@ -31,6 +31,7 @@ files:
31
31
  - core/boolean.rb
32
32
  - core/class.rb
33
33
  - core/comparable.rb
34
+ - core/date.rb
34
35
  - core/enumerable.rb
35
36
  - core/error.rb
36
37
  - core/hash.rb
@@ -52,6 +53,7 @@ files:
52
53
  - core/time.rb
53
54
  - lib/opal.rb
54
55
  - lib/opal/builder.rb
56
+ - lib/opal/erb.rb
55
57
  - lib/opal/grammar.rb
56
58
  - lib/opal/grammar.y
57
59
  - lib/opal/lexer.rb
@@ -133,6 +135,9 @@ files:
133
135
  - spec/core/class/instance_methods_spec.rb
134
136
  - spec/core/class/last_value_spec.rb
135
137
  - spec/core/class/new_spec.rb
138
+ - spec/core/date/new_spec.rb
139
+ - spec/core/date/to_s_spec.rb
140
+ - spec/core/date/today_spec.rb
136
141
  - spec/core/enumerable/all_spec.rb
137
142
  - spec/core/enumerable/any_spec.rb
138
143
  - spec/core/enumerable/collect_spec.rb
@@ -207,18 +212,22 @@ files:
207
212
  - spec/core/kernel/equal_spec.rb
208
213
  - spec/core/kernel/equal_value_spec.rb
209
214
  - spec/core/kernel/extend_spec.rb
215
+ - spec/core/kernel/format_spec.rb
210
216
  - spec/core/kernel/instance_eval_spec.rb
211
217
  - spec/core/kernel/instance_variable_get_spec.rb
212
218
  - spec/core/kernel/instance_variable_set_spec.rb
213
219
  - spec/core/kernel/loop_spec.rb
214
220
  - spec/core/kernel/match_spec.rb
221
+ - spec/core/kernel/method_spec.rb
215
222
  - spec/core/kernel/methods_spec.rb
216
223
  - spec/core/kernel/nil_spec.rb
217
224
  - spec/core/kernel/p_spec.rb
225
+ - spec/core/kernel/printf_spec.rb
218
226
  - spec/core/kernel/proc_spec.rb
219
227
  - spec/core/kernel/rand_spec.rb
220
228
  - spec/core/kernel/respond_to_spec.rb
221
229
  - spec/core/kernel/send_spec.rb
230
+ - spec/core/kernel/sprintf_spec.rb
222
231
  - spec/core/kernel/tap_spec.rb
223
232
  - spec/core/kernel/to_json_spec.rb
224
233
  - spec/core/kernel/to_s_spec.rb
@@ -298,6 +307,7 @@ files:
298
307
  - spec/core/string/element_reference_spec.rb
299
308
  - spec/core/string/empty_spec.rb
300
309
  - spec/core/string/end_with_spec.rb
310
+ - spec/core/string/format_spec.rb
301
311
  - spec/core/string/gsub_spec.rb
302
312
  - spec/core/string/include_spec.rb
303
313
  - spec/core/string/intern_spec.rb
@@ -412,6 +422,7 @@ files:
412
422
  - spec/language/variables_spec.rb
413
423
  - spec/language/while_spec.rb
414
424
  - spec/language/yield_spec.rb
425
+ - spec/parser/erb_spec.rb
415
426
  - spec/parser/simple_spec.rb
416
427
  - spec/spec_helper.rb
417
428
  - spec/test_case.html
@@ -435,7 +446,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
435
446
  version: '0'
436
447
  requirements: []
437
448
  rubyforge_project:
438
- rubygems_version: 1.8.11
449
+ rubygems_version: 1.8.24
439
450
  signing_key:
440
451
  specification_version: 3
441
452
  summary: Ruby runtime and core library for javascript
@@ -513,6 +524,9 @@ test_files:
513
524
  - spec/core/class/instance_methods_spec.rb
514
525
  - spec/core/class/last_value_spec.rb
515
526
  - spec/core/class/new_spec.rb
527
+ - spec/core/date/new_spec.rb
528
+ - spec/core/date/to_s_spec.rb
529
+ - spec/core/date/today_spec.rb
516
530
  - spec/core/enumerable/all_spec.rb
517
531
  - spec/core/enumerable/any_spec.rb
518
532
  - spec/core/enumerable/collect_spec.rb
@@ -587,18 +601,22 @@ test_files:
587
601
  - spec/core/kernel/equal_spec.rb
588
602
  - spec/core/kernel/equal_value_spec.rb
589
603
  - spec/core/kernel/extend_spec.rb
604
+ - spec/core/kernel/format_spec.rb
590
605
  - spec/core/kernel/instance_eval_spec.rb
591
606
  - spec/core/kernel/instance_variable_get_spec.rb
592
607
  - spec/core/kernel/instance_variable_set_spec.rb
593
608
  - spec/core/kernel/loop_spec.rb
594
609
  - spec/core/kernel/match_spec.rb
610
+ - spec/core/kernel/method_spec.rb
595
611
  - spec/core/kernel/methods_spec.rb
596
612
  - spec/core/kernel/nil_spec.rb
597
613
  - spec/core/kernel/p_spec.rb
614
+ - spec/core/kernel/printf_spec.rb
598
615
  - spec/core/kernel/proc_spec.rb
599
616
  - spec/core/kernel/rand_spec.rb
600
617
  - spec/core/kernel/respond_to_spec.rb
601
618
  - spec/core/kernel/send_spec.rb
619
+ - spec/core/kernel/sprintf_spec.rb
602
620
  - spec/core/kernel/tap_spec.rb
603
621
  - spec/core/kernel/to_json_spec.rb
604
622
  - spec/core/kernel/to_s_spec.rb
@@ -678,6 +696,7 @@ test_files:
678
696
  - spec/core/string/element_reference_spec.rb
679
697
  - spec/core/string/empty_spec.rb
680
698
  - spec/core/string/end_with_spec.rb
699
+ - spec/core/string/format_spec.rb
681
700
  - spec/core/string/gsub_spec.rb
682
701
  - spec/core/string/include_spec.rb
683
702
  - spec/core/string/intern_spec.rb
@@ -792,6 +811,7 @@ test_files:
792
811
  - spec/language/variables_spec.rb
793
812
  - spec/language/while_spec.rb
794
813
  - spec/language/yield_spec.rb
814
+ - spec/parser/erb_spec.rb
795
815
  - spec/parser/simple_spec.rb
796
816
  - spec/spec_helper.rb
797
817
  - spec/test_case.html