File indexing completed on 2024-04-21 03:45:23

0001 #  SPDX-FileCopyrightText: 2009 Cies Breijs
0002 #
0003 #  SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 require File.dirname(__FILE__) + '/spec_helper.rb'
0006 $i = Interpreter.instance
0007 
0008 describe "if (and else) statement" do
0009 
0010   it "should work with one clause" do
0011     $i.should_run_clean <<-EOS
0012       if true {
0013         assert true
0014       }
0015       if false {
0016         assert false  # not executed
0017       }
0018     EOS
0019   end
0020 
0021   it "should work with two clauses" do
0022     $i.should_run_clean <<-EOS
0023       if true {
0024         assert true
0025       } else {
0026         assert false  # not executed
0027       }
0028       if false {
0029         assert false  # not executed
0030       } else {
0031         assert true
0032       }
0033     EOS
0034   end
0035 
0036   it "should be flexible" do
0037     $i.should_run_clean <<-EOS
0038       if
0039       if true
0040       if false
0041       if true {}
0042       if false {}
0043     EOS
0044   end
0045 
0046   it "should not allow scopeless oneliners" do
0047     $i.should_run_clean <<-EOS
0048       $x = 0
0049       if false
0050         $x = 1  # should be executed, it's outside the if clause
0051       assert $x == 1
0052     EOS
0053   end
0054 
0055   it "should work when nested" do
0056     $i.should_run_clean <<-EOS
0057       $x = 0
0058       if true {
0059         if true {
0060           if true {
0061             if false {
0062               assert false
0063             } else {
0064               $x = 1
0065             }
0066           }
0067         }
0068       }
0069       assert $x == 1
0070     EOS
0071   end
0072 end