Skip to content

Commit

Permalink
fix addModel and add test, expose current model (#110)
Browse files Browse the repository at this point in the history
Follow up to #105 (comment)
  • Loading branch information
jakemac53 authored Oct 22, 2024
1 parent 47b2e10 commit 2ce8d77
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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', () {
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));
});
});
});
}

0 comments on commit 2ce8d77

Please sign in to comment.