diff --git a/src/main/java/org/eolang/jeo/representation/directives/DirectivesValues.java b/src/main/java/org/eolang/jeo/representation/directives/DirectivesValues.java index bc08e91e1..16c487116 100644 --- a/src/main/java/org/eolang/jeo/representation/directives/DirectivesValues.java +++ b/src/main/java/org/eolang/jeo/representation/directives/DirectivesValues.java @@ -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; @@ -40,6 +41,11 @@ */ public final class DirectivesValues implements Iterable { + /** + * Pattern to remove all non-digits from the beginning of a string. + */ + private static final Pattern DIGITS = Pattern.compile("^[0-9]"); + /** * Tuple name. */ @@ -90,10 +96,20 @@ public Iterator 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"); + } } diff --git a/src/test/java/org/eolang/jeo/representation/directives/DirectivesValuesTest.java b/src/test/java/org/eolang/jeo/representation/directives/DirectivesValuesTest.java index ea8162780..bbd3f8a74 100644 --- a/src/test/java/org/eolang/jeo/representation/directives/DirectivesValuesTest.java +++ b/src/test/java/org/eolang/jeo/representation/directives/DirectivesValuesTest.java @@ -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; @@ -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].*") + ); + } }