Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FED-1763 Fix reenabled contributor tests #864

Merged
merged 5 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ mixin ComponentDeclarationAssistApi on AssistContributorBase {
orbp.Union<orbp.BoilerplateState, orbp.BoilerplateStateMixin>? get state => componentDeclaration!.state;

bool _validateAndDetectBoilerplate() {
if (node is! SimpleIdentifier || node.parent is! ClassDeclaration) return false;
final parent = node.parent as ClassDeclaration?;
// In newer analyzer versions, the class's name within a class declaration is no longer a SimpleIdentifier,
// and the node returned from the class name token is the ClassDeclaration itself.
final classNode =
node.tryCast<SimpleIdentifier>()?.parent.tryCast<ClassDeclaration>() ?? node.tryCast<ClassDeclaration>();
if (classNode == null) return false;

final members = orbp.detectBoilerplateMembers(node.thisOrAncestorOfType<CompilationUnit>()!);
final declarations = orbp.getBoilerplateDeclarations(members, errorCollector).toList();

componentDeclaration = declarations.whereType<orbp.ClassComponentDeclaration>().firstWhereOrNull((c) {
return c.component.node == parent;
return c.component.node == classNode;
});

_isAValidComponentDeclaration =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AddUseOrCreateRefAssistTest extends AssistTestBase {
String usageSourceWithinClassComponent({required bool fixed}) => '''
import 'package:over_react/over_react.dart';

part 'test.over_react.g.dart';
part '{{FILE_BASENAME_WITHOUT_EXTENSION}}.over_react.g.dart';

UiFactory<HasNoRefsProps> HasNoRefs = castUiFactory(_\$HasNoRefs); // ignore: undefined_identifier

Expand Down Expand Up @@ -84,7 +84,7 @@ final HasRefs = uiFunction<UiProps>(
final source = newSource('''
import 'package:over_react/over_react.dart';

part 'test.over_react.g.dart';
part '{{FILE_BASENAME_WITHOUT_EXTENSION}}.over_react.g.dart';

UiFactory<HasRefsProps> HasRefs = castUiFactory(_\$HasRefs); // ignore: undefined_identifier

Expand Down Expand Up @@ -114,46 +114,46 @@ class HasRefsComponent extends UiComponent2<HasRefsProps> {
var selection = createSelection(source, 'return (#Child#()');
final change = await expectAndGetSingleAssist(selection);
source = applySourceChange(change, source);
expect(source.contents.data, usageSourceWithinClassComponent(fixed: true));
expect(source.contents.data, substituteSource(usageSourceWithinClassComponent(fixed: true), path: source.uri.path));
}

Future<void> test_classComponentAssist_zeroWidthSelection() async {
var source = newSource(usageSourceWithinClassComponent(fixed: false));
var selection = createSelection(source, 'return (##Child()');
final change = await expectAndGetSingleAssist(selection);
source = applySourceChange(change, source);
expect(source.contents.data, usageSourceWithinClassComponent(fixed: true));
expect(source.contents.data, substituteSource(usageSourceWithinClassComponent(fixed: true), path: source.uri.path));
}

Future<void> test_classComponentAssist_propCascadeSelection() async {
var source = newSource(usageSourceWithinClassComponent(fixed: false));
var selection = createSelection(source, '..id ##= \'foo\'');
final change = await expectAndGetSingleAssist(selection);
source = applySourceChange(change, source);
expect(source.contents.data, usageSourceWithinClassComponent(fixed: true));
expect(source.contents.data, substituteSource(usageSourceWithinClassComponent(fixed: true), path: source.uri.path));
}

Future<void> test_fnComponentAssist_componentNameSelection() async {
var source = newSource(usageSourceWithinFnComponent(fixed: false));
var selection = createSelection(source, 'return (#Child#()');
final change = await expectAndGetSingleAssist(selection);
source = applySourceChange(change, source);
expect(source.contents.data, usageSourceWithinFnComponent(fixed: true));
expect(source.contents.data, substituteSource(usageSourceWithinFnComponent(fixed: true), path: source.uri.path));
}

Future<void> test_fnComponentAssist_zeroWidthSelection() async {
var source = newSource(usageSourceWithinFnComponent(fixed: false));
var selection = createSelection(source, 'return (##Child()');
final change = await expectAndGetSingleAssist(selection);
source = applySourceChange(change, source);
expect(source.contents.data, usageSourceWithinFnComponent(fixed: true));
expect(source.contents.data, substituteSource(usageSourceWithinFnComponent(fixed: true), path: source.uri.path));
}

Future<void> test_fnComponentAssist_propCascadeSelection() async {
var source = newSource(usageSourceWithinFnComponent(fixed: false));
var selection = createSelection(source, '..id ##= \'foo\'');
final change = await expectAndGetSingleAssist(selection);
source = applySourceChange(change, source);
expect(source.contents.data, usageSourceWithinFnComponent(fixed: true));
expect(source.contents.data, substituteSource(usageSourceWithinFnComponent(fixed: true), path: source.uri.path));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mixin BoilerplateAssistTestStrings {
String fileName = 'test.dart';
import 'package:path/path.dart' as p;

mixin BoilerplateAssistTestStrings {
static const prefix = 'Foo';

static const stateMixin = '''
Expand All @@ -27,10 +27,10 @@ mixin ${prefix}State on UiState {}
withState ? 'FluxUiStatefulComponent2<${prefix}Props, ${prefix}State>' : 'FluxUiComponent2<${prefix}Props>';

String simpleUiComponentSource(
{bool isStateful = false, bool shouldIncludeSelection = false, bool includeDefaultProps = true}) {
{required String filename, bool isStateful = false, bool shouldIncludeSelection = false, bool includeDefaultProps = true}) {
return '''
import 'package:over_react/over_react.dart';
part 'test.over_react.g.dart';
part '${p.basenameWithoutExtension(filename)}.over_react.g.dart';

UiFactory<${prefix}Props> $prefix = _\$$prefix; // ignore: undefined_identifier

Expand All @@ -46,10 +46,10 @@ ${includeDefaultProps ? getDefaultProps : ''}${includeDefaultProps && isStateful
}

String fluxUiComponentSource(
{bool isStateful = false, bool shouldIncludeSelection = false, bool includeDefaultProps = true}) {
{required String filename, bool isStateful = false, bool shouldIncludeSelection = false, bool includeDefaultProps = true}) {
return '''
import 'package:over_react/over_react.dart';
part 'test.over_react.g.dart';
part '${p.basenameWithoutExtension(filename)}.over_react.g.dart';

UiFactory<${prefix}Props> $prefix = _\$$prefix; // ignore: undefined_identifier

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import '../test_bases/assist_test_base.dart';
import 'boilerplate_assist_utils.dart';

void main() {
// These tests can't run until this diagnostic is re-enabled in the plugin.
// defineReflectiveSuite(() {
// defineReflectiveTests(AddStatefulnessAssist);
// defineReflectiveTests(RemoveStatefulnessAssist);
// });
defineReflectiveSuite(() {
defineReflectiveTests(AddStatefulnessAssist);
defineReflectiveTests(RemoveStatefulnessAssist);
});
}

@reflectiveTest
Expand All @@ -30,16 +29,16 @@ class AddStatefulnessAssist extends AssistTestBase with BoilerplateAssistTestStr
}

Future<void> test_noAssistOnFactory() async {
final generatedSource = simpleUiComponentSource();

final fileName = uniqueSourceFileName();
final generatedSource = simpleUiComponentSource(filename: fileName);
var source = newSource(generatedSource, path: fileName);
var selection = createSelection(source, r'UiFactory<FooProps> #Foo# = _$Foo;');
await expectNoAssist(selection);
}

Future<void> test_noAssistOnProps() async {
final generatedSource = simpleUiComponentSource();

final fileName = uniqueSourceFileName();
final generatedSource = simpleUiComponentSource(filename: fileName);
var source = newSource(generatedSource, path: fileName);
var selection = createSelection(source, 'mixin #FooProps# on UiProps {}');
await expectNoAssist(selection);
Expand All @@ -48,7 +47,7 @@ class AddStatefulnessAssist extends AssistTestBase with BoilerplateAssistTestStr
Future<void> test_noAssistOnOldBoilerplate() async {
const oldBoilerplate = '''
import 'package:over_react/over_react.dart';
part 'test.over_react.g.dart';
part '{{FILE_BASENAME_WITHOUT_EXTENSION}}.over_react.g.dart';

@Factory()
UiFactory<TestProps> Test = _\$Test;
Expand All @@ -63,6 +62,7 @@ class TestComponent extends UiComponent<TestProps> {
}
''';

final fileName = uniqueSourceFileName();
var source = newSource(oldBoilerplate, path: fileName);
var selection = createSelection(source, 'class #TestComponent# extends');
await expectNoAssist(selection);
Expand All @@ -71,7 +71,7 @@ class TestComponent extends UiComponent<TestProps> {
Future<void> test_noAssistOnUnknownBase() async {
const unknownBase = '''
import 'package:over_react/over_react.dart';
part 'test.over_react.g.dart';
part '{{FILE_BASENAME_WITHOUT_EXTENSION}}.over_react.g.dart';

UiFactory<TestProps> Test = _\$Test; // ignore: undefined_identifier

Expand All @@ -83,41 +83,46 @@ class TestComponent extends AbstractBazComponent<TestProps> {
}
''';

final fileName = uniqueSourceFileName();
var source = newSource(unknownBase, path: fileName);
var selection = createSelection(source, 'class #TestComponent# extends');
await expectNoAssist(selection);
}

Future<void> test_addsStatefulness() async {
var retrievedSource = newSource(simpleUiComponentSource(), path: fileName);
final fileName = uniqueSourceFileName();
var retrievedSource = newSource(simpleUiComponentSource(filename: fileName), path: fileName);
var selection = createSelection(retrievedSource, componentNameSelector);
final change = await expectAndGetSingleAssist(selection);
retrievedSource = applySourceChange(change, retrievedSource);
expect(retrievedSource.contents.data, simpleUiComponentSource(isStateful: true));
expect(retrievedSource.contents.data, simpleUiComponentSource(filename: fileName, isStateful: true));
}

Future<void> test_addsStatefulnessWithoutDefaultProps() async {
var retrievedSource = newSource(simpleUiComponentSource(includeDefaultProps: false), path: fileName);
final fileName = uniqueSourceFileName();
var retrievedSource = newSource(simpleUiComponentSource(filename: fileName, includeDefaultProps: false), path: fileName);
var selection = createSelection(retrievedSource, componentNameSelector);
final change = await expectAndGetSingleAssist(selection);
retrievedSource = applySourceChange(change, retrievedSource);
expect(retrievedSource.contents.data, simpleUiComponentSource(isStateful: true, includeDefaultProps: false));
expect(retrievedSource.contents.data, simpleUiComponentSource(filename: fileName, isStateful: true, includeDefaultProps: false));
}

Future<void> test_addsStatefulnessToFlux() async {
var retrievedSource = newSource(fluxUiComponentSource(), path: fileName);
final fileName = uniqueSourceFileName();
var retrievedSource = newSource(fluxUiComponentSource(filename: fileName), path: fileName);
var selection = createSelection(retrievedSource, componentNameSelector);
final change = await expectAndGetSingleAssist(selection);
retrievedSource = applySourceChange(change, retrievedSource);
expect(retrievedSource.contents.data, fluxUiComponentSource(isStateful: true));
expect(retrievedSource.contents.data, fluxUiComponentSource(filename: fileName, isStateful: true));
}

Future<void> test_addsStatefulnessToFluxWithoutDefaultProps() async {
var retrievedSource = newSource(fluxUiComponentSource(includeDefaultProps: false), path: fileName);
final fileName = uniqueSourceFileName();
var retrievedSource = newSource(fluxUiComponentSource(filename: fileName, includeDefaultProps: false), path: fileName);
var selection = createSelection(retrievedSource, componentNameSelector);
final change = await expectAndGetSingleAssist(selection);
retrievedSource = applySourceChange(change, retrievedSource);
expect(retrievedSource.contents.data, fluxUiComponentSource(isStateful: true, includeDefaultProps: false));
expect(retrievedSource.contents.data, fluxUiComponentSource(filename: fileName, isStateful: true, includeDefaultProps: false));
}
}

Expand All @@ -135,16 +140,17 @@ class RemoveStatefulnessAssist extends AssistTestBase with BoilerplateAssistTest
}

Future<void> test_noAssistOnFactory() async {
final generatedSource = simpleUiComponentSource(isStateful: true);

final fileName = uniqueSourceFileName();
final generatedSource = simpleUiComponentSource(filename: fileName, isStateful: true);
var source = newSource(generatedSource, path: fileName);
var selection = createSelection(source, r'UiFactory<FooProps> #Foo# = _$Foo;');
await expectNoAssist(selection);
}

Future<void> test_noAssistOnProps() async {
final generatedSource = simpleUiComponentSource(isStateful: true);

final fileName = uniqueSourceFileName();
final generatedSource = simpleUiComponentSource(filename: fileName, isStateful: true);
var source = newSource(generatedSource, path: fileName);
var selection = createSelection(source, 'mixin #FooProps# on UiProps {}');
await expectNoAssist(selection);
Expand All @@ -153,7 +159,7 @@ class RemoveStatefulnessAssist extends AssistTestBase with BoilerplateAssistTest
Future<void> test_noAssistOnOldBoilerplate() async {
const oldBoilerplate = '''
import 'package:over_react/over_react.dart';
part 'test.over_react.g.dart';
part '{{FILE_BASENAME_WITHOUT_EXTENSION}}.over_react.g.dart';

@Factory()
UiFactory<TestProps> Test = _\$Test;
Expand All @@ -171,23 +177,25 @@ class TestComponent extends UiStatefulComponent<TestProps, TestState> {
}
''';

final fileName = uniqueSourceFileName();
var source = newSource(oldBoilerplate, path: fileName);
var selection = createSelection(source, 'class #TestComponent# extends');
await expectNoAssist(selection);
}

Future<void> test_removesStatefulness() async {
var retrievedSource = newSource(simpleUiComponentSource(isStateful: true), path: fileName);
final fileName = uniqueSourceFileName();
var retrievedSource = newSource(simpleUiComponentSource(filename: fileName, isStateful: true), path: fileName);
var selection = createSelection(retrievedSource, componentNameSelector);
final change = await expectAndGetSingleAssist(selection);
retrievedSource = applySourceChange(change, retrievedSource);
expect(retrievedSource.contents.data, simpleUiComponentSource());
expect(retrievedSource.contents.data, simpleUiComponentSource(filename: fileName, ));
}

Future<void> test_removesStatefulnessWithNoInitalState() async {
const statefulComponentWithNoInitialState = r'''
import 'package:over_react/over_react.dart';
part 'test.over_react.g.dart';
part '{{FILE_BASENAME_WITHOUT_EXTENSION}}.over_react.g.dart';

UiFactory<FooProps> Foo = _$Foo; // ignore: undefined_identifier

Expand All @@ -205,18 +213,20 @@ class FooComponent extends UiStatefulComponent2<FooProps, FooState> {
}
''';

final fileName = uniqueSourceFileName();
var retrievedSource = newSource(statefulComponentWithNoInitialState, path: fileName);
var selection = createSelection(retrievedSource, componentNameSelector);
final change = await expectAndGetSingleAssist(selection);
retrievedSource = applySourceChange(change, retrievedSource);
expect(retrievedSource.contents.data, simpleUiComponentSource());
expect(retrievedSource.contents.data, simpleUiComponentSource(filename: fileName));
}

Future<void> test_removesStatefulnessToFlux() async {
var retrievedSource = newSource(fluxUiComponentSource(isStateful: true), path: fileName);
final fileName = uniqueSourceFileName();
var retrievedSource = newSource(fluxUiComponentSource(filename: fileName, isStateful: true), path: fileName);
var selection = createSelection(retrievedSource, componentNameSelector);
final change = await expectAndGetSingleAssist(selection);
retrievedSource = applySourceChange(change, retrievedSource);
expect(retrievedSource.contents.data, fluxUiComponentSource());
expect(retrievedSource.contents.data, fluxUiComponentSource(filename: fileName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var foo = (Dom.div()
final source = newSource(/*language=dart*/ r'''
import 'package:over_react/over_react.dart';

part 'test.over_react.g.dart';
part '{{FILE_BASENAME_WITHOUT_EXTENSION}}.over_react.g.dart';

mixin FooState on UiState {
var foo;
Expand Down
Loading
Loading