Skip to content

Commit 5f3a987

Browse files
authored
Merge pull request #8 from QuickWrite/feature/add-tests
Add tests and fix the bugs that were found while creating all of these tests.
2 parents fc8725b + acdc9da commit 5f3a987

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1647
-474
lines changed
Lines changed: 8 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
package net.quickwrite.fluent4j.ast;
22

3-
import net.quickwrite.fluent4j.ast.placeable.AttributeReference;
4-
import net.quickwrite.fluent4j.ast.placeable.SelectExpression;
5-
import net.quickwrite.fluent4j.ast.placeable.TermReference;
6-
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
7-
import net.quickwrite.fluent4j.ast.placeable.base.FluentSelectable;
83
import net.quickwrite.fluent4j.exception.FluentParseException;
9-
import net.quickwrite.fluent4j.exception.FluentSelectException;
10-
import net.quickwrite.fluent4j.parser.FluentParser;
114
import net.quickwrite.fluent4j.util.StringSlice;
125
import net.quickwrite.fluent4j.util.StringSliceUtil;
136
import net.quickwrite.fluent4j.util.args.FluentArgs;
147
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
15-
import org.apache.commons.lang3.tuple.ImmutablePair;
16-
import org.apache.commons.lang3.tuple.Pair;
178

18-
import java.util.ArrayList;
199
import java.util.LinkedList;
2010
import java.util.List;
2111

@@ -51,11 +41,17 @@ private List<FluentElement> parse(final StringSlice content) {
5141

5242
while (!content.isBigger()) {
5343
if (content.getChar() == '{') {
54-
elements.add(getPlaceable(content));
44+
content.increment();
45+
elements.add(StringSliceUtil.getPlaceable(content));
46+
47+
if (content.getChar() != '}') {
48+
throw new FluentParseException('}', content.getCharUTF16(), content.getAbsolutePosition());
49+
}
50+
content.increment();
5551
} else {
5652
FluentTextElement text = getText(content, firstLine);
5753

58-
if (text != null) {
54+
if (text != null && !text.isEmpty()) {
5955
elements.add(text);
6056
}
6157
}
@@ -88,107 +84,10 @@ private FluentTextElement getText(final StringSlice content, final boolean first
8884
return new FluentTextElement(content.substring(start, content.getPosition()), whitespace);
8985
}
9086

91-
private FluentPlaceable getPlaceable(final StringSlice content) {
92-
content.increment();
93-
StringSliceUtil.skipWhitespaceAndNL(content);
94-
95-
FluentPlaceable placeable = StringSliceUtil.getExpression(content);
96-
97-
boolean canSelect = placeable instanceof FluentSelectable;
98-
99-
StringSliceUtil.skipWhitespaceAndNL(content);
100-
101-
if (canSelect && content.getChar() == '-') {
102-
content.increment();
103-
if (content.getChar() != '>') {
104-
throw new FluentParseException("->", "-" + content.getCharUTF16(), content.getAbsolutePosition());
105-
}
106-
107-
content.increment();
108-
109-
StringSliceUtil.skipWhitespaceAndNL(content);
110-
111-
List<FluentVariant> fluentVariants = new ArrayList<>();
112-
FluentVariant defaultVariant = null;
113-
114-
while (content.getChar() != '}') {
115-
Pair<FluentVariant, Boolean> variant = getVariant(content);
116-
117-
fluentVariants.add(variant.getLeft());
118-
119-
if (!variant.getRight()) {
120-
continue;
121-
}
122-
123-
if (defaultVariant != null) {
124-
throw new FluentSelectException("Only one variant can be marked as default (*)");
125-
}
126-
127-
defaultVariant = variant.getLeft();
128-
}
129-
130-
if (defaultVariant == null) {
131-
throw new FluentSelectException("Expected one of the variants to be marked as default (*)");
132-
}
133-
134-
placeable = new SelectExpression(placeable, fluentVariants, defaultVariant);
135-
}
136-
137-
StringSliceUtil.skipWhitespaceAndNL(content);
138-
139-
if (content.getChar() != '}') {
140-
throw new FluentParseException('}', content.getCharUTF16(), content.getAbsolutePosition());
141-
}
142-
143-
content.increment();
144-
145-
return placeable;
146-
}
147-
14887
public String getIdentifier() {
14988
return this.identifier;
15089
}
15190

152-
private Pair<FluentVariant, Boolean> getVariant(final StringSlice content) {
153-
StringSliceUtil.skipWhitespaceAndNL(content);
154-
155-
boolean isDefault = false;
156-
157-
if (content.getChar() == '*') {
158-
isDefault = true;
159-
content.increment();
160-
}
161-
162-
if (content.getChar() != '[') {
163-
throw new FluentParseException('[', content.getCharUTF16(), content.getAbsolutePosition());
164-
}
165-
166-
content.increment();
167-
168-
StringSliceUtil.skipWhitespace(content);
169-
170-
StringSlice identifier = getVariantIdentifier(content);
171-
172-
StringSliceUtil.skipWhitespace(content);
173-
174-
if (content.getChar() != ']') {
175-
throw getVariantException(content, identifier.toString(), "]");
176-
}
177-
178-
content.increment();
179-
180-
final Pair<StringSlice, Integer> stringSliceContent = FluentParser.getMessageContent(content,
181-
character -> character == '[' || character == '*' || character == '}');
182-
183-
final FluentAttribute attribute = new FluentAttribute(
184-
identifier,
185-
stringSliceContent.getLeft(),
186-
stringSliceContent.getRight()
187-
);
188-
189-
return new ImmutablePair<>(new FluentVariant(attribute), isDefault);
190-
}
191-
19291
private StringSlice getVariantIdentifier(final StringSlice content) {
19392
char character = content.getChar();
19493
final int start = content.getPosition();
@@ -238,16 +137,4 @@ public String stringValue() {
238137
public List<FluentElement> getElements() {
239138
return this.fluentElements;
240139
}
241-
242-
private FluentParseException getVariantException(final StringSlice content, final String prev, final String expected) {
243-
int start = content.getPosition();
244-
245-
while (content.getChar() != ']' && !content.isBigger()) {
246-
content.increment();
247-
}
248-
249-
return new FluentParseException(expected,
250-
prev + content.substring(start, content.getPosition()).toString(),
251-
content.getAbsolutePosition());
252-
}
253140
}

src/main/java/net/quickwrite/fluent4j/ast/FluentTextElement.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ private String getText(final StringSlice content, final int whitespace) {
6767
return text.toString();
6868
}
6969

70+
public boolean isEmpty() {
71+
return text.isEmpty();
72+
}
73+
7074
@Override
7175
public boolean matches(final DirectFluentBundle bundle, final FluentElement selector) {
7276
return selector.stringValue().equals(this.text);

src/main/java/net/quickwrite/fluent4j/ast/FluentVariant.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.quickwrite.fluent4j.ast;
22

3+
import net.quickwrite.fluent4j.exception.FluentParseException;
34
import net.quickwrite.fluent4j.util.args.FluentArgs;
45
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
56
import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
@@ -21,14 +22,18 @@ public class FluentVariant implements FluentElement {
2122
private final FluentPlaceable identifier;
2223
private final FluentAttribute content;
2324

24-
public FluentVariant(FluentAttribute content) {
25+
public FluentVariant(final FluentAttribute content) {
2526
this.identifier = getIdentifier(content.identifier);
2627
this.content = content;
2728
}
2829

29-
private FluentPlaceable getIdentifier(String slice) {
30+
private FluentPlaceable getIdentifier(final String slice) {
3031
if (Character.isDigit(slice.charAt(0))) {
31-
return NumberLiteral.getNumberLiteral(slice);
32+
try {
33+
return NumberLiteral.getNumberLiteral(slice);
34+
} catch (final NumberFormatException ignored) {
35+
throw new FluentParseException("Expected Number but got \"" + slice + "\"");
36+
}
3237
}
3338

3439
return new StringLiteral(slice);

src/main/java/net/quickwrite/fluent4j/ast/placeable/base/FluentFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public FluentFunction(final String functionName, final FluentArgs arguments) {
2727
throw new FluentParseException("The callee has to be an upper-case identifier or a term");
2828
}
2929

30-
this.arguments = arguments;
30+
this.arguments = arguments != null ? arguments : FluentArgs.EMPTY_ARGS;
3131
}
3232

3333
/**

0 commit comments

Comments
 (0)