Skip to content

Commit

Permalink
Merge pull request #20 from 2021111502/master
Browse files Browse the repository at this point in the history
补充测试
  • Loading branch information
FerdinandSu authored Dec 3, 2023
2 parents 7373bbb + d1d4ae7 commit 6d7c344
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,4 @@
<tag>${project.version}</tag>
</scm>

</project>
</project>
25 changes: 24 additions & 1 deletion src/test/java/ReFreSH/JMobileSuit/Demo/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

public class ClientTest {

Expand Down Expand Up @@ -85,5 +85,28 @@ public void testSleep() {
}

// Additional tests for other methods can be added here...
@Test
public void testSleep2() {
SleepArgument argument = new SleepArgument();
argument.Name.add("Bob");
argument.isSleeping = false;

client.Sleep(argument);
Mockito.verify(mockIoServer).WriteLine("Bob is not sleeping.");
}

@Test
public void testParse() {
GoodMorningParameter param = new GoodMorningParameter();
String[] options = {"Alice"};
assertTrue(param.parse(options));
assertEquals(param.name, "Alice");
String[] options2 = {""};
String[] options3 = {"Alice", "Bob"};
assertTrue(param.parse(options2));
assertFalse(param.parse(options3));
assertEquals(param.name,"");

}
}

42 changes: 42 additions & 0 deletions src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public void testWriteDebug() throws Exception {

// 验证模拟对象的 LogDebug 方法是否被调用,并且传入的参数是否符合预期
verify(mockLogger, times(1)).debug(anyString());

//空字符串也会被调用
ioserver.WriteDebug("");
verify(mockLogger, times(2)).debug(anyString());
}

@Test
Expand Down Expand Up @@ -167,6 +171,32 @@ public void testGetAndSetInput() throws Exception {
e.printStackTrace();
}

assertEquals(testInput, inputContent.toString());

}

@Test
public void testGetAndSetInput2() throws Exception {
//验证空字符串
String testInput = "";
InputStream inputStream = new ByteArrayInputStream(testInput.getBytes());

IOServer ioserver = getInstance();
ioserver.SetInput(inputStream);

InputStream result = ioserver.GetInput();

byte[] buffer = new byte[1024];
int bytesRead;
StringBuilder inputContent = new StringBuilder();
try {
while ((bytesRead = result.read(buffer)) != -1) {
inputContent.append(new String(buffer, 0, bytesRead));
}
} catch (Exception e) {
e.printStackTrace();
}

assertEquals(testInput, inputContent.toString());
}

Expand Down Expand Up @@ -352,5 +382,17 @@ public void testSetPrefix() {
assertEquals(Integer.valueOf(10), prefixLengthStack.pop());
}

@Test
public void testSelectColor() {
ColorSetting colorSetting = new ColorSetting();
assertEquals(colorSetting.DefaultColor, ColorSetting.selectColor(OutputType.Default, null, colorSetting));
assertEquals(colorSetting.PromptColor, ColorSetting.selectColor(OutputType.Prompt, null, colorSetting));
assertEquals(colorSetting.ErrorColor, ColorSetting.selectColor(OutputType.Error, null, colorSetting));
assertEquals(colorSetting.AllOkColor, ColorSetting.selectColor(OutputType.AllOk, null, colorSetting));
assertEquals(colorSetting.ListTitleColor, ColorSetting.selectColor(OutputType.ListTitle, null, colorSetting));
assertEquals(colorSetting.CustomInformationColor, ColorSetting.selectColor(OutputType.CustomInfo, null, colorSetting));
assertEquals(colorSetting.InformationColor, ColorSetting.selectColor(OutputType.MobileSuitInfo, null, colorSetting));
assertEquals(ConsoleColor.Blue, ColorSetting.selectColor(OutputType.MobileSuitInfo, ConsoleColor.Blue, colorSetting));
}

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package ReFreSH.JMobileSuit.NeuesProjekt;

import ReFreSH.JMobileSuit.IO.*;
import ReFreSH.JMobileSuit.IO.ColorSetting;
import ReFreSH.JMobileSuit.IO.CommonPromptServer;
import ReFreSH.JMobileSuit.IO.ConsoleColor;
import ReFreSH.JMobileSuit.IO.IOServer;
import ReFreSH.JMobileSuit.SuitConfiguration;
import ReFreSH.JMobileSuit.TraceBack;
import org.mockito.ArgumentCaptor;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.mockito.ArgumentCaptor;

import java.lang.reflect.Field;
import java.util.List;
import ReFreSH.JMobileSuit.IO.ColorSetting;



import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.*;


public class PowerLineThemedPromptServerTest {
Expand Down Expand Up @@ -65,22 +67,31 @@ public void testPrintMethod() throws NoSuchFieldException, IllegalAccessExceptio
//promptServer.LastInformation = "TestInfo";



Field ReturnValueField = CommonPromptServer.class.getDeclaredField("LastReturnValue");
ReturnValueField.setAccessible(true);
ReturnValueField.set(promptServer, "TestReturn");
//promptServer.LastReturnValue = "TestReturn";




Field PromptInformationField = CommonPromptServer.class.getDeclaredField("LastPromptInformation");
PromptInformationField.setAccessible(true);
PromptInformationField.set(promptServer, "PromptInfo");
//promptServer.LastPromptInformation = "PromptInfo";

// 调用Print方法
promptServer.Print();
TraceBackField.set(promptServer, TraceBack.Prompt);
promptServer.Print();
TraceBackField.set(promptServer, TraceBack.OnExit);
promptServer.Print();
TraceBackField.set(promptServer, TraceBack.InvalidCommand);
promptServer.Print();
TraceBackField.set(promptServer, TraceBack.ObjectNotFound);
promptServer.Print();
TraceBackField.set(promptServer, TraceBack.MemberNotFound);
promptServer.Print();
TraceBackField.set(promptServer, TraceBack.AppException);
promptServer.Print();

// 使用ArgumentCaptor来捕获write方法的调用参数
ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
Expand All @@ -98,6 +109,4 @@ public void testPrintMethod() throws NoSuchFieldException, IllegalAccessExceptio
}




}

0 comments on commit 6d7c344

Please sign in to comment.