This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added text differences * added text diff to index.html ,at the navbar and added test for compareTextPage * added check line in option * fixed the TestCompareTextAPI
- Loading branch information
Showing
12 changed files
with
440 additions
and
17 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,33 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/ahelmy/xdev/internal" | ||
"github.com/gofiber/fiber/v3" | ||
) | ||
|
||
const ( | ||
TextsPath = "/text" | ||
) | ||
|
||
type CompareRequest struct { | ||
Text1 string `json:"text1"` | ||
Text2 string `json:"text2"` | ||
CheckLine bool `json:"check_line"` | ||
} | ||
|
||
func compareTextAPI(app *fiber.App) { | ||
app.Post(APIPrefix+TextsPath, func(c fiber.Ctx) error { | ||
req := CompareRequest{} | ||
err := json.Unmarshal(c.Request().Body(), &req) | ||
|
||
if err != nil { | ||
return c.JSON(Response{Success: false, Message: err.Error()}) | ||
} | ||
diffs := internal.CompareText(req.Text1, req.Text2, req.CheckLine) | ||
|
||
res := Response{ | ||
Success: true, Data: map[string]interface{}{"diffs": diffs}} | ||
return c.JSON(res) | ||
}) | ||
} |
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,49 @@ | ||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/gofiber/fiber/v3" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestCompareTextAPI(t *testing.T) { | ||
app := fiber.New() | ||
compareTextAPI(app) | ||
|
||
t.Run("Valid Request", func(t *testing.T) { | ||
compareRequest := CompareRequest{ | ||
Text1: "text", | ||
Text2: "text bro", | ||
CheckLine: false, | ||
} | ||
reqBody, _ := json.Marshal(compareRequest) | ||
|
||
req := httptest.NewRequest(http.MethodPost, "/api/text", bytes.NewReader(reqBody)) | ||
req.Header.Set("Content-Type", "application/json") | ||
res, err := app.Test(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
|
||
var response Response | ||
err = json.NewDecoder(res.Body).Decode(&response) | ||
assert.NoError(t, err) | ||
assert.True(t, response.Success) | ||
}) | ||
|
||
t.Run("JSON Unmarshal Error", func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodPost, "/api/text", bytes.NewReader([]byte("invalid json"))) | ||
req.Header.Set("Content-Type", "application/json") | ||
resp, err := app.Test(req) | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, resp.StatusCode) | ||
|
||
var response Response | ||
err = json.NewDecoder(resp.Body).Decode(&response) | ||
assert.NoError(t, err) | ||
assert.False(t, response.Success) | ||
}) | ||
} |
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
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,9 @@ | ||
package internal | ||
|
||
import "github.com/sergi/go-diff/diffmatchpatch" | ||
|
||
func CompareText(txt1 string, txt2 string, checkLines bool) string { | ||
dmp := diffmatchpatch.New() | ||
diffs := dmp.DiffMain(txt1, txt2, checkLines) | ||
return dmp.DiffPrettyHtml(diffs) | ||
} |
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,47 @@ | ||
package internal | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestCompareTexts(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
txt1 string | ||
txt2 string | ||
expected string | ||
}{ | ||
{ | ||
name: "Same texts", | ||
txt1: "Hello, World", | ||
txt2: "Hello, World", | ||
expected: "<span>Hello, World</span>", | ||
}, | ||
{ | ||
name: "Texts with additional words", | ||
txt1: "Hello World", | ||
txt2: "Hello", | ||
expected: "<span>Hello</span><del style=\"background:#ffe6e6;\"> World</del>", | ||
}, | ||
{ | ||
name: "Texts with less words", | ||
txt1: "Hello", | ||
txt2: "Hello World", | ||
expected: "<span>Hello</span><ins style=\"background:#e6ffe6;\"> World</ins>", | ||
}, | ||
{ | ||
name: "Texts with different characters", | ||
txt1: "Hello", | ||
txt2: "Helli", | ||
expected: "<span>Hell</span><del style=\"background:#ffe6e6;\">o</del><ins style=\"background:#e6ffe6;\">i</ins>", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := CompareText(tt.txt1, tt.txt2, false) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} |
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
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
Oops, something went wrong.