// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../dart/resolution/context_collection_resolution.dart';

main() {
  defineReflectiveSuite(() {
    defineReflectiveTests(FfiNativeCallableListenersMustReturnVoid);
  });
}

@reflectiveTest
class FfiNativeCallableListenersMustReturnVoid
    extends PubPackageResolutionTest {
  test_NativeCallable_isolateLocal_argumentMustBeAConstant() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  int e = 123;
  NativeCallable<Int32 Function(Int32)>.isolateLocal(f, exceptionalReturn: e);
}
''',
      [error(diag.argumentMustBeAConstant, 143, 1)],
    );
  }

  test_NativeCallable_isolateLocal_exceptionMustBeASubtype() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Int32 Function(Int32)>.isolateLocal(f, exceptionalReturn: '?');
}
''',
      [error(diag.mustBeASubtype, 128, 3)],
    );
  }

  test_NativeCallable_isolateLocal_inferred() async {
    await assertErrorsInCode(r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Int32 Function(Int32)>? callback;
  callback = NativeCallable.isolateLocal(f, exceptionalReturn: 4);
  callback.close();
}
''', []);
  }

  test_NativeCallable_isolateLocal_invalidExceptionValue() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
void f(int i) => i * 2;
void g() {
  NativeCallable<Void Function(Int32)>.isolateLocal(f, exceptionalReturn: 4);
}
''',
      [error(diag.invalidExceptionValue, 109, 20)],
    );
  }

  test_NativeCallable_isolateLocal_missingExceptionValue() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Int32 Function(Int32)>.isolateLocal(f);
}
''',
      [error(diag.missingExceptionValue, 55, 53)],
    );
  }

  test_NativeCallable_isolateLocal_mustBeANativeFunctionType() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<int Function(int)>.isolateLocal(f, exceptionalReturn: 4);
}
''',
      [
        error(
          diag.mustBeANativeFunctionType,
          55,
          46,
          messageContains: [
            "The type 'int Function(int)' given to 'NativeCallable' must be",
          ],
        ),
      ],
    );
  }

  test_NativeCallable_isolateLocal_mustBeASubtype() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Int32 Function(Double)>.isolateLocal(f, exceptionalReturn: 4);
}
''',
      [error(diag.mustBeASubtype, 107, 1)],
    );
  }

  test_NativeCallable_isolateLocal_mustHaveTypeArgs() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable.isolateLocal(f, exceptionalReturn: 4);
}
''',
      [error(diag.mustBeANativeFunctionType, 55, 27)],
    );
  }

  test_NativeCallable_isolateLocal_ok() async {
    await assertErrorsInCode(r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Int32 Function(Int32)>.isolateLocal(f, exceptionalReturn: 4);
}
''', []);
  }

  test_NativeCallable_isolateLocal_okVoid() async {
    await assertErrorsInCode(r'''
import 'dart:ffi';
void f(int i) => i * 2;
void g() {
  NativeCallable<Void Function(Int32)>.isolateLocal(f);
}
''', []);
  }

  test_NativeCallable_isolateLocal_voidReturnPermissive() async {
    await assertErrorsInCode(r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Void Function(Int32)>.isolateLocal(f);
}
''', []);
  }

  test_NativeCallable_listener_inferred() async {
    await assertErrorsInCode(r'''
import 'dart:ffi';
void f(int i) => i * 2;
void g() {
  NativeCallable<Void Function(Int32)>? callback;
  callback = NativeCallable.listener(f);
  callback.close();
}
''', []);
  }

  test_NativeCallable_listener_mustBeANativeFunctionType() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
void f(int i) => i * 2;
void g() {
  NativeCallable<void Function(int)>.listener(f);
}
''',
      [error(diag.mustBeANativeFunctionType, 56, 43)],
    );
  }

  test_NativeCallable_listener_mustBeASubtype() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
void f(int i) => i * 2;
void g() {
  NativeCallable<Void Function(Double)>.listener(f);
}
''',
      [error(diag.mustBeASubtype, 103, 1)],
    );
  }

  test_NativeCallable_listener_mustHaveTypeArgs() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable.listener(f);
}
''',
      [error(diag.mustBeANativeFunctionType, 55, 23)],
    );
  }

  test_NativeCallable_listener_mustReturnVoid() async {
    await assertErrorsInCode(
      r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Int32 Function(Int32)>.listener(f);
}
''',
      [error(diag.mustReturnVoid, 102, 1)],
    );
  }

  test_NativeCallable_listener_ok() async {
    await assertErrorsInCode(r'''
import 'dart:ffi';
void f(int i) => i * 2;
void g() {
  NativeCallable<Void Function(Int32)>.listener(f);
}
''', []);
  }

  test_NativeCallable_listener_voidReturnPermissive() async {
    await assertErrorsInCode(r'''
import 'dart:ffi';
int f(int i) => i * 2;
void g() {
  NativeCallable<Void Function(Int32)>.listener(f);
}
''', []);
  }
}
