Skip to content

Commit

Permalink
Add prettify API with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThisaruGuruge committed Apr 4, 2024
1 parent b9483e7 commit fa9d2c9
Show file tree
Hide file tree
Showing 19 changed files with 695 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ballerina/json_api.bal
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,19 @@ public isolated function parseStream(stream<byte[], error?> s, Options options =
#
# + v - Source anydata value
# + return - representation of `v` as value of type json
public isolated function toJson(anydata v)
public isolated function toJson(anydata v)
returns json|Error = @java:Method {'class: "io.ballerina.lib.data.jsondata.json.Native"} external;

# Prettifies the provided JSON value.
#
# + value - The `json` value to be prettified
# + indentation - The number of spaces for an indentation
# + return - The prettified `json` as a string
public isolated function prettify(json value, int indentation = 4) returns string {
string indent = getIndentation(indentation);
return prettifyJson(value, indent, 0);
}

# Represent the options that can be used to modify the behaviour of the projection.
#
# + allowDataProjection - enable or disable projection
Expand Down
224 changes: 224 additions & 0 deletions ballerina/tests/prettify_test.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com).
//
// WSO2 LLC. licenses this file to you 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.

import ballerina/test;

@test:Config {
groups: ["prettify", "string"]
}
function testStringValue() returns error? {
json value = "Sam";
string actual = prettify(value);
string expected = check getStringContentFromFile("string_value.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "int"]
}
function testIntValue() returns error? {
json value = 515;
string actual = prettify(value);
string expected = check getStringContentFromFile("int_value.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "boolean"]
}
function testBooleanValue() returns error? {
json value = false;
string actual = prettify(value);
string expected = check getStringContentFromFile("boolean_value.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "null"]
}
function testNullValue() returns error? {
json value = null;
string actual = prettify(value);
string expected = " null";
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array"]
}
function testStringArray() returns error? {
json value = ["sam", "bam", "tan"];
string actual = prettify(value);
string expected = check getStringContentFromFile("string_array.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array"]
}
function testEmptyArray() returns error? {
json value = [];
string actual = prettify(value);
string expected = check getStringContentFromFile("empty_array.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testEmptyMap() returns error? {
json value = {};
string actual = prettify(value);
string expected = check getStringContentFromFile("empty_map.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testArrayOfEmptyMaps() returns error? {
json value = [
{},
{},
{}
];
string actual = prettify(value);
string expected = check getStringContentFromFile("array_of_empty_maps.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testMapWithStringField() returns error? {
json value = {
name: "Walter White"
};
string actual = prettify(value);
string expected = check getStringContentFromFile("map_with_string_field.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testMapWithMultipleStringFields() returns error? {
json value = {
name: "Walter White",
subject: "Chemistry",
city: "Albequerque"
};
string actual = prettify(value);
string expected = check getStringContentFromFile("map_with_multiple_string_fields.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testMap() returns error? {
json value = {
person: {
name: "Walter White",
age: 51,
address: {
number: 308,
street: "Negra Arroyo Lane",
city: "Albequerque"
}
}
};
string actual = prettify(value);
string expected = check getStringContentFromFile("map.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "map"]
}
function testArrayOfMap() returns error? {
json value = [
{
name: "Walter White",
age: 51,
address: {
number: 308,
street: "Negra Arroyo Lane",
city: "Albequerque"
}
},
{
name: "Jesse Pinkman",
age: 26,
address: {
number: 9809,
street: "Margo Street",
city: "Albequerque"
}
}
];
string actual = prettify(value);
string expected = check getStringContentFromFile("array_of_map.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array", "map"]
}
function testComplexExample() returns error? {
json value = check getJsonContentFromFile("complex_example.json");
string actual = prettify(value);
string expected = check getStringContentFromFile("complex_example.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array", "map"]
}
function testComplexExampleWithCustomIndentation() returns error? {
json value = check getJsonContentFromFile("complex_example.json");
string actual = prettify(value, 2);
string expected = check getStringContentFromFile("complex_example_with_custom_indentation.json");
test:assertEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "array", "map"]
}
function testComplexExampleWithCustomIndentationInvalidTest() returns error? {
json value = check getJsonContentFromFile("complex_example.json");
string actual = prettify(value, 2);
string expected = check getStringContentFromFile("complex_example.json");
test:assertNotEquals(actual, expected);
}

@test:Config {
groups: ["prettify", "null"]
}
function testJsonValueWithNull() returns error? {
json value = {
name: "Walter White",
age: null,
address: {
number: 308,
street: (),
city: "Albequerque"
}
};
string actual = prettify(value);
string expected = check getStringContentFromFile("json_value_with_null.json");
test:assertEquals(actual, expected);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{},
{},
{}
]
20 changes: 20 additions & 0 deletions ballerina/tests/resources/expected_results/array_of_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"name": "Walter White",
"age": 51,
"address": {
"number": 308,
"street": "Negra Arroyo Lane",
"city": "Albequerque"
}
},
{
"name": "Jesse Pinkman",
"age": 26,
"address": {
"number": 9809,
"street": "Margo Street",
"city": "Albequerque"
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false
87 changes: 87 additions & 0 deletions ballerina/tests/resources/expected_results/complex_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"colors": [
{
"color": "black",
"category": "hue",
"type": "primary",
"code": {
"rgba": [
255,
255,
255,
1
],
"hex": "#000"
}
},
{
"color": "white",
"category": "value",
"code": {
"rgba": [
0,
0,
0,
1
],
"hex": "#FFF"
}
},
{
"color": "red",
"category": "hue",
"type": "primary",
"code": {
"rgba": [
255,
0,
0,
1
],
"hex": "#FF0"
}
},
{
"color": "blue",
"category": "hue",
"type": "primary",
"code": {
"rgba": [
0,
0,
255,
1
],
"hex": "#00F"
}
},
{
"color": "yellow",
"category": "hue",
"type": "primary",
"code": {
"rgba": [
255,
255,
0,
1
],
"hex": "#FF0"
}
},
{
"color": "green",
"category": "hue",
"type": "secondary",
"code": {
"rgba": [
0,
255,
0,
1
],
"hex": "#0F0"
}
}
]
}
Loading

0 comments on commit fa9d2c9

Please sign in to comment.