import ballerina/test;
@test:Config
function testAssertIntEquals (){    int answer = 0;
    int a = 5;
    int b = 3;
    answer = intAdd(a, b);
    test:assertEquals(answer, 8, msg = "intAdd function failed");}
@test:Config
function testAssertFloatEquals () {
    float a = 10.000;
    float b = 20.050;
    float answer = floatAdd(a, b);
    test:assertEquals(answer, 30.050, msg = "floatAdd function failed");
}
@test:Config
function testAssertStringEquals () {
    string a = "John";
    string b = "Doe";
    string concatenated = stringConcat(a, b);
    test:assertEquals(concatenated, "JohnDoe", msg = "string concatenation failed");
}
@test:Config
function testAssertJsonEquals () {    json a = { "name": "Ballerina" };
    json b = { "name": "Ballerina" };
    test:assertEquals(a, b, msg = "json assert equals failed");
}
@test:Config
function testAssertBooleanEquals () {    boolean x = true;
    boolean y = true;
    test:assertEquals(x, y, msg = "assertBooleanEquals failed");
}
@test:Config
function testAssertStringArrayEquals () {    string[] x = ["A", "B", "C"];
    string[] y = ["A", "B", "C"];    test:assertEquals(x, y, msg = "String arrays are not equal");}
@test:Config
function testAssertIntArrayEquals () {    int[] x = [1, 2, 3];
    int[] y = [1, 2, 3];    test:assertEquals(x, y, msg = "Int arrays are not equal");
}
@test:Config
function testAssertFloatArrayEquals () {    float[] x = [1.1, 2.2, 3.3];
    float[] y = [1.1, 2.2, 3.3];    test:assertEquals(x, y, msg = "failed");
}
@test:Config
function testAssertNotEqualsString () {    string s1 = "abc";
    string s2 = "def";    test:assertNotEquals(s1, s2, msg = "Strings are equal");}
@test:Config
function testAssertNotEqualsJson () {    json s1 = {"a":"b"};
    json s2 = {"a":"c"};
    test:assertNotEquals(s1, s2, msg = "Json are equal");
}
@test:Config
function testAssertTrue () {
    boolean value = true;
    test:assertTrue(value, msg = "assertTrue failed");
}
@test:Config
function testAssertFalse () {    boolean value = false;
    test:assertFalse(value, msg = "assertFalse failed");
}
@test:Config
function testAssertFail1 () {
    try {
        test:assertFail(msg = "Exception Never occured");    } catch (error e) {
    }
}
@test:Config
function testAssertFail2 () {
    if (true) {
        return;
    }
    test:assertFail(msg = "assertFailed");
}
function intAdd (int a, int b) returns (int) {
    return (a + b);
}function floatAdd (float a, float b) returns (float) {
    return (a + b);
}function stringConcat (string a, string b) returns (string) {
    return (a + b);
}

Testerina Assertions

Testerina, has inbuilt assertions which enables developers to assert a outcome against a expected outcome. This sample illustrate usage of different assertions

import ballerina/test;

===== Assert Equals ===== Assert equal allows you to compare primitive types to composite objects

@test:Config
function testAssertIntEquals (){

Comparing Integer values

    int answer = 0;
    int a = 5;
    int b = 3;
    answer = intAdd(a, b);
    test:assertEquals(answer, 8, msg = "intAdd function failed");
}
@test:Config
function testAssertFloatEquals () {
    float a = 10.000;
    float b = 20.050;
    float answer = floatAdd(a, b);
    test:assertEquals(answer, 30.050, msg = "floatAdd function failed");
}

Comparing Float values

@test:Config
function testAssertStringEquals () {
    string a = "John";
    string b = "Doe";
    string concatenated = stringConcat(a, b);
    test:assertEquals(concatenated, "JohnDoe", msg = "string concatenation failed");
}

Comparing String values

@test:Config
function testAssertJsonEquals () {

Comparing Json objects

    json a = { "name": "Ballerina" };
    json b = { "name": "Ballerina" };
    test:assertEquals(a, b, msg = "json assert equals failed");
}
@test:Config
function testAssertBooleanEquals () {

Comparing boolean values

    boolean x = true;
    boolean y = true;
    test:assertEquals(x, y, msg = "assertBooleanEquals failed");
}
@test:Config
function testAssertStringArrayEquals () {

Comparing string arrays

    string[] x = ["A", "B", "C"];
    string[] y = ["A", "B", "C"];
    test:assertEquals(x, y, msg = "String arrays are not equal");
}
@test:Config
function testAssertIntArrayEquals () {

Comparing Integer arrays

    int[] x = [1, 2, 3];
    int[] y = [1, 2, 3];
    test:assertEquals(x, y, msg = "Int arrays are not equal");
}
@test:Config
function testAssertFloatArrayEquals () {

Comparing Float arrays

    float[] x = [1.1, 2.2, 3.3];
    float[] y = [1.1, 2.2, 3.3];
    test:assertEquals(x, y, msg = "failed");
}

===== Assert Not Equals ==== // This is the negation of asset equals.

@test:Config
function testAssertNotEqualsString () {

Comparing distinct strings

    string s1 = "abc";
    string s2 = "def";
    test:assertNotEquals(s1, s2, msg = "Strings are equal");
}
@test:Config
function testAssertNotEqualsJson () {

Comparing distinct Json

    json s1 = {"a":"b"};
    json s2 = {"a":"c"};
    test:assertNotEquals(s1, s2, msg = "Json are equal");
}

===== Assert True ===== // Assert true allows you to compare a boolean value

@test:Config
function testAssertTrue () {
    boolean value = true;
    test:assertTrue(value, msg = "assertTrue failed");
}

Asserting true

@test:Config
function testAssertFalse () {

Asserting false

    boolean value = false;
    test:assertFalse(value, msg = "assertFalse failed");
}

==== Asset Fail ==== // Assert fail allows you to fail a test intentionally

@test:Config
function testAssertFail1 () {
    try {

Failing a test with assert fail

        test:assertFail(msg = "Exception Never occured");

I’m expecting a error

    } catch (error e) {
    }
}

Do more assertions

@test:Config
function testAssertFail2 () {
    if (true) {
        return;
    }
    test:assertFail(msg = "assertFailed");
}

Assert Fail example 2

function intAdd (int a, int b) returns (int) {
    return (a + b);
}

Test functions

function floatAdd (float a, float b) returns (float) {
    return (a + b);
}
function stringConcat (string a, string b) returns (string) {
    return (a + b);
}
$ ballerina test testerina-assertions.bal
---------------------------------------------------------------------------
    T E S T S
---------------------------------------------------------------------------
---------------------------------------------------------------------------
Running Tests of Package: .
---------------------------------------------------------------------------
Tests run: 14, Passed: 14, Failures: 0, Skipped: 0 - in TestSuite
---------------------------------------------------------------------------
Results:
Tests run: 14, Passed: 14, Failures: 0, Skipped: 0
---------------------------------------------------------------------------
Summary:
................................................................... SUCCESS
---------------------------------------------------------------------------