Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added text differences #54

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func AddAPILayer(app *fiber.App) {
hashAPI(app)
timeAPI(app)
propertiesAPI(app)
compareTextAPI(app)
}

func healthCheck(app *fiber.App) {
Expand Down
33 changes: 33 additions & 0 deletions api/text.go
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)
})
}
49 changes: 49 additions & 0 deletions api/text_test.go
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)
})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/google/uuid v1.6.0
github.com/itchyny/json2yaml v0.1.4
github.com/oklog/ulid/v2 v2.1.0
github.com/sergi/go-diff v1.3.1
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.18.0
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofiber/fiber/v3 v3.0.0-20240106164914-960b65258767 h1:3v7G15sjIVzo/HuqQ37veATuZkdYESgncM7oejXNphw=
Expand All @@ -24,6 +25,7 @@ github.com/itchyny/json2yaml v0.1.4 h1:/pErVOXGG5iTyXHi/QKR4y3uzhLjGTEmmJIy97YT+
github.com/itchyny/json2yaml v0.1.4/go.mod h1:6iudhBZdarpjLFRNj+clWLAkGft+9uCcjAZYXUH9eGI=
github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4=
github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
Expand All @@ -46,10 +48,14 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand All @@ -65,7 +71,10 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 9 additions & 0 deletions internal/text.go
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)
}
47 changes: 47 additions & 0 deletions internal/text_test.go
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)
})
}
}
43 changes: 26 additions & 17 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ import (
)

const (
Prefix = "ui/"
APIPrefix = "/api"
MainLayout = Prefix + "layouts/main"
BasePath = "ui"
JSONPath = "/json"
YAMLPath = "/yaml"
JWTPath = "/jwt"
UUIDPath = "/uuid"
ULIDPath = "/ulid"
PasswordPath = "/password"
Base64Path = "/base64"
URLPath = "/url"
HashPath = "/hash"
TimePath = "/time"
Properties = "/properties"
Prefix = "ui/"
APIPrefix = "/api"
MainLayout = Prefix + "layouts/main"
BasePath = "ui"
JSONPath = "/json"
YAMLPath = "/yaml"
JWTPath = "/jwt"
UUIDPath = "/uuid"
ULIDPath = "/ulid"
PasswordPath = "/password"
Base64Path = "/base64"
URLPath = "/url"
HashPath = "/hash"
TimePath = "/time"
PropertiesPath = "/properties"
TextsPath = "/text"
)

//go:embed ui/*
Expand Down Expand Up @@ -150,7 +151,7 @@ func StartServer(port int32, isVerbose bool) {
hashPage(app)
timePage(app)
propertiesPage(app)

compareTextPage(app)
log.Fatal(app.Listen(":"+strconv.FormatInt(int64(port), 10), fiber.ListenConfig{EnablePrefork: true}))
}

Expand Down Expand Up @@ -249,12 +250,20 @@ func timePage(app *fiber.App) {
}

func propertiesPage(app *fiber.App) {
app.Get(Properties, func(c fiber.Ctx) error {
app.Get(PropertiesPath, func(c fiber.Ctx) error {
return c.Render(Prefix+"properties", newMap(map[string]any{
"Title": "Properties",
}), MainLayout)
})

}
func compareTextPage(app *fiber.App) {
app.Get(TextsPath, func(c fiber.Ctx) error {
return c.Render(Prefix+"text", newMap(map[string]any{
"Title": "Text BS",
}), MainLayout)
})

}

func defineResources(app *fiber.App) {
Expand Down
18 changes: 18 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,21 @@ func TestPropertiesPage(t *testing.T) {
}
})
}

func TestCompareTextPage(t *testing.T) {
app := newApp()
compareTextPage(app)

t.Run("Test Compare Text Page", func(t *testing.T) {
// Create a test request to the "/text-comparer" route
req := httptest.NewRequest(http.MethodGet, "/text-comparer", nil)
resp, err := app.Test(req)
if err != nil {
t.Fatalf("Failed to send test request: %v", err)
}
// Check the response status code
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code %d, but got %d", http.StatusOK, resp.StatusCode)
}
})
}
13 changes: 13 additions & 0 deletions server/ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,18 @@ <h5 class="card-title">Properties</h5>
</a>
</div>


<div class="col-md-4 col-lg-3 mb-2 d-flex gap-3">
<a href="/text" class="card-link">
<div class="card min-h-200" >
<div class="card-body">
<h5 class="card-title">Text BS</h5>
<p class="card-text">
Find text differences.</p>
</div>
</div>
</a>
</div>

</div>
</div>
3 changes: 3 additions & 0 deletions server/ui/partials/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<li class="nav-item">
<a class="nav-link" href="/properties">Properties</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/text">Text</a>
</li>
</ul>
</div>
</div>
Expand Down
Loading
Loading