| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
| 10 | type genericScanner interface { |
| 11 | Err() error |
| 12 | Scan() bool |
| 13 | } |
| 14 | |
| 15 | func assertNextScan(t *testing.T, scanner genericScanner) { |
| 16 | assert.True(t, scanner.Scan()) |
| 17 | assert.Nil(t, scanner.Err()) |
| 18 | } |
| 19 | |
| 20 | func assertScannerDone(t *testing.T, scanner genericScanner) { |
| 21 | assert.False(t, scanner.Scan()) |
| 22 | assert.Nil(t, scanner.Err()) |
| 23 | } |
| 24 | |
| 25 | func TestLsTreeParser(t *testing.T) { |
| 26 | stdout := "100644 blob d899f6551a51cf19763c5955c7a06a2726f018e9 42 .gitattributes\000100644 blob 4d343e022e11a8618db494dc3c501e80c7e18197 126 PB SCN 16 Odhrán.wav" |
| 27 | scanner := NewLsTreeScanner(strings.NewReader(stdout)) |
| 28 | |
| 29 | assertNextTreeBlob(t, scanner, "d899f6551a51cf19763c5955c7a06a2726f018e9", ".gitattributes") |
| 30 | assertNextTreeBlob(t, scanner, "4d343e022e11a8618db494dc3c501e80c7e18197", "PB SCN 16 Odhrán.wav") |
| 31 | assertScannerDone(t, scanner) |
| 32 | } |
| 33 | |
| 34 | func assertNextTreeBlob(t *testing.T, scanner *LsTreeScanner, oid, filename string) { |
| 35 | assertNextScan(t, scanner) |
| 36 | b := scanner.TreeBlob() |
| 37 | assert.NotNil(t, b) |
| 38 | assert.Equal(t, oid, b.Oid) |
| 39 | assert.Equal(t, filename, b.Filename) |
| 40 | } |
| 41 | |
| 42 | func BenchmarkLsTreeParser(b *testing.B) { |
| 43 | stdout := "100644 blob d899f6551a51cf19763c5955c7a06a2726f018e9 42 .gitattributes\000100644 blob 4d343e022e11a8618db494dc3c501e80c7e18197 126 PB SCN 16 Odhrán.wav" |
| 44 | |
| 45 | // run the Fib function b.N times |
| 46 | for n := 0; n < b.N; n++ { |
| 47 | scanner := NewLsTreeScanner(strings.NewReader(stdout)) |
| 48 | for scanner.Scan() { |
| 49 | } |
| 50 | } |
| 51 | } |