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

br.com.objectos.code.JavaPoetAssert Maven / Gradle / Ivy

/*
 * Copyright 2015 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.code;

import static br.com.objectos.testing.MoreMatchers.hasSize;
import static br.com.objectos.testing.MoreMatchers.is;
import static br.com.objectos.testing.MoreMatchers.toStringEqualTo;
import static org.hamcrest.MatcherAssert.assertThat;

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

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

  private JavaPoetAssert() {
  }

  public static AtAssert at(String dir) {
    return new AtAssert(dir);
  }

  public static class AtAssert {

    private final String dir;

    private AtAssert(String dir) {
      this.dir = dir;
    }

    public ListAssert that(List list) {
      return new ListAssert(dir, list);
    }

    public OptionalAssert that(Optional optional) {
      return new OptionalAssert(dir, optional);
    }

    public SingleAssert that(Object single) {
      return new SingleAssert(dir, single);
    }

  }

  public static class ListAssert {

    private final String dir;
    private final List list;

    private ListAssert(String dir, List list) {
      this.dir = dir;
      this.list = list;
    }

    public void hasToStringEqualTo(List expectedList) {
      assertThat(list, hasSize(expectedList.size()));
      for (int i = 0; i < list.size(); i++) {
        Object res = list.get(i);
        String expected = expectedList.get(i);
        assertThat(res, toStringEqualTo(dir + "/" + expected));
      }
    }

  }

  public static class OptionalAssert {

    private final String dir;
    private final Optional optional;

    public OptionalAssert(String dir, Optional optional) {
      this.dir = dir;
      this.optional = optional;
    }

    public void hasToStringEqualTo(String expected) {
      assertThat(optional.isPresent(), is(expected != null));
      if (expected != null) {
        assertThat(optional.get(), toStringEqualTo(dir + "/" + expected));
      }
    }

  }

  public static class SingleAssert {

    private final String dir;
    private final Object single;

    public SingleAssert(String dir, Object single) {
      this.dir = dir;
      this.single = single;
    }

    public void hasToStringEqualTo(String expected) {
      assertThat(single, toStringEqualTo(dir + "/" + expected));
    }

  }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy