// 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(InvalidVisibilityAnnotationTest);
  });
}

@reflectiveTest
class InvalidVisibilityAnnotationTest extends PubPackageResolutionTest {
  @override
  void setUp() {
    super.setUp();
    writeTestPackageConfigWithMeta();
  }

  test_fields_multipleMixed() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
class C {
  @visibleForTesting int _a = 0, b = 0;
}
''',
      [
        error(diag.invalidVisibilityAnnotation, 46, 17),
        error(diag.unusedField, 68, 2),
      ],
    );
  }

  test_fields_multiplePrivate() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
class C {
  @visibleForTesting int _a = 0, _b = 0;
}
''',
      [
        error(diag.invalidVisibilityAnnotation, 46, 17),
        error(diag.invalidVisibilityAnnotation, 46, 17),
        error(diag.unusedField, 68, 2),
        error(diag.unusedField, 76, 2),
      ],
    );
  }

  test_fields_multiplePublic() async {
    await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
class C {
  @visibleForTesting int a = 0, b = 0;
}
''');
  }

  test_privateClass() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting class _C {}
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 58, 2),
      ],
    );
  }

  test_privateConstructor() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
class C {
  @visibleForTesting C._() {}
}
''',
      [error(diag.invalidVisibilityAnnotation, 46, 17)],
    );
  }

  test_privateEnum() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting enum _E {a, b}
void f(_E e) => e == _E.a || e == _E.b;
''',
      [error(diag.invalidVisibilityAnnotation, 34, 17)],
    );
  }

  test_privateExtensionType() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting extension type _E(int i) {}
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 67, 2),
      ],
    );
  }

  test_privateField() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
class C {
  @visibleForTesting int _a = 1;
}
''',
      [
        error(diag.invalidVisibilityAnnotation, 46, 17),
        error(diag.unusedField, 68, 2),
      ],
    );
  }

  test_privateMethod() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
class C {
  @visibleForTesting void _m() {}
}
''',
      [
        error(diag.invalidVisibilityAnnotation, 46, 17),
        error(diag.unusedElement, 69, 2),
      ],
    );
  }

  test_privateMixin() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting mixin _M {}
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 58, 2),
      ],
    );
  }

  test_privateTopLevelFunction() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting void _f() {}
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 57, 2),
      ],
    );
  }

  test_privateTopLevelVariable() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting final _a = 1;
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 58, 2),
      ],
    );
  }

  test_privateTypedef() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting typedef _T = Function();
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 60, 2),
      ],
    );
  }

  test_topLevelVariable_multipleMixed() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting final _a = 1, b = 2;
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 58, 2),
      ],
    );
  }

  test_topLevelVariable_multiplePrivate() async {
    await assertErrorsInCode(
      r'''
import 'package:meta/meta.dart';
@visibleForTesting final _a = 1, _b = 2;
''',
      [
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.invalidVisibilityAnnotation, 34, 17),
        error(diag.unusedElement, 58, 2),
        error(diag.unusedElement, 66, 2),
      ],
    );
  }

  test_topLevelVariable_multiplePublic() async {
    await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
@visibleForTesting final a = 1, b = 2;
''');
  }

  test_valid() async {
    await assertNoErrorsInCode(r'''
import 'package:meta/meta.dart';
@visibleForTesting void f() {}
@visibleForTesting enum E {a, b, c}
@visibleForTesting typedef T = Function();
@visibleForTesting class C1 {}
@visibleForTesting extension type ET1(int i) {}
extension type ET2(int i) {
  @visibleForTesting void m() {}
}
@visibleForTesting mixin M {}
class C2 {
  @visibleForTesting C2.named() {}
}
class C3 {
  @visibleForTesting void m() {}
}
''');
  }
}
