forked from colbymchenry/codegraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswift-objc-bridge.test.ts
More file actions
189 lines (155 loc) · 6.2 KB
/
Copy pathswift-objc-bridge.test.ts
File metadata and controls
189 lines (155 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { describe, it, expect } from 'vitest';
import {
objcSelectorForSwiftMethod,
objcSelectorForSwiftInit,
objcAccessorsForSwiftProperty,
swiftBaseNamesForObjcSelector,
detectExplicitObjcName,
isObjcExposed,
} from '../src/resolution/swift-objc-bridge';
describe('Swift → ObjC selector bridging (auto-name rules)', () => {
describe('objcSelectorForSwiftMethod', () => {
it('no parameters → bare base name', () => {
expect(objcSelectorForSwiftMethod('play', [])).toBe('play');
});
it('single _ param → base + ":"', () => {
expect(objcSelectorForSwiftMethod('play', ['_'])).toBe('play:');
expect(objcSelectorForSwiftMethod('play', [null])).toBe('play:');
});
it('single labeled param → "baseWithLabel:"', () => {
expect(objcSelectorForSwiftMethod('play', ['song'])).toBe('playWithSong:');
});
it('multi-param with leading _ → "base:label2:..."', () => {
expect(objcSelectorForSwiftMethod('play', ['_', 'by'])).toBe('play:by:');
expect(
objcSelectorForSwiftMethod('tableView', ['_', 'didSelectRowAtIndexPath'])
).toBe('tableView:didSelectRowAtIndexPath:');
});
it('multi-param with leading explicit label → "baseWithFirst:rest:"', () => {
expect(objcSelectorForSwiftMethod('play', ['song', 'by'])).toBe(
'playWithSong:by:'
);
});
it('@objc(custom:) overrides the rule literally', () => {
expect(
objcSelectorForSwiftMethod('whateverName', ['ignored'], 'custom:')
).toBe('custom:');
});
it('returns null on empty base name', () => {
expect(objcSelectorForSwiftMethod('', [])).toBeNull();
});
});
describe('objcSelectorForSwiftInit', () => {
it('init() → "init"', () => {
expect(objcSelectorForSwiftInit([], [])).toBe('init');
});
it('init(name:) → "initWithName:"', () => {
expect(objcSelectorForSwiftInit(['name'], ['name'])).toBe('initWithName:');
});
it('init(name:, age:) → "initWithName:age:"', () => {
expect(objcSelectorForSwiftInit(['name', 'age'], ['name', 'age'])).toBe(
'initWithName:age:'
);
});
it('init(_ name:) uses internal name → "initWithName:"', () => {
expect(objcSelectorForSwiftInit(['_'], ['name'])).toBe('initWithName:');
});
it('@objc(custom) override on init', () => {
expect(objcSelectorForSwiftInit(['name'], ['name'], 'custom:')).toBe(
'custom:'
);
});
});
describe('objcAccessorsForSwiftProperty', () => {
it('getter = name, setter = setName:', () => {
expect(objcAccessorsForSwiftProperty('name')).toEqual({
getter: 'name',
setter: 'setName:',
});
});
it('camelCase → set capitalizes first', () => {
expect(objcAccessorsForSwiftProperty('isReady')).toEqual({
getter: 'isReady',
setter: 'setIsReady:',
});
});
it('explicit @objc(custom) overrides getter name', () => {
expect(objcAccessorsForSwiftProperty('name', 'displayName')).toEqual({
getter: 'displayName',
setter: 'setDisplayName:',
});
});
});
});
describe('ObjC selector → Swift base name candidates (reverse map)', () => {
it('bare no-colon selector → itself', () => {
expect(swiftBaseNamesForObjcSelector('play')).toEqual(['play']);
});
it('"play:" → ["play"]', () => {
expect(swiftBaseNamesForObjcSelector('play:')).toEqual(['play']);
});
it('"playWithSong:" → ["playWithSong", "play"]', () => {
expect(swiftBaseNamesForObjcSelector('playWithSong:').sort()).toEqual(
['play', 'playWithSong'].sort()
);
});
it('Cocoa-style "objectForKey:" → includes "object"', () => {
expect(swiftBaseNamesForObjcSelector('objectForKey:')).toContain('object');
});
it('Cocoa-style "stringWithFormat:" → includes "string"', () => {
expect(swiftBaseNamesForObjcSelector('stringWithFormat:')).toContain('string');
});
it('Cocoa-style "imageNamed:inBundle:" → first keyword has no preposition, falls through', () => {
// First keyword is `imageNamed` — no With/For/By in it, so candidates is
// just the raw keyword. (`Named` is not in our preposition list — keep
// it that way, otherwise we over-match on perfectly normal verbs.)
expect(swiftBaseNamesForObjcSelector('imageNamed:inBundle:')).toEqual(['imageNamed']);
});
it('"play:by:" → ["play"]', () => {
expect(swiftBaseNamesForObjcSelector('play:by:')).toEqual(['play']);
});
it('"playWithSong:by:" → ["playWithSong", "play"]', () => {
expect(swiftBaseNamesForObjcSelector('playWithSong:by:').sort()).toEqual(
['play', 'playWithSong'].sort()
);
});
it('"initWithName:" → includes "init"', () => {
expect(swiftBaseNamesForObjcSelector('initWithName:')).toContain('init');
});
it('"initWithName:age:" → includes "init"', () => {
expect(swiftBaseNamesForObjcSelector('initWithName:age:')).toContain('init');
});
it('"setName:" → includes the property name "name"', () => {
expect(swiftBaseNamesForObjcSelector('setName:')).toContain('name');
});
it('"tableView:didSelectRowAtIndexPath:" → ["tableView"]', () => {
expect(
swiftBaseNamesForObjcSelector('tableView:didSelectRowAtIndexPath:')
).toEqual(['tableView']);
});
});
describe('Source-window attribute detection', () => {
it('detects literal @objc(custom)', () => {
expect(detectExplicitObjcName(' @objc(custom:)\n func foo() {}')).toBe(
'custom:'
);
});
it('returns null for plain @objc', () => {
expect(detectExplicitObjcName('@objc func foo() {}')).toBeNull();
});
it('returns null when no @objc at all', () => {
expect(detectExplicitObjcName('public func foo() {}')).toBeNull();
});
it('isObjcExposed true for @objc', () => {
expect(isObjcExposed('@objc func foo() {}')).toBe(true);
});
it('isObjcExposed true for @objc(custom)', () => {
expect(isObjcExposed('@objc(custom:) func foo() {}')).toBe(true);
});
it('isObjcExposed false for no annotation', () => {
expect(isObjcExposed('public func foo() {}')).toBe(false);
});
it('@nonobjc opts out even if @objc also present (e.g. inside @objcMembers class)', () => {
expect(isObjcExposed('@nonobjc @objc func foo() {}')).toBe(false);
});
});