Skip to content

Commit

Permalink
Merge pull request #2 from acmcodercom/strictMode
Browse files Browse the repository at this point in the history
implement the strict mode and refactor the tests
  • Loading branch information
lifubang authored Jan 21, 2022
2 parents d39f777 + 950c19b commit 0f99068
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 95 deletions.
2 changes: 1 addition & 1 deletion testdata/step.judger/datav5.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
33
4
4
4 5 6

3
2 changes: 1 addition & 1 deletion testdata/step.judger/userv5.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

4

4
45 6

3

11 changes: 8 additions & 3 deletions tool/differ.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ func clearString(str string) string {
}

// DiffOut is to diff the output between user and testcase
// TODO: we need to implement the "strictMode" mode in the future.
func DiffOut(userOut, dataOut string, diffIgnoreHead bool, strictMode bool) (bool, error) {
src, err := os.Open(userOut)
if err != nil {
Expand All @@ -37,7 +36,10 @@ func DiffOut(userOut, dataOut string, diffIgnoreHead bool, strictMode bool) (boo
if srcIgnoreHead {
srcIgnoreHead = false
} else {
trimStr := clearString(str)
trimStr := str
if !strictMode {
trimStr = clearString(trimStr)
}
if len(trimStr) > 0 {
strSrc.WriteString(trimStr)
}
Expand All @@ -51,7 +53,10 @@ func DiffOut(userOut, dataOut string, diffIgnoreHead bool, strictMode bool) (boo
if dstIgnoreHead {
dstIgnoreHead = false
} else {
trimStr := clearString(str)
trimStr := str
if !strictMode {
trimStr = clearString(trimStr)
}
if len(trimStr) > 0 {
strDest.WriteString(trimStr)
}
Expand Down
191 changes: 101 additions & 90 deletions tool/differ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,111 @@ import (
"testing"
)

func TestDiffOut(t *testing.T) {
func TestDiffOutFunc(t *testing.T) {
path, err := filepath.Abs(".")
if err != nil {
t.Fatal(err)
}
nodiff, err := DiffOut(filepath.Join(path, "../testdata/step.judger/user.out"), filepath.Join(path, "../testdata/step.judger/data.out"), false, false)
if err != nil {
t.Fatal(err)
}
if nodiff {
t.Logf("No diff")
} else {
t.Fatal("What's the diff?")
}
}

func TestDiffOutV1(t *testing.T) {
path, err := filepath.Abs(".")
if err != nil {
t.Fatal(err)
}
nodiff, err := DiffOut(filepath.Join(path, "../testdata/step.judger/userv1.out"), filepath.Join(path, "../testdata/step.judger/datav1.out"), false, false)
if err != nil {
t.Fatal(err)
}
if nodiff {
t.Logf("No diff")
} else {
t.Fatal("What's the diff?")
}
}

// TestDiffOutV2 is to ignore the space when comparing
func TestDiffOutV2(t *testing.T) {
path, err := filepath.Abs(".")
if err != nil {
t.Fatal(err)
}
nodiff, err := DiffOut(filepath.Join(path, "../testdata/step.judger/userv2.out"), filepath.Join(path, "../testdata/step.judger/datav2.out"), false, false)
if err != nil {
t.Fatal(err)
}
if nodiff {
t.Logf("No diff")
} else {
t.Fatal("What's the diff?")
}
}

func TestDiffOutV3(t *testing.T) {
path, err := filepath.Abs(".")
if err != nil {
t.Fatal(err)
}
nodiff, err := DiffOut(filepath.Join(path, "../testdata/step.judger/userv3.out"), filepath.Join(path, "../testdata/step.judger/datav3.out"), false, false)
if err != nil {
t.Fatal(err)
}
if nodiff {
t.Logf("No diff")
} else {
t.Fatal("What's the diff?")
}
}

func TestDiffOutV4(t *testing.T) {
path, err := filepath.Abs(".")
if err != nil {
t.Fatal(err)
}
nodiff, err := DiffOut(filepath.Join(path, "../testdata/step.judger/userv4.out"), filepath.Join(path, "../testdata/step.judger/datav4.out"), true, false)
if err != nil {
t.Fatal(err)
}
if nodiff {
t.Logf("No diff")
} else {
t.Fatal("What's the diff?")
}
}

// TestDiffOutV5 is to ignore the enter key when comparing
func TestDiffOutV5(t *testing.T) {
path, err := filepath.Abs(".")
if err != nil {
t.Fatal(err)
}
nodiff, err := DiffOut(filepath.Join(path, "../testdata/step.judger/userv5.out"), filepath.Join(path, "../testdata/step.judger/datav5.out"), false, false)
if err != nil {
t.Fatal(err)
}
if nodiff {
t.Logf("No diff")
} else {
t.Fatal("What's the diff?")
tests := []struct {
desc string
userOut string
dataOut string
ignoreHead bool
strictMode bool
shouldSame bool
}{
{
desc: "(L)There is an enter key in each line",
userOut: "user.out",
dataOut: "data.out",
ignoreHead: false,
strictMode: false,
shouldSame: true,
},
{
desc: "(S)There is an enter key in each line",
userOut: "user.out",
dataOut: "data.out",
ignoreHead: false,
strictMode: true,
shouldSame: false,
},
{
desc: "(S)Both are the same contents",
userOut: "userv1.out",
dataOut: "datav1.out",
ignoreHead: false,
strictMode: true,
shouldSame: true,
},
{
desc: "(L)Enter VS Space",
userOut: "userv2.out",
dataOut: "datav2.out",
ignoreHead: false,
strictMode: false,
shouldSame: true,
},
{
desc: "(S)Enter VS Space",
userOut: "userv2.out",
dataOut: "datav2.out",
ignoreHead: false,
strictMode: true,
shouldSame: false,
},
{
desc: "(L)New lines in the last",
userOut: "userv3.out",
dataOut: "datav3.out",
ignoreHead: false,
strictMode: false,
shouldSame: true,
},
{
desc: "(S)New lines in the last",
userOut: "userv3.out",
dataOut: "datav3.out",
ignoreHead: false,
strictMode: true,
shouldSame: true,
},
{
desc: "(L)Ignore the first line",
userOut: "userv4.out",
dataOut: "datav4.out",
ignoreHead: true,
strictMode: false,
shouldSame: true,
},
{
desc: "(L)So many white characters",
userOut: "userv5.out",
dataOut: "datav5.out",
ignoreHead: false,
strictMode: false,
shouldSame: true,
},
{
desc: "(S)So many white characters",
userOut: "userv5.out",
dataOut: "datav5.out",
ignoreHead: false,
strictMode: true,
shouldSame: false,
},
}
for idx, tc := range tests {
t.Logf("%d. %s:\n", idx+1, tc.desc)
nodiff, err := DiffOut(filepath.Join(path, "../testdata/step.judger/", tc.userOut), filepath.Join(path, "../testdata/step.judger/", tc.dataOut), tc.ignoreHead, tc.strictMode)
if err != nil {
t.Fatal(err)
}
if nodiff == tc.shouldSame {
t.Logf("PASS!")
} else {
t.Fatalf("Should %v, got %v: %s\n", tc.shouldSame, nodiff, tc.desc)
}
}
}

0 comments on commit 0f99068

Please sign in to comment.