Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Kotlin class assertion generation problem

未关闭
#193 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
java, kotlin
领域
tooling

调研方向

从 Klass.kt 和 JavaClass.java 中的 Kotlin 复现代码开始,然后检查生成 AbstractKlassAssert 的生成器路径。确认生成的断言方法只输出一次,并确认生成的 Java 类能够编译。

由索引模型根据 Issue 内容生成。

描述

There is a small problem:

Generator is creating assertion methods twice in Abstract***Assert classes for code where Kotlin class with lateinit collection field is generified with complex java class. Generated code cannot compile

Example to reproduce the problem:

Klass.kt:

class Klass {
  lateinit var collection: Collection<JavaClass>
}

JavaClass.java:

public class JavaClass {}

Result:

/**
 * Abstract base class for {@link Klass} specific assertions - Generated by CustomAssertionGenerator.
 */
@javax.annotation.Generated(value="assertj-assertions-generator")
public abstract class AbstractKlassAssert<S extends AbstractKlassAssert<S, A>, A extends Klass> extends AbstractObjectAssert<S, A> {

  /**
   * Creates a new <code>{@link AbstractKlassAssert}</code> to make assertions on actual Klass.
   * @param actual the Klass we want to make assertions on.
   */
  protected AbstractKlassAssert(A actual, Class<S> selfType) {
    super(actual, selfType);
  }

  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.getCollection(), collection);

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements in Collection.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.getCollection(), collection.toArray());

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.getCollection(), collection);

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements in Collection and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.getCollection(), collection.toArray());

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.getCollection(), collection);

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements in Collection.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.getCollection(), collection.toArray());

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass has no collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection is not empty.
   */
  public S hasNoCollection() {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // we override the default error message with a more explicit one
    String assertjErrorMessage = "\nExpecting :\n  <%s>\nnot to have collection but had :\n  <%s>";

    // check
    if (actual.getCollection().iterator().hasNext()) {
      failWithMessage(assertjErrorMessage, actual, actual.getCollection());
    }

    // return the current assertion for method chaining
    return myself;
  }


  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.collection, collection);

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection contains the given JavaClass elements in Collection.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContains(info, actual.collection, collection.toArray());

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.collection, collection);

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection contains <b>only</b> the given JavaClass elements in Collection and nothing else in whatever order.
   * @param collection the given elements that should be contained in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection does not contain all given JavaClass elements.
   */
  public S hasOnlyCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message, to set another message call: info.overridingErrorMessage("my error message");
    Iterables.instance().assertContainsOnly(info, actual.collection, collection.toArray());

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(JavaClass... collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass varargs is not null.
    if (collection == null) failWithMessage("Expecting collection parameter not to be null.");

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.collection, collection);

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass's collection does not contain the given JavaClass elements in Collection.
   *
   * @param collection the given elements that should not be in actual Klass's collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection contains any given JavaClass elements.
   */
  public S doesNotHaveCollection(java.util.Collection<? extends JavaClass> collection) {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // check that given JavaClass collection is not null.
    if (collection == null) {
      failWithMessage("Expecting collection parameter not to be null.");
      return myself; // to fool Eclipse "Null pointer access" warning on toArray.
    }

    // check with standard error message (use overridingErrorMessage before contains to set your own message).
    Iterables.instance().assertDoesNotContain(info, actual.collection, collection.toArray());

    // return the current assertion for method chaining
    return myself;
  }

  /**
   * Verifies that the actual Klass has no collection.
   * @return this assertion object.
   * @throws AssertionError if the actual Klass's collection is not empty.
   */
  public S hasNoCollection() {
    // check that actual Klass we want to make assertions on is not null.
    isNotNull();

    // we override the default error message with a more explicit one
    String assertjErrorMessage = "\nExpecting :\n  <%s>\nnot to have collection but had :\n  <%s>";

    // check
    if (actual.collection.iterator().hasNext()) {
      failWithMessage(assertjErrorMessage, actual, actual.collection);
    }

    // return the current assertion for method chaining
    return myself;
  }


}
主要语言
Java
星标
72
派生
47
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

assertj/assertj-generator 的其他 Issue

查看 assertj/assertj-generator 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。