Skip to content

Commit

Permalink
Test html tag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mchowning committed May 20, 2020
1 parent 36dc5bf commit 29dd24a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
15 changes: 15 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ jobs:
include_job_number_field: false
include_project_field: false
failure_message: ':red_circle: Scheduled tests failed on Android device!'
android-native-unit-tests:
parameters:
post-to-slack:
description: Post to Slack when tests fail. SLACK_WEBHOOK ENV variable must be set.
type: boolean
default: false
docker:
- image: circleci/android:api-29-node
steps:
- checkout
- run:
name: Run Android native unit tests
command: cd android && ./gradlew testDebug
ios-device-checks:
parameters:
post-to-slack:
Expand Down Expand Up @@ -204,6 +217,8 @@ workflows:
name: Test iOS on Device
- android-device-checks:
name: Test Android on Device
- android-native-unit-tests:
name: Android Native Unit Tests

ui-tests-full-scheduled:
jobs:
Expand Down
1 change: 1 addition & 0 deletions react-native-aztec/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ dependencies {
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
testImplementation 'junit:junit:4.12'

if (rootProject.ext.buildGutenbergFromSource) {
implementation "com.facebook.react:react-native:+" // From node_modules.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.wordpress.mobile.ReactNativeAztec;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import org.jetbrains.annotations.NotNull;
import org.wordpress.aztec.spans.UnknownHtmlSpan;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
Expand All @@ -23,13 +27,20 @@ private static String createMessage(int selectionStart, int selectionEnd, int te
@NotNull
private static List<String> getHtmlTags(ReactAztecText view, int textLength) {
List<String> tags = new ArrayList<>();
Pattern tagPattern = Pattern.compile("<([^\\\\s>/]+)>");
for (UnknownHtmlSpan span : view.getText().getSpans(0, textLength, UnknownHtmlSpan.class)) {
String rawHtml = span.getRawHtml().toString();
Matcher matcher = tagPattern.matcher(rawHtml);
while (matcher.find()) {
tags.add(matcher.group(1));
}
tags.addAll(parseTags(rawHtml));
}
return tags;
}

@VisibleForTesting
@NonNull
static List<String> parseTags(String html) {
List<String> tags = new ArrayList<>();
Matcher matcher = Pattern.compile("<([^\\\\s>/]+)>").matcher(html);
while (matcher.find()) {
tags.add(matcher.group(1));
}
return tags;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.wordpress.mobile.ReactNativeAztec;

import org.junit.Test;

import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class IllegalSelectionIndexExceptionTest {

@Test
public void getHtmlTag_returns_only_tag() {
List<String> actual = IllegalSelectionIndexException.parseTags("hi <mark>there Bob</mark>. How are you?");
List<String> expected = Collections.singletonList("mark");
assertEquals(expected, actual);
}
}

0 comments on commit 29dd24a

Please sign in to comment.