All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.scalatest.TimesOnInt.scala Maven / Gradle / Ivy

Go to download

ScalaTest is a free, open-source testing toolkit for Scala and Java programmers.

There is a newer version: 2.0.M6-SNAP4
Show newest version
/*
 * Copyright 2001-2012 Artima, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.scalatest

/**
 * Trait providing an implicit conversion that adds a times method to Ints that
 * will repeat a given side-effecting operation multiple times.
 * 
 * 

* Here's an example in which a friendly greeting is printed three times: *

* *
 * 3 times println("Hello again, world!")
 * 
* *

* Running the above code would yield this output: *

* *
 * Hello again, world!
 * Hello again, world!
 * Hello again, world!
 * 
* *

* If you need to repeat a block of statements multiple times, just enclose them in parentheses, like this: *

* *
 * 2 times {
 *  print("Hello ")
 *  print("again, ")
 *  println("world!")
 * }
 * 
* *

* Running the above code would yield: *

* *
 * Hello again, world!
 * Hello again, world!
 * 
* *

* This trait enables times to be invoked on 0 and any positive integer, * but attempting to invoke times on a negative integer will result in an IllegalArgumentException. *

* * @author Bill Venners */ trait TimesOnInt { /** * Class used via an implicit conversion to enable a times method to be invoked * on Ints to repeat a given side-effecting operation multiple times. * *

* When an instance of this class is constructed, 0 and any positive number may be passed as num, * but a negative number will result in an IllegalArgumentException. If constructed with 0, the times method * on the resulting instance will return without invoking the function passed to it. If constructed with 1, the times method * will invoke the function passed to it once then return. *

* * @param num the integer to which the times method will be added. * @throws IllegalArgumentException if num is less than zero. */ class Repeater(num: Int) { require(num >= 0, "The integer on which times was invoked was less than zero: " + num) /** * Executes the passed by-name parameter num number of times. * *

* If the function completes abruptly with an exception, this method will complete abruptly with the same * exception immediately. Thus in the case of an exception, this method may actually invoke the * passed function fewer than num times. *

* * @param fun the by-name parameter to execute num times */ def times(fun: => Unit) { var i = 0 while (i < num) { fun i += 1 } } } /** * Implicit conversion that adds a times method to Ints that * will repeat a given side-effecting operation multiple times. * * @param num the integer to which the times method will be added. */ implicit def convertIntToRepeater(num: Int): Repeater = new Repeater(num) } /** * Companion object that facilitates the importing of TimesOnInt members as an alternative to mixing it in. * *

* One use case of this companion object is to import TimesOnInt members so you can use them in the Scala interpreter. * Here's an example: *

* *
 * scala> import org.scalatest.TimesOnInt._
 * import org.scalatest.TimesOnInt._
 * 
 * scala> 3 times println("Hello again, world!")
 * Hello again, world!
 * Hello again, world!
 * Hello again, world!
 * 
*/ object TimesOnInt extends TimesOnInt




© 2015 - 2024 Weber Informatics LLC | Privacy Policy