// Copyright (c) 2019, 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/error/codes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

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

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

@reflectiveTest
class InstantiateAbstractClassTest extends PubPackageResolutionTest {
  test_const_generic() async {
    await assertErrorsInCode(
      '''
abstract class A<E> {
  const A();
}
void f() {
  var a = const A<int>();
}''',
      [
        error(WarningCode.unusedLocalVariable, 54, 1),
        error(CompileTimeErrorCode.instantiateAbstractClass, 64, 6),
      ],
    );

    assertType(findNode.instanceCreation('const A<int>'), 'A<int>');
  }

  test_const_simple() async {
    await assertErrorsInCode(
      '''
abstract class A {
  const A();
}
void f() {
  A a = const A();
}''',
      [
        error(WarningCode.unusedLocalVariable, 49, 1),
        error(CompileTimeErrorCode.instantiateAbstractClass, 59, 1),
      ],
    );
  }

  test_new_generic() async {
    await assertErrorsInCode(
      '''
abstract class A<E> {}
void f() {
  new A<int>();
}
''',
      [error(CompileTimeErrorCode.instantiateAbstractClass, 40, 6)],
    );

    assertType(findNode.instanceCreation('new A<int>'), 'A<int>');
  }

  test_new_interfaceTypeTypedef() async {
    await assertErrorsInCode(
      '''
abstract class A {}
typedef B = A;
void f() {
  new B();
}
''',
      [error(CompileTimeErrorCode.instantiateAbstractClass, 52, 1)],
    );
  }

  test_new_nonGeneric() async {
    await assertErrorsInCode(
      '''
abstract class A {}
void f() {
  new A();
}
''',
      [error(CompileTimeErrorCode.instantiateAbstractClass, 37, 1)],
    );
  }

  test_noKeyword_generic() async {
    await assertErrorsInCode(
      '''
abstract class A<E> {}
void f() {
  A<int>();
}
''',
      [error(CompileTimeErrorCode.instantiateAbstractClass, 36, 6)],
    );

    assertType(findNode.instanceCreation('A<int>'), 'A<int>');
  }

  test_noKeyword_interfaceTypeTypedef() async {
    await assertErrorsInCode(
      '''
abstract class A {}
typedef B = A;
void f() {
  B();
}
''',
      [error(CompileTimeErrorCode.instantiateAbstractClass, 48, 1)],
    );
  }

  test_noKeyword_nonGeneric() async {
    await assertErrorsInCode(
      '''
abstract class A {}
void f() {
  A();
}
''',
      [error(CompileTimeErrorCode.instantiateAbstractClass, 33, 1)],
    );
  }
}
