Skip to content
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
64 changes: 64 additions & 0 deletions pkgs/code_builder/test/specs/function_type_test.dart
Original file line number Diff line number Diff line change
@@ -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),
);
});
});
}
35 changes: 35 additions & 0 deletions pkgs/code_builder/test/specs/record_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,39 @@ void main() {
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,
);
});
});
}
70 changes: 70 additions & 0 deletions pkgs/code_builder/test/specs/type_reference_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,75 @@ void main() {
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', )"),
);
});
});
}
Loading