Google Java Format + Verify Google Java Комбинация форматов не работает Gradle - PullRequest
0 голосов
/ 20 июня 2020

Я новичок в Gradle; Я просто добавил его в свой главный репозиторий , чтобы посмотреть, как он работает. Вот как выглядит мой build.gradle файл:

plugins {
    id 'java'

    id 'application'

    id 'com.github.sherter.google-java-format' version '0.8'
}

repositories {
    jcenter()
}

dependencies {
    compile fileTree(include: ['jblas-1.2.4.jar'], dir: 'libs')

    implementation 'com.google.guava:guava:28.1-jre'

    testImplementation 'junit:junit:4.12'
}

application {
    mainClassName = 'sorting.bubblesort.BubbleSort'
}

Как видите, я применил плагин под названием google-java-format. Однако я столкнулся с проблемой с двумя задачами:

googleJavaFormat
verifyGoogleJavaFormat

Я могу запустить googleJavaFormat, и он говорит, что он выполняется успешно. Однако сразу после этого verifyGoogleJavaFormat сообщает, что он был отформатирован неправильно. Вот единственный файл, который явно не отформатирован правильно:

package DataStructures.MinPriorityQueue;

import datastructures.minpriorityqueue.MinHeap.MinHeap;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.PriorityQueue;

import static org.junit.Assert.*;

public class MinHeapTest {
  @Test
  public void containsTest() {
    for (int i = 0; i < 10000; i++) {
      HashSet<Integer> set = new HashSet<>();
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      for (int j = 0; j < 1000; j++)
        if (Math.random() >= 0.5) {
          set.add(j);
          heap.add(j);
        }
      for (Integer integer : set) assertTrue(heap.contains(integer));
    }
  }

  @Test
  public void isEmptyTest() {
    for (int i = 0; i < 100; i++) {
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      assertTrue(heap.isEmpty());
      int randomAmount = 1 + (int) (Math.random() * 1000);
      for (int j = 0; j < randomAmount; j++) heap.add((int) (Math.random() * 1000));
      assertFalse(heap.isEmpty());
    }
  }

  @Test
  public void extractMinTest() {
    for (int i = 0; i < 1000; i++) {
      PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Integer::compare);
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      for (int j = 0; j < 1000; j++)
        if (Math.random() >= 0.5) {
          priorityQueue.add(j);
          heap.add(j);
        }
      assertEquals(priorityQueue.poll(), heap.extractMin());
    }
  }

  @Test
  public void chainedExtractMinTest() {
    for (int i = 0; i < 1000; i++) {
      int[] toAdd = new int[1000];
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      for (int j = 0; j < toAdd.length; j++) {
        toAdd[j] = (int) (Math.random() * 1000);
        heap.add(toAdd[j]);
      }
      int[] sorted = toAdd.clone();
      Arrays.sort(sorted);
      int[] heapValues = new int[1000];
      for (int j = 0; j < heapValues.length; j++) heapValues[j] = heap.extractMin();
      assertArrayEquals(sorted, heapValues);
    }
  }

  @Test
  public void decreaseKeyTest() {
    for (int i = 0; i < 1000; i++) {
      MinHeap<Integer> heap = new MinHeap<>(Integer::compare);
      PriorityQueue<Integer> queue = new PriorityQueue<>(Integer::compare);
      for (int j = 0; j < 1000; j++) heap.add(j);
      for (Integer j : heap) heap.decreaseValue(j, (int) (Math.random() * j));
      for (Integer j : heap) queue.add(j);
      while (!heap.isEmpty()) assertEquals(queue.poll(), heap.extractMin());
    }
  }
}

Даже после того, как я запустил googleJavaFormat в этом файле, verifyGoogleJavaFormat не удастся. Почему это происходит?

1 Ответ

0 голосов
/ 20 июня 2020

Хорошо, я получил свой ответ. Поскольку я использую Intellij, он перемещается по импорту. Например, когда он видит звездочку * в импорте, он расширяет ее. Я только что отключил File | Настройки | Редактор | Общие | Автоматический импорт и снимите флажок оптимизировать импорт на лету.

...