|
| 1 | +// Copyright 2022 The Gogs 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 | + |
| 5 | +package git |
| 6 | + |
| 7 | +import ( |
| 8 | + "runtime" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRepository_Grep_Simple(t *testing.T) { |
| 15 | + want := []*GrepResult{ |
| 16 | + { |
| 17 | + Tree: "HEAD", |
| 18 | + Path: "src/Main.groovy", |
| 19 | + Line: 7, |
| 20 | + Column: 5, |
| 21 | + Text: "int programmingPoints = 10", |
| 22 | + }, { |
| 23 | + Tree: "HEAD", |
| 24 | + Path: "src/Main.groovy", |
| 25 | + Line: 10, |
| 26 | + Column: 33, |
| 27 | + Text: `println "${name} has at least ${programmingPoints} programming points."`, |
| 28 | + }, { |
| 29 | + Tree: "HEAD", |
| 30 | + Path: "src/Main.groovy", |
| 31 | + Line: 11, |
| 32 | + Column: 12, |
| 33 | + Text: `println "${programmingPoints} squared is ${square(programmingPoints)}"`, |
| 34 | + }, { |
| 35 | + Tree: "HEAD", |
| 36 | + Path: "src/Main.groovy", |
| 37 | + Line: 12, |
| 38 | + Column: 12, |
| 39 | + Text: `println "${programmingPoints} divided by 2 bonus points is ${divide(programmingPoints, 2)}"`, |
| 40 | + }, { |
| 41 | + Tree: "HEAD", |
| 42 | + Path: "src/Main.groovy", |
| 43 | + Line: 13, |
| 44 | + Column: 12, |
| 45 | + Text: `println "${programmingPoints} minus 7 bonus points is ${subtract(programmingPoints, 7)}"`, |
| 46 | + }, { |
| 47 | + Tree: "HEAD", |
| 48 | + Path: "src/Main.groovy", |
| 49 | + Line: 14, |
| 50 | + Column: 12, |
| 51 | + Text: `println "${programmingPoints} plus 3 bonus points is ${sum(programmingPoints, 3)}"`, |
| 52 | + }, |
| 53 | + } |
| 54 | + got := testrepo.Grep("programmingPoints") |
| 55 | + assert.Equal(t, want, got) |
| 56 | +} |
| 57 | + |
| 58 | +func TestRepository_Grep_IgnoreCase(t *testing.T) { |
| 59 | + want := []*GrepResult{ |
| 60 | + { |
| 61 | + Tree: "HEAD", |
| 62 | + Path: "README.txt", |
| 63 | + Line: 9, |
| 64 | + Column: 36, |
| 65 | + Text: "* [email protected]:matthewmccullough/hellogitworld.git", |
| 66 | + }, { |
| 67 | + Tree: "HEAD", |
| 68 | + Path: "README.txt", |
| 69 | + Line: 10, |
| 70 | + Column: 38, |
| 71 | + Text: "* git://github.com/matthewmccullough/hellogitworld.git", |
| 72 | + }, { |
| 73 | + Tree: "HEAD", |
| 74 | + Path: "README.txt", |
| 75 | + Line: 11, |
| 76 | + Column: 58, |
| 77 | + Text: "* https://[email protected]/matthewmccullough/hellogitworld.git", |
| 78 | + }, { |
| 79 | + Tree: "HEAD", |
| 80 | + Path: "src/Main.groovy", |
| 81 | + Line: 9, |
| 82 | + Column: 10, |
| 83 | + Text: `println "Hello ${name}"`, |
| 84 | + }, { |
| 85 | + Tree: "HEAD", |
| 86 | + Path: "src/main/java/com/github/App.java", |
| 87 | + Line: 4, |
| 88 | + Column: 4, |
| 89 | + Text: " * Hello again", |
| 90 | + }, { |
| 91 | + Tree: "HEAD", |
| 92 | + Path: "src/main/java/com/github/App.java", |
| 93 | + Line: 5, |
| 94 | + Column: 4, |
| 95 | + Text: " * Hello world!", |
| 96 | + }, { |
| 97 | + Tree: "HEAD", |
| 98 | + Path: "src/main/java/com/github/App.java", |
| 99 | + Line: 6, |
| 100 | + Column: 4, |
| 101 | + Text: " * Hello", |
| 102 | + }, { |
| 103 | + Tree: "HEAD", |
| 104 | + Path: "src/main/java/com/github/App.java", |
| 105 | + Line: 13, |
| 106 | + Column: 30, |
| 107 | + Text: ` System.out.println( "Hello World!" );`, |
| 108 | + }, |
| 109 | + } |
| 110 | + got := testrepo.Grep("Hello", GrepOptions{IgnoreCase: true}) |
| 111 | + assert.Equal(t, want, got) |
| 112 | +} |
| 113 | + |
| 114 | +func TestRepository_Grep_ExtendedRegexp(t *testing.T) { |
| 115 | + if runtime.GOOS == "darwin" { |
| 116 | + t.Skip("Skipping testing on macOS") |
| 117 | + return |
| 118 | + } |
| 119 | + want := []*GrepResult{ |
| 120 | + { |
| 121 | + Tree: "HEAD", |
| 122 | + Path: "src/main/java/com/github/App.java", |
| 123 | + Line: 13, |
| 124 | + Column: 30, |
| 125 | + Text: ` System.out.println( "Hello World!" );`, |
| 126 | + }, |
| 127 | + } |
| 128 | + got := testrepo.Grep(`Hello\sW\w+`, GrepOptions{ExtendedRegexp: true}) |
| 129 | + assert.Equal(t, want, got) |
| 130 | +} |
| 131 | + |
| 132 | +func TestRepository_Grep_WordRegexp(t *testing.T) { |
| 133 | + want := []*GrepResult{ |
| 134 | + { |
| 135 | + Tree: "HEAD", |
| 136 | + Path: "src/main/java/com/github/App.java", |
| 137 | + Line: 5, |
| 138 | + Column: 10, |
| 139 | + Text: ` * Hello world!`, |
| 140 | + }, |
| 141 | + } |
| 142 | + got := testrepo.Grep("world", GrepOptions{WordRegexp: true}) |
| 143 | + assert.Equal(t, want, got) |
| 144 | +} |
0 commit comments