mirror of
https://github.com/ManyakRus/starter.git
synced 2024-11-21 18:16:31 +02:00
сделал Int32FromString()
This commit is contained in:
parent
5cca5bfde9
commit
3922dc0eb8
6
.golangci.yml
Normal file
6
.golangci.yml
Normal file
@ -0,0 +1,6 @@
|
||||
# Options for analysis running.
|
||||
linters-settings:
|
||||
gocyclo:
|
||||
# Minimal code complexity to report.
|
||||
# Default: 30 (but we recommend 10-20)
|
||||
min-complexity: 10
|
2
Makefile
2
Makefile
@ -49,3 +49,5 @@ lines:
|
||||
go_lines_count ./ ./docs/lines_count.txt 10
|
||||
licenses:
|
||||
golicense -out-xlsx=./docs/licenses.xlsx $(FILEAPP)
|
||||
gocyclo:
|
||||
golangci-lint run ./... --disable-all -E gocyclo -v
|
||||
|
@ -978,3 +978,13 @@ func IsInt(s string) bool {
|
||||
Otvet = true
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// Int32FromString - возвращает int32 из строки
|
||||
func Int32FromString(s string) (int64, error) {
|
||||
var Otvet int64
|
||||
var err error
|
||||
|
||||
Otvet, err = strconv.ParseInt(s, 10, 32)
|
||||
|
||||
return Otvet, err
|
||||
}
|
||||
|
@ -873,3 +873,38 @@ func TestIsInt(t *testing.T) {
|
||||
t.Errorf("Expected false for string containing non-digit characters, but got: %v", nonDigitResult)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInt32FromString(t *testing.T) {
|
||||
// Test converting a valid string to int32
|
||||
input1 := "12345"
|
||||
expected1 := int64(12345)
|
||||
result1, err1 := Int32FromString(input1)
|
||||
if err1 != nil {
|
||||
t.Errorf("Expected no error, but got: %v", err1)
|
||||
}
|
||||
if result1 != expected1 {
|
||||
t.Errorf("Expected %d, but got: %d", expected1, result1)
|
||||
}
|
||||
|
||||
// Test converting an empty string to int32
|
||||
input2 := ""
|
||||
expected2 := int64(0)
|
||||
result2, err2 := Int32FromString(input2)
|
||||
if err2 == nil {
|
||||
t.Errorf("Expected error, but got: %v", err2)
|
||||
}
|
||||
if result2 != expected2 {
|
||||
t.Errorf("Expected %d, but got: %d", expected2, result2)
|
||||
}
|
||||
|
||||
// Test converting an invalid string to int32
|
||||
input3 := "abc"
|
||||
expected3 := int64(0)
|
||||
result3, err3 := Int32FromString(input3)
|
||||
if err3 == nil {
|
||||
t.Error("Expected an error, but got none")
|
||||
}
|
||||
if result3 != expected3 {
|
||||
t.Errorf("Expected %d, but got: %d", expected3, result3)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user