forked from auberonedu/truffula
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathTruffulaPrinterTest.java
More file actions
232 lines (188 loc) · 8.94 KB
/
TruffulaPrinterTest.java
File metadata and controls
232 lines (188 loc) · 8.94 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TruffulaPrinterTest {
/**
* Checks if the current operating system is Windows.
*
* This method reads the "os.name" system property and checks whether it
* contains the substring "win", which indicates a Windows-based OS.
*
* You do not need to modify this method.
*
* @return true if the OS is Windows, false otherwise
*/
private static boolean isWindows() {
String os = System.getProperty("os.name").toLowerCase();
return os.contains("win");
}
/**
* Creates a hidden file in the specified parent folder.
*
* The filename MUST start with a dot (.).
*
* On Unix-like systems, files prefixed with a dot (.) are treated as hidden.
* On Windows, this method also sets the DOS "hidden" file attribute.
*
* You do not need to modify this method, but you SHOULD use it when creating hidden files
* for your tests. This will make sure that your tests work on both Windows and UNIX-like systems.
*
* @param parentFolder the directory in which to create the hidden file
* @param filename the name of the hidden file; must start with a dot (.)
* @return a File object representing the created hidden file
* @throws IOException if an I/O error occurs during file creation or attribute setting
* @throws IllegalArgumentException if the filename does not start with a dot (.)
*/
private static File createHiddenFile(File parentFolder, String filename) throws IOException {
if(!filename.startsWith(".")) {
throw new IllegalArgumentException("Hidden files/folders must start with a '.'");
}
File hidden = new File(parentFolder, filename);
hidden.createNewFile();
if(isWindows()) {
Path path = Paths.get(hidden.toURI());
Files.setAttribute(path, "dos:hidden", Boolean.TRUE, LinkOption.NOFOLLOW_LINKS);
}
return hidden;
}
@Test
public void testPrintTree_ExactOutput_WithCustomPrintStream(@TempDir File tempDir) throws IOException {
// Build the example directory structure:
// myFolder/
// .hidden.txt
// Apple.txt
// banana.txt
// Documents/
// images/
// Cat.png
// cat.png
// Dog.png
// notes.txt
// README.md
// zebra.txt
// Create "myFolder"
File myFolder = new File(tempDir, "myFolder");
assertTrue(myFolder.mkdir(), "myFolder should be created");
// Create visible files in myFolder
File apple = new File(myFolder, "Apple.txt");
File banana = new File(myFolder, "banana.txt");
File zebra = new File(myFolder, "zebra.txt");
apple.createNewFile();
banana.createNewFile();
zebra.createNewFile();
// Create a hidden file in myFolder
createHiddenFile(myFolder, ".hidden.txt");
// Create subdirectory "Documents" in myFolder
File documents = new File(myFolder, "Documents");
assertTrue(documents.mkdir(), "Documents directory should be created");
// Create files in Documents
File readme = new File(documents, "README.md");
File notes = new File(documents, "notes.txt");
readme.createNewFile();
notes.createNewFile();
// Create subdirectory "images" in Documents
File images = new File(documents, "images");
assertTrue(images.mkdir(), "images directory should be created");
// Create files in images
File cat = new File(images, "cat.png");
File dog = new File(images, "Dog.png");
cat.createNewFile();
dog.createNewFile();
// Set up TruffulaOptions with showHidden = false and useColor = true
TruffulaOptions options = new TruffulaOptions(myFolder, false, true);
// Capture output using a custom PrintStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(baos);
// Instantiate TruffulaPrinter with custom PrintStream
TruffulaPrinter printer = new TruffulaPrinter(options, printStream);
// Call printTree (output goes to printStream)
printer.printTree();
// Retrieve printed output
String output = baos.toString();
String nl = System.lineSeparator();
// Build expected output with exact colors and indentation
ConsoleColor reset = ConsoleColor.RESET;
ConsoleColor white = ConsoleColor.WHITE;
ConsoleColor purple = ConsoleColor.PURPLE;
ConsoleColor yellow = ConsoleColor.YELLOW;
StringBuilder expected = new StringBuilder();
expected.append(white).append("myFolder/").append(nl).append(reset);
expected.append(purple).append(" Apple.txt").append(nl).append(reset);
expected.append(purple).append(" banana.txt").append(nl).append(reset);
expected.append(purple).append(" Documents/").append(nl).append(reset);
expected.append(yellow).append(" images/").append(nl).append(reset);
expected.append(white).append(" cat.png").append(nl).append(reset);
expected.append(white).append(" Dog.png").append(nl).append(reset);
expected.append(yellow).append(" notes.txt").append(nl).append(reset);
expected.append(yellow).append(" README.md").append(nl).append(reset);
expected.append(purple).append(" zebra.txt").append(nl).append(reset);
// Assert that the output matches the expected output exactly
assertEquals(expected.toString(), output);
}
@Test
public void testWave4OutputWorkingCorrectly(@TempDir File tempDir) throws IOException{
File myFolder = new File(tempDir, "myFolder");
assertTrue(myFolder.mkdir(), "myFolder should be created");
// Create visible files in myFolder
File apple = new File(myFolder, "Apple.txt");
File banana = new File(myFolder, "banana.txt");
File zebra = new File(myFolder, "zebra.txt");
apple.createNewFile();
banana.createNewFile();
zebra.createNewFile();
TruffulaOptions options = new TruffulaOptions(myFolder, true, false);
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(output);
TruffulaPrinter printer = new TruffulaPrinter(options, ps);
printer.printTree();
String result = output.toString();
assertTrue(result.contains("Apple.txt"));
assertTrue(result.contains("banana.txt"));
assertTrue(result.contains("zebra.txt"));
}
@Test
public void testWave5HiddenFiles(@TempDir File tempDir) throws IOException{
File myFolder = new File(tempDir, "myFolder");
assertTrue(myFolder.mkdir(), "myFolder should be created");
// Create visible files in myFolder
File apple = new File(myFolder, "visible.txt");
assertTrue(apple.createNewFile());
createHiddenFile(myFolder, ".hidden.txt");
TruffulaOptions options = new TruffulaOptions(myFolder, false, false);
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(output);
TruffulaPrinter printer = new TruffulaPrinter(options, ps);
printer.printTree();
String result = output.toString();
assertTrue(result.contains("visible.txt"));
assertTrue(!result.contains(".hidden.txt"));
}
@Test
public void testWave6ColorFiles(@TempDir File tempDir) throws IOException{
File root = new File(tempDir, "root");
assertTrue(root.mkdir());
File subDir1 = new File(root, "subDir1");
assertTrue(subDir1.mkdir());
File subDir2 = new File(subDir1, "subDir2");
assertTrue(subDir2.mkdir());
TruffulaOptions options = new TruffulaOptions(root, true, true); // useColor = true
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(output);
TruffulaPrinter printer = new TruffulaPrinter(options, ps);
printer.printTree();
String result = output.toString();
// Check colors cycle correctly: WHITE, PURPLE, YELLOW
assertTrue(result.contains(ConsoleColor.WHITE.getCode()), "Level 0 should be white");
assertTrue(result.contains(ConsoleColor.PURPLE.getCode()), "Level 1 should be purple");
assertTrue(result.contains(ConsoleColor.YELLOW.getCode()), "Level 2 should be yellow");
}
}