-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRStringExample.java
More file actions
59 lines (41 loc) · 1.86 KB
/
RStringExample.java
File metadata and controls
59 lines (41 loc) · 1.86 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
package com.holi;
import java.util.Map;
import org.junit.Test;
import static com.holi.Context.from;
import static com.holi.utils.MappedVariables.with;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Created by selonj on 16-9-5.
*/
public class RStringExample {
@Test public void capitalize() throws Exception {
RString string = RString.valueOf("java string");
RString result = string.replace("(?<=^|\\s+)(\\w)", (groups) -> groups.get(1).toUpperCase());
assertThat(result.toString(), equalTo("Java String"));
}
@Test public void toCamelCase() throws Exception {
RString string = RString.valueOf("java string");
RString result = string.replace("(?:\\s+)(\\w)", (groups) -> groups.get(1).toUpperCase());
assertThat(result.toString(), equalTo("javaString"));
}
@Test public void toSnakeCase() throws Exception {
RString string = RString.valueOf("java string");
RString result = string.replace("(?:\\s+)(\\w)", (groups) -> "_" + groups.get(1));
assertThat(result.toString(), equalTo("java_string"));
}
@Test public void replaceVariableWithMappedValue() throws Exception {
RString string = RString.valueOf("{user}@example.com");
Map<String, Object> variables = with("user=foo");
assertThat(string.replace(from(variables)).toString(), equalTo("foo@example.com"));
}
@Test public void replaceVariableWithArrayValues() throws Exception {
RString string = RString.valueOf("{user}@example.com");
Object[] variables = {"foo"};
assertThat(string.replace(from(variables)).toString(), equalTo("foo@example.com"));
}
@Test public void dropsEscapeCharAndIgnoringReplaceIfVariableExpressionStartsWithEscapeChar() throws Exception {
RString string = RString.valueOf("\\{user\\}");
assertThat(string.replace(from("anything")).toString(), equalTo("{user}"));
}
}