1#!/usr/bin/env python3
2#  Copyright 2016 Google Inc. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS-IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16from absl.testing import parameterized
17from fruit_test_common import *
18
19COMMON_DEFINITIONS = '''
20    #include "test_common.h"
21
22    using namespace std;
23    using namespace fruit::impl;
24    '''
25
26class TestLambdaInvoker(parameterized.TestCase):
27    def test_invoke_no_args(self):
28        source = '''
29            int main() {
30              // This is static because the lambda must have no captures.
31              static int num_invocations = 0;
32
33              auto l = []() {
34                ++num_invocations;
35              };
36              using L = decltype(l);
37              LambdaInvoker::invoke<L>();
38              Assert(num_invocations == 1);
39            }
40            '''
41        expect_success(
42            COMMON_DEFINITIONS,
43            source,
44            locals())
45
46    def test_invoke_some_args(self):
47        source = '''
48            int main() {
49              // This is static because the lambda must have no captures.
50              static int num_invocations = 0;
51
52              auto l = [](int n, double x) {
53                Assert(n == 5);
54                Assert(x == 3.14);
55                ++num_invocations;
56              };
57              using L = decltype(l);
58              LambdaInvoker::invoke<L>(5, 3.14);
59              Assert(num_invocations == 1);
60            }
61            '''
62        expect_success(
63            COMMON_DEFINITIONS,
64            source,
65            locals())
66
67if __name__ == '__main__':
68    absltest.main()
69