Skip to content

Commit 2922f05

Browse files
committed
Add token field validation & support for numeral field names
1 parent 0acf1b9 commit 2922f05

File tree

2 files changed

+18
-21
lines changed

2 files changed

+18
-21
lines changed

src/main/java/io/github/noncat_lang/Template.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public Template withToken(@NonNull String field, @NonNull Token token) {
3636
if (field.isBlank()) {
3737
throw new IllegalArgumentException("Token field should not be blank/empty");
3838
}
39+
if (!field.matches("[a-zA-Z0-9]+")) {
40+
throw new IllegalArgumentException("Token field does not match pattern [a-zA-Z0-9]+");
41+
}
3942
tokens.put(field, token);
4043
return this;
4144
}
@@ -82,7 +85,7 @@ private Map<String, String> decodeAllToken(Matcher matcher) {
8285
}
8386

8487
private String decodeToken(Entry<String, Token> entry, Matcher matcher) {
85-
return entry.getValue().decode(matcher.group(entry.getKey()));
88+
return entry.getValue().decode(matcher.group("x" + entry.getKey()));
8689
}
8790

8891
private String parseElement(ElementContext element) {
@@ -96,7 +99,7 @@ private String parseArg(ArgContext arg) {
9699
if (token == null) {
97100
throw new MissingTokenException(String.format("Token for field '%s' is missing", id));
98101
}
99-
return String.format("(?<%s>%s)", id, token.getRegex());
102+
return String.format("(?<x%s>%s)", id, token.getRegex());
100103
}
101104

102105
private String getId(ArgContext arg) {

src/test/java/io/github/noncat_lang/TemplateTest.java

+13-19
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ static Stream<Arguments> simpleParams() {
3333
Arguments.of("[a-zA-Z]+", "Hello ${world}!", "world", "LangSec", "Hello LangSec!"),
3434
Arguments.of("[a-zA-Z]+", "Hello {${world}}!", "world", "LangSec", "Hello {LangSec}!"),
3535
Arguments.of("[a-zA-Z]+", "Hello $${world}!", "world", "LangSec", "Hello $LangSec!"),
36-
Arguments.of("[a-zA-Z]+", "Hello ${} ${world}!", "world", "LangSec", "Hello ${} LangSec!"));
36+
Arguments.of("[a-zA-Z]+", "Hello ${} ${world}!", "world", "LangSec", "Hello ${} LangSec!"),
37+
Arguments.of("[a-zA-Z]+", "Hello ${0}!", "0", "LangSec", "Hello LangSec!"));
3738
}
3839

3940
@ParameterizedTest
@@ -144,28 +145,21 @@ void tokenNull() {
144145
assertThatThrownBy(() -> template.withToken("any", token)).isExactlyInstanceOf(NullPointerException.class);
145146
}
146147

147-
@Test
148-
void tokenFieldNull() {
149-
Template template = Template.of("any");
150-
Token token = Token.of("any");
151-
String field = null;
152-
assertThatThrownBy(() -> template.withToken(field, token)).isExactlyInstanceOf(NullPointerException.class);
153-
}
154-
155-
@Test
156-
void tokenFieldEmpty() {
157-
Template template = Template.of("any");
158-
Token token = Token.of("any");
159-
String field = "";
160-
assertThatThrownBy(() -> template.withToken(field, token)).isExactlyInstanceOf(IllegalArgumentException.class);
148+
static Stream<Arguments> invalidTokenFields() {
149+
return Stream.of(
150+
Arguments.of(null, NullPointerException.class, "field is marked non-null but is null"),
151+
Arguments.of("", IllegalArgumentException.class, "Token field should not be blank/empty"),
152+
Arguments.of(" ", IllegalArgumentException.class, "Token field should not be blank/empty"),
153+
Arguments.of("#", IllegalArgumentException.class, "Token field does not match pattern [a-zA-Z0-9]+"));
161154
}
162155

163-
@Test
164-
void tokenFieldBlank() {
156+
@ParameterizedTest
157+
@MethodSource("invalidTokenFields")
158+
void tokenFieldEmpty(String field, Class<Exception> expectedException, String expectedMessage) {
165159
Template template = Template.of("any");
166160
Token token = Token.of("any");
167-
String field = " ";
168-
assertThatThrownBy(() -> template.withToken(field, token)).isExactlyInstanceOf(IllegalArgumentException.class);
161+
assertThatThrownBy(() -> template.withToken(field, token)).isExactlyInstanceOf(expectedException)
162+
.hasMessage(expectedMessage);
169163
}
170164

171165
}

0 commit comments

Comments
 (0)