• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dart-lang / ffigen / 6470767942

10 Oct 2023 02:29PM UTC coverage: 91.966% (-0.6%) from 92.593%
6470767942

push

github

web-flow
Refactor `sameFfiDartAndCType` to not require a `Writer` (#629)

40 of 40 new or added lines in 12 files covered. (100.0%)

3709 of 4033 relevant lines covered (91.97%)

28.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/lib/src/code_generator/enum_class.dart
1
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
2
// for details. All rights reserved. Use of this source code is governed by a
3
// BSD-style license that can be found in the LICENSE file.
4

5
import 'binding.dart';
6
import 'binding_string.dart';
7
import 'native_type.dart';
8
import 'type.dart';
9
import 'utils.dart';
10
import 'writer.dart';
11

12
/// A binding for enums in C.
13
///
14
/// For a C enum -
15
/// ```c
16
/// enum Fruits {apple, banana = 10};
17
/// ```
18
/// The generated dart code is
19
///
20
/// ```dart
21
/// class Fruits {
22
///   static const apple = 0;
23
///   static const banana = 10;
24
/// }
25
/// ```
26
class EnumClass extends BindingType {
27
  static final nativeType = NativeType(SupportedNativeType.Int32);
30✔
28

29
  final List<EnumConstant> enumConstants;
30

31
  EnumClass({
14✔
32
    String? usr,
33
    String? originalName,
34
    required String name,
35
    String? dartDoc,
36
    List<EnumConstant>? enumConstants,
37
  })  : enumConstants = enumConstants ?? [],
14✔
38
        super(
14✔
39
          usr: usr,
40
          originalName: originalName,
41
          name: name,
42
          dartDoc: dartDoc,
43
        );
44

45
  @override
12✔
46
  BindingString toBindingString(Writer w) {
47
    final s = StringBuffer();
12✔
48
    final enclosingClassName = name;
12✔
49

50
    if (dartDoc != null) {
12✔
51
      s.write(makeDartDoc(dartDoc!));
9✔
52
    }
53

54
    /// Adding [enclosingClassName] because dart doesn't allow class member
55
    /// to have the same name as the class.
56
    final localUniqueNamer = UniqueNamer({enclosingClassName});
24✔
57

58
    // Print enclosing class.
59
    s.write('abstract class $enclosingClassName {\n');
24✔
60
    const depth = '  ';
61
    for (final ec in enumConstants) {
21✔
62
      final enumValueName = localUniqueNamer.makeUnique(ec.name);
18✔
63
      if (ec.dartDoc != null) {
9✔
64
        s.write('$depth/// ');
3✔
65
        s.writeAll(ec.dartDoc!.split('\n'), '\n$depth/// ');
9✔
66
        s.write('\n');
3✔
67
      }
68
      s.write('${depth}static const int $enumValueName = ${ec.value};\n');
27✔
69
    }
70
    s.write('}\n\n');
12✔
71

72
    return BindingString(
12✔
73
        type: BindingStringType.enumClass, string: s.toString());
12✔
74
  }
75

76
  @override
14✔
77
  void addDependencies(Set<Binding> dependencies) {
78
    if (dependencies.contains(this)) return;
14✔
79

80
    dependencies.add(this);
14✔
81
  }
82

83
  @override
5✔
84
  String getCType(Writer w) => nativeType.getCType(w);
10✔
85

86
  @override
5✔
87
  String getFfiDartType(Writer w) => nativeType.getFfiDartType(w);
10✔
88

89
  @override
3✔
90
  bool get sameFfiDartAndCType => nativeType.sameFfiDartAndCType;
6✔
91

92
  @override
×
93
  bool get sameDartAndCType => nativeType.sameDartAndCType;
×
94

95
  @override
2✔
96
  String? getDefaultValue(Writer w, String nativeLib) => '0';
97
}
98

99
/// Represents a single value in an enum.
100
class EnumConstant {
101
  final String? originalName;
102
  final String? dartDoc;
103
  final String name;
104
  final int value;
105
  const EnumConstant({
11✔
106
    String? originalName,
107
    required this.name,
108
    required this.value,
109
    this.dartDoc,
110
  }) : originalName = originalName ?? name;
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc