mirror of
https://github.com/skidoodle/pastebin
synced 2026-04-28 03:07:40 +02:00
small refactor
This commit is contained in:
+15
-28
@@ -1,8 +1,8 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@@ -11,33 +11,20 @@ func TestGenerateId(t *testing.T) {
|
||||
id, err := generateId()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 10, len(id))
|
||||
for _, char := range id {
|
||||
assert.True(t, strings.ContainsRune(charset, char))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHighlight(t *testing.T) {
|
||||
goContent := `package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, World!")
|
||||
}`
|
||||
|
||||
// Test with a specific language extension (.go) -> SHOULD highlight
|
||||
highlighted, err := highlight(goContent, "go", "monokai")
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, highlighted, `style="color:#f92672"`, "Should highlight Go code with .go extension")
|
||||
|
||||
// Test with a generic extension (.txt) -> SHOULD auto-detect and highlight
|
||||
highlighted, err = highlight(goContent, "txt", "monokai")
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, highlighted, `style="color:#f92672"`, "Should auto-detect Go code with .txt extension")
|
||||
|
||||
// NEW TEST: No extension -> SHOULD NOT highlight
|
||||
highlighted, err = highlight(goContent, "", "monokai")
|
||||
assert.NoError(t, err)
|
||||
assert.NotContains(t, highlighted, `style="color:#f92672"`, "Should NOT highlight Go code when no extension is given")
|
||||
assert.Contains(t, highlighted, "package main", "Should still contain the original text")
|
||||
func TestHash(t *testing.T) {
|
||||
c := "test content"
|
||||
h1 := hash(c)
|
||||
h2 := hash(c)
|
||||
assert.Equal(t, h1, h2)
|
||||
assert.NotEmpty(t, h1)
|
||||
}
|
||||
|
||||
func TestTimeAgo(t *testing.T) {
|
||||
now := time.Now()
|
||||
assert.Equal(t, "0s ago", TimeAgo(now))
|
||||
assert.Equal(t, "1m ago", TimeAgo(now.Add(-61*time.Second)))
|
||||
assert.Equal(t, "1h ago", TimeAgo(now.Add(-3601*time.Second)))
|
||||
assert.Equal(t, "1d ago", TimeAgo(now.Add(-86401*time.Second)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user