Skip to content

Commit c4c0264

Browse files
committed
Add string tests ... (#27)
- String API - String Comparison - Escape Sequences - String Formatting
1 parent 16adec7 commit c4c0264

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package pl.mperor.lab.java;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.ByteArrayInputStream;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.Scanner;
9+
10+
public class ConsoleSupportTest {
11+
12+
@Test
13+
public void testSimulatedSystemInScanner() {
14+
byte[] bytes = """
15+
Hello
16+
World""".getBytes(StandardCharsets.UTF_8);
17+
18+
var original = System.in;
19+
System.setIn(new ByteArrayInputStream(bytes));
20+
21+
try (Scanner sc = new Scanner(System.in)) {
22+
Assertions.assertEquals("Hello", sc.next());
23+
Assertions.assertEquals("World", sc.next());
24+
}
25+
System.setIn(original);
26+
}
27+
28+
}

basics/src/test/java/pl/mperor/lab/java/data/type/NumberSystemsTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,37 @@
33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
55

6-
import java.util.regex.Pattern;
7-
86
public class NumberSystemsTest {
97

108
@Test
119
public void testBinary() {
1210
String raw = "1011";
1311
int radix = 2;
14-
Assertions.assertTrue(Pattern.compile("[01]+").asMatchPredicate().test(raw));
12+
Assertions.assertTrue(raw.matches("[01]+"));
1513
Assertions.assertEquals(0b1011, Integer.parseInt(raw, radix));
1614
}
1715

1816
@Test
1917
public void testOctal() {
2018
String raw = "13";
2119
int radix = 8;
22-
Assertions.assertTrue(Pattern.compile("[0-7]+").asMatchPredicate().test(raw));
20+
Assertions.assertTrue(raw.matches("[0-7]+"));
2321
Assertions.assertEquals(013, Integer.parseInt(raw, radix));
2422
}
2523

2624
@Test
2725
public void testHex() {
2826
String raw = "1a";
2927
int radix = 16;
30-
Assertions.assertTrue(Pattern.compile("[0-9A-Fa-f]+").asMatchPredicate().test(raw));
28+
Assertions.assertTrue(raw.matches("[0-9A-Fa-f]+"));
3129
Assertions.assertEquals(0x1a, Integer.parseInt(raw, radix));
3230
}
3331

3432
@Test
3533
public void testDecimal() {
3634
String raw = "111";
3735
int radix = 10;
38-
Assertions.assertTrue(Pattern.compile("[0-9]+").asMatchPredicate().test(raw));
36+
Assertions.assertTrue(raw.matches("[0-9]+"));
3937
Assertions.assertEquals(111, Integer.parseInt(raw, radix));
4038
}
4139

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package pl.mperor.lab.java.data.type;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.List;
7+
import java.util.Locale;
8+
9+
public class StringTest {
10+
11+
@Test
12+
public void testStringCommonlyUsedMethods() {
13+
String abc = "abc";
14+
Assertions.assertEquals(3, abc.length());
15+
Assertions.assertTrue(abc.contains("b"));
16+
Assertions.assertTrue(!abc.isBlank() && !abc.isEmpty());
17+
Assertions.assertTrue(abc.startsWith("a") && abc.endsWith("c"));
18+
Assertions.assertEquals("a*c", abc.replace('b', '*'));
19+
Assertions.assertEquals("bc", abc.substring(1, abc.length()));
20+
Assertions.assertArrayEquals(new String[]{"google", "com"}, "google.com".split("\\."));
21+
Assertions.assertTrue("-1".matches("^-?\\d+$"), "Like isInteger(...)");
22+
}
23+
24+
@Test
25+
public void testStringImmutability() {
26+
String original = "| Fix";
27+
String modified = original
28+
.replace('|', ' ')
29+
.strip()
30+
.concat("ed")
31+
.toUpperCase();
32+
33+
Assertions.assertEquals("| Fix", original);
34+
Assertions.assertEquals("FIXED", modified);
35+
}
36+
37+
@Test
38+
public void testCompareStrings() {
39+
String qwerty = "qwerty";
40+
String newQwerty = new String(qwerty);
41+
Assertions.assertTrue("qwerty" == qwerty, "String Pool exits!");
42+
Assertions.assertFalse(qwerty == newQwerty);
43+
Assertions.assertTrue(qwerty == newQwerty.intern(), "'intern()' returns string from the pool");
44+
Assertions.assertTrue(qwerty.equals(newQwerty));
45+
Assertions.assertTrue(qwerty.equalsIgnoreCase("Qwerty"));
46+
}
47+
48+
@Test
49+
public void testEscapeSequences() {
50+
String doubleQuote = "\"Title\"";
51+
String tabulator = "\tabc";
52+
String backslash = "C:\\Windows";
53+
String newLine = "...\n***";
54+
String carriageReturn = "[ ] 0%\r[==========] 100%";
55+
String unicodeCode = "Copyright \u00A9";
56+
List.of(doubleQuote, tabulator, backslash, newLine, carriageReturn, unicodeCode)
57+
.forEach(System.out::println);
58+
}
59+
60+
@Test
61+
public void testStringFormatting() {
62+
// Format specifier: %[flags][width][.precision]conversion
63+
// Escaping %%
64+
String format = "%s | %d | %#x | %.2f | %b | %c | %% |%n";
65+
String formatted = String.format(Locale.UK, format, "Str", 1, 10, 1.23, true, '.');
66+
var separator = System.getProperty("line.separator");
67+
Assertions.assertEquals("Str | 1 | 0xa | 1.23 | true | . | % |" + separator, formatted);
68+
}
69+
70+
}

jdk/src/test/java/pl/mperor/lab/java/Java4.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public void testRegex() {
5151

5252
private String getTestHtml() {
5353
return """
54-
\
5554
<!DOCTYPE html>
5655
<html lang="en">
5756
<head>

0 commit comments

Comments
 (0)