Skip to content

Commit

Permalink
Reuse FieldsExtractorTest for processor and intellij modules (#570)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #570

Extracted FieldsExtractorTestHelper to reuse in FieldsExtractorTest and PsiFieldsExtractorTest

Reviewed By: muraziz

Differential Revision: D15512731

fbshipit-source-id: bf98aa9a4bececd5e7d0277f5145453da484f4e8
  • Loading branch information
Kata Zavorotchenko authored and facebook-github-bot committed Aug 2, 2019
1 parent 8bf9d8b commit fa87c02
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 48 deletions.
3 changes: 2 additions & 1 deletion litho-intellij-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ apply plugin: 'idea'
apply plugin: 'org.jetbrains.intellij'
apply plugin: 'java'

sourceSets.test.java.srcDir project(':litho-processor').file("src/test/java")

dependencies {
testImplementation deps.junit
testImplementation deps.mockitoCore

// Processor
implementation project(':litho-sections-annotations')
implementation project(':litho-sections-processor')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.facebook.litho.intellij.extensions.EventLogger;
import com.facebook.litho.intellij.logging.LithoLoggerProvider;
import com.google.common.annotations.VisibleForTesting;

import java.util.Map;

/** Only logs an event if a particular timespanMillis has passed without it logging another event */
Expand All @@ -29,6 +31,7 @@ class DebounceEventLogger implements EventLogger {
this(timespanMillis, LithoLoggerProvider.getEventLogger());
}

@VisibleForTesting
DebounceEventLogger(long timespanMillis, EventLogger logger) {
this.timespanMillis = timespanMillis;
this.eventLogger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@
*/
package com.facebook.litho.specmodels.processor;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.facebook.litho.intellij.LithoPluginTestHelper;
import com.facebook.litho.specmodels.internal.ImmutableList;
import com.facebook.litho.specmodels.model.FieldModel;
import com.intellij.psi.PsiClass;
import com.squareup.javapoet.FieldSpec;
import javax.lang.model.element.Modifier;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

Expand All @@ -47,25 +42,11 @@ public void tearDown() throws Exception {
public void extractFields() {
testHelper.getPsiClass(
psiClasses -> {
assertNotNull(psiClasses);
Assert.assertNotNull(psiClasses);
PsiClass psiClass = psiClasses.get(0);

ImmutableList<FieldModel> fieldModels = PsiFieldsExtractor.extractFields(psiClass);

assertEquals(fieldModels.size(), 2);

FieldSpec extractedIntField = fieldModels.get(0).field;
assertEquals(extractedIntField.name, "intField");
assertEquals(extractedIntField.modifiers.size(), 3);
assertTrue(extractedIntField.hasModifier(Modifier.PUBLIC));
assertTrue(extractedIntField.hasModifier(Modifier.STATIC));
assertTrue(extractedIntField.hasModifier(Modifier.FINAL));

FieldSpec extractedFloatField = fieldModels.get(1).field;
assertEquals(extractedFloatField.name, "floatField");
assertEquals(extractedFloatField.modifiers.size(), 1);
assertTrue(extractedFloatField.hasModifier(Modifier.STATIC));

FieldsExtractorTestHelper.fieldExtraction(fieldModels);
return true;
},
"TwoFieldsClass.java");
Expand All @@ -75,13 +56,11 @@ public void extractFields() {
public void extractNoFields() {
testHelper.getPsiClass(
psiClasses -> {
assertNotNull(psiClasses);
Assert.assertNotNull(psiClasses);
PsiClass psiClass = psiClasses.get(0);

ImmutableList<FieldModel> fieldModels = PsiFieldsExtractor.extractFields(psiClass);

assertEquals(fieldModels.size(), 0);

FieldsExtractorTestHelper.noFieldExtraction(fieldModels);
return true;
},
"NoFieldsClass.java");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
class TwoFieldsClass {
public static final int intField = 0;
private static final int intField = 0;
static float floatField = 0f;

private boolean isTest() {
Expand Down
2 changes: 2 additions & 0 deletions litho-it/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ android {
lintOptions {
abortOnError false
}

sourceSets.test.java.srcDir project(':litho-processor').file("src/test/java")
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ fb_java_test(
make_dep_path("litho-processor/src/main/java/com/facebook/litho/specmodels:specmodels"),
make_dep_path("litho-processor/src/main/java/com/facebook/litho/specmodels/internal:internal"),
make_dep_path("litho-testing/src/main/java/com/facebook/litho/testing/specmodels:specmodels"),
make_dep_path("litho-processor/src/test/java/com/facebook/litho/specmodels/processor:processor"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@
*/
package com.facebook.litho.specmodels.processor;

import static org.assertj.core.api.Java6Assertions.assertThat;

import com.facebook.litho.specmodels.internal.ImmutableList;
import com.facebook.litho.specmodels.model.FieldModel;
import com.google.testing.compile.CompilationRule;
import com.squareup.javapoet.FieldSpec;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -50,30 +46,18 @@ private int getOne() {
}

@Test
public void testFieldExtraction() {
public void fieldExtraction() {
final TypeElement element =
compilationRule.getElements().getTypeElement(TwoFieldsClass.class.getCanonicalName());
ImmutableList<FieldModel> fieldModels = FieldsExtractor.extractFields(element);
assertThat(fieldModels).hasSize(2);

FieldSpec extractedIntField = fieldModels.get(0).field;
assertThat(extractedIntField.name).isEqualTo("intField");
assertThat(extractedIntField.modifiers).hasSize(3);
assertThat(extractedIntField.hasModifier(Modifier.PRIVATE));
assertThat(extractedIntField.hasModifier(Modifier.STATIC));
assertThat(extractedIntField.hasModifier(Modifier.FINAL));

FieldSpec extractedFloatField = fieldModels.get(1).field;
assertThat(extractedFloatField.name).isEqualTo("floatField");
assertThat(extractedFloatField.modifiers).hasSize(1);
assertThat(extractedFloatField.hasModifier(Modifier.STATIC));
FieldsExtractorTestHelper.fieldExtraction(fieldModels);
}

@Test
public void testNoFieldExtraction() {
public void noFieldExtraction() {
final TypeElement element =
compilationRule.getElements().getTypeElement(NoFieldsClass.class.getCanonicalName());
ImmutableList<FieldModel> fieldModels = FieldsExtractor.extractFields(element);
assertThat(fieldModels).hasSize(0);
FieldsExtractorTestHelper.noFieldExtraction(fieldModels);
}
}
2 changes: 2 additions & 0 deletions litho-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies {
// Processor
implementation deps.javapoet
implementation deps.supportAnnotations

testImplementation deps.junit
}

apply from: rootProject.file('gradle/release.gradle')
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright (c) 2017-present, Facebook, Inc.
#
# This source code is licensed under the Apache 2.0 license found in the
# LICENSE file in the root directory of this source tree.
load(
"//tools/build_defs/oss:litho_defs.bzl",
"LITHO_JUNIT_TARGET",
"LITHO_VISIBILITY",
"fb_java_test",
"make_dep_path",
)

fb_java_test(
name = "processor",
srcs = glob(
["**/*.java"],
exclude = [""],
include_dotfiles = False,
),
source = "8",
target = "8",
visibility = LITHO_VISIBILITY,
deps = [
LITHO_JUNIT_TARGET,
make_dep_path("litho-processor/src/main/java/com/facebook/litho/specmodels:specmodels"),
make_dep_path("litho-processor/src/main/java/com/facebook/litho/specmodels/internal:internal"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019-present Facebook, Inc.
*
* 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 com.facebook.litho.specmodels.processor;

import com.facebook.litho.specmodels.internal.ImmutableList;
import com.facebook.litho.specmodels.model.FieldModel;
import com.squareup.javapoet.FieldSpec;
import javax.lang.model.element.Modifier;
import org.junit.Assert;

public class FieldsExtractorTestHelper {

static void fieldExtraction(ImmutableList<FieldModel> fieldModels) {
Assert.assertEquals(2, fieldModels.size());

FieldSpec extractedIntField = fieldModels.get(0).field;
Assert.assertEquals("intField", extractedIntField.name);
Assert.assertEquals(3, extractedIntField.modifiers.size());
Assert.assertTrue(extractedIntField.hasModifier(Modifier.PRIVATE));
Assert.assertTrue(extractedIntField.hasModifier(Modifier.STATIC));
Assert.assertTrue(extractedIntField.hasModifier(Modifier.FINAL));

FieldSpec extractedFloatField = fieldModels.get(1).field;
Assert.assertEquals("floatField", extractedFloatField.name);
Assert.assertEquals(1, extractedFloatField.modifiers.size());
Assert.assertTrue(extractedFloatField.hasModifier(Modifier.STATIC));
}

static void noFieldExtraction(ImmutableList<FieldModel> fieldModels) {
Assert.assertTrue(fieldModels.isEmpty());
}
}

0 comments on commit fa87c02

Please sign in to comment.