Skip to content

Commit

Permalink
Allow both upper and lower case in unicode escape.
Browse files Browse the repository at this point in the history
Issue #1
  • Loading branch information
leadpony committed Sep 4, 2019
1 parent 62525b1 commit 88238a3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.leadpony</groupId>
<artifactId>jsonp-test-suite</artifactId>
<version>1.3.0</version>
<version>1.4.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>JSON-P Test Suite</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2019 the JSON-P Test Suite Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.leadpony.jsonp.testsuite.helper;

import org.assertj.core.api.AbstractAssert;

/**
* @author leadpony
*/
public final class JsonAssertions {

public static JsonAssertion assertThat(String actual) {
return new JsonAssertion(actual);
}

private JsonAssertions() {
}

/**
* @author leadpony
*/
public static final class JsonAssertion extends AbstractAssert<JsonAssertion, String> {

private JsonAssertion(String actual) {
super(actual, JsonAssertion.class);
}

public JsonAssertion isEqualTo(String expected) {
isNotNull();
if (!normalizeJson(actual).equals(normalizeJson(expected))) {
failWithMessage("Expected JSON to be <%s> but was <%s>", expected, actual);
}
return this;
}

private static String normalizeJson(String json) {
StringBuilder builder = new StringBuilder();
final int length = json.length();
for (int i = 0; i < length; i++) {
char c = json.charAt(i);
builder.append(c);
if (c == '\"') {
while (++i < length) {
c = json.charAt(i);
builder.append(c);
if (c == '\"') {
break;
} else if (c == '\\') {
if (++i < length) {
c = json.charAt(i);
builder.append(c);
if (c == 'u') {
int j = 0;
while (j++ < 4 && ++i < length) {
c = json.charAt(i);
builder.append(toUpperHexDigit(c));
}
}
}
}
}
}
}
return builder.toString();
}

private static char toUpperHexDigit(char c) {
if ('a' <= c && c <= 'f') {
return (char) ('A' + (c - 'a'));
}
return c;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.leadpony.jsonp.testsuite.helper.JsonAssertions;

/**
* A test type to test {@link JsonGenerator}.
Expand Down Expand Up @@ -272,7 +273,7 @@ public void writeShouldWriteJsonValue(JsonValueTestCase test) {
g.write(test.getJsonValue());
});

assertThat(actual).isEqualTo(test.getString());
JsonAssertions.assertThat(actual).isEqualTo(test.getString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
*/
package org.leadpony.jsonp.testsuite.tests;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.stream.Stream;

import javax.json.JsonString;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.leadpony.jsonp.testsuite.helper.JsonAssertions;

/**
* A test type to test {@link JsonString}.
Expand All @@ -42,6 +41,6 @@ public void toStringShouldEscapeString(JsonValueTestCase test) {

String actual = sut.toString();

assertThat(actual).isEqualTo(test.getString());
JsonAssertions.assertThat(actual).isEqualTo(test.getString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.leadpony.jsonp.testsuite.helper.JsonAssertions;

/**
* A test type to test {@link JsonWriter}.
Expand All @@ -57,7 +58,7 @@ public void writeShouldWriteJsonValueAsExpected(JsonValueTestCase test) {
writer.write(test.getJsonValue());
});

assertThat(actual).isEqualTo(test.getString());
JsonAssertions.assertThat(actual).isEqualTo(test.getString());
}

@ParameterizedTest
Expand Down

0 comments on commit 88238a3

Please sign in to comment.