github.com/vipcoin-gold/reviewdog@v1.0.2/parser/diff_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"google.golang.org/protobuf/encoding/protojson"
    10  )
    11  
    12  func ExampleDiffParser() {
    13  	const sample = `diff --git a/gofmt.go b/gofmt.go
    14  --- a/gofmt.go	2020-07-26 08:01:09.260800318 +0000
    15  +++ b/gofmt.go	2020-07-26 08:01:09.260800318 +0000
    16  @@ -1,6 +1,6 @@
    17   package testdata
    18   
    19  -func    fmt     () {
    20  +func fmt() {
    21   	// test
    22   	// test line
    23   	// test line
    24  @@ -10,11 +10,11 @@
    25   	// test line
    26   	// test line
    27   
    28  -println(
    29  -		"hello, gofmt test"    )
    30  -//comment
    31  +	println(
    32  +		"hello, gofmt test")
    33  +	//comment
    34   }
    35   
    36  +type s struct{ A int }
    37   
    38  -type s struct { A int }
    39   func (s s) String() { return "s" }
    40  `
    41  	const strip = 1
    42  	p := NewDiffParser(strip)
    43  	diagnostics, err := p.Parse(strings.NewReader(sample))
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	for _, d := range diagnostics {
    48  		rdjson, _ := protojson.MarshalOptions{Indent: "  "}.Marshal(d)
    49  		var out bytes.Buffer
    50  		json.Indent(&out, rdjson, "", "  ")
    51  		fmt.Println(out.String())
    52  	}
    53  	// Output:
    54  	// {
    55  	//   "location": {
    56  	//     "path": "gofmt.go",
    57  	//     "range": {
    58  	//       "start": {
    59  	//         "line": 3
    60  	//       },
    61  	//       "end": {
    62  	//         "line": 3
    63  	//       }
    64  	//     }
    65  	//   },
    66  	//   "suggestions": [
    67  	//     {
    68  	//       "range": {
    69  	//         "start": {
    70  	//           "line": 3
    71  	//         },
    72  	//         "end": {
    73  	//           "line": 3
    74  	//         }
    75  	//       },
    76  	//       "text": "func fmt() {"
    77  	//     }
    78  	//   ],
    79  	//   "originalOutput": "gofmt.go:3:-func    fmt     () {\ngofmt.go:3:+func fmt() {"
    80  	// }
    81  	// {
    82  	//   "location": {
    83  	//     "path": "gofmt.go",
    84  	//     "range": {
    85  	//       "start": {
    86  	//         "line": 13
    87  	//       },
    88  	//       "end": {
    89  	//         "line": 15
    90  	//       }
    91  	//     }
    92  	//   },
    93  	//   "suggestions": [
    94  	//     {
    95  	//       "range": {
    96  	//         "start": {
    97  	//           "line": 13
    98  	//         },
    99  	//         "end": {
   100  	//           "line": 15
   101  	//         }
   102  	//       },
   103  	//       "text": "\tprintln(\n\t\t\"hello, gofmt test\")\n\t//comment"
   104  	//     }
   105  	//   ],
   106  	//   "originalOutput": "gofmt.go:13:-println(\ngofmt.go:14:-\t\t\"hello, gofmt test\"    )\ngofmt.go:15:-//comment\ngofmt.go:13:+\tprintln(\ngofmt.go:14:+\t\t\"hello, gofmt test\")\ngofmt.go:15:+\t//comment"
   107  	// }
   108  	// {
   109  	//   "location": {
   110  	//     "path": "gofmt.go",
   111  	//     "range": {
   112  	//       "start": {
   113  	//         "line": 18,
   114  	//         "column": 1
   115  	//       },
   116  	//       "end": {
   117  	//         "line": 18,
   118  	//         "column": 1
   119  	//       }
   120  	//     }
   121  	//   },
   122  	//   "suggestions": [
   123  	//     {
   124  	//       "range": {
   125  	//         "start": {
   126  	//           "line": 18,
   127  	//           "column": 1
   128  	//         },
   129  	//         "end": {
   130  	//           "line": 18,
   131  	//           "column": 1
   132  	//         }
   133  	//       },
   134  	//       "text": "type s struct{ A int }\n"
   135  	//     }
   136  	//   ],
   137  	//   "originalOutput": "gofmt.go:18:+type s struct{ A int }"
   138  	// }
   139  	// {
   140  	//   "location": {
   141  	//     "path": "gofmt.go",
   142  	//     "range": {
   143  	//       "start": {
   144  	//         "line": 19
   145  	//       },
   146  	//       "end": {
   147  	//         "line": 19
   148  	//       }
   149  	//     }
   150  	//   },
   151  	//   "suggestions": [
   152  	//     {
   153  	//       "range": {
   154  	//         "start": {
   155  	//           "line": 19
   156  	//         },
   157  	//         "end": {
   158  	//           "line": 19
   159  	//         }
   160  	//       }
   161  	//     }
   162  	//   ],
   163  	//   "originalOutput": "gofmt.go:19:-type s struct { A int }"
   164  	// }
   165  }