File indexing completed on 2024-04-28 07:33:13

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 "repeat loop" do
0009   it "should work for positive natural numbers" do
0010     $i.should_run_clean <<-EOS
0011       $x1 = 0
0012       repeat 1 {
0013         $x1 = $x1 + 1
0014       }
0015       assert $x1 == 1
0016 
0017       $x2 = 0
0018       repeat 2 {
0019         $x2 = $x2 + 1
0020       }
0021       assert $x2 == 2
0022 
0023       $x3 = 0
0024       repeat 3 {
0025         $x3 = $x3 + 1
0026       }
0027       assert $x3 == 3
0028     EOS
0029   end
0030 
0031   it "should work for positive decimal numbers" do
0032     $i.should_run_clean <<-EOS
0033       $x1 = 0
0034       repeat 1.1 {
0035         $x1 = $x1 + 1
0036       }
0037       assert $x1 == 1
0038 
0039       $x2 = 0
0040       repeat 1.5 {
0041         $x2 = $x2 + 1
0042       }
0043       assert $x2 == 2
0044 
0045       $x3 = 0
0046       repeat 2.9 {
0047         $x3 = $x3 + 1
0048       }
0049       assert $x3 == 3
0050     EOS
0051   end
0052 
0053   it "should not execute the scope when argument is < 0.5" do
0054     $i.should_run_clean <<-EOS
0055       repeat 0.49 {
0056         assert false
0057       }
0058       repeat 0.1 {
0059         assert false
0060       }
0061     EOS
0062   end
0063 
0064   it "should not execute the scope when argument is zero" do
0065     $i.should_run_clean <<-EOS
0066       repeat 0 {
0067         assert false
0068       }
0069     EOS
0070   end
0071 
0072   it "should not execute the scope when argument is empty" do
0073     # TODO decide: do we really want this? or do we like the current behavior in which unassigned 
0074     $i.should_run_clean <<-EOS
0075       repeat $x {
0076         assert false
0077       }
0078     EOS
0079   end
0080 
0081   it "should not execute the scope when argument is negative" do
0082     $i.should_run_clean <<-EOS
0083       repeat -.1 {
0084         assert false
0085       }
0086       repeat -1 {
0087         assert false
0088       }
0089       repeat -10 {
0090         assert false
0091       }
0092     EOS
0093   end
0094 
0095   it "should produce an error when a string is given" do
0096     $i.run('repeat "123" {}').errors?.should be_true
0097   end
0098 
0099   it "should produce an error when a boolean is given" do
0100     $i.run('repeat true {}').errors?.should be_true
0101   end
0102 
0103   it "should produce an error when argument omitted" do
0104     $i.run('repeat {}').errors?.should be_true
0105   end
0106 
0107   it "should produce an error when scope is omitted" do
0108     $i.run('repeat 3').errors?.should be_true
0109   end
0110 end