-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make code testable, add test against native xxd
- Loading branch information
Showing
2 changed files
with
78 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"io" | ||
"io/ioutil" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
"testing/quick" | ||
) | ||
|
||
var xxdFile = flag.String("xxdFile", "", "File to test against.") | ||
|
||
func TestXXD(t *testing.T) { | ||
if *xxdFile == "" { | ||
t.Skip("-xxdFile argument not given") | ||
} | ||
data, err := ioutil.ReadFile(*xxdFile) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
test := func(fn func(r io.Reader, w io.Writer) error) func(n uint64) []string { | ||
return func(n uint64) []string { | ||
size := n % uint64(len(data)) | ||
var out bytes.Buffer | ||
if err := fn(bytes.NewBuffer(data[0:size]), &out); err != nil { | ||
return []string{err.Error()} | ||
} | ||
return strings.Split(out.String(), "\n") | ||
} | ||
} | ||
if err := quick.CheckEqual(test(XXD), test(xxdNative), nil); err != nil { | ||
cErr := err.(*quick.CheckEqualError) | ||
size := cErr.In[0].(uint64) % uint64(len(data)) | ||
for i := range cErr.Out1[0].([]string) { | ||
got := cErr.Out1[0].([]string)[i] | ||
want := cErr.Out2[0].([]string)[i] | ||
if got != want { | ||
t.Errorf("size: %d\n\ngot : %s\nwant: %s\n", size, got, want) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
func xxdNative(r io.Reader, w io.Writer) error { | ||
xxd := exec.Command("xxd", "-") | ||
xxd.Stdin = r | ||
xxd.Stdout = w | ||
xxd.Stderr = w | ||
return xxd.Run() | ||
} |