Skip to content

Commit

Permalink
refactor: Change isError to isValid
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatsnake committed Jan 29, 2023
1 parent b833a81 commit 9a6be93
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 29 deletions.
8 changes: 4 additions & 4 deletions pkg/camp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestVerify(t *testing.T) {
testValidValue := verifyArgs{[]int{1, 1, 1, 1, 1}, []int{1, 1, 0, 3, 0}, [][]int{{1, 0, 0, 2, 0}, {2, 0, 0, 1, 0}, {0, 0, 0, 2, 1}, {0, 2, 0, 0, 0}, {0, 1, 0, 2, 1}}}
result, err := Verify(testValidValue.Solution, testValidValue.RowTents, testValidValue.ColumnTents)

if err != nil || result.IsError {
t.Errorf("Verify(...) with valid args FAILED. Expected error != nil & result.IsError = false, but got %v, %v", err, result.IsError)
if err != nil || result.IsValid {
t.Errorf("Verify(...) with valid args FAILED. Expected error != nil & result.IsValid = false, but got %v, %v", err, result.IsValid)
} else {
t.Logf("Verify(...) PASSED")
}
Expand All @@ -50,8 +50,8 @@ func TestVerify(t *testing.T) {

result, err = Verify(testInvalidValue.Solution, testInvalidValue.RowTents, testInvalidValue.ColumnTents)

if err != nil || !result.IsError {
t.Errorf("Verify(...) with invalid args FAILED. Expected error = nil & result.IsError = true, but got %v, %v", err, result.IsError)
if err != nil || !result.IsValid {
t.Errorf("Verify(...) with invalid args FAILED. Expected error = nil & result.IsValid = true, but got %v, %v", err, result.IsValid)
} else {
t.Logf("Verify(...) PASSED")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/camp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Core struct {
}

type VerifyResult struct {
IsError bool `json:"isError"`
IsValid bool `json:"isValid"`
Position string `json:"position"`
Message string `json:"message"`
}
6 changes: 3 additions & 3 deletions pkg/sudoku/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestVerify(t *testing.T) {
// check 100% filled task
result := testCore.Verify()

if result.IsError {
if !result.IsValid {
t.Errorf("testCore.Verify() FAILED. Should be verified, but got error at position %+v", result.Position)
} else {
t.Logf("testCore.Verify() PASSED")
Expand All @@ -119,7 +119,7 @@ func TestVerify(t *testing.T) {
testCore.Task[0][1] = 1
result = testCore.Verify()

if !result.IsError {
if result.IsValid {
t.Errorf("testCore.Verify() FAILED. Should be an error, but got verified")
} else {
t.Logf("testCore.Verify() PASSED")
Expand All @@ -130,7 +130,7 @@ func TestVerify(t *testing.T) {
testCore.Task[1][0] = 1
result = testCore.Verify()

if !result.IsError {
if result.IsValid {
t.Errorf("testCore.Verify() FAILED. Should be an error, but got verified")
} else {
t.Logf("testCore.Verify() PASSED")
Expand Down
2 changes: 1 addition & 1 deletion pkg/sudoku/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ type Core struct {
}

type VerificationResult struct {
IsError bool `json:"isError"`
IsValid bool `json:"isValid"`
Position string `json:"position"`
}
8 changes: 4 additions & 4 deletions pkg/sudoku/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

func verify(grid [9][9]int) *VerificationResult {
verifyRows := checkRows(grid)
if verifyRows.IsError {
if !verifyRows.IsValid {
return verifyRows
}

verifyColumns := checkColumns(grid)
if verifyColumns.IsError {
if !verifyColumns.IsValid {
return verifyColumns
}

Expand All @@ -27,12 +27,12 @@ func сheckLines(grid [9][9]int, lineType string) *VerificationResult {
for j := 1; j <= len(row); j++ {
if j != row[j-1] {
return &VerificationResult{
true, lineType + "-" + strconv.Itoa(i+1),
false, lineType + "-" + strconv.Itoa(i+1),
}
}
}
}
return &VerificationResult{false, ""}
return &VerificationResult{true, ""}
}

func checkRows(grid [9][9]int) *VerificationResult {
Expand Down
14 changes: 3 additions & 11 deletions pkg/takuzu/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,22 @@ func TestVerify(t *testing.T) {
res, err := tc.Verify()
if err == nil {
t.Errorf("TestVerify FAILED. Should return error, but got %+v", res)
} else {
t.Logf("Verify() PASSED")
}

tc.Task = tc.Field

res, _ = tc.Verify()
if res.IsError {
if !res.IsValid {
t.Errorf("TestVerify FAILED. Should be verified, but got error: %+v", res)
} else {
t.Logf("Verify() PASSED")
}

tc.Task[0][0] = "1"
tc.Task[0][1] = "1"
tc.Task[0][2] = "1"

res, _ = tc.Verify()
if !res.IsError {
if res.IsValid {
t.Errorf("TestVerify FAILED. Should be not verified, but verified: %+v", res)
} else {
t.Logf("Verify() PASSED")
}

tc.Generate()
Expand All @@ -119,9 +113,7 @@ func TestVerify(t *testing.T) {
tc.Task[2][0] = "1"

res, _ = tc.Verify()
if !res.IsError {
if res.IsValid {
t.Errorf("TestVerify FAILED. Should be not verified, but verified: %+v", res)
} else {
t.Logf("Verify() PASSED")
}
}
2 changes: 1 addition & 1 deletion pkg/takuzu/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Core struct {
}

type VerificationResult struct {
IsError bool `json:"isError"`
IsValid bool `json:"isValid"`
Message string `json:"message"`
Position []string `json:"position"`
}
8 changes: 4 additions & 4 deletions pkg/takuzu/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func verify(field [][]string) (*VerificationResult, error) {

result := checkLines(rows, "row")

if result.IsError {
if !result.IsValid {
return result, errors.New(result.Message)
}

result = checkLines(columns, "column")

if result.IsError {
if !result.IsValid {
return result, errors.New(result.Message)
}

Expand Down Expand Up @@ -62,7 +62,7 @@ func checkDuplication(lines []string, lineType string) []string {

func defineVerificationError(msg string, pos []string) *VerificationResult {
return &VerificationResult{
IsError: true,
IsValid: false,
Message: msg,
Position: pos,
}
Expand All @@ -83,7 +83,7 @@ func checkBalance(line string) bool {

func checkLines(lines []string, lineType string) *VerificationResult {
result := &VerificationResult{
IsError: false,
IsValid: true,
Message: "",
Position: []string{},
}
Expand Down

0 comments on commit 9a6be93

Please sign in to comment.