
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.assertion.ObjectAssertion.assertThat;
import static br.com.objectos.assertion.OptionalAssertion.assertThat;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
import br.com.objectos.assertion.ListAssertion;
import br.com.objectos.io.Resource;
/**
* @author [email protected] (Marcio Endo)
*/
public class JavaPoetAssert {
private JavaPoetAssert() {
}
public static AtAssert at(String dir) {
return new AtAssert(dir);
}
private static void assertToString(Object subject, String expected) {
assertThat(subject.toString().trim()).hasToString(expected.trim());
}
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) {
ListAssertion.assertThat(list).hasSize(expectedList.size());
for (int i = 0; i < list.size(); i++) {
Object res = list.get(i);
String expected = expectedList.get(i);
assertToString(res, expectedToString(expected));
}
}
private String expectedToString(String resourceName) {
try {
return Resource.of(dir + "/" + resourceName).readToString();
} catch (IOException e) {
return "";
}
}
}
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(expected != null);
if (expected != null) {
assertToString(optional.get(), expectedToString(expected));
}
}
private String expectedToString(String resourceName) {
try {
return Resource.of(dir + "/" + resourceName).readToString();
} catch (IOException e) {
return "";
}
}
}
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) {
assertToString(single, expectedToString(expected));
}
private String expectedToString(String resourceName) {
try {
return Resource.of(dir + "/" + resourceName).readToString();
} catch (IOException e) {
return "";
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy