Skip to content
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
2 changes: 2 additions & 0 deletions .github/workflows/commit-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ on:
- develop
- feature/**
- release/**
- test/**
- bugfix/**

jobs:
lint-commits:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/.idea
/vendor
/coverage.txt
# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: install tests

install:
go mod tidy && go mod vendor

tests:
go test -v ./tests
4 changes: 4 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func InArray[T comparable](needle T, haystack []T) bool {
}

func Explode(separator string, stringStr string) []string {
if separator == "" {
return []string{stringStr}
}

return strings.Split(stringStr, separator)
}

Expand Down
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
coverage:
status:
project:
default:
target: 90%
patch:
default:
target: 90%
111 changes: 111 additions & 0 deletions tests/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package tests

import (
"fmt"
"github.com/gouef/utils"
"github.com/stretchr/testify/assert"
"testing"
)

func TestInArray(t *testing.T) {
tests := []struct {
needle string
haystack []string
expected bool
}{
{"apple", []string{"apple", "banana", "cherry"}, true},
{"orange", []string{"apple", "banana", "cherry"}, false},
{"", []string{"apple", "banana", "cherry"}, false},
}

for _, tt := range tests {
t.Run(tt.needle, func(t *testing.T) {
result := utils.InArray(tt.needle, tt.haystack)
assert.Equal(t, tt.expected, result)
})
}
}

func TestExplode(t *testing.T) {
tests := []struct {
separator string
input string
expected []string
}{
{",", "apple,banana,cherry", []string{"apple", "banana", "cherry"}},
{" ", "apple banana cherry", []string{"apple", "banana", "cherry"}},
{";", "apple;banana;cherry", []string{"apple", "banana", "cherry"}},
{"", "apple", []string{"apple"}},
}

for _, tt := range tests {
t.Run(tt.separator, func(t *testing.T) {
result := utils.Explode(tt.separator, tt.input)
assert.Equal(t, tt.expected, result)
})
}
}

func TestImplode(t *testing.T) {
tests := []struct {
separator string
elements []string
expected string
}{
{",", []string{"apple", "banana", "cherry"}, "apple,banana,cherry"},
{" ", []string{"apple", "banana", "cherry"}, "apple banana cherry"},
{";", []string{"apple", "banana", "cherry"}, "apple;banana;cherry"},
{"", []string{"apple"}, "apple"},
}

for _, tt := range tests {
t.Run(tt.separator, func(t *testing.T) {
result := utils.Implode(tt.separator, tt.elements)
assert.Equal(t, tt.expected, result)
})
}
}

func TestSlice(t *testing.T) {
tests := []struct {
arr []int
start int
length int
expected []int
}{
{[]int{1, 2, 3, 4, 5}, 1, 3, []int{2, 3, 4}},
{[]int{1, 2, 3, 4, 5}, -2, 3, []int{4, 5}},
{[]int{1, 2, 3, 4, 5}, 0, 2, []int{1, 2}},
{[]int{1, 2, 3, 4, 5}, 2, 5, []int{3, 4, 5}},
{[]int{1, 2, 3, 4, 5}, -6, 2, nil}, // out of range
{[]int{1, 2, 3, 4, 5}, 3, 10, []int{4, 5}}, // length beyond array length
{[]int{}, 0, 1, []int{}}, // empty array
}

for _, tt := range tests {
t.Run(fmt.Sprintf("start:%d length:%d", tt.start, tt.length), func(t *testing.T) {
result := utils.Slice(tt.arr, tt.start, tt.length)
assert.Equal(t, tt.expected, result)
})
}
}

func TestIsset(t *testing.T) {
tests := []struct {
array map[interface{}]interface{}
key interface{}
expected bool
}{
{map[interface{}]interface{}{"a": 1, "b": 2}, "a", true},
{map[interface{}]interface{}{"a": 1, "b": 2}, "c", false},
{map[interface{}]interface{}{"a": 1, "b": 2}, 1, false},
{map[interface{}]interface{}{"a": 1, "b": 2}, "b", true},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("key:%v", tt.key), func(t *testing.T) {
result := utils.Isset(tt.array, tt.key)
assert.Equal(t, tt.expected, result)
})
}
}
53 changes: 53 additions & 0 deletions tests/ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package tests

import (
"fmt"
"github.com/gouef/utils"
"github.com/stretchr/testify/assert"
"testing"
)

func TestIp2long(t *testing.T) {
tests := []struct {
ip string
expected uint32
err error
}{
{"192.168.1.1", 3232235777, nil}, // Standardní IPv4 adresa
{"255.255.255.255", 4294967295, nil}, // Maximalní IPv4 adresa
{"0.0.0.0", 0, nil}, // Minimální IPv4 adresa
{"invalid_ip", 0, fmt.Errorf("invalid IP address: invalid_ip")}, // Neplatná adresa
{"256.256.256.256", 0, fmt.Errorf("invalid IP address: 256.256.256.256")}, // Neplatná adresa
{"fe80::1", 0, fmt.Errorf("not an IPv4 address: fe80::1")}, // IPv6 adresa
}

for _, tt := range tests {
t.Run(tt.ip, func(t *testing.T) {
result, err := utils.Ip2long(tt.ip)
if tt.err != nil {
assert.EqualError(t, err, tt.err.Error())
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}

func TestLong2ip(t *testing.T) {
tests := []struct {
ipLong uint32
expected string
}{
{3232235777, "192.168.1.1"},
{4294967295, "255.255.255.255"},
{0, "0.0.0.0"},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.ipLong), func(t *testing.T) {
result := utils.Long2ip(tt.ipLong)
assert.Equal(t, tt.expected, result)
})
}
}
11 changes: 6 additions & 5 deletions math_test.go → tests/math_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package utils
package tests

import (
"github.com/gouef/utils"
"github.com/stretchr/testify/assert"
"testing"
)

func TestRound(t *testing.T) {
result := RoundTo(1234, 1)
result := utils.RoundTo(1234, 1)
assert.Equal(t, float64(1230), result, "Expected 1230")

result1 := RoundTo(1234, 2)
result1 := utils.RoundTo(1234, 2)
assert.Equal(t, float64(1200), result1, "Expected 1200")

result2 := RoundTo(1234, 3)
result2 := utils.RoundTo(1234, 3)
assert.Equal(t, float64(1000), result2, "Expected 1000")
}

func TestRoundTens(t *testing.T) {
result := RoundTens(123)
result := utils.RoundTens(123)
assert.Equal(t, float64(120), result, "Expected 120")
}

Expand Down
90 changes: 90 additions & 0 deletions tests/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package tests

import (
"testing"

"github.com/gouef/utils"
"github.com/stretchr/testify/assert"
)

func TestSubstr(t *testing.T) {
tests := []struct {
input string
start int
length *int
expected string
}{
{"hello world", 0, nil, "hello world"}, // Celý řetězec
{"hello world", 0, ptr(5), "hello"}, // Začátek + délka
{"hello world", 6, ptr(5), "world"}, // Od určité pozice
{"hello world", -5, ptr(3), "wor"}, // Záporný start
{"hello world", -5, ptr(10), "world"}, // Záporný start přesahující délku
{"hello world", 11, ptr(5), ""}, // Start mimo délku řetězce
{"hello world", -12, ptr(5), ""}, // Záporný start mimo délku řetězce
{"hello world", 3, ptr(20), "lo world"}, // Délka přesahující konec
}

for _, tt := range tests {
result := utils.Substr(tt.input, tt.start, tt.length)
assert.Equal(t, tt.expected, result, "Input: %s, Start: %d, Length: %v", tt.input, tt.start, tt.length)

// Test, že Substring funguje stejně
resultSubstring := utils.Substring(tt.input, tt.start, tt.length)
assert.Equal(t, tt.expected, resultSubstring, "Substring failed: Input: %s, Start: %d, Length: %v", tt.input, tt.start, tt.length)
}
}

// Pomocná funkce pro ukazatel na int
func ptr(i int) *int {
return &i
}

func TestStrpos(t *testing.T) {
tests := []struct {
stringStr string
needle string
expected int
}{
{"hello world", "world", 6}, // Výskyt podřetězce
{"hello world", "hello", 0}, // Výskyt na začátku
{"hello world hello", "hello", 12}, // Poslední výskyt
{"hello world", "x", -1}, // Žádný výskyt
{"hello", "", 5}, // Prázdný needle
{"", "hello", -1}, // Prázdný řetězec
}

for _, tt := range tests {
result := utils.Strpos(tt.stringStr, tt.needle)
assert.Equal(t, tt.expected, result, "Strpos failed: Input: %s, Needle: %s", tt.stringStr, tt.needle)

// Test, že StringPosition funguje stejně
resultPosition := utils.StringPosition(tt.stringStr, tt.needle)
assert.Equal(t, tt.expected, resultPosition, "StringPosition failed: Input: %s, Needle: %s", tt.stringStr, tt.needle)
}
}

func TestStrncmp(t *testing.T) {
tests := []struct {
s1 string
s2 string
n int
expected int
}{
{"hello", "hello", 5, 0}, // Stejné řetězce
{"hello", "hella", 5, 1}, // Rozdílné znaky na konci
{"hello", "hellz", 4, 0}, // Porovnání prvních 4 znaků
{"hello", "world", 3, -1}, // Rozdílné řetězce
{"hello", "hello world", 5, 0}, // Prvních 5 znaků je stejné
{"hello", "he", 5, 1}, // Jeden řetězec kratší
{"he", "hello", 5, -1}, // Opačný případ
}

for _, tt := range tests {
result := utils.Strncmp(tt.s1, tt.s2, tt.n)
assert.Equal(t, tt.expected, result, "Strncmp failed: S1: %s, S2: %s, N: %d", tt.s1, tt.s2, tt.n)

// Test, že StringCompare funguje stejně
resultCompare := utils.StringCompare(tt.s1, tt.s2, tt.n)
assert.Equal(t, tt.expected, resultCompare, "StringCompare failed: S1: %s, S2: %s, N: %d", tt.s1, tt.s2, tt.n)
}
}
1 change: 1 addition & 0 deletions tests/translator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package tests
50 changes: 50 additions & 0 deletions tests/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tests

import (
"testing"

"github.com/gouef/utils"
"github.com/stretchr/testify/assert"
)

func TestDetectType(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{"123", int32(123)},
{"2147483647", int32(2147483647)}, // Max int32
{"9223372036854775807", int64(9223372036854775807)}, // Max int64
{"3.14", float32(3.14)},
{"2.718", float32(2.718)},
{"1.7976931348623157e+308", float64(1.7976931348623157e+308)}, // Max float64
{"true", true},
{"false", false},
{"hello", "hello"},
}

for _, tt := range tests {
result := utils.DetectType(tt.input)
assert.Equal(t, tt.expected, result, "Input: %s", tt.input)
}
}

func TestIsInt(t *testing.T) {
tests := []struct {
input string
expectedOk bool
expected int
}{
{"123", true, 123},
{"2147483647", true, 2147483647}, // Max int32
{"9223372036854775807", false, 0}, // Max int64 (bude vráceno false)
{"hello", false, 0},
{"3.14", false, 0},
}

for _, tt := range tests {
ok, result := utils.IsInt(tt.input)
assert.Equal(t, tt.expectedOk, ok, "Input: %s", tt.input)
assert.Equal(t, tt.expected, result, "Input: %s", tt.input)
}
}
1 change: 1 addition & 0 deletions tests/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package tests
1 change: 0 additions & 1 deletion translator_test.go

This file was deleted.

Loading
Loading