Skip to content

Commit

Permalink
Merge pull request #177 from ballerina-platform/bdev
Browse files Browse the repository at this point in the history
Remove the usage of chackpanic
  • Loading branch information
BuddhiWathsala authored May 10, 2021
2 parents 12e0a45 + f33c8f1 commit 9cedc1b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
8 changes: 6 additions & 2 deletions io-ballerina/string_reader.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ public class StringReader {
# + encoding - Encoding of the characters of the content
public isolated function init(string content, string encoding = "UTF-8") {
byte[] contentBytes = content.toBytes();
ReadableByteChannel byteChannel = checkpanic createReadableChannel(contentBytes);
self.charChannel = new ReadableCharacterChannel(byteChannel, encoding);
ReadableByteChannel|Error byteChannel = createReadableChannel(contentBytes);
if byteChannel is Error {
self.charChannel = ();
} else {
self.charChannel = new ReadableCharacterChannel(byteChannel, encoding);
}
}

# Reads string as JSON using the reader.
Expand Down
56 changes: 28 additions & 28 deletions io-ballerina/tests/xml_io.bal
Original file line number Diff line number Diff line change
Expand Up @@ -238,28 +238,28 @@ isolated function testFileWriteXmlWithOverwrite() {
}

@test:Config {}
isolated function testFileWriteDocTypedXml() {
isolated function testFileWriteDocTypedXml() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile4.xml";
string resultFilePath = "tests/resources/expectedXmlCharsFile4.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";

xml content = checkpanic fileReadXml(originalFilePath);
xml content = check fileReadXml(originalFilePath);
string doctypeValue = "<!DOCTYPE note SYSTEM \"Note.dtd\">";
var writeResult = fileWriteXml(filePath, content, doctype = {system: "Note.dtd"});
if (writeResult is Error) {
test:assertFail(msg = writeResult.message());
}
string readResult = checkpanic fileReadString(filePath);
string expectedResult = checkpanic fileReadString(resultFilePath);
string readResult = check fileReadString(filePath);
string expectedResult = check fileReadString(resultFilePath);
test:assertEquals(readResult, expectedResult);
}

@test:Config {}
isolated function testFileWriteDocTypedWithMultiRoots() {
isolated function testFileWriteDocTypedWithMultiRoots() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile4.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";

xml content = checkpanic fileReadXml(originalFilePath);
xml content = check fileReadXml(originalFilePath);
xml x1 = xml `<body>Don't forget me this weekend!</body>`;

var writeResult = fileWriteXml(filePath, xml:concat(content, x1));
Expand All @@ -271,11 +271,11 @@ isolated function testFileWriteDocTypedWithMultiRoots() {
}

@test:Config {}
isolated function testFileWriteDocTypedWithAppend() {
isolated function testFileWriteDocTypedWithAppend() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile4.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";

xml content = checkpanic fileReadXml(originalFilePath);
xml content = check fileReadXml(originalFilePath);
var writeResult = fileWriteXml(filePath, content, fileWriteOption = APPEND);
if (writeResult is Error) {
test:assertEquals(writeResult.message(), "The file append operation is not allowed for Document Entity");
Expand All @@ -285,12 +285,12 @@ isolated function testFileWriteDocTypedWithAppend() {
}

@test:Config {}
isolated function testFileAppendDocTypedXml() {
isolated function testFileAppendDocTypedXml() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile5.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";
string resultFilePath = "tests/resources/expectedXmlCharsFile5.xml";

xml content1 = checkpanic fileReadXml(originalFilePath);
xml content1 = check fileReadXml(originalFilePath);
xml content2 = xml `<body>Don't forget me this weekend!</body>`;
var writeResult = fileWriteXml(filePath, content1);
if (writeResult is Error) {
Expand All @@ -301,18 +301,18 @@ isolated function testFileAppendDocTypedXml() {
if (appendResult is Error) {
test:assertFail(msg = appendResult.message());
}
string readResult = checkpanic fileReadString(filePath);
string expectedResult = checkpanic fileReadString(resultFilePath);
string readResult = check fileReadString(filePath);
string expectedResult = check fileReadString(resultFilePath);
test:assertEquals(readResult, expectedResult);
}

@test:Config {}
isolated function testFileWriteDocTypedXmlWithInternalSubset() {
isolated function testFileWriteDocTypedXmlWithInternalSubset() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile6.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";
string resultFilePath = "tests/resources/expectedXmlCharsFile6.xml";

xml content = checkpanic fileReadXml(originalFilePath);
xml content = check fileReadXml(originalFilePath);
string startElement = "<!DOCTYPE note ";
string endElement = ">";
string internalSub = string `[
Expand All @@ -326,18 +326,18 @@ isolated function testFileWriteDocTypedXmlWithInternalSubset() {
if (writeResult is Error) {
test:assertFail(msg = writeResult.message());
}
string readResult = checkpanic fileReadString(filePath);
string expectedResult = checkpanic fileReadString(resultFilePath);
string readResult = check fileReadString(filePath);
string expectedResult = check fileReadString(resultFilePath);
test:assertEquals(readResult, expectedResult);
}

@test:Config {}
isolated function testFileWriteDocTypedXmlWithPrioritizeInternalSubset() {
isolated function testFileWriteDocTypedXmlWithPrioritizeInternalSubset() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile6.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";
string resultFilePath = "tests/resources/expectedXmlCharsFile6.xml";

xml content = checkpanic fileReadXml(originalFilePath);
xml content = check fileReadXml(originalFilePath);
string startElement = "<!DOCTYPE note ";
string endElement = ">";
string systemId = "http://www.w3.org/TR/html4/loose.dtd";
Expand All @@ -355,18 +355,18 @@ isolated function testFileWriteDocTypedXmlWithPrioritizeInternalSubset() {
if (writeResult is Error) {
test:assertFail(msg = writeResult.message());
}
string readResult = checkpanic fileReadString(filePath);
string expectedResult = checkpanic fileReadString(resultFilePath);
string readResult = check fileReadString(filePath);
string expectedResult = check fileReadString(resultFilePath);
test:assertEquals(readResult, expectedResult);
}

@test:Config {}
isolated function testFileWriteDocTypedXmlWithPublicAndSystemId() {
isolated function testFileWriteDocTypedXmlWithPublicAndSystemId() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile7.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";
string resultFilePath = "tests/resources/expectedXmlCharsFile7.xml";

xml content = checkpanic fileReadXml(originalFilePath);
xml content = check fileReadXml(originalFilePath);
string doctypeValue = "<!DOCTYPE note PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
string publicId = "-//W3C//DTD HTML 4.01 Transitional//EN";
string systemId = "http://www.w3.org/TR/html4/loose.dtd";
Expand All @@ -377,26 +377,26 @@ isolated function testFileWriteDocTypedXmlWithPublicAndSystemId() {
if (writeResult is Error) {
test:assertFail(msg = writeResult.message());
}
string readResult = checkpanic fileReadString(filePath);
string expectedResult = checkpanic fileReadString(resultFilePath);
string readResult = check fileReadString(filePath);
string expectedResult = check fileReadString(resultFilePath);
test:assertEquals(readResult, expectedResult);
}

@test:Config {}
isolated function testFileWriteDocTypedXmlWithPublic() {
isolated function testFileWriteDocTypedXmlWithPublic() returns Error? {
string filePath = TEMP_DIR + "xmlCharsFile7.xml";
string originalFilePath = "tests/resources/originalXmlContent.xml";
string resultFilePath = "tests/resources/expectedXmlCharsFile8.xml";

xml content = checkpanic fileReadXml(originalFilePath);
xml content = check fileReadXml(originalFilePath);
string doctypeValue = "<!DOCTYPE note PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
string publicId = "-//W3C//DTD HTML 4.01 Transitional//EN";
var writeResult = fileWriteXml(filePath, content, doctype={'public: publicId});
if (writeResult is Error) {
test:assertFail(msg = writeResult.message());
}
string readResult = checkpanic fileReadString(filePath);
string expectedResult = checkpanic fileReadString(resultFilePath);
string readResult = check fileReadString(filePath);
string expectedResult = check fileReadString(resultFilePath);
test:assertEquals(readResult, expectedResult);
}

Expand Down

0 comments on commit 9cedc1b

Please sign in to comment.