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

org.truth0.subjects.StringSubject Maven / Gradle / Ivy

/*
 * Copyright (c) 2011 David Saff
 * Copyright (c) 2011 Christian Gruber
 *
 * 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.truth0.subjects;

import com.google.common.annotations.GwtCompatible;

import org.truth0.FailureStrategy;

/**
 * Propositions for String subjects
 *
 * @author David Saff
 * @author Christian Gruber ([email protected])
 */
@GwtCompatible
public class StringSubject extends Subject {
  public StringSubject(FailureStrategy failureStrategy, String string) {
    super(failureStrategy, string);
  }

  @Override
  public void isEqualTo(Object other) {
    if (!(other instanceof String)) {
      fail("is", other);
    }
    if (getSubject() == null) {
      if(other != null) {
        fail("is", other);
      }
    } else {
      if (!getSubject().equals(other)) {
        if (other instanceof String) {
          failureStrategy.failComparing("", (String) other, getSubject());
        } else {
          fail("is", other);
        }
      }
    }
  }

  public void contains(String string) {
    if (getSubject() == null) {
      if (string != null) {
        fail("contains", string);
      }
    } else if (!getSubject().contains(string)) {
      fail("contains", string);
    }
  }

  public void startsWith(String string) {
    if (getSubject() == null) {
      if (string != null) {
        fail("starts with", string);
      }
    } else if (!getSubject().startsWith(string)) {
      fail("starts with", string);
    }
  }

  public void endsWith(String string) {
    if (getSubject() == null) {
      if (string != null) {
        fail("ends with", string);
      }
    } else if (!getSubject().endsWith(string)) {
      fail("ends with", string);
    }
  }

  public static final SubjectFactory STRING =
      new SubjectFactory() {
        @Override public StringSubject getSubject(FailureStrategy fs, String target) {
          return new StringSubject(fs, target);
        }
      };

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy