Update syntaxHighlight test
This commit is contained in:
		
							parent
							
								
									3eea327671
								
							
						
					
					
						commit
						eb232037a1
					
				| @ -1,6 +1,7 @@ | ||||
| package ru.noties.markwon.renderer; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.os.Build; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.text.SpannableStringBuilder; | ||||
| @ -19,21 +20,34 @@ import ru.noties.markwon.SyntaxHighlight; | ||||
| import ru.noties.markwon.spans.SpannableTheme; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| import static org.junit.Assert.assertTrue; | ||||
| import static org.mockito.ArgumentMatchers.any; | ||||
| import static org.mockito.ArgumentMatchers.anyBoolean; | ||||
| import static org.mockito.Mockito.mock; | ||||
| import static org.mockito.Mockito.when; | ||||
| 
 | ||||
| @RunWith(RobolectricTestRunner.class) | ||||
| @Config(manifest = Config.NONE) | ||||
| @Config(manifest = Config.NONE, sdk = { | ||||
|         Build.VERSION_CODES.JELLY_BEAN, | ||||
|         Build.VERSION_CODES.M, | ||||
|         Build.VERSION_CODES.O | ||||
| }) | ||||
| public class SyntaxHighlightTest { | ||||
| 
 | ||||
|     // codeSpan must be before actual highlight spans (true reverse of builder) | ||||
| 
 | ||||
|     // if we go with path of reversing spans inside SpannableBuilder (which | ||||
|     // might extend SpannableStringBuilder like https://github.com/noties/Markwon/pull/71) | ||||
|     // then on M (23) codeSpan will always be _before_ actual highlight and thus | ||||
|     // no highlight will be present | ||||
|     // note that bad behaviour is present on M (emulator/device/robolectric) | ||||
|     // other SDKs are added to validate that they do not fail | ||||
|     @Test | ||||
|     public void test() { | ||||
| 
 | ||||
|         final Object highlightSpan = new Object(); | ||||
|         class Highlight { | ||||
|         } | ||||
| 
 | ||||
|         final Object codeSpan = new Object(); | ||||
| 
 | ||||
|         final SyntaxHighlight highlight = new SyntaxHighlight() { | ||||
| @ -41,7 +55,9 @@ public class SyntaxHighlightTest { | ||||
|             @Override | ||||
|             public CharSequence highlight(@Nullable String info, @NonNull String code) { | ||||
|                 final SpannableStringBuilder builder = new SpannableStringBuilder(code); | ||||
|                 builder.setSpan(highlightSpan, 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | ||||
|                 for (int i = 0, length = code.length(); i < length; i++) { | ||||
|                     builder.setSpan(new Highlight(), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | ||||
|                 } | ||||
|                 return builder; | ||||
|             } | ||||
|         }; | ||||
| @ -57,16 +73,39 @@ public class SyntaxHighlightTest { | ||||
| 
 | ||||
|         final SpannableBuilder builder = new SpannableBuilder(); | ||||
| 
 | ||||
|         append(builder, "# Header 1\n", new Object()); | ||||
|         append(builder, "## Header 2\n", new Object()); | ||||
|         append(builder, "### Header 3\n", new Object()); | ||||
| 
 | ||||
|         final int start = builder.length(); | ||||
| 
 | ||||
|         final SpannableMarkdownVisitor visitor = new SpannableMarkdownVisitor(configuration, builder); | ||||
|         final FencedCodeBlock fencedCodeBlock = new FencedCodeBlock(); | ||||
|         fencedCodeBlock.setLiteral("{code}"); | ||||
| 
 | ||||
|         visitor.visit(fencedCodeBlock); | ||||
| 
 | ||||
|         final Object[] spans = builder.spannableStringBuilder().getSpans(0, builder.length(), Object.class); | ||||
|         final int end = builder.length(); | ||||
| 
 | ||||
|         assertEquals(2, spans.length); | ||||
|         append(builder, "### Footer 3\n", new Object()); | ||||
|         append(builder, "## Footer 2\n", new Object()); | ||||
|         append(builder, "# Footer 1\n", new Object()); | ||||
| 
 | ||||
|         final Object[] spans = builder.spannableStringBuilder().getSpans(start, end, Object.class); | ||||
| 
 | ||||
|         // each character + code span | ||||
|         final int length = fencedCodeBlock.getLiteral().length() + 1; | ||||
|         assertEquals(length, spans.length); | ||||
|         assertEquals(codeSpan, spans[0]); | ||||
|         assertEquals(highlightSpan, spans[1]); | ||||
| 
 | ||||
|         for (int i = 1; i < length; i++) { | ||||
|             assertTrue(spans[i] instanceof Highlight); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|     private static void append(@NonNull SpannableBuilder builder, @NonNull String text, @NonNull Object span) { | ||||
|         final int start = builder.length(); | ||||
|         builder.append(text); | ||||
|         builder.setSpan(span, start, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Dimitry Ivanov
						Dimitry Ivanov