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

fix addModel and add test, expose current model #110

Merged
merged 1 commit into from
Oct 22, 2024
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
7 changes: 6 additions & 1 deletion pkgs/dart_model/lib/src/scopes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,17 @@ class MacroScope {
/// multiple queries.
void addModel(Model model) {
if (_accumulatedModel case var accumulated?) {
accumulated.mergeWith(model);
_accumulatedModel = accumulated.mergeWith(model);
} else {
_accumulatedModel = model;
}
}

/// The current accumulated model for this macro scope.
///
/// This is only safe to use after [addModel] has been called at least once.
Model get model => _accumulatedModel!;

static MacroScope get current {
final scope = Scope._currentOrNull;
return switch (scope) {
Expand Down
20 changes: 19 additions & 1 deletion pkgs/dart_model/test/scopes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:dart_model/src/json_buffer/json_buffer_builder.dart';
import 'package:test/test.dart';

void main() {
group(Scope, () {
group('Scope', () {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I am converting these all to strings because anything else breaks the VsCode integration

for (final scope in [Scope.none, Scope.macro, Scope.query]) {
test('create maps and serialize work in $scope', () {
expect(scope.run(() => Scope.createMap(TypedMapSchema({}))),
Expand All @@ -33,5 +33,23 @@ void main() {
Scope.macro.run(() => Scope.none.run(() {}));
Scope.query.run(() => Scope.none.run(() {}));
});

test('addModel merges the model with the previous', () {
late Model initial;
late Model firstQuery;
Scope.query.run(() {
initial = Model()..uris['a'] = (Library()..scopes['A'] = Interface());
firstQuery = Model()
..uris['a'] = (Library()..scopes['B'] = Interface());
});
Scope.macro.run(() {
MacroScope.current.addModel(initial);
expect(MacroScope.current.model.uris['a']!.scopes['A'], isNot(null));
expect(MacroScope.current.model.uris['a']!.scopes['B'], null);
MacroScope.current.addModel(firstQuery);
expect(MacroScope.current.model.uris['a']!.scopes['A'], isNot(null));
expect(MacroScope.current.model.uris['a']!.scopes['B'], isNot(null));
});
});
});
}