swift-objc-bridge.test.ts 6.2 KB

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