-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept i18n errors #684
Merged
Merged
Accept i18n errors #684
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
48eb885
WIP Provide i18n form errors
lauraluiz 25429f4
Fix test
lauraluiz d945212
Merge branch 'pre-1.0.0' into i18n-errors
lauraluiz 982159c
Change to messages file to add more cases
lauraluiz b34ea1d
Test error formatter
lauraluiz 88127e7
Support hash args
lauraluiz 778d598
Fix test
lauraluiz 56e72e5
Remove unused error key
lauraluiz 2eec280
Change hashArgs to a more neutral name
lauraluiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
74 changes: 74 additions & 0 deletions
74
common/test/com/commercetools/sunrise/common/utils/ErrorFormatterImplTest.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,74 @@ | ||
package com.commercetools.sunrise.common.utils; | ||
|
||
import com.commercetools.sunrise.common.template.i18n.I18nIdentifier; | ||
import com.commercetools.sunrise.common.template.i18n.I18nIdentifierFactory; | ||
import com.commercetools.sunrise.common.template.i18n.I18nResolver; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Spy; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static java.util.Collections.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class ErrorFormatterImplTest { | ||
|
||
private static final List<Locale> LOCALES = singletonList(Locale.ENGLISH); | ||
private static final String MESSAGE_KEY = "error.required"; | ||
private static final I18nIdentifier I18N_IDENTIFIER = I18nIdentifier.of("main", MESSAGE_KEY); | ||
private static final Map<String, Object> ARGS_WITH_FIELD = singletonMap("field", "username"); | ||
|
||
@Mock | ||
private I18nResolver i18nResolver; | ||
@Spy | ||
private I18nIdentifierFactory i18nIdentifierFactory; | ||
|
||
@InjectMocks | ||
private ErrorFormatterImpl errorFormatter; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
when(i18nResolver.getOrKey(any(), any(), any())).thenCallRealMethod(); | ||
} | ||
|
||
@Test | ||
public void translatesMessageKey() throws Exception { | ||
mockI18nResolverWithError(); | ||
final String errorMessage = errorFormatter.format(LOCALES, MESSAGE_KEY, emptyMap()); | ||
assertThat(errorMessage).isEqualTo("Required field"); | ||
} | ||
|
||
@Test | ||
public void returnsMessageKeyWhenNoMatch() throws Exception { | ||
mockI18nResolverWithoutError(); | ||
final String errorMessage = errorFormatter.format(LOCALES, MESSAGE_KEY, emptyMap()); | ||
assertThat(errorMessage).isEqualTo(MESSAGE_KEY); | ||
} | ||
|
||
@Test | ||
public void returnsMessageKeyWithFieldIfProvided() throws Exception { | ||
mockI18nResolverWithError(); | ||
final String errorMessage = errorFormatter.format(LOCALES, MESSAGE_KEY, ARGS_WITH_FIELD); | ||
assertThat(errorMessage).isEqualTo("Required field: username"); | ||
} | ||
|
||
private void mockI18nResolverWithoutError() { | ||
when(i18nResolver.get(any(), eq(I18N_IDENTIFIER), anyMap())).thenReturn(Optional.empty()); | ||
} | ||
|
||
private void mockI18nResolverWithError() { | ||
when(i18nResolver.get(any(), eq(I18N_IDENTIFIER), anyMap())).thenReturn(Optional.of("Required field")); | ||
when(i18nResolver.get(any(), eq(I18N_IDENTIFIER), eq(singletonMap("field", "username")))).thenReturn(Optional.of("Required field: username")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the new tests 👍