MCPcopy Create free account
hub / github.com/docopt/docopt.go / parseTest

Function parseTest

docopt_test.go:1444–1496  ·  view source on GitHub ↗
(raw []byte)

Source from the content-addressed store, hash-verified

1442}
1443
1444func parseTest(raw []byte) ([]testcase, error) {
1445 var res []testcase
1446 commentPattern := regexp.MustCompile("#.*")
1447 raw = commentPattern.ReplaceAll(raw, []byte(""))
1448 raw = bytes.TrimSpace(raw)
1449 if bytes.HasPrefix(raw, []byte(`"""`)) {
1450 raw = raw[3:]
1451 }
1452
1453 id := 0
1454 for _, fixture := range bytes.Split(raw, []byte(`r"""`)) {
1455 doc, _, body := stringPartition(string(fixture), `"""`)
1456 for _, cas := range strings.Split(body, "$")[1:] {
1457 argvString, _, expectString := stringPartition(strings.TrimSpace(cas), "\n")
1458 prog, _, argvString := stringPartition(strings.TrimSpace(argvString), " ")
1459 argv := []string{}
1460 if len(argvString) > 0 {
1461 argv = strings.Fields(argvString)
1462 }
1463 var expectUntyped interface{}
1464 err := json.Unmarshal([]byte(expectString), &expectUntyped)
1465 if err != nil {
1466 return nil, err
1467 }
1468 switch expect := expectUntyped.(type) {
1469 case string: // user-error
1470 res = append(res, testcase{id, doc, prog, argv, nil, true})
1471 case map[string]interface{}:
1472 // convert []interface{} values to []string
1473 // convert float64 values to int
1474 for k, vUntyped := range expect {
1475 switch v := vUntyped.(type) {
1476 case []interface{}:
1477 itemList := make([]string, len(v))
1478 for i, itemUntyped := range v {
1479 if item, ok := itemUntyped.(string); ok {
1480 itemList[i] = item
1481 }
1482 }
1483 expect[k] = itemList
1484 case float64:
1485 expect[k] = int(v)
1486 }
1487 }
1488 res = append(res, testcase{id, doc, prog, argv, expect, false})
1489 default:
1490 return nil, fmt.Errorf("unhandled json data type")
1491 }
1492 id++
1493 }
1494 }
1495 return res, nil
1496}
1497
1498// parseOutput wraps the Parse() function to also return stdout
1499func parseOutput(doc string, argv []string, help bool, version string,

Callers 1

TestFileTestcasesFunction · 0.85

Calls 1

stringPartitionFunction · 0.85

Tested by

no test coverage detected