-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete the work on Mocking frame (#1872)
* chore: merge from origin pr * chore: merge from origin pr * feat: StrangerRelationChangeEvent * feat: GroupEntranceAnnouncementChangeEvent * feat: MemberHonorChangeEvent * feat: GroupTalkativeChangeEvent * fix: fix all mentioned problems * fix: modify as requirement * doc: group.controlPane Co-authored-by: Him188 <Him188@mamoe.net> * fix: assertNotEqual in MockFriendTest * feat: mockAbsoluteFile and mockAbsoluteFolder(WIP) * improve: mockAbsoluteFile and mockAbsoluteFolder (WIP) * fix: if parent == root, null * improve: mockAbsoluteFile and mockAbsoluteFolder (WIP) * getUrl in mockAbsoluteFile * add remoteFile v2 to Group * add contentsCount * override AvatarUrl in mockContact * member test * fix: data race in MockAbsFile _exists Co-authored-by: Him188 <Him188@mamoe.net> * perf: remove general avatarUrl * chore: move package to net.mamoe.mirai.mock.internal.absoluteFile * fix: update tests following suggestions * fix: reduce utils methods * fix: absoluteFile rename * fix: add random suspend Co-authored-by: Him188 <Him188@mamoe.net>
- Loading branch information
Showing
25 changed files
with
770 additions
and
173 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
108 changes: 108 additions & 0 deletions
108
mirai-core-mock/src/internal/absolutefile/MockAbsoluteFile.kt
This file contains 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,108 @@ | ||
/* | ||
* Copyright 2019-2022 Mamoe Technologies and contributors. | ||
* | ||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. | ||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. | ||
* | ||
* https://github.com/mamoe/mirai/blob/dev/LICENSE | ||
*/ | ||
@file:Suppress("invisible_member", "INVISIBLE_REFERENCE") | ||
|
||
package net.mamoe.mirai.mock.internal.absolutefile | ||
|
||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import net.mamoe.mirai.contact.FileSupported | ||
import net.mamoe.mirai.contact.file.AbsoluteFile | ||
import net.mamoe.mirai.contact.file.AbsoluteFolder | ||
import net.mamoe.mirai.internal.message.FileMessageImpl | ||
import net.mamoe.mirai.message.data.FileMessage | ||
import net.mamoe.mirai.mock.utils.mock | ||
|
||
internal class MockAbsoluteFile( | ||
override val sha1: ByteArray, | ||
override val md5: ByteArray, | ||
private val files: MockRemoteFiles, | ||
override var parent: AbsoluteFolder?, | ||
override val id: String, | ||
override var name: String, | ||
override var absolutePath: String, | ||
override val contact: FileSupported = files.contact, | ||
override var expiryTime: Long = 0L, | ||
override val size: Long = 0, | ||
override val isFile: Boolean = true, | ||
override val isFolder: Boolean = false, | ||
override val uploadTime: Long = 0, | ||
override var lastModifiedTime: Long = 0, | ||
override val uploaderId: Long = 0 | ||
) : AbsoluteFile { | ||
@Volatile | ||
private var _exists = true | ||
override suspend fun moveTo(folder: AbsoluteFolder): Boolean { | ||
if (!exists()) return false | ||
files.fileSystem.resolveById(id)!!.moveTo(files.fileSystem.resolveById(folder.id)!!) | ||
this.parent = folder | ||
refresh() | ||
return true | ||
} | ||
|
||
override suspend fun getUrl(): String = | ||
files.contact.bot.mock().tmpFsServer.resolveHttpUrl(files.fileSystem.resolveById(id)!!.resolveNativePath()) | ||
|
||
override fun toMessage(): FileMessage { | ||
//todo busId | ||
return FileMessageImpl(id, 0, name, size) | ||
} | ||
|
||
override suspend fun refreshed(): AbsoluteFile? = | ||
parent!!.files().filter { it.id == id }.firstOrNull() | ||
|
||
override suspend fun exists(): Boolean = _exists | ||
|
||
override suspend fun renameTo(newName: String): Boolean { | ||
if (!exists()) return false | ||
if (files.fileSystem.resolveById(id)!!.rename(newName)) { | ||
refresh() | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
override suspend fun delete(): Boolean { | ||
if (!exists()) return false | ||
if (files.fileSystem.resolveById(id)!!.delete()) { | ||
_exists = false | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
override suspend fun refresh(): Boolean { | ||
val new = refreshed() | ||
if (new == null) { | ||
_exists = false | ||
return false | ||
} | ||
_exists = true | ||
this.parent = new.parent | ||
this.expiryTime = new.expiryTime | ||
this.name = new.name | ||
this.lastModifiedTime = new.lastModifiedTime | ||
this.absolutePath = new.absolutePath | ||
return true | ||
} | ||
|
||
override fun toString(): String = "MockAbsoluteFile(id=$id,absolutePath=$absolutePath,name=$name)" | ||
override fun equals(other: Any?): Boolean = | ||
other != null && other is AbsoluteFile && other.id == id | ||
|
||
override fun hashCode(): Int { | ||
// from absoluteFileImpl | ||
var result = super.hashCode() | ||
result = 31 * result + expiryTime.hashCode() | ||
result = 31 * result + size.hashCode() | ||
result = 31 * result + sha1.contentHashCode() | ||
result = 31 * result + md5.contentHashCode() | ||
return result | ||
} | ||
} |
Oops, something went wrong.