/*
 *   Copyright (c) 2019, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 *  WSO2 Inc. 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.
 */
package org.ballerinalang.test.expressions.access;

import org.ballerinalang.test.BCompileUtil;
import org.ballerinalang.test.BRunUtil;
import org.ballerinalang.test.CompileResult;
import org.ballerinalang.test.exceptions.BLangTestException;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.ballerinalang.test.BAssertUtil.validateError;

/**
 * Test cases for combinations of field, optional field and member access.
 *
 * @since 1.0
 */
public class AccessTest {

    private CompileResult result;
    private CompileResult negativeResult;

    @BeforeClass
    public void setup() {
        result = BCompileUtil.compile("test-src/expressions/access/access.bal");
        negativeResult = BCompileUtil.compile("test-src/expressions/access/access_negative.bal");
    }

    @Test
    public void testNegativeCases() {
        Assert.assertEquals(negativeResult.getErrorCount(), 3);
        int i = 0;
        validateError(negativeResult, i++, "invalid operation: type 'Beta?[]?' does not support member access", 40, 18);
        validateError(negativeResult, i++, "incompatible types: expected 'string', found 'string?'", 53, 17);
        validateError(negativeResult, i, "invalid operation: type 'Delta?' does not support member access", 70, 17);
    }

    @Test(dataProvider = "fieldAndOptionalFieldAccessFunctions")
    public void testFieldAndOptionalFieldAccess(String function) {
        Object returns = BRunUtil.invoke(result, function);
        Assert.assertTrue((Boolean) returns);
    }

    @DataProvider(name = "fieldAndOptionalFieldAccessFunctions")
    public Object[][] fieldAndOptionalFieldAccessFunctions() {
        return new Object[][] {
                { "testFieldAccessWithOptionalFieldAccess1" },
                { "testFieldAccessWithOptionalFieldAccess2" },
                { "testFieldAccessWithOptionalFieldAccess3" }
        };
    }

    @Test(dataProvider = "fieldOptionalFieldAndMemberAccessFunctions")
    public void testFieldOptionalFieldAndMemberAccess(String function) {
        Object returns = BRunUtil.invoke(result, function);
        Assert.assertTrue((Boolean) returns);
    }

    @DataProvider(name = "fieldOptionalFieldAndMemberAccessFunctions")
    public Object[][] fieldOptionalFieldAndMemberAccessFunctions() {
        return new Object[][] {
                { "testFieldOptionalFieldAndMemberAccess1" },
                { "testFieldOptionalFieldAndMemberAccess2" }
        };
    }

    @Test(expectedExceptions = BLangTestException.class,
            expectedExceptionsMessageRegExp = ".*array index out of range: index: 0, size: 0.*")
    public void testFieldOptionalFieldAndMemberAccess2() {
        BRunUtil.invoke(result, "testFieldOptionalFieldAndMemberAccess3");
    }

    @Test
    public void testMemberAccessOnNillableObjectField() {
        Object returns = BRunUtil.invoke(result, "testMemberAccessOnNillableObjectField");
        Assert.assertTrue((Boolean) returns);
    }

    @Test
    public void testNilLiftingOnMemberAccessOnNillableObjectField() {
        Object returns = BRunUtil.invoke(result, "testNilLiftingOnMemberAccessOnNillableObjectField");
        Assert.assertTrue((Boolean) returns);
    }

    @Test
    public void testSimpleTypeAccessOnFunctionPointer() {
        BRunUtil.invoke(result, "testSimpleTypeAccessOnFunctionPointer");
    }

    @Test
    public void testAccessOnGroupedExpressions() {
        Object returns = BRunUtil.invoke(result, "testAccessOnGroupedExpressions");
        Assert.assertTrue((Boolean) returns);
    }

    @AfterClass
    public void tearDown() {
        result = null;
        negativeResult = null;
    }
}
