// Copyright (c) 2020, 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(ConstInitializedWithNonConstantValueTest);
  });
}

@reflectiveTest
class ConstInitializedWithNonConstantValueTest
    extends PubPackageResolutionTest {
  test_dynamic() async {
    await assertErrorsInCode(
      r'''
f(p) {
  const c = p;
}
''',
      [
        error(diag.unusedLocalVariable, 15, 1),
        error(diag.constInitializedWithNonConstantValue, 19, 1),
      ],
    );
  }

  test_finalField() async {
    // Regression test for bug #25526; previously, two errors were reported.
    await assertErrorsInCode(
      r'''
class Foo {
  final field = 0;
  foo([int x = field]) {}
}
''',
      [
        error(diag.nonConstantDefaultValue, 46, 5),
        error(diag.implicitThisReferenceInInitializer, 46, 5),
      ],
    );
  }

  test_functionExpression() async {
    await assertErrorsInCode(
      '''
const a = () {};
''',
      [error(diag.constInitializedWithNonConstantValue, 10, 5)],
    );
  }

  test_missingConstInListLiteral() async {
    await assertNoErrorsInCode('''
const List L = [0];
''');
  }

  test_missingConstInMapLiteral() async {
    await assertNoErrorsInCode('''
const Map M = {'a' : 0};
''');
  }

  test_newInstance_constConstructor() async {
    await assertErrorsInCode(
      r'''
class A {
  const A();
}
const a = new A();
''',
      [error(diag.constInitializedWithNonConstantValue, 35, 7)],
    );
  }

  test_newInstance_externalFactoryConstConstructor() async {
    // We can't evaluate "const A()" because its constructor is external.  But
    // the code is correct--we shouldn't report an error.
    await assertNoErrorsInCode(r'''
class A {
  external const factory A();
}
const x = const A();
''');
  }

  test_propertyExtraction_targetNotConst() async {
    await assertErrorsInCode(
      r'''
class A {
  const A();
  int m() => 0;
}
final a = const A();
const c = a.m;
''',
      [error(diag.constInitializedWithNonConstantValue, 72, 1)],
    );
  }

  test_typeLiteral_interfaceType() async {
    await assertNoErrorsInCode(r'''
const a = int;
''');
  }

  test_typeLiteral_typeAlias_interfaceType() async {
    await assertNoErrorsInCode(r'''
typedef A = int;
const a = A;
''');
  }
}
