forked from facebook/litho
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This implements a store for serializing and deserializing prop names through a Filer, allowing for saving and retrieving names otherwise inaccessible across module boundaries. Reviewed By: IanChilds Differential Revision: D6313805 fbshipit-source-id: de998db666305cc04861ca46f0851600ad8d39e1
- Loading branch information
1 parent
eee1b21
commit 25a72a8
Showing
2 changed files
with
187 additions
and
3 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
...it/src/test/java/com/facebook/litho/specmodels/processor/PropNameInterStageStoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 2017-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
package com.facebook.litho.specmodels.processor; | ||
|
||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Matchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.facebook.litho.annotations.ResType; | ||
import com.facebook.litho.specmodels.internal.ImmutableList; | ||
import com.facebook.litho.specmodels.model.PropModel; | ||
import com.facebook.litho.testing.assertj.LithoAssertions; | ||
import com.facebook.litho.testing.specmodels.MockMethodParamModel; | ||
import com.facebook.litho.testing.specmodels.MockSpecModel; | ||
import com.squareup.javapoet.ClassName; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.Optional; | ||
import javax.annotation.processing.Filer; | ||
import javax.tools.FileObject; | ||
import javax.tools.JavaFileManager; | ||
import javax.tools.StandardLocation; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Answers; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
/** Tests {@link PropNameInterStageStore} */ | ||
public class PropNameInterStageStoreTest { | ||
@Mock(answer = Answers.RETURNS_DEEP_STUBS) | ||
Filer mFiler; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
} | ||
|
||
@Test | ||
public void testLoad() throws IOException { | ||
final PropNameInterStageStore store = new PropNameInterStageStore(mFiler); | ||
|
||
final FileObject fileObject = makeFileObjectForString("arg0\narg1\n"); | ||
when(mFiler.getResource(any(JavaFileManager.Location.class), anyString(), anyString())) | ||
.thenReturn(fileObject); | ||
|
||
final Optional<ImmutableList<String>> strings = | ||
store.loadNames(new MockName("com.example.MyComponentSpec")); | ||
LithoAssertions.assertThat(strings.isPresent()).isTrue(); | ||
LithoAssertions.assertThat(strings.get()).containsExactly("arg0", "arg1"); | ||
|
||
verify(mFiler) | ||
.getResource( | ||
StandardLocation.CLASS_PATH, "", "META-INF/litho/com.example.MyComponentSpec.props"); | ||
} | ||
|
||
@Test | ||
public void testSave() throws IOException { | ||
final PropNameInterStageStore store = new PropNameInterStageStore(mFiler); | ||
|
||
final MockSpecModel specModel = | ||
MockSpecModel.newBuilder() | ||
.props(ImmutableList.of(makePropModel("param0"), makePropModel("param1"))) | ||
.specTypeName(ClassName.get(MyTestSpec.class)) | ||
.build(); | ||
store.saveNames(specModel); | ||
|
||
verify(mFiler) | ||
.createResource( | ||
StandardLocation.CLASS_OUTPUT, | ||
"", | ||
"META-INF/litho/com.facebook.litho.specmodels.processor.PropNameInterStageStoreTest.MyTestSpec.props"); | ||
|
||
// Not checking the actually written values here because Java IO is a horrible mess. | ||
} | ||
|
||
public static class MyTestSpec {} | ||
|
||
static FileObject makeFileObjectForString(String value) throws IOException { | ||
final ByteArrayInputStream inputStream = new ByteArrayInputStream(value.getBytes()); | ||
final FileObject file = mock(FileObject.class); | ||
when(file.openInputStream()).thenReturn(inputStream); | ||
return file; | ||
} | ||
|
||
static PropModel makePropModel(String name) { | ||
return new PropModel( | ||
MockMethodParamModel.newBuilder().name(name).build(), false, ResType.BOOL, ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters