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

org.scalautils.AbstractStringUniformity.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2001-2013 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.scalautils

/**
 * Convenience base trait for string Uniformitys.
 *
 * 

* This trait defines a normalizedCanHandle method that returns true if the passed * Any is a String and a normalizedOrSame method that * normalizes any passed Strings via the normalized method, which is * left abstract for subclasses to fill in. *

* *

* Here's an example in which AbstractStringUniformity is used to normalize strings * by ensuring the first character, if any, is capitalized: *

* *
 * val capitalized: Uniformity[String] =
 *   new AbstractStringUniformity {
 *     def normalized(s: String): String =
 *       if (s.isEmpty) "" else s.charAt(0).toUpper + s.substring(1)
 *   }
 * 
* *

* Here's an example of using the capitalized Uniformity with a Matcher expression: *

* *
 * scala> import org.scalatest._
 * import org.scalatest._
 *
 * scala> import Matchers._
 * import Matchers._
 *
 * scala> import org.scalautils._
 * import org.scalautils._
 *
 * scala> val capitalized: Uniformity[String] =
 *      |   new AbstractStringUniformity {
 *      |     def normalized(s: String): String =
 *      |       if (s.isEmpty) "" else s.charAt(0).toUpper + s.substring(1)
 *      |   }
 * capitalized: org.scalautils.Uniformity[String] = $anon$1@65601e00
 *
 * scala> "Hello" should equal ("hello") (after being capitalized)
 * 
*/ trait AbstractStringUniformity extends Uniformity[String] { /** * Returns true if the passed Any is a String. * * @return true if the passed Any is a String. */ final def normalizedCanHandle(b: Any): Boolean = b.isInstanceOf[String] /** * Normalizes the passed object if it is a String. * *

* This method returns either: *

* *
    *
  • if the passed object is a String, the result of passing that string to normalized
  • *
  • else, the same exact object that was passed *

    * * @return a normalized form of any passed String, or the same object if not a String. */ final def normalizedOrSame(b: Any) = b match { case s: String => normalized(s) case _ => b } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy