// 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/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';

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

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

@reflectiveTest
class FinalNotInitializedTest extends PubPackageResolutionTest {
  test_class_field_abstract() async {
    await assertNoErrorsInCode('''
abstract class A {
  abstract final int x;
}
''');
  }

  test_class_field_abstract_with_constructor() async {
    await assertNoErrorsInCode('''
abstract class A {
  abstract final int x;
  A();
}
''');
  }

  test_class_field_external() async {
    await assertNoErrorsInCode('''
class A {
  external final int x;
}
''');
  }

  test_class_field_external_static() async {
    await assertNoErrorsInCode('''
class A {
  external static final int x;
}
''');
  }

  test_class_field_external_with_constructor() async {
    await assertNoErrorsInCode('''
class A {
  external final int x;
  A();
}
''');
  }

  test_class_field_late_noConstructor_noInitializer() async {
    await assertNoErrorsInCode('''
class C {
  late final f;
}
''');
  }

  test_class_field_late_unnamedConstructor_noInitializer() async {
    await assertNoErrorsInCode('''
class C {
  late final f;
  C();
}
''');
  }

  test_class_field_noConstructor_initializer() async {
    await assertNoErrorsInCode('''
class C {
  final f = 1;
}
''');
  }

  test_class_field_noConstructor_noInitializer() async {
    await assertErrorsInCode(
      '''
abstract class A {
  final int x;
}
''',
      [error(diag.finalNotInitialized, 31, 1)],
    );
  }

  test_class_field_primaryConstructor_hasBody_fieldsOne_declarationOne_bodyZero() async {
    await assertNoErrorsInCode('''
class C(this.f) {
  final int f;
  this;
}
''');
  }

  test_class_field_primaryConstructor_hasBody_fieldsOne_declarationZero_bodyOne() async {
    await assertNoErrorsInCode('''
class C() {
  final int f;
  this : f = 2;
}
''');
  }

  test_class_field_primaryConstructor_hasBody_fieldsOne_declarationZero_bodyZero() async {
    await assertErrorsInCode(
      '''
class C() {
  final int f;
  this;
}
''',
      [error(diag.finalNotInitializedConstructor1, 6, 1)],
    );
  }

  test_class_field_primaryConstructor_hasBody_fieldsTwo_declarationOne_bodyOne() async {
    await assertNoErrorsInCode('''
class C(this.f1) {
  final int f1;
  final int f2;
  this : f2 = 0;
}
''');
  }

  test_class_field_primaryConstructor_hasBody_fieldsTwo_declarationOne_bodyZero() async {
    await assertErrorsInCode(
      '''
class C(this.f1) {
  final int f1;
  final int f2;
  this;
}
''',
      [error(diag.finalNotInitializedConstructor1, 6, 1)],
    );
  }

  test_class_field_primaryConstructor_hasBody_fieldsTwo_declarationTwo_bodyZero() async {
    await assertNoErrorsInCode('''
class C(this.f1, this.f2) {
  final int f1;
  final int f2;
  this;
}
''');
  }

  test_class_field_primaryConstructor_hasBody_fieldsTwo_declarationZero_bodyOne() async {
    await assertErrorsInCode(
      '''
class C() {
  final int f1;
  final int f2;
  this: f1 = 0;
}
''',
      [error(diag.finalNotInitializedConstructor1, 6, 1)],
    );
  }

  test_class_field_primaryConstructor_hasBody_fieldsTwo_declarationZero_bodyTwo() async {
    await assertNoErrorsInCode('''
class C() {
  final int f1;
  final int f2;
  this: f1 = 0, f2 = 0;
}
''');
  }

  test_class_field_primaryConstructor_hasBody_noDeclaration_fieldsOne_bodyOne() async {
    await assertErrorsInCode(
      '''
class C {
  final int f;
  this : f = 0;
}
''',
      [
        error(diag.finalNotInitialized, 22, 1),
        error(diag.primaryConstructorBodyWithoutDeclaration, 27, 13),
      ],
    );
  }

  test_class_field_primaryConstructor_noBody_fieldsOne_declarationZero() async {
    await assertErrorsInCode(
      '''
class C() {
  final int f;
}
''',
      [error(diag.finalNotInitializedConstructor1, 6, 1)],
    );
  }

  test_class_field_primaryConstructor_noBody_fieldsTwo_declarationOne() async {
    await assertErrorsInCode(
      '''
class C(this.f1) {
  final int f1;
  final int f2;
}
''',
      [error(diag.finalNotInitializedConstructor1, 6, 1)],
    );
  }

  test_class_field_primaryConstructor_noBody_fieldsTwo_declarationTwo() async {
    await assertNoErrorsInCode('''
class C(this.f1, this.f2) {
  final int f1;
  final int f2;
}
''');
  }

  @SkippedTest() // TODO(scheglov): implement augmentation
  test_class_field_unnamedConstructor_augmentationDeclares_constructorInitializer() async {
    newFile('$testPackageLibPath/a.dart', r'''
part of 'test.dart';

augment class C {
  C() : f = 2;
}
''');

    await assertNoErrorsInCode('''
part 'a.dart';

class C {
  final int f;
}
''');
  }

  test_class_field_unnamedConstructor_constructorInitializer() async {
    await assertNoErrorsInCode('''
class C {
  final f;
  C() : f = 2;
}
''');
  }

  test_class_field_unnamedConstructor_fieldFormalParameter() async {
    await assertNoErrorsInCode('''
class C {
  final f;
  C(this.f);
}
''');
  }

  test_class_field_unnamedConstructor_initializer() async {
    await assertNoErrorsInCode('''
class C {
  final f = 1;
  C();
}
''');
  }

  test_class_instanceField_final_factoryConstructor_only() async {
    await assertErrorsInCode(
      '''
class A {
  final int x;

  factory A() => throw 0;
}''',
      [error(diag.finalNotInitialized, 22, 1)],
    );
  }

  test_enum_field_constructorFieldInitializer() async {
    await assertNoErrorsInCode('''
enum E {
  v;
  final int x;
  const E() : x = 0;
}
''');
  }

  test_enum_field_fieldFormalParameter() async {
    await assertNoErrorsInCode('''
enum E {
  v(0);
  final int x;
  const E(this.x);
}
''');
  }

  test_enum_field_hasInitializer() async {
    await assertNoErrorsInCode('''
enum E {
  v;
  final int x = 0;
}
''');
  }

  test_enum_field_noConstructor_noInitializer() async {
    await assertErrorsInCode(
      '''
enum E {
  v;
  final int x;
}
''',
      [error(diag.finalNotInitialized, 26, 1)],
    );
  }

  test_extension_static() async {
    await assertErrorsInCode(
      '''
extension E on String {
  static final F;
}''',
      [error(diag.finalNotInitialized, 39, 1)],
    );
  }

  test_instanceField_final() async {
    await assertErrorsInCode(
      '''
class A {
  final F;
}''',
      [error(diag.finalNotInitialized, 18, 1)],
    );
  }

  test_instanceField_final_static() async {
    await assertErrorsInCode(
      '''
class A {
  static final F;
}''',
      [error(diag.finalNotInitialized, 25, 1)],
    );
  }

  test_library_final() async {
    await assertErrorsInCode(
      '''
final F;
''',
      [error(diag.finalNotInitialized, 6, 1)],
    );
  }

  test_local_final() async {
    await assertErrorsInCode(
      '''
f() {
  final int x;
}''',
      [error(diag.unusedLocalVariable, 18, 1)],
    );
  }

  test_localVariable_initializer() async {
    await assertErrorsInCode(
      '''
f() {
  late final x = 1;
}
''',
      [error(diag.unusedLocalVariable, 19, 1)],
    );
  }

  test_localVariable_noInitializer() async {
    await assertErrorsInCode(
      '''
f() {
  late final x;
}
''',
      [error(diag.unusedLocalVariable, 19, 1)],
    );
  }

  test_mixin() async {
    await assertErrorsInCode(
      '''
mixin M {
  final int x;
}
''',
      [error(diag.finalNotInitialized, 22, 1)],
    );
  }

  test_mixin_OK() async {
    await assertNoErrorsInCode(r'''
mixin M {
  final int f = 0;
}
''');
  }

  test_topLevel_external() async {
    await assertNoErrorsInCode('''
external final int x;
''');
  }

  test_topLevel_final() async {
    await assertErrorsInCode(
      '''
final int x;
''',
      [error(diag.finalNotInitialized, 10, 1)],
    );
  }
}
