mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-15 07:37:08 +08:00
Fix tests
This commit is contained in:
parent
0406a6dc1b
commit
392d62142b
mirai-core-api/src
commonMain/kotlin/message/data
jvmTest/kotlin/event
mirai-core/src/jvmTest/kotlin
@ -13,10 +13,7 @@
|
||||
@file:Suppress(
|
||||
"EXPERIMENTAL_API_USAGE",
|
||||
"unused",
|
||||
"WRONG_MODIFIER_CONTAINING_DECLARATION",
|
||||
"DEPRECATION",
|
||||
"UnusedImport",
|
||||
"EXPOSED_SUPER_CLASS",
|
||||
"DEPRECATION_ERROR", "NOTHING_TO_INLINE"
|
||||
)
|
||||
|
||||
@ -36,6 +33,8 @@ import net.mamoe.mirai.Bot
|
||||
import net.mamoe.mirai.IMirai
|
||||
import net.mamoe.mirai.Mirai
|
||||
import net.mamoe.mirai.contact.Contact
|
||||
import net.mamoe.mirai.contact.Contact.Companion.sendImage
|
||||
import net.mamoe.mirai.contact.Contact.Companion.uploadImage
|
||||
import net.mamoe.mirai.message.code.CodableMessage
|
||||
import net.mamoe.mirai.message.data.Image.Key.FRIEND_IMAGE_ID_REGEX_1
|
||||
import net.mamoe.mirai.message.data.Image.Key.FRIEND_IMAGE_ID_REGEX_2
|
||||
|
@ -16,6 +16,7 @@ import net.mamoe.mirai.event.events.MessageEvent
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
internal class EventChannelTest {
|
||||
@Suppress("UNUSED_VARIABLE")
|
||||
@Test
|
||||
fun testVariance() {
|
||||
var global: EventChannel<Event> = GlobalEventChannel
|
||||
|
@ -90,7 +90,7 @@ internal class JvmMethodEventsTest {
|
||||
}
|
||||
|
||||
TestClass().run {
|
||||
this.registerEvents()
|
||||
this.globalEventChannel().registerListenerHost(this)
|
||||
|
||||
runBlocking {
|
||||
TestEvent().broadcast()
|
||||
@ -119,7 +119,7 @@ internal class JvmMethodEventsTest {
|
||||
}
|
||||
|
||||
TestClass().run {
|
||||
this.registerEvents()
|
||||
this.globalEventChannel().registerListenerHost(this)
|
||||
|
||||
runBlocking {
|
||||
TestEvent().broadcast()
|
||||
@ -150,7 +150,7 @@ internal class JvmMethodEventsTest {
|
||||
}
|
||||
|
||||
TestClass().run {
|
||||
this.registerEvents()
|
||||
this.globalEventChannel().registerListenerHost(this)
|
||||
|
||||
runBlocking {
|
||||
TestEvent().broadcast()
|
||||
|
@ -40,7 +40,7 @@ internal class JvmMethodEventsTestJava : SimpleListenerHost() {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
this.registerEvents()
|
||||
this.globalEventChannel().registerListenerHost(this)
|
||||
TestEvent().__broadcastJava()
|
||||
assertEquals(3, called.get(), null)
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ class SimpleListenerHostTestJava {
|
||||
}
|
||||
}
|
||||
val scope = CoroutineScope(EmptyCoroutineContext)
|
||||
scope.registerEvents(host)
|
||||
scope.globalEventChannel().registerListenerHost(host)
|
||||
object : AbstractEvent() {}.__broadcastJava()
|
||||
if (!called.get()) {
|
||||
throw AssertionError("JavaTest: SimpleListenerHost Failed.")
|
||||
|
@ -10,33 +10,47 @@
|
||||
import kotlin.coroutines.CoroutineContext;
|
||||
import net.mamoe.mirai.Bot;
|
||||
import net.mamoe.mirai.BotFactory;
|
||||
import net.mamoe.mirai.contact.Contact;
|
||||
import net.mamoe.mirai.contact.Group;
|
||||
import net.mamoe.mirai.event.EventHandler;
|
||||
import net.mamoe.mirai.event.ListeningStatus;
|
||||
import net.mamoe.mirai.event.SimpleListenerHost;
|
||||
import net.mamoe.mirai.event.events.GroupMessageEvent;
|
||||
import net.mamoe.mirai.event.events.MessageEvent;
|
||||
import net.mamoe.mirai.message.data.Image;
|
||||
import net.mamoe.mirai.message.data.MessageChain;
|
||||
import net.mamoe.mirai.message.data.MessageSource;
|
||||
import net.mamoe.mirai.message.data.MessageUtils;
|
||||
import net.mamoe.mirai.utils.BotConfiguration;
|
||||
import net.mamoe.mirai.utils.ExternalResource;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
/**
|
||||
* 仅用来测试调用,不会被单元测试运行
|
||||
*/
|
||||
@SuppressWarnings({"unused", "UnusedAssignment"})
|
||||
public class JavaApiTests {
|
||||
public static void main(String[] args) {
|
||||
Bot bot = BotFactory.INSTANCE.newBot(11, "", configuration -> {
|
||||
configuration.fileBasedDeviceInfo();
|
||||
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PHONE);
|
||||
});
|
||||
@NotNull
|
||||
Bot bot;
|
||||
|
||||
public JavaApiTests(@NotNull Bot bot) {
|
||||
this.bot = bot;
|
||||
}
|
||||
|
||||
public void generalCalls() {
|
||||
bot.login();
|
||||
|
||||
bot.getAsFriend().sendMessage("test"); // blocking bridge
|
||||
bot.getOtherClients().getOrFail(1).getBot();
|
||||
}
|
||||
|
||||
bot.getEventChannel().subscribe(MessageEvent.class, event -> {
|
||||
|
||||
return ListeningStatus.LISTENING;
|
||||
});
|
||||
public void events() {
|
||||
bot.getEventChannel().subscribe(MessageEvent.class, event -> ListeningStatus.LISTENING);
|
||||
|
||||
bot.getEventChannel().subscribeAlways(GroupMessageEvent.class, event -> {
|
||||
Bot b = event.getBot();
|
||||
@ -58,4 +72,85 @@ public class JavaApiTests {
|
||||
|
||||
bot.getEventChannel().registerListenerHost(slh);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private <T> T magic() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
MessageChain chain = magic();
|
||||
|
||||
@NotNull
|
||||
Contact contact = magic();
|
||||
|
||||
public void messages() {
|
||||
|
||||
Image image = (Image) chain.stream().filter(Image.class::isInstance).findFirst().orElse(null);
|
||||
|
||||
assert image != null;
|
||||
|
||||
String url = Image.queryUrl(image);
|
||||
Image.fromId("123");
|
||||
|
||||
MessageSource source = magic();
|
||||
|
||||
MessageUtils.getBot(source);
|
||||
MessageUtils.calculateImageMd5(image);
|
||||
MessageUtils.isContentEmpty(image);
|
||||
|
||||
MessageSource.quote(source);
|
||||
MessageSource.quote(chain);
|
||||
|
||||
MessageSource.recall(source);
|
||||
}
|
||||
|
||||
ExternalResource resource = magic();
|
||||
|
||||
public void externalResource() throws IOException {
|
||||
resource.inputStream();
|
||||
|
||||
|
||||
contact.uploadImage(resource); // base method
|
||||
|
||||
|
||||
ExternalResource r;
|
||||
|
||||
r = ExternalResource.create((InputStream) magic()); // throws IOException
|
||||
r = ExternalResource.create((File) magic());
|
||||
r = ExternalResource.create((RandomAccessFile) magic());
|
||||
|
||||
ExternalResource.uploadAsImage(r, contact); // returns Image
|
||||
|
||||
ExternalResource.sendAsImage(r, contact); // returns MessageReceipt
|
||||
|
||||
|
||||
ExternalResource.uploadAsImage((ExternalResource) magic(), contact); // returns Image
|
||||
ExternalResource.uploadAsImage((File) magic(), contact); // returns Image
|
||||
ExternalResource.uploadAsImage((InputStream) magic(), contact); // returns Image
|
||||
|
||||
ExternalResource.sendAsImage((ExternalResource) magic(), contact); // returns MessageReceipt
|
||||
ExternalResource.sendAsImage((File) magic(), contact); // returns MessageReceipt
|
||||
ExternalResource.sendAsImage((InputStream) magic(), contact); // returns MessageReceipt
|
||||
|
||||
Contact.uploadImage(contact, (ExternalResource) magic()); // returns Image
|
||||
Contact.uploadImage(contact, (File) magic()); // returns Image
|
||||
Contact.uploadImage(contact, (InputStream) magic()); // returns Image
|
||||
|
||||
Contact.sendImage(contact, (ExternalResource) magic()); // returns MessageReceipt
|
||||
Contact.sendImage(contact, (File) magic()); // returns MessageReceipt
|
||||
Contact.sendImage(contact, (InputStream) magic()); // returns MessageReceipt
|
||||
|
||||
// experimental
|
||||
ExternalResource.uploadAsGroupVoice(magic(), (Group) contact);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Bot bot = BotFactory.INSTANCE.newBot(11, "", configuration -> {
|
||||
configuration.fileBasedDeviceInfo();
|
||||
configuration.setProtocol(BotConfiguration.MiraiProtocol.ANDROID_PHONE);
|
||||
});
|
||||
|
||||
new JavaApiTests(bot);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user