本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.9KB

  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "testing"
  8. "code.gitea.io/gitea/modules/git"
  9. )
  10. func BenchmarkGetCommitGraph(b *testing.B) {
  11. currentRepo, err := git.OpenRepository(".")
  12. if err != nil {
  13. b.Error("Could not open repository")
  14. }
  15. for i := 0; i < b.N; i++ {
  16. graph, err := GetCommitGraph(currentRepo, 1)
  17. if err != nil {
  18. b.Error("Could get commit graph")
  19. }
  20. if len(graph) < 100 {
  21. b.Error("Should get 100 log lines.")
  22. }
  23. }
  24. }
  25. func BenchmarkParseCommitString(b *testing.B) {
  26. testString := "* DATA:||4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|Kjell Kvinge|kjell@kvinge.biz|4e61bac|Add route for graph"
  27. for i := 0; i < b.N; i++ {
  28. graphItem, err := graphItemFromString(testString, nil)
  29. if err != nil {
  30. b.Error("could not parse teststring")
  31. }
  32. if graphItem.Author != "Kjell Kvinge" {
  33. b.Error("Did not get expected data")
  34. }
  35. }
  36. }
  37. func TestCommitStringParsing(t *testing.T) {
  38. dataFirstPart := "* DATA:||4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|Author|user@mail.something|4e61bac|"
  39. tests := []struct {
  40. shouldPass bool
  41. testName string
  42. commitMessage string
  43. }{
  44. {true, "normal", "not a fancy message"},
  45. {true, "extra pipe", "An extra pipe: |"},
  46. {true, "extra 'Data:'", "DATA: might be trouble"},
  47. }
  48. for _, test := range tests {
  49. t.Run(test.testName, func(t *testing.T) {
  50. testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage)
  51. graphItem, err := graphItemFromString(testString, nil)
  52. if err != nil && test.shouldPass {
  53. t.Errorf("Could not parse %s", testString)
  54. return
  55. }
  56. if test.commitMessage != graphItem.Subject {
  57. t.Errorf("%s does not match %s", test.commitMessage, graphItem.Subject)
  58. }
  59. })
  60. }
  61. }
上海开阖软件有限公司 沪ICP备12045867号-1