[code_builder] Expand code coverage (#2409)
diff --git a/pkgs/code_builder/test/specs/function_type_test.dart b/pkgs/code_builder/test/specs/function_type_test.dart
new file mode 100644
index 0000000..2491a26
--- /dev/null
+++ b/pkgs/code_builder/test/specs/function_type_test.dart
@@ -0,0 +1,64 @@
+// Copyright (c) 2026, 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:code_builder/code_builder.dart';
+import 'package:test/test.dart';
+
+import '../common.dart';
+
+void main() {
+ late DartEmitter emitter;
+
+ useDartfmt();
+
+ setUp(() => emitter = DartEmitter.scoped(useNullSafetySyntax: true));
+
+ group('FunctionType API contracts', () {
+ test('properties should be correct', () {
+ final funcType = FunctionType();
+ expect(funcType.url, isNull);
+ expect(funcType.symbol, isNull);
+ expect(funcType.type, same(funcType));
+ });
+
+ test('newInstance should throw UnsupportedError', () {
+ final funcType = FunctionType();
+ expect(() => funcType.newInstance([]), throwsUnsupportedError);
+ });
+
+ test('newInstanceNamed should throw UnsupportedError', () {
+ final funcType = FunctionType();
+ expect(
+ () => funcType.newInstanceNamed('name', []),
+ throwsUnsupportedError,
+ );
+ });
+
+ test('constInstance should throw UnsupportedError', () {
+ final funcType = FunctionType();
+ expect(() => funcType.constInstance([]), throwsUnsupportedError);
+ });
+
+ test('constInstanceNamed should throw UnsupportedError', () {
+ final funcType = FunctionType();
+ expect(
+ () => funcType.constInstanceNamed('name', []),
+ throwsUnsupportedError,
+ );
+ });
+
+ test('should support toTypeDef', () {
+ final funcType = FunctionType(
+ (b) =>
+ b
+ ..returnType = refer('String')
+ ..requiredParameters.add(refer('int')),
+ );
+ expect(
+ funcType.toTypeDef('MyFunc'),
+ equalsDart('typedef MyFunc = String Function(int);', emitter),
+ );
+ });
+ });
+}
diff --git a/pkgs/code_builder/test/specs/record_type_test.dart b/pkgs/code_builder/test/specs/record_type_test.dart
index 55f5318..f840fe3 100644
--- a/pkgs/code_builder/test/specs/record_type_test.dart
+++ b/pkgs/code_builder/test/specs/record_type_test.dart
@@ -95,4 +95,39 @@
equalsDart('(({int named, int other})?,)', emitter),
);
});
+
+ group('RecordType API contracts', () {
+ test('properties should be correct', () {
+ final recordType = RecordType();
+ expect(recordType.url, isNull);
+ expect(recordType.symbol, isNull);
+ expect(recordType.type, same(recordType));
+ });
+
+ test('newInstance should throw UnsupportedError', () {
+ final recordType = RecordType();
+ expect(() => recordType.newInstance([]), throwsUnsupportedError);
+ });
+
+ test('newInstanceNamed should throw UnsupportedError', () {
+ final recordType = RecordType();
+ expect(
+ () => recordType.newInstanceNamed('name', []),
+ throwsUnsupportedError,
+ );
+ });
+
+ test('constInstance should throw UnsupportedError', () {
+ final recordType = RecordType();
+ expect(() => recordType.constInstance([]), throwsUnsupportedError);
+ });
+
+ test('constInstanceNamed should throw UnsupportedError', () {
+ final recordType = RecordType();
+ expect(
+ () => recordType.constInstanceNamed('name', []),
+ throwsUnsupportedError,
+ );
+ });
+ });
}
diff --git a/pkgs/code_builder/test/specs/type_reference_test.dart b/pkgs/code_builder/test/specs/type_reference_test.dart
index 9291cad..5b2aa01 100644
--- a/pkgs/code_builder/test/specs/type_reference_test.dart
+++ b/pkgs/code_builder/test/specs/type_reference_test.dart
@@ -66,5 +66,75 @@
equalsDart(r'List<int?>', emitter),
);
});
+
+ test('should support generic bound', () {
+ expect(
+ TypeReference(
+ (b) =>
+ b
+ ..symbol = 'T'
+ ..bound = refer('num'),
+ ),
+ equalsDart(r'T extends num', emitter),
+ );
+ });
+ });
+
+ group('TypeReference API', () {
+ final typeRef = TypeReference((b) => b..symbol = 'Foo');
+
+ test('properties should be exposed', () {
+ final localTypeRef = TypeReference(
+ (b) =>
+ b
+ ..symbol = 'Foo'
+ ..url = 'package:foo/foo.dart',
+ );
+ expect(localTypeRef.symbol, 'Foo');
+ expect(localTypeRef.url, 'package:foo/foo.dart');
+ expect(localTypeRef.type, same(localTypeRef));
+ });
+
+ test('should support newInstance', () {
+ expect(
+ typeRef.newInstance([literal(42)], {'bar': literal('baz')}, [
+ refer('int'),
+ ]),
+ equalsDart(r"Foo<int>(42, bar: 'baz', )"),
+ );
+ });
+
+ test('should support newInstanceNamed', () {
+ expect(
+ typeRef.newInstanceNamed(
+ 'fromMap',
+ [literal(42)],
+ {'bar': literal('baz')},
+ [refer('int')],
+ ),
+ equalsDart(r"Foo.fromMap<int>(42, bar: 'baz', )"),
+ );
+ });
+
+ test('should support constInstance', () {
+ expect(
+ typeRef.constInstance([literal(42)], {'bar': literal('baz')}, [
+ refer('int'),
+ ]),
+ equalsDart(r"const Foo<int>(42, bar: 'baz', )"),
+ );
+ });
+
+ test('should support constInstanceNamed', () {
+ expect(
+ typeRef.constInstanceNamed(
+ 'fromMap',
+ [literal(42)],
+ {'bar': literal('baz')},
+ [refer('int')],
+ ),
+ equalsDart(r"const Foo.fromMap<int>(42, bar: 'baz', )"),
+ );
+ });
});
}