Skip to content
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

add some Unit test to IOServer #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unbelievable to use Jvav 1.6 in 2024. as described in POM.xml, JMobileSuit uses Jvav 21.

Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@

<build>
<finalName>${project.organization.name}.${project.name}.${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
<defaultGoal>Package</defaultGoal>
<pluginManagement>
<plugins>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ReFreSH/JMobileSuit/IO/IOServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public String getContent(){
*/
public IOServer() {
Prompt = PromptServer.getInstance();
// ColorSetting = ReFreSH.JMobileSuit.IO.ColorSetting.getInstance();
ColorSetting = ReFreSH.JMobileSuit.IO.ColorSetting.getInstance();
_input = System.in;
Output = System.out;
Error = System.err;
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/ReFreSH/JMobileSuit/IO/IOServerTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public void testWriteWithColor() {
assertEquals("Colored Text\u001B[;31m", ioServer.getContent());
}

@Test
public void testWriteDefault() {
ioServer.Write("Default Text");
assertEquals("Default Text\u001B[;1;m", ioServer.getContent());
}

@Test
public void testIsInputRedirected2() {
assertTrue(ioServer.IsInputRedirected());
Expand Down Expand Up @@ -292,6 +298,33 @@ public void testReadLineWithoutPrompt() {
assertEquals(testInput, result);
}

@Test
public void testReadLineWithoutInputStream() {
String testInput = "";
InputStream inputStream = new ByteArrayInputStream(testInput.getBytes());
IOServer ioserver = getInstance();
ioserver.SetInput(inputStream);
String prompt = "";
boolean newLine = true;
String defaultValue ="Default Value";
String result = ioserver.ReadLine(prompt, defaultValue, newLine);

assertEquals(defaultValue, result);
}

@Test
public void testReadLineWithPromptAndNewline() {
String testInput = "Test Input";
InputStream inputStream = new ByteArrayInputStream(testInput.getBytes());
IOServer ioserver = getInstance();
ioserver.SetInput(inputStream);
String prompt = "";
boolean newLine = true;
String result = ioserver.ReadLine(prompt, newLine);

assertEquals(testInput, result);
}

@Test
public void testRead() throws IOException {
String testInput = "Test Input";
Expand Down Expand Up @@ -425,6 +458,69 @@ public void testSetPrefix() {
assertEquals(1, prefixLengthStack.size());
assertEquals(Integer.valueOf(10), prefixLengthStack.pop());
}
@Test
public void testSelectColorWithDefault() {
ColorSetting colorSetting = new ColorSetting();

// 测试当 customColor 为 null 时返回默认颜色
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use English for I18n

assertEquals(ConsoleColor.White, ColorSetting.selectColor(OutputType.Default, null, colorSetting));
assertEquals(ConsoleColor.Magenta, ColorSetting.selectColor(OutputType.Prompt, null, colorSetting));
assertEquals(ConsoleColor.Red, ColorSetting.selectColor(OutputType.Error, null, colorSetting));
assertEquals(ConsoleColor.Green, ColorSetting.selectColor(OutputType.AllOk, null, colorSetting));
assertEquals(ConsoleColor.Yellow, ColorSetting.selectColor(OutputType.ListTitle, null, colorSetting));
assertEquals(ConsoleColor.DarkCyan, ColorSetting.selectColor(OutputType.CustomInfo, null, colorSetting));
assertEquals(ConsoleColor.DarkBlue, ColorSetting.selectColor(OutputType.MobileSuitInfo, null, colorSetting));
}


@Test
public void testSelectColorWithCustomColor() {
ColorSetting colorSetting = new ColorSetting();

ConsoleColor customColor = ConsoleColor.Blue; // 定义一个自定义颜色
// 测试当 customColor 非空时返回自定义颜色
assertEquals(customColor, ColorSetting.selectColor(OutputType.Default, customColor, colorSetting));
assertEquals(customColor, ColorSetting.selectColor(OutputType.Prompt, customColor, colorSetting));
assertEquals(customColor, ColorSetting.selectColor(OutputType.Error, customColor, colorSetting));
assertEquals(customColor, ColorSetting.selectColor(OutputType.AllOk, customColor, colorSetting));
assertEquals(customColor, ColorSetting.selectColor(OutputType.ListTitle, customColor, colorSetting));
assertEquals(customColor, ColorSetting.selectColor(OutputType.CustomInfo, customColor, colorSetting));
assertEquals(customColor, ColorSetting.selectColor(OutputType.MobileSuitInfo, customColor, colorSetting));
}

@Test
public void testGetInstance() {
ColorSetting colorSetting1 = ColorSetting.getInstance();
ColorSetting colorSetting2 = ColorSetting.getInstance();

assertNotNull(colorSetting1);
assertNotNull(colorSetting2);
// 检查是否每次都返回新的实例
assertNotSame(colorSetting1, colorSetting2);
}
@Test
public void testDefaultColorsInitialization() {
ColorSetting colorSetting = new ColorSetting();

assertEquals(ConsoleColor.White, colorSetting.DefaultColor);
assertEquals(ConsoleColor.Magenta, colorSetting.PromptColor);
assertEquals(ConsoleColor.Red, colorSetting.ErrorColor);
assertEquals(ConsoleColor.Green, colorSetting.AllOkColor);
assertEquals(ConsoleColor.Yellow, colorSetting.ListTitleColor);
assertEquals(ConsoleColor.DarkCyan, colorSetting.CustomInformationColor);
assertEquals(ConsoleColor.DarkBlue, colorSetting.InformationColor);
}
@Test
public void testColorForDifferentOutputTypes() {
ColorSetting colorSetting = new ColorSetting();

assertEquals(ConsoleColor.White, colorSetting.DefaultColor);
assertEquals(ConsoleColor.Magenta, colorSetting.PromptColor);
assertEquals(ConsoleColor.Red, colorSetting.ErrorColor);
assertEquals(ConsoleColor.Green, colorSetting.AllOkColor);
assertEquals(ConsoleColor.Yellow, colorSetting.ListTitleColor);
assertEquals(ConsoleColor.DarkCyan, colorSetting.CustomInformationColor);
assertEquals(ConsoleColor.DarkBlue, colorSetting.InformationColor);
}

}