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

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

Go to download

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

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

/**
 * Provides methods that produce Normalization[String] instances for various
 * ways to normalize strings.
 *
 * @author Bill Venners
 */
trait StringNormalizations {

  abstract class StringNormalization extends Normalization[String] {
    /**
     * Indicates whether the passed object is an instance of String.
     *
     * @return true if the passed object is a String.
     */
    def normalizedIfInstanceOfA(b: Any) =
      b match {
        case s: String => normalized(s)
        case _ => b
     }
  }

  /**
   * Produces a Normalization[String] whose normalized method
   * returns the result of invoking toLowerCase on the passed string.
   *
   * @return a Normalization[String] that normalizes by transforming strings to lower case.
   */
  val lowerCased: Normalization[String] =
    new StringNormalization {

      /**
       * Returns the result of invoking toLowerCase on the passed string.
       *
       * @return the passed string transformed to lower case.
       */
      def normalized(s: String): String = s.toLowerCase
    }

  /**
   * Produces a Normalization[String] whose normalized method
   * returns the result of invoking trim on the passed string.
   *
   * @return a Normalization[String] that normalizes strings by trimming them.
   */
  val trimmed: Normalization[String] =
    new StringNormalization {

      /**
       * Returns the result of invoking trimmed on the passed string.
       *
       * @return the passed string with any white space trimmed off either end.
       */
      def normalized(s: String): String = s.trim
    }
} 

/**
 * Companion object to trait StringNormalizations that provides an 
 * alternative to mixing it in.
 */
object StringNormalizations extends StringNormalizations




© 2015 - 2025 Weber Informatics LLC | Privacy Policy