MCPcopy Create free account
hub / github.com/EndlessCheng/codeforces-go / RunLeetCodeClassWithExamples

Function RunLeetCodeClassWithExamples

leetcode/testutil/leetcode.go:329–469  ·  view source on GitHub ↗

若反射出来的函数或 rawExamples 数据不合法,则会返回一个非空的 error,否则返回 nil

(t *testing.T, constructor interface{}, rawExamples [][3]string, targetCaseNum int)

Source from the content-addressed store, hash-verified

327
328// 若反射出来的函数或 rawExamples 数据不合法,则会返回一个非空的 error,否则返回 nil
329func RunLeetCodeClassWithExamples(t *testing.T, constructor interface{}, rawExamples [][3]string, targetCaseNum int) (err error) {
330 cType := reflect.TypeOf(constructor)
331 if cType.Kind() != reflect.Func {
332 return fmt.Errorf("constructor must be a function")
333 }
334 if cType.NumOut() != 1 {
335 return fmt.Errorf("constructor must have one and only one return value")
336 }
337 allCasesOk := true
338 cFunc := reflect.ValueOf(constructor)
339
340 // 例如,-1 表示最后一个测试用例
341 if targetCaseNum < 0 {
342 targetCaseNum += len(rawExamples) + 1
343 }
344
345outer:
346 for curCaseNum, example := range rawExamples {
347 if targetCaseNum > 0 && curCaseNum+1 != targetCaseNum {
348 continue
349 }
350
351 names := strings.TrimSpace(example[0])
352 inputArgs := strings.TrimSpace(example[1])
353 rawExpectedOut := strings.TrimSpace(example[2])
354
355 // parse called names
356 // 调用 parseRawArray 确保数据是合法的
357 methodNames, er := parseRawArray(names)
358 if er != nil {
359 return er
360 }
361 for i, name := range methodNames {
362 name = name[1 : len(name)-1] // 移除引号
363 name = strings.Title(name) // 首字母大写以匹配模板方法名称
364 methodNames[i] = name
365 }
366
367 // parse inputs
368 rawArgsList, er := parseRawArray(inputArgs)
369 if er != nil {
370 return er
371 }
372 if len(rawArgsList) != len(methodNames) {
373 return fmt.Errorf("invalid test data: mismatch names and input args (%d != %d)", len(methodNames), len(rawArgsList))
374 }
375
376 // parse constructor input
377 constructorArgs, er := parseRawArray(rawArgsList[0])
378 if er != nil {
379 return er
380 }
381 constructorIns := make([]reflect.Value, len(constructorArgs))
382 for i, arg := range constructorArgs {
383 constructorIns[i], err = parseRawArg(cType.In(i), arg)
384 if err != nil {
385 return
386 }

Callers 15

TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92
TestFunction · 0.92

Calls 6

parseRawArrayFunction · 0.85
parseRawArgFunction · 0.85
toRawStringFunction · 0.85
isTLEFunction · 0.70
SetMethod · 0.45
StringMethod · 0.45

Tested by 15

TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74
TestFunction · 0.74