code-parser.spec.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. import { VarType } from '../../types'
  2. import { extractFunctionParams, extractReturnType } from './code-parser'
  3. import { CodeLanguage } from './types'
  4. const SAMPLE_CODES = {
  5. python3: {
  6. noParams: 'def main():',
  7. singleParam: 'def main(param1):',
  8. multipleParams: `def main(param1, param2, param3):
  9. return {"result": param1}`,
  10. withTypes: `def main(param1: str, param2: int, param3: List[str]):
  11. result = process_data(param1, param2)
  12. return {"output": result}`,
  13. withDefaults: `def main(param1: str = "default", param2: int = 0):
  14. return {"data": param1}`,
  15. },
  16. javascript: {
  17. noParams: 'function main() {',
  18. singleParam: 'function main(param1) {',
  19. multipleParams: `function main(param1, param2, param3) {
  20. return { result: param1 }
  21. }`,
  22. withComments: `// Main function
  23. function main(param1, param2) {
  24. // Process data
  25. return { output: process(param1, param2) }
  26. }`,
  27. withSpaces: 'function main( param1 , param2 ) {',
  28. },
  29. }
  30. describe('extractFunctionParams', () => {
  31. describe('Python3', () => {
  32. test('handles no parameters', () => {
  33. const result = extractFunctionParams(SAMPLE_CODES.python3.noParams, CodeLanguage.python3)
  34. expect(result).toEqual([])
  35. })
  36. test('extracts single parameter', () => {
  37. const result = extractFunctionParams(SAMPLE_CODES.python3.singleParam, CodeLanguage.python3)
  38. expect(result).toEqual(['param1'])
  39. })
  40. test('extracts multiple parameters', () => {
  41. const result = extractFunctionParams(SAMPLE_CODES.python3.multipleParams, CodeLanguage.python3)
  42. expect(result).toEqual(['param1', 'param2', 'param3'])
  43. })
  44. test('handles type hints', () => {
  45. const result = extractFunctionParams(SAMPLE_CODES.python3.withTypes, CodeLanguage.python3)
  46. expect(result).toEqual(['param1', 'param2', 'param3'])
  47. })
  48. test('handles default values', () => {
  49. const result = extractFunctionParams(SAMPLE_CODES.python3.withDefaults, CodeLanguage.python3)
  50. expect(result).toEqual(['param1', 'param2'])
  51. })
  52. })
  53. // JavaScriptのテストケース
  54. describe('JavaScript', () => {
  55. test('handles no parameters', () => {
  56. const result = extractFunctionParams(SAMPLE_CODES.javascript.noParams, CodeLanguage.javascript)
  57. expect(result).toEqual([])
  58. })
  59. test('extracts single parameter', () => {
  60. const result = extractFunctionParams(SAMPLE_CODES.javascript.singleParam, CodeLanguage.javascript)
  61. expect(result).toEqual(['param1'])
  62. })
  63. test('extracts multiple parameters', () => {
  64. const result = extractFunctionParams(SAMPLE_CODES.javascript.multipleParams, CodeLanguage.javascript)
  65. expect(result).toEqual(['param1', 'param2', 'param3'])
  66. })
  67. test('handles comments in code', () => {
  68. const result = extractFunctionParams(SAMPLE_CODES.javascript.withComments, CodeLanguage.javascript)
  69. expect(result).toEqual(['param1', 'param2'])
  70. })
  71. test('handles whitespace', () => {
  72. const result = extractFunctionParams(SAMPLE_CODES.javascript.withSpaces, CodeLanguage.javascript)
  73. expect(result).toEqual(['param1', 'param2'])
  74. })
  75. })
  76. })
  77. const RETURN_TYPE_SAMPLES = {
  78. python3: {
  79. singleReturn: `
  80. def main(param1):
  81. return {"result": "value"}`,
  82. multipleReturns: `
  83. def main(param1, param2):
  84. return {"result": "value", "status": "success"}`,
  85. noReturn: `
  86. def main():
  87. print("Hello")`,
  88. complexReturn: `
  89. def main():
  90. data = process()
  91. return {"result": data, "count": 42, "messages": ["hello"]}`,
  92. nestedObject: `
  93. def main(name, age, city):
  94. return {
  95. 'personal_info': {
  96. 'name': name,
  97. 'age': age,
  98. 'city': city
  99. },
  100. 'timestamp': int(time.time()),
  101. 'status': 'active'
  102. }`,
  103. },
  104. javascript: {
  105. singleReturn: `
  106. function main(param1) {
  107. return { result: "value" }
  108. }`,
  109. multipleReturns: `
  110. function main(param1) {
  111. return { result: "value", status: "success" }
  112. }`,
  113. withParentheses: `
  114. function main() {
  115. return ({ result: "value", status: "success" })
  116. }`,
  117. noReturn: `
  118. function main() {
  119. console.log("Hello")
  120. }`,
  121. withQuotes: `
  122. function main() {
  123. return { "result": 'value', 'status': "success" }
  124. }`,
  125. nestedObject: `
  126. function main(name, age, city) {
  127. return {
  128. personal_info: {
  129. name: name,
  130. age: age,
  131. city: city
  132. },
  133. timestamp: Date.now(),
  134. status: 'active'
  135. }
  136. }`,
  137. withJSDoc: `
  138. /**
  139. * Creates a user profile with personal information and metadata
  140. * @param {string} name - The user's name
  141. * @param {number} age - The user's age
  142. * @param {string} city - The user's city of residence
  143. * @returns {Object} An object containing the user profile
  144. */
  145. function main(name, age, city) {
  146. return {
  147. result: {
  148. personal_info: {
  149. name: name,
  150. age: age,
  151. city: city
  152. },
  153. timestamp: Date.now(),
  154. status: 'active'
  155. }
  156. };
  157. }`,
  158. },
  159. }
  160. describe('extractReturnType', () => {
  161. // Python3のテスト
  162. describe('Python3', () => {
  163. test('extracts single return value', () => {
  164. const result = extractReturnType(RETURN_TYPE_SAMPLES.python3.singleReturn, CodeLanguage.python3)
  165. expect(result).toEqual({
  166. result: {
  167. type: VarType.string,
  168. children: null,
  169. },
  170. })
  171. })
  172. test('extracts multiple return values', () => {
  173. const result = extractReturnType(RETURN_TYPE_SAMPLES.python3.multipleReturns, CodeLanguage.python3)
  174. expect(result).toEqual({
  175. result: {
  176. type: VarType.string,
  177. children: null,
  178. },
  179. status: {
  180. type: VarType.string,
  181. children: null,
  182. },
  183. })
  184. })
  185. test('returns empty object when no return statement', () => {
  186. const result = extractReturnType(RETURN_TYPE_SAMPLES.python3.noReturn, CodeLanguage.python3)
  187. expect(result).toEqual({})
  188. })
  189. test('handles complex return statement', () => {
  190. const result = extractReturnType(RETURN_TYPE_SAMPLES.python3.complexReturn, CodeLanguage.python3)
  191. expect(result).toEqual({
  192. result: {
  193. type: VarType.string,
  194. children: null,
  195. },
  196. count: {
  197. type: VarType.string,
  198. children: null,
  199. },
  200. messages: {
  201. type: VarType.string,
  202. children: null,
  203. },
  204. })
  205. })
  206. test('handles nested object structure', () => {
  207. const result = extractReturnType(RETURN_TYPE_SAMPLES.python3.nestedObject, CodeLanguage.python3)
  208. expect(result).toEqual({
  209. personal_info: {
  210. type: VarType.string,
  211. children: null,
  212. },
  213. timestamp: {
  214. type: VarType.string,
  215. children: null,
  216. },
  217. status: {
  218. type: VarType.string,
  219. children: null,
  220. },
  221. })
  222. })
  223. })
  224. // JavaScriptのテスト
  225. describe('JavaScript', () => {
  226. test('extracts single return value', () => {
  227. const result = extractReturnType(RETURN_TYPE_SAMPLES.javascript.singleReturn, CodeLanguage.javascript)
  228. expect(result).toEqual({
  229. result: {
  230. type: VarType.string,
  231. children: null,
  232. },
  233. })
  234. })
  235. test('extracts multiple return values', () => {
  236. const result = extractReturnType(RETURN_TYPE_SAMPLES.javascript.multipleReturns, CodeLanguage.javascript)
  237. expect(result).toEqual({
  238. result: {
  239. type: VarType.string,
  240. children: null,
  241. },
  242. status: {
  243. type: VarType.string,
  244. children: null,
  245. },
  246. })
  247. })
  248. test('handles return with parentheses', () => {
  249. const result = extractReturnType(RETURN_TYPE_SAMPLES.javascript.withParentheses, CodeLanguage.javascript)
  250. expect(result).toEqual({
  251. result: {
  252. type: VarType.string,
  253. children: null,
  254. },
  255. status: {
  256. type: VarType.string,
  257. children: null,
  258. },
  259. })
  260. })
  261. test('returns empty object when no return statement', () => {
  262. const result = extractReturnType(RETURN_TYPE_SAMPLES.javascript.noReturn, CodeLanguage.javascript)
  263. expect(result).toEqual({})
  264. })
  265. test('handles quoted keys', () => {
  266. const result = extractReturnType(RETURN_TYPE_SAMPLES.javascript.withQuotes, CodeLanguage.javascript)
  267. expect(result).toEqual({
  268. result: {
  269. type: VarType.string,
  270. children: null,
  271. },
  272. status: {
  273. type: VarType.string,
  274. children: null,
  275. },
  276. })
  277. })
  278. test('handles nested object structure', () => {
  279. const result = extractReturnType(RETURN_TYPE_SAMPLES.javascript.nestedObject, CodeLanguage.javascript)
  280. expect(result).toEqual({
  281. personal_info: {
  282. type: VarType.string,
  283. children: null,
  284. },
  285. timestamp: {
  286. type: VarType.string,
  287. children: null,
  288. },
  289. status: {
  290. type: VarType.string,
  291. children: null,
  292. },
  293. })
  294. })
  295. })
  296. })