Skip to content
This repository was archived by the owner on Sep 12, 2018. It is now read-only.

Commit a3925ae

Browse files
committed
Added specs for README examples.
1 parent 1c6345d commit a3925ae

File tree

2 files changed

+58
-29
lines changed

2 files changed

+58
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,10 @@ special semantics that need to be taken into account.
214214

215215
Ruby input | PHP output
216216
-------------------------------------|--------------------------------------
217-
if true then ... else ... end | if (TRUE) { ... } else { ... }
218217
... if true | if (TRUE) { ... }
219-
unless true then ... else ... end | if (!TRUE) { ... } else { ... }
218+
if true then ... else ... end | if (TRUE) { ... } else { ... }
220219
... unless true | if (!TRUE) { ... }
220+
unless true then ... else ... end | if (!TRUE) { ... } else { ... }
221221
while $x; ...; end | while ($x) { ... }
222222
until $x; ...; end | while (!$x) { ... }
223223
for $x in $y; ...; end | foreach ($y as $x) { ... }

spec/php_generator_spec.rb

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,17 @@
189189
end
190190

191191
context "instance method calls" do
192-
#it "should support instance method calls" # TODO
192+
it "should support instance method calls" do
193+
phpize('$object.method').should == '$object->method()'
194+
phpize{ $object.method }.should == '$object->method()'
195+
end
196+
end
197+
198+
context "string operators" do
199+
it "should support the . (concatenation) operator" do
200+
phpize('"123" << "456"').should == '"123" . "456"'
201+
phpize{ "123" << "456" }.should == '"123" . "456"'
202+
end
193203
end
194204

195205
context "arithmetic operators" do
@@ -246,6 +256,13 @@
246256
end
247257

248258
context "bitwise operators" do
259+
it "should support the ~ (bitwise not) operator" do
260+
phpize('~1').should == '~1'
261+
phpize{ ~1 }.should == '~1'
262+
phpize('~$x').should == '~$x'
263+
phpize{ ~$x }.should == '~$x'
264+
end
265+
249266
it "should support the & (bitwise and) operator" do
250267
phpize('1 & 0').should == '1 & 0'
251268
phpize{ 1 & 0 }.should == '1 & 0'
@@ -267,13 +284,6 @@
267284
phpize{ $a ^ $b }.should == '$a ^ $b'
268285
end
269286

270-
it "should support the ~ (bitwise not) operator" do
271-
phpize('~1').should == '~1'
272-
phpize{ ~1 }.should == '~1'
273-
phpize('~$x').should == '~$x'
274-
phpize{ ~$x }.should == '~$x'
275-
end
276-
277287
#it "should support the << (bitwise left shift) operator" # TODO
278288

279289
it "should support the >> (bitwise right shift) operator" do
@@ -361,23 +371,10 @@
361371
end
362372
end
363373

364-
context "string operators" do
365-
it "should support the . (concatenation) operator" do
366-
phpize('"123" << "456"').should == '"123" . "456"'
367-
phpize{ "123" << "456" }.should == '"123" . "456"'
368-
end
369-
end
370-
371374
context "control structures" do
372-
it "should support return statements" do
373-
phpize('return').should == 'return'
374-
phpize{ return }.should == 'return'
375-
phpize('return nil').should == 'return NULL'
376-
phpize{ return nil }.should == 'return NULL'
377-
phpize('return true').should == 'return TRUE'
378-
phpize{ return true }.should == 'return TRUE'
379-
phpize('return 42').should == 'return 42'
380-
phpize{ return 42 }.should == 'return 42'
375+
it "should support if statement modifiers" do
376+
phpize('return if true').should == 'if (TRUE) { return; }'
377+
phpize{ return if true }.should == 'if (TRUE) { return; }'
381378
end
382379

383380
it "should support if statements with an empty then branch" do
@@ -397,6 +394,11 @@
397394

398395
#it "should support if/elseif statements" # TODO
399396

397+
it "should support unless statement modifiers" do
398+
phpize('return unless true').should == 'if (!TRUE) { return; }'
399+
phpize{ return unless true }.should == 'if (!TRUE) { return; }'
400+
end
401+
400402
it "should support unless statements with an empty then branch" do
401403
# FIXME: the parse tree for this is at present indistinguishable from
402404
# "if true then end", so there probably is a bug in RubyParser:
@@ -416,12 +418,39 @@
416418
phpize{ unless false then 1 else 0 end }.should == 'if (FALSE) { 0; } else { 1; }'
417419
end
418420

419-
#it "should support while statements" # TODO
421+
it "should support while statements" do
422+
phpize('while true; sleep 1; end').should == 'while (TRUE) { sleep(1); }'
423+
phpize{ while true; sleep 1; end }.should == 'while (TRUE) { sleep(1); }'
424+
end
425+
426+
it "should support until statements" do
427+
phpize('until true; sleep 1; end').should == 'while (!TRUE) { sleep(1); }'
428+
phpize{ until true; sleep 1; end }.should == 'while (!TRUE) { sleep(1); }'
429+
end
430+
420431
#it "should support do-while statements" # TODO
421-
#it "should support for statements" # TODO
422-
#it "should support foreach statements" # TODO
432+
433+
it "should support for statements" do
434+
phpize('for $x in [1, 2, 3]; echo $x; end').should == 'foreach (array(1, 2, 3) as $x) { echo($x); }'
435+
phpize{ for $x in [1, 2, 3]; echo $x; end }.should == 'foreach (array(1, 2, 3) as $x) { echo($x); }'
436+
phpize('for $k, $v in {"a" => 1}; echo $k << $v; end').should == 'foreach (array("a" => 1) as $k => $v) { echo($k . $v); }'
437+
phpize{ for $k, $v in {"a" => 1}; echo $k << $v; end }.should == 'foreach (array("a" => 1) as $k => $v) { echo($k . $v); }'
438+
end
439+
423440
#it "should support break statements" # TODO
424441
#it "should support continue statements" # TODO
442+
443+
it "should support return statements" do
444+
phpize('return').should == 'return'
445+
phpize{ return }.should == 'return'
446+
phpize('return nil').should == 'return NULL'
447+
phpize{ return nil }.should == 'return NULL'
448+
phpize('return true').should == 'return TRUE'
449+
phpize{ return true }.should == 'return TRUE'
450+
phpize('return 42').should == 'return 42'
451+
phpize{ return 42 }.should == 'return 42'
452+
end
453+
425454
#it "should support switch statements" # TODO
426455
end
427456

0 commit comments

Comments
 (0)