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

br.com.objectos.core.testing.TestableIsEqualHelper Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.core.testing;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author [email protected] (Marcio Endo)
 */
public class TestableIsEqualHelper {

  private final List assertions;

  private TestableIsEqualHelper() {
    assertions = new ArrayList<>();
  }

  static TestableIsEqualHelper start() {
    return new TestableIsEqualHelper();
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  public  TestableIsEqualHelper equal(T thisObj, T thatObj) {
    if (thisObj instanceof List) {
      List thisList = (List) thisObj;
      List thatList = (List) thatObj;
      ListAssertion assertion = new ListAssertion(thisList, thatList);
      assertions.add(assertion);
      return this;
    }

    if (thisObj instanceof Optional) {
      Optional thisOptional = (Optional) thisObj;
      Optional thatOptional = (Optional) thatObj;

      Object thisMaybe = thisOptional.orElse(null);
      Object thatMaybe = thatOptional.orElse(null);
      return equal(thisMaybe, thatMaybe);
    }

    if (thisObj instanceof Testable) {
      Testable thisTestable = (Testable) thisObj;
      Testable thatTestable = (Testable) thatObj;
      TestableLegacyAssertion assertion = new TestableLegacyAssertion(thisTestable, thatTestable);
      assertions.add(assertion);
      return this;
    }

    if (thisObj instanceof br.com.objectos.testable.Testable) {
      br.com.objectos.testable.Testable thisTestable = (br.com.objectos.testable.Testable) thisObj;
      br.com.objectos.testable.Testable thatTestable = (br.com.objectos.testable.Testable) thatObj;
      TestableAssertion assertion = new TestableAssertion(thisTestable, thatTestable);
      assertions.add(assertion);
      return this;
    }

    ObjectAssertion assertion = new ObjectAssertion(thisObj, thatObj);
    assertions.add(assertion);
    return this;
  }

  @Deprecated
  public > TestableIsEqualHelper allEqual(List thisList, List thatList) {
    assertions.add(new ListTestableAssertion(thisList, thatList));
    return this;
  }

  public TestableIsEqualHelper equal(boolean thisBool, boolean thatBool) {
    assertions.add(new BooleanAssertion(thisBool, thatBool));
    return this;
  }

  public TestableIsEqualHelper equal(double thisDbl, double thatDbl) {
    assertions.add(new DoubleAssertion(thisDbl, thatDbl));
    return this;
  }

  public TestableIsEqualHelper equal(int thisInt, int thatInt) {
    assertions.add(new IntAssertion(thisInt, thatInt));
    return this;
  }

  public TestableIsEqualHelper notNull(Object thisObj, Object thatObj) {
    assertions.add(new NotNullAssertion(thisObj, thatObj));
    return this;
  }

  public boolean result() {
    int idx = 0;

    for (Assertion equality : assertions) {
      equality.compute(idx++);
    }

    return true;
  }

  private static class NotNullAssertion extends Assertion {

    private final Object thisObj;
    private final Object thatObj;

    public NotNullAssertion(Object thisObj, Object thatObj) {
      this.thisObj = thisObj;
      this.thatObj = thatObj;
    }

    @Override
    boolean test() {
      return thisObj != null && thatObj != null;
    }

    @Override
    String getThis() {
      return toString(thisObj);
    }

    @Override
    String getThat() {
      return toString(thatObj);
    }

  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy