<commit_before>#!/usr/bin/env python3
#  Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed 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.

from nose2.tools.params import params

from fruit_test_common import *

COMMON_DEFINITIONS = '''
    #include "test_common.h"

    struct X;

    struct Annotation1 {};
    using XAnnot1 = fruit::Annotated<Annotation1, X>;
    '''

def test_success():
    source = '''
        struct X {
          int n;

          X(int n)
            : n(n) {
          }
        };

        fruit::Component<X> getComponent(X& x) {
          return fruit::createComponent()
            .bindInstance(x);
        }

        int main() {
          X x(34);
          fruit::Injector<X> injector(getComponent(x));
          X& x1 = injector.get<X&>();
          Assert(&x == &x1);
        }
        '''
    expect_success(COMMON_DEFINITIONS, source)

def test_success_annotated():
    source = '''
        struct X {
          int n;

          X(int n)
            : n(n) {
          }
        };

        fruit::Component<XAnnot1> getComponent(X& x) {
          return fruit::createComponent()
            .bindInstance<XAnnot1>(x);
        }

        int main() {
          X x(34);
          fruit::Injector<XAnnot1> injector(getComponent(x));
          X& x1 = injector.get<fruit::Annotated<Annotation1, X&>>();
          Assert(&x == &x1);
        }
        '''
    expect_success(COMMON_DEFINITIONS, source)

@params(
    ('X', 'X&'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X&>'))
def test_success_two_explicit_type_arguments(XAnnot, XRefAnnot):
    source = '''
        struct X {
          int n;

          X(int n)
            : n(n) {
          }
        };

        fruit::Component<XAnnot> getComponent(X& x) {
          return fruit::createComponent()
            .bindInstance<XAnnot, X>(x);
        }

        int main() {
          X x(34);
          fruit::Injector<XAnnot> injector(getComponent(x));
          X& x1 = injector.get<XRefAnnot>();
          Assert(&x == &x1);
        }
        '''
    expect_success(COMMON_DEFINITIONS, source, locals())

@params('const X', 'X*', 'const X*', 'const X&', 'std::shared_ptr<X>')
def test_non_normalized_type_error(XVariant):
    if XVariant.endswith('&'):
        XVariantRegexp = re.escape(XVariant[:-1])
    else:
        XVariantRegexp = re.escape(XVariant)
    source = '''
        struct X {};

        fruit::Component<> getComponent(XVariant x) {
          return fruit::createComponent()
            .bindInstance(x);
        }
        '''
    expect_compile_error(
        'NonClassTypeError<XVariantRegexp,X>',
        'A non-class type T was specified. Use C instead.',
        COMMON_DEFINITIONS,
        source,
        locals())

@params(
    ('const X', 'const X'),
    ('X*', 'X\*'),
    ('const X*', 'const X\*'),
    ('X&', 'X&'),
    # Note: here the type in the error is 'const X', not 'const X&', because
    # we check that the type of the value is normalized first.
    ('const X&', 'const X'),
    ('std::shared_ptr<X>', 'std::shared_ptr<X>'),
)
def test_non_normalized_type_error_with_annotation(XVariant, XVariantRegexp):
    source = '''
        struct X {};

        fruit::Component<> getComponent(XVariant x) {
          return fruit::createComponent()
            .bindInstance<fruit::Annotated<Annotation1, XVariant>>(x);
        }
        '''
    expect_compile_error(
        'NonClassTypeError<XVariantRegexp,X>',
        'A non-class type T was specified. Use C instead.',
        COMMON_DEFINITIONS,
        source,
        locals())

@params(
    ('const X', 'const X'),
    ('X*', 'X*'),
    ('const X*', 'const X*'),
    ('X&', 'X&'),
    ('const X&', 'const X&'),
    ('std::shared_ptr<X>', 'std::shared_ptr<X>'),

    ('fruit::Annotated<Annotation1, const X>', 'const X'),
    ('fruit::Annotated<Annotation1, X*>', 'X*'),
    ('fruit::Annotated<Annotation1, const X*>', 'const X*'),
    ('fruit::Annotated<Annotation1, X&>', 'X&'),
    ('fruit::Annotated<Annotation1, const X&>', 'const X&'),
    ('fruit::Annotated<Annotation1, std::shared_ptr<X>>', 'std::shared_ptr<X>'),

    ('fruit::Annotated<Annotation1, X>', 'const X'),
    ('fruit::Annotated<Annotation1, X>', 'X*'),
    ('fruit::Annotated<Annotation1, X>', 'const X*'),
    ('fruit::Annotated<Annotation1, X>', 'X&'),
    ('fruit::Annotated<Annotation1, X>', 'const X&'),
    ('fruit::Annotated<Annotation1, X>', 'std::shared_ptr<X>'),
)
def test_non_normalized_type_error_two_explicit_type_arguments(XAnnotVariant, XVariant):
    XVariantRegexp = re.escape(XVariant)
    source = '''
        struct X {};

        fruit::Component<> getComponent(XVariant x) {
          return fruit::createComponent()
            .bindInstance<XAnnotVariant, XVariant>(x);
        }<commit_msg>Fix an escaping issue in some e2e tests that was causing test failures in OS X.<commit_after>#!/usr/bin/env python3
#  Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed 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.

from nose2.tools.params import params

from fruit_test_common import *

COMMON_DEFINITIONS = '''
    #include "test_common.h"

    struct X;

    struct Annotation1 {};
    using XAnnot1 = fruit::Annotated<Annotation1, X>;
    '''

def escape_regex(regex):
    # We un-escape the space because we strip the spaces in fruit_test_common, and this would otherwise leave a
    # stray backslash.
    return re.escape(regex).replace('\\ ', ' ')

def test_success():
    source = '''
        struct X {
          int n;

          X(int n)
            : n(n) {
          }
        };

        fruit::Component<X> getComponent(X& x) {
          return fruit::createComponent()
            .bindInstance(x);
        }

        int main() {
          X x(34);
          fruit::Injector<X> injector(getComponent(x));
          X& x1 = injector.get<X&>();
          Assert(&x == &x1);
        }
        '''
    expect_success(COMMON_DEFINITIONS, source)

def test_success_annotated():
    source = '''
        struct X {
          int n;

          X(int n)
            : n(n) {
          }
        };

        fruit::Component<XAnnot1> getComponent(X& x) {
          return fruit::createComponent()
            .bindInstance<XAnnot1>(x);
        }

        int main() {
          X x(34);
          fruit::Injector<XAnnot1> injector(getComponent(x));
          X& x1 = injector.get<fruit::Annotated<Annotation1, X&>>();
          Assert(&x == &x1);
        }
        '''
    expect_success(COMMON_DEFINITIONS, source)

@params(
    ('X', 'X&'),
    ('fruit::Annotated<Annotation1, X>', 'fruit::Annotated<Annotation1, X&>'))
def test_success_two_explicit_type_arguments(XAnnot, XRefAnnot):
    source = '''
        struct X {
          int n;

          X(int n)
            : n(n) {
          }
        };

        fruit::Component<XAnnot> getComponent(X& x) {
          return fruit::createComponent()
            .bindInstance<XAnnot, X>(x);
        }

        int main() {
          X x(34);
          fruit::Injector<XAnnot> injector(getComponent(x));
          X& x1 = injector.get<XRefAnnot>();
          Assert(&x == &x1);
        }
        '''
    expect_success(COMMON_DEFINITIONS, source, locals())

@params('const X', 'X*', 'const X*', 'const X&', 'std::shared_ptr<X>')
def test_non_normalized_type_error(XVariant):
    if XVariant.endswith('&'):
        XVariantRegexp = escape_regex(XVariant[:-1])
    else:
        XVariantRegexp = escape_regex(XVariant)
    source = '''
        struct X {};

        fruit::Component<> getComponent(XVariant x) {
          return fruit::createComponent()
            .bindInstance(x);
        }
        '''
    expect_compile_error(
        'NonClassTypeError<XVariantRegexp,X>',
        'A non-class type T was specified. Use C instead.',
        COMMON_DEFINITIONS,
        source,
        locals())

@params(
    ('const X', 'const X'),
    ('X*', 'X\*'),
    ('const X*', 'const X\*'),
    ('X&', 'X&'),
    # Note: here the type in the error is 'const X', not 'const X&', because
    # we check that the type of the value is normalized first.
    ('const X&', 'const X'),
    ('std::shared_ptr<X>', 'std::shared_ptr<X>'),
)
def test_non_normalized_type_error_with_annotation(XVariant, XVariantRegexp):
    source = '''
        struct X {};

        fruit::Component<> getComponent(XVariant x) {
          return fruit::createComponent()
            .bindInstance<fruit::Annotated<Annotation1, XVariant>>(x);
        }
        '''
    expect_compile_error(
        'NonClassTypeError<XVariantRegexp,X>',
        'A non-class type T was specified. Use C instead.',
        COMMON_DEFINITIONS,
        source,
        locals())

@params(
    ('const X', 'const X'),
    ('X*', 'X*'),
    ('const X*', 'const X*'),
    ('X&', 'X&'),
    ('const X&', 'const X&'),
    ('std::shared_ptr<X>', 'std::shared_ptr<X>'),

    ('fruit::Annotated<Annotation1, const X>', 'const X'),
    ('fruit::Annotated<Annotation1, X*>', 'X*'),
    ('fruit::Annotated<Annotation1, const X*>', 'const X*'),
    ('fruit::Annotated<Annotation1, X&>', 'X&'),
    ('fruit::Annotated<Annotation1, const X&>', 'const X&'),
    ('fruit::Annotated<Annotation1, std::shared_ptr<X>>', 'std::shared_ptr<X>'),

    ('fruit::Annotated<Annotation1, X>', 'const X'),
    ('fruit::Annotated<Annotation1, X>', 'X*'),
    ('fruit::Annotated<Annotation1, X>', 'const X*'),
    ('fruit::Annotated<Annotation1, X>', 'X&'),
    ('fruit::Annotated<Annotation1, X>', 'const X&'),
    ('fruit::Annotated<Annotation1, X>', 'std::shared_ptr<X>'),
)
def test_non_normalized_type_error_two_explicit_type_arguments(XAnnotVariant, XVariant):
    XVariantRegexp = escape_regex(XVariant)
    source = '''
        struct X {};

        fruit::Component<> getComponent(XVariant x) {
          return fruit::createComponent()
            .bindInstance<XAnnotVariant, XVariant>(x);
        }
{'fraction_non_alphanumeric': 0.15625574131912548, 'fraction_numerical': 0.008359360646702186, 'mean_word_length': 2.6302100700233413, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2616808', 'n_tokens_mistral': 3526, 'n_tokens_neox': 3460, 'n_words': 822}