-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Start to add tests for modules/base/tool #90
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
11c9160
Start to add tests for modules/base/tool
metalmatze d874a9b
Add unit test for base.FileSize()
metalmatze 70fb1cf
Use testify/assert for all tests in tool_test.go
metalmatze a5d0b4d
Vendor github.com/stretchr/testify for the assert package
metalmatze f81711f
Test AvatarLink and refactor with tests passing
metalmatze 030ba28
Add tests for EllipsisString() and fix bug if param length < 3
metalmatze 86c32f2
Use TruncateString(sha1,10) in ShortSha and deprecate it.
metalmatze b00d82d
Add tests for TruncateString()
metalmatze d417aed
Add tests for StringsToInt64s() & Int64sToStrings()
metalmatze 4235fff
Add test for Int64sToMap()
metalmatze 2bb188a
Add test for IsLetter()
metalmatze 95d4cd9
Make long godoc comment 2 lines
metalmatze e74868a
Add test for IsTextFile()
metalmatze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,185 @@ | ||
package base | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/go-gitea/gitea/modules/setting" | ||
"github.com/stretchr/testify/assert" | ||
"strk.kbt.io/projects/go/libravatar" | ||
) | ||
|
||
func TestEncodeMD5(t *testing.T) { | ||
assert.Equal(t, | ||
"3858f62230ac3c915f300c664312c63f", | ||
EncodeMD5("foobar"), | ||
) | ||
} | ||
|
||
func TestEncodeSha1(t *testing.T) { | ||
assert.Equal(t, | ||
"8843d7f92416211de9ebb963ff4ce28125932878", | ||
EncodeSha1("foobar"), | ||
) | ||
} | ||
|
||
func TestShortSha(t *testing.T) { | ||
assert.Equal(t, "veryverylo", ShortSha("veryverylong")) | ||
} | ||
|
||
// TODO: Test DetectEncoding() | ||
|
||
func TestBasicAuthDecode(t *testing.T) { | ||
_, _, err := BasicAuthDecode("?") | ||
assert.Equal(t, "illegal base64 data at input byte 0", err.Error()) | ||
|
||
user, pass, err := BasicAuthDecode("Zm9vOmJhcg==") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "foo", user) | ||
assert.Equal(t, "bar", pass) | ||
} | ||
|
||
func TestBasicAuthEncode(t *testing.T) { | ||
assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar")) | ||
} | ||
|
||
func TestGetRandomString(t *testing.T) { | ||
assert.Len(t, GetRandomString(4), 4) | ||
} | ||
|
||
// TODO: Test PBKDF2() | ||
// TODO: Test VerifyTimeLimitCode() | ||
// TODO: Test CreateTimeLimitCode() | ||
|
||
func TestHashEmail(t *testing.T) { | ||
assert.Equal(t, | ||
"d41d8cd98f00b204e9800998ecf8427e", | ||
HashEmail(""), | ||
) | ||
assert.Equal(t, | ||
"353cbad9b58e69c96154ad99f92bedc7", | ||
HashEmail("[email protected]"), | ||
) | ||
} | ||
|
||
func TestAvatarLink(t *testing.T) { | ||
setting.EnableFederatedAvatar = false | ||
setting.LibravatarService = nil | ||
setting.DisableGravatar = true | ||
|
||
assert.Equal(t, "/img/avatar_default.png", AvatarLink("")) | ||
|
||
setting.DisableGravatar = false | ||
assert.Equal(t, | ||
"353cbad9b58e69c96154ad99f92bedc7", | ||
AvatarLink("[email protected]"), | ||
) | ||
|
||
setting.EnableFederatedAvatar = true | ||
assert.Equal(t, | ||
"353cbad9b58e69c96154ad99f92bedc7", | ||
AvatarLink("[email protected]"), | ||
) | ||
setting.LibravatarService = libravatar.New() | ||
assert.Equal(t, | ||
"http://cdn.libravatar.org/avatar/353cbad9b58e69c96154ad99f92bedc7", | ||
AvatarLink("[email protected]"), | ||
) | ||
} | ||
|
||
// TODO: computeTimeDiff() | ||
// TODO: TimeSincePro() | ||
// TODO: timeSince() | ||
// TODO: RawTimeSince() | ||
// TODO: TimeSince() | ||
|
||
func TestFileSize(t *testing.T) { | ||
var size int64 | ||
size = 512 | ||
assert.Equal(t, "512B", FileSize(size)) | ||
size = size * 1024 | ||
assert.Equal(t, "512KB", FileSize(size)) | ||
size = size * 1024 | ||
assert.Equal(t, "512MB", FileSize(size)) | ||
size = size * 1024 | ||
assert.Equal(t, "512GB", FileSize(size)) | ||
size = size * 1024 | ||
assert.Equal(t, "512TB", FileSize(size)) | ||
size = size * 1024 | ||
assert.Equal(t, "512PB", FileSize(size)) | ||
//size = size * 1024 TODO: Fix bug for EB | ||
//assert.Equal(t, "512EB", FileSize(size)) | ||
} | ||
|
||
// TODO: Subtract() | ||
|
||
func TestEllipsisString(t *testing.T) { | ||
assert.Equal(t, "...", EllipsisString("foobar", 0)) | ||
assert.Equal(t, "...", EllipsisString("foobar", 1)) | ||
assert.Equal(t, "...", EllipsisString("foobar", 2)) | ||
assert.Equal(t, "...", EllipsisString("foobar", 3)) | ||
assert.Equal(t, "f...", EllipsisString("foobar", 4)) | ||
assert.Equal(t, "fo...", EllipsisString("foobar", 5)) | ||
assert.Equal(t, "foobar", EllipsisString("foobar", 6)) | ||
assert.Equal(t, "foobar", EllipsisString("foobar", 10)) | ||
} | ||
|
||
func TestTruncateString(t *testing.T) { | ||
assert.Equal(t, "", TruncateString("foobar", 0)) | ||
assert.Equal(t, "f", TruncateString("foobar", 1)) | ||
assert.Equal(t, "fo", TruncateString("foobar", 2)) | ||
assert.Equal(t, "foo", TruncateString("foobar", 3)) | ||
assert.Equal(t, "foob", TruncateString("foobar", 4)) | ||
assert.Equal(t, "fooba", TruncateString("foobar", 5)) | ||
assert.Equal(t, "foobar", TruncateString("foobar", 6)) | ||
assert.Equal(t, "foobar", TruncateString("foobar", 7)) | ||
} | ||
|
||
func TestStringsToInt64s(t *testing.T) { | ||
assert.Equal(t, []int64{}, StringsToInt64s([]string{})) | ||
assert.Equal(t, | ||
[]int64{1, 4, 16, 64, 256}, | ||
StringsToInt64s([]string{"1", "4", "16", "64", "256"}), | ||
) | ||
|
||
// TODO: StringsToInt64s should return ([]int64, error) | ||
assert.Equal(t, []int64{-1, 0, 0}, StringsToInt64s([]string{"-1", "a", "$"})) | ||
} | ||
|
||
func TestInt64sToStrings(t *testing.T) { | ||
assert.Equal(t, []string{}, Int64sToStrings([]int64{})) | ||
assert.Equal(t, | ||
[]string{"1", "4", "16", "64", "256"}, | ||
Int64sToStrings([]int64{1, 4, 16, 64, 256}), | ||
) | ||
} | ||
|
||
func TestInt64sToMap(t *testing.T) { | ||
assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{})) | ||
assert.Equal(t, | ||
map[int64]bool{1: true, 4: true, 16: true}, | ||
Int64sToMap([]int64{1, 4, 16}), | ||
) | ||
} | ||
|
||
func TestIsLetter(t *testing.T) { | ||
assert.True(t, IsLetter('a')) | ||
assert.True(t, IsLetter('e')) | ||
assert.True(t, IsLetter('q')) | ||
assert.True(t, IsLetter('z')) | ||
assert.True(t, IsLetter('A')) | ||
assert.True(t, IsLetter('E')) | ||
assert.True(t, IsLetter('Q')) | ||
assert.True(t, IsLetter('Z')) | ||
assert.True(t, IsLetter('_')) | ||
assert.False(t, IsLetter('-')) | ||
assert.False(t, IsLetter('1')) | ||
assert.False(t, IsLetter('$')) | ||
} | ||
|
||
func TestIsTextFile(t *testing.T) { | ||
assert.True(t, IsTextFile([]byte{})) | ||
assert.True(t, IsTextFile([]byte("lorem ipsum"))) | ||
} | ||
|
||
// TODO: IsImageFile(), currently no idea how to test | ||
// TODO: IsPDFFile(), currently no idea how to test |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need a test for a really federated avatar, try [email protected]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't make a difference for the test. right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well, this one isn't really testing the federated part (I've seen packages just prefixing the hash with "cdn.libravatar.org" and be happy with it, it's not our case, we're actually doing the DNS lookups)