@Before public void setUp() { MockitoAnnotations.initMocks(this); mElements = mCompilationRule.getElements(); mTypes = mCompilationRule.getTypes(); mTypeElement = mElements.getTypeElement(TestMyLayoutSpec.class.getCanonicalName()); }
@Test public void testNormalClass() { final Elements elements = mCompilationRule.getElements(); final TypeElement typeElement = elements.getTypeElement(String.class.getName()); assertThat(SpecElementTypeDeterminator.determine(typeElement)) .isEqualTo(SpecElementType.JAVA_CLASS); }
public @Rule CompilationRule rule = new CompilationRule(); private Elements elements; private Types types; @Before public void setup() { elements = rule.getElements(); types = rule.getTypes(); } @Test void testStuff() { TypeElement typeElement = elements.getTypeElement("some.fully.qualified.ClassName"); // do stuff. }
@Test public void testKotlinPropDefaultsExtractionWithoutGetAnnotation() { final Elements elements = mCompilationRule.getElements(); final TypeElement element = elements.getTypeElement(TestClassWithoutGetAnnotation.class.getCanonicalName()); final ImmutableList<PropDefaultModel> propDefaults = PropDefaultsExtractor.getPropDefaults(element); assertThat(propDefaults).hasSize(1); assertThat(propDefaults.get(0).getName()).isEqualTo("title"); }
@Before public void setUp() throws Exception { Elements elements = mCompilationRule.getElements(); Types types = mCompilationRule.getTypes(); mTagModels = TagExtractor.extractTagsFromSpecClass( types, elements.getTypeElement(TestClass.class.getCanonicalName())); }
@Test public void testKotlinPropDefaultsExtractionWithGetAnnotation() { final Elements elements = mCompilationRule.getElements(); final TypeElement element = elements.getTypeElement(TestClassWithGet.class.getCanonicalName()); final ImmutableList<PropDefaultModel> propDefaults = PropDefaultsExtractor.getPropDefaults(element); assertThat(propDefaults).hasSize(1); assertThat(propDefaults.get(0).getName()).isEqualTo("title"); } }
@Before public void setUp() { MockitoAnnotations.initMocks(this); Elements elements = mCompilationRule.getElements(); Types types = mCompilationRule.getTypes(); TypeElement typeElement = elements.getTypeElement(TestGroupSectionSpec.class.getCanonicalName()); mSpecModel = mGroupSectionSpecModelFactory.create( elements, types, typeElement, mMessager, RunMode.normal(), null, null); }
@Test public void testKotlinSingleton() { final Elements elements = mCompilationRule.getElements(); final TypeElement typeElement = elements.getTypeElement( SpecElementTypeDeterminatorTest.FakeKotlinSingleton.class.getCanonicalName()); assertThat(SpecElementTypeDeterminator.determine(typeElement)) .isEqualTo(SpecElementType.KOTLIN_SINGLETON); } }
@Before public void setUp() { Elements elements = mCompilationRule.getElements(); Types types = mCompilationRule.getTypes(); TypeElement typeElement = elements.getTypeElement(WorkingRangeGeneratorTest.TestSpec.class.getCanonicalName()); mSpecModel = mLayoutSpecModelFactory.create( elements, types, typeElement, mock(Messager.class), RunMode.normal(), null, null); }
@Test public void testGenerateTypeSpecForRegularType() { final TypeElement typeElement = mCompilationRule.getElements().getTypeElement(SpecModelUtilsTest.class.getCanonicalName()); final TypeSpec typeSpec = SpecModelUtils.generateTypeSpec(typeElement.asType()); assertThat(typeSpec.isValid()).isTrue(); } }
@Before public void setUp() { Elements elements = mCompilationRule.getElements(); Types types = mCompilationRule.getTypes(); TypeElement typeElement = elements.getTypeElement(TestSpec.class.getCanonicalName()); mSpecModel = mLayoutSpecModelFactory.create( elements, types, typeElement, mock(Messager.class), RunMode.normal(), null, null); }
@Test public void testExtraction() { final TypeElement typeElement = mCompilationRule.getElements().getTypeElement(TestMyLayoutSpec.class.getCanonicalName()); final TypeElement testSpecValue = TestTargetExtractor.getTestSpecValue(typeElement); assertThat(testSpecValue) .isNotNull() .hasToString( "com.facebook.litho.specmodels.processor.TestTargetExtractorTest.MyLayoutSpec"); } }
@Test public void testHasOnErrorDelegate() { final Elements elements = mCompilationRule.getElements(); final TypeElement typeElement = elements.getTypeElement(TestSpec.class.getCanonicalName()); final Types types = mCompilationRule.getTypes(); final LayoutSpecModel specModel = mLayoutSpecModelFactory.create( elements, types, typeElement, mMessager, RunMode.normal(), null, null); assertThat(ErrorEventHandlerGenerator.hasOnErrorDelegateMethod(specModel.getDelegateMethods())) .isTrue(); }
@Test public void testNoFieldExtraction() { final TypeElement element = compilationRule.getElements().getTypeElement(NoFieldsClass.class.getCanonicalName()); ImmutableList<FieldModel> fieldModels = FieldsExtractor.extractFields(element); assertThat(fieldModels).hasSize(0); } }
@Test public void testDuplicatePropValidationError() { final Elements elements = mCompilationRule.getElements(); final Types types = mCompilationRule.getTypes(); final TypeElement typeElement = elements.getTypeElement(DupeMountSpec.class.getCanonicalName()); final MountSpecModel mountSpecModel = mFactory.create( elements, types, typeElement, mock(Messager.class), RunMode.normal(), null, null); final List<SpecModelValidationError> specModelValidationErrors = SpecModelValidation.validateMountSpecModel(mountSpecModel, RunMode.normal()); assertThat(specModelValidationErrors) .extracting("message") .contains( "The prop prop1 is defined differently in different methods. " + "Ensure that each instance of this prop is declared in the same way " + "(this means having the same type, resType and values for isOptional, " + "isCommonProp and overrideCommonPropBehavior)."); } }
@Test public void testCalculateLevelOfComponentInCollections() { Elements elements = mCompilationRule.getElements(); TypeElement typeElement = elements.getTypeElement(CollectionObject.class.getCanonicalName()); List<? extends Element> fields = typeElement.getEnclosedElements(); TypeSpec arg0 = SpecModelUtils.generateTypeSpec(fields.get(0).asType()); TypeSpec arg1 = SpecModelUtils.generateTypeSpec(fields.get(1).asType()); TypeSpec arg2 = SpecModelUtils.generateTypeSpec(fields.get(2).asType()); assertThat(arg0.getClass()).isEqualTo(DeclaredTypeSpec.class); assertThat(arg1.getClass()).isEqualTo(DeclaredTypeSpec.class); assertThat(arg2.getClass()).isEqualTo(DeclaredTypeSpec.class); assertThat( ComponentBodyGenerator.calculateLevelOfComponentInCollections((DeclaredTypeSpec) arg0)) .isEqualTo(1); assertThat( ComponentBodyGenerator.calculateLevelOfComponentInCollections((DeclaredTypeSpec) arg1)) .isEqualTo(2); assertThat( ComponentBodyGenerator.calculateLevelOfComponentInCollections((DeclaredTypeSpec) arg2)) .isEqualTo(0); }
@Before public void setUp() { Elements elements = mCompilationRule.getElements(); Types types = mCompilationRule.getTypes(); TypeElement typeElement = elements.getTypeElement(TestSpec.class.getCanonicalName()); mSpecModel = mLayoutSpecModelFactory.create( elements, types, typeElement, mock(Messager.class), RunMode.normal(), null, null); EventDeclarationModel eventDeclarationModel = new EventDeclarationModel( ClassName.OBJECT, ClassName.OBJECT, ImmutableList.of( new FieldModel( FieldSpec.builder(TypeName.INT, "field1", Modifier.PUBLIC).build(), new Object()), new FieldModel( FieldSpec.builder(TypeName.INT, "field2", Modifier.PUBLIC).build(), new Object())), new Object()); when(mMockSpecModel.getEventDeclarations()).thenReturn(ImmutableList.of(eventDeclarationModel)); when(mMockSpecModel.getContextClass()).thenReturn(ClassNames.COMPONENT_CONTEXT); when(mMockSpecModel.getComponentName()).thenReturn("Test"); when(mMockSpecModel.getScopeMethodName()).thenReturn("getComponentScope"); }
@Test public void testFieldExtraction() { 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)); }
@Test public void testDuplicatePropValidationError() { final Elements elements = mCompilationRule.getElements(); final Types types = mCompilationRule.getTypes(); final TypeElement typeElement = elements.getTypeElement(DupeLayoutSpec.class.getCanonicalName()); final LayoutSpecModel layoutSpecModel = mFactory.create( elements, types, typeElement, mock(Messager.class), RunMode.normal(), null, null); final List<SpecModelValidationError> specModelValidationErrors = SpecModelValidation.validateLayoutSpecModel(layoutSpecModel, RunMode.normal()); assertThat(specModelValidationErrors) .extracting("message") .contains( "The prop prop1 is defined differently in different methods. " + "Ensure that each instance of this prop is declared in the same way " + "(this means having the same type, resType and values for isOptional, " + "isCommonProp and overrideCommonPropBehavior)."); } }
@Test public void testAnnotationExtraction() { final Elements elements = mCompilationRule.getElements(); final TypeElement element = elements.getTypeElement(TestClass.class.getCanonicalName()); final ImmutableList<AnnotationSpec> annotationSpecs = AnnotationExtractor.extractValidAnnotations(element); assertThat(annotationSpecs) .hasSize(1) .withFailMessage("Only the @Deprecated annotation should be extracted."); assertThat(annotationSpecs) .contains(AnnotationSpec.builder(Deprecated.class).build()) .doesNotContain(AnnotationSpec.builder(OnCreateInitialState.class).build()) .doesNotContain(AnnotationSpec.builder(SourceAnnotation.class).build()); } }