/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF 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 bugs

import org.junit.jupiter.api.Test

import static groovy.test.GroovyAssert.assertScript
import static groovy.test.GroovyAssert.shouldFail

final class Groovy8678 {

    @Test
    void testFloatingPointLiteralWithoutLeadingZero() {
        assertScript '''
            def aFloat = .42
            assert (aFloat as String) == '0.42'
            assert aFloat / .21 == 2
            assert aFloat / 2 == .21
        '''
    }

    private class WithMethods {
        def '42'() {
            'FortyTwo'
        }
        def '42d'() {
            'FortyTwoDee'
        }
        def get84() {
            'EightyFour'
        }
        def get84f() {
            'EightyFourEff'
        }
        def call(arg) {
            "Arg: ${arg}"
        }
    }

    @Test
    void testMethodCallQuoted() {
        def x = new WithMethods()
        assert x.'42'() == 'FortyTwo'
        assert x
        // with new line in between
                .'42'() == 'FortyTwo'
        assert x.'42d'() == 'FortyTwoDee'
        assert x.'84' == 'EightyFour'
        assert x.'84f' == 'EightyFourEff'
    }

    @Test
    void testCallWithNumberArgument() {
        def x = new WithMethods()
        assert (x .42 == 'Arg: 0.42')
        assert (x.42 == 'Arg: 0.42')
        assert (x .42f == 'Arg: 0.42')
        assert (x.42f == 'Arg: 0.42')
        assert (x .84d == 'Arg: 0.84')
        assert (x.84f == 'Arg: 0.84')
    }

    @Test
    void testMissingPropertyOrMethod() {
        def x = new WithMethods()

        def throwable = shouldFail(MissingPropertyException) { x.'42' }
        assert throwable.message.startsWith('No such property: 42 for class: bugs.Groovy8678$WithMethods')

        throwable = shouldFail(MissingPropertyException) { x.'42d' }
        assert throwable.message.startsWith('No such property: 42d for class: bugs.Groovy8678$WithMethods')

        throwable = shouldFail(MissingMethodException) { x.'84'() }
        assert throwable.message.startsWith('No signature of method: 84 for class: bugs.Groovy8678$WithMethods is applicable for argument types: () values: []')

        throwable = shouldFail(MissingMethodException) { x.'84f'() }
        assert throwable.message.startsWith('No signature of method: 84f for class: bugs.Groovy8678$WithMethods is applicable for argument types: () values: []')
    }

    @Test
    void testCompilationFailure() {
        shouldNotCompile('\'42\'', '\'FortyTwo\'', '42',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (BigDecimal) values: [0.42]'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.42\''))
        shouldNotCompile('\'42\'', '\'FortyTwo\'', '42()',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: java.math.BigDecimal is applicable for argument types: () values: []'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.42\''))
        shouldNotCompile('\'42d\'', '\'FortyTwo\'', '42d',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (Double) values: [0.42]'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.42d\''))
        shouldNotCompile('\'42d\'', '\'FortyTwo\'', '42d()',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: java.lang.Double is applicable for argument types: () values: []'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.42d\''))
        shouldNotCompile('get84', '\'EightyFour\'', '84',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (BigDecimal) values: [0.84]'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.84\''))
        shouldNotCompile('get84', '\'EightyFour\'', '84()',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: java.math.BigDecimal is applicable for argument types: () values: []'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.84\''))
        shouldNotCompile('get84f', '\'EightyFourEff\'', '84f',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (Float) values: [0.84]'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.84f\''))
        shouldNotCompile('get84f', '\'EightyFourEff\'', '84f()',
                // after GROOVY-8678
                MissingMethodException,
                msg -> msg.startsWith('No signature of method: call for class: java.lang.Float is applicable for argument types: () values: []'))
                // before GROOVY-8678
                // MultipleCompilationErrorsException,
                // msg -> msg.contains('Unexpected input: \'x.84f\''))
    }

    private static void shouldNotCompile(String methodName,
                                 String returnValue,
                                 String expression,
                                 Class<? extends Exception> exceptionType,
                                 Closure<String> messageAssertion) {
        def throwable = shouldFail exceptionType, """
            class WithMethods {
                def ${methodName}() {
                    ${returnValue}
                }
            }
            def x = new WithMethods()
            x.${expression}
        """.stripIndent()
        assert messageAssertion(throwable.message)
    }

    @Category(BigDecimal)
    private class BigDecimalCategory {
        // using @Category here has a similar effect like
        //     BigDecimal.metaClass.call << { delegate * 2 }
        // but without the side effects on the state of BigDecimal's metaclass after the test
        def call() {
            return this * 2
        }
    }

    @Category(Float)
    private class FloatCategory {
        // using @Category here has a similar effect like
        //     Float.metaClass.call << { delegate * 2 }
        // but without the side effects on the state of Float's metaclass after the test
        def call() {
            return this * 2
        }
    }

    @Category(Double)
    private class DoubleCategory {
        // using @Category here has a similar effect like
        //     Double.metaClass.call << { delegate * 2 }
        // but without the side effects on the state of Double's metaclass after the test
        def call() {
            return this * 2
        }
    }

    @Test
    void testCategories() {
        def x = new WithMethods()
        use (BigDecimalCategory) {
            assert (x.42() == 'Arg: 0.84')
        }
        use (FloatCategory) {
            assert (x.42f() == 'Arg: 0.84')
        }
        use (DoubleCategory) {
            assert (x.42d() == 'Arg: 0.84')
        }
    }
}
