Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#753): Fix The Problem With The First Digit Naming #755

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.xembly.Directive;

Expand All @@ -40,6 +41,11 @@
*/
public final class DirectivesValues implements Iterable<Directive> {

/**
* Pattern to remove all non-digits from the beginning of a string.
*/
private static final Pattern DIGITS = Pattern.compile("^[0-9]");

/**
* Tuple name.
*/
Expand Down Expand Up @@ -90,10 +96,20 @@ public Iterator<Directive> iterator() {
private String nonEmptyName() {
final String result;
if (this.name.isEmpty()) {
result = UUID.randomUUID().toString().toLowerCase(Locale.getDefault());
result = DirectivesValues.randomName();
} else {
result = this.name;
}
return result;
}

/**
* Generate random name.
* @return Random name.
*/
private static String randomName() {
return DirectivesValues.DIGITS.matcher(
UUID.randomUUID().toString().toLowerCase(Locale.getDefault())
).replaceAll("a");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
package org.eolang.jeo.representation.directives;

import com.jcabi.matchers.XhtmlMatchers;
import org.eolang.jeo.representation.xmir.XmlNode;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.xembly.ImpossibleModificationException;
import org.xembly.Xembler;
Expand All @@ -46,4 +49,19 @@ void convertsToXmir() throws ImpossibleModificationException {
)
);
}

@RepeatedTest(100)
void generatesRandomNameWithoutFirstDigit() {
MatcherAssert.assertThat(
"We expect that the name of the sequence will be generated randomly and will not start with a digit",
new XmlNode(
new Xembler(
new DirectivesValues("", "some-value")
).xmlQuietly()
).attribute("name").orElseThrow(
() -> new IllegalStateException("Name attribute is absent")
),
Matchers.matchesRegex("^[^0-9].*")
);
}
}
Loading