-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlagsTest.java
More file actions
98 lines (77 loc) · 3.47 KB
/
Copy pathFlagsTest.java
File metadata and controls
98 lines (77 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.github.yin.flags;
import com.github.yin.flags.testclasses.TestFlagDesc;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
public class FlagsTest {
public static final String TESTFLAGS_PACKAGE = TestFlagDesc.class.getPackage().getName();
@BeforeClass
public static void setupClass() {
//TODO yin: instantiate a flags instance for the future
}
@Test
public void onlyFlag() throws Exception {
List<String> nonFlags = Flags.parse(new String[]{"--dummy", "value"},
Arrays.asList(TESTFLAGS_PACKAGE));
assertEquals("should inject flag value", "value", TestFlagDesc.getDummy());
assertEquals(nonFlags.size(), 0);
}
@Test
public void onlyArgs() throws Exception {
List<String> nonFlags = Flags.parse(new String[]{"just", "args"},
Arrays.asList(TESTFLAGS_PACKAGE));
assertEquals("should inject flag value", "", TestFlagDesc.getDummy());
assertArrayEquals("Arguments should be: just, args",
nonFlags.toArray(new String[0]), new String[] { "just", "args" });
}
@Test
public void flagsThenArgs() throws Exception {
List<String> nonFlags = Flags.parse(new String[]{"--dummy", "value", "just", "args"},
Arrays.asList(TESTFLAGS_PACKAGE));
assertEquals("should inject flag value", "value", TestFlagDesc.getDummy());
assertArrayEquals("Arguments should be: just, args",
nonFlags.toArray(new String[0]), new String[] { "just", "args" });
}
@Test
public void argsThenFlags() throws Exception {
List<String> nonFlags = Flags.parse(new String[]{"just", "args", "--dummy", "value"},
Arrays.asList(TESTFLAGS_PACKAGE));
assertEquals("should inject flag value", "value", TestFlagDesc.getDummy());
assertArrayEquals("Arguments should be: just, args",
nonFlags.toArray(new String[0]), new String[] { "just", "args" });
}
@Test
public void validator_valid() throws Exception {
List<String> nonFlags = Flags.parse(new String[]{"--withValidator", "valid"},
Arrays.asList(TESTFLAGS_PACKAGE));
assertEquals("should inject flag value", "valid", TestFlagDesc.getWithValidator());
}
@Test
public void validator_invalid() throws Exception {
try {
List<String> nonFlags = Flags.parse(new String[]{"--withValidator", TestFlagDesc.INVALID_VALUE},
Arrays.asList(TESTFLAGS_PACKAGE));
fail("Should have thrown ParseException");
} catch(Flags.ParseException ex) {
assertFalse(ex.getMessage().trim().isEmpty());
// success
}
}
@Test
public void printUsage() throws Exception {
PrintStream stdout = System.out;
ByteArrayOutputStream catchStream = new ByteArrayOutputStream();
System.setOut(new PrintStream(catchStream));
Flags.printUsage(TESTFLAGS_PACKAGE);
String result = new String(catchStream.toByteArray());
System.setOut(stdout);
System.out.print(result);
assertTrue("should print class description", result.contains("#classDocumentation"));
assertTrue("should print flag description", result.contains("#flagDocumentation"));
assertTrue("should print flag name", result.contains("dummy"));
}
}