Improve toString() for Profiles.of() implementation
Prior to this commit, the toString() implementation in ProfilesParser
(which is the internal implementation of Profiles.of()) concatenated
profile expressions with " or ".
For example, the string representation of
Profiles.of("spring & framework", "java | kotlin") was
"spring & framework or java | kotlin".
This commit improves the toString() implementation by wrapping
individual profile expressions in parentheses and concatenating them
with " | ".
For example, the string representation from the previous example is now
"(spring & framework) | (java | kotlin)".
This makes it easier to read (for example, when debugging) and
comprehend.
As an additional benefit, the result of invoking toString() can even be
used as a logically equivalent composite profile expression, though
that is not the primary goal of this change.
Closes gh-30374
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -272,6 +272,12 @@ class ProfilesTests {
|
||||
assertComplexExpression(profiles);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ofComplexExpressionEnclosedInParentheses() {
|
||||
Profiles profiles = Profiles.of("((spring & framework) | (spring & java))");
|
||||
assertComplexExpression(profiles);
|
||||
}
|
||||
|
||||
private void assertComplexExpression(Profiles profiles) {
|
||||
assertThat(profiles.matches(activeProfiles("spring"))).isFalse();
|
||||
assertThat(profiles.matches(activeProfiles("spring", "framework"))).isTrue();
|
||||
@@ -291,8 +297,27 @@ class ProfilesTests {
|
||||
assertThat(Profiles.of("spring")).hasToString("spring");
|
||||
assertThat(Profiles.of("(spring & framework) | (spring & java)")).hasToString("(spring & framework) | (spring & java)");
|
||||
assertThat(Profiles.of("(spring&framework)|(spring&java)")).hasToString("(spring&framework)|(spring&java)");
|
||||
assertThat(Profiles.of("spring & framework", "java | kotlin")).hasToString("spring & framework or java | kotlin");
|
||||
assertThat(Profiles.of("java | kotlin", "spring & framework")).hasToString("java | kotlin or spring & framework");
|
||||
assertThat(Profiles.of("spring & framework", "java | kotlin")).hasToString("(spring & framework) | (java | kotlin)");
|
||||
assertThat(Profiles.of("java | kotlin", "spring & framework")).hasToString("(java | kotlin) | (spring & framework)");
|
||||
assertThat(Profiles.of("java | kotlin", "spring & framework", "cat | dog")).hasToString("(java | kotlin) | (spring & framework) | (cat | dog)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringGeneratesValidCompositeProfileExpression() {
|
||||
assertThatToStringGeneratesValidCompositeProfileExpression("spring");
|
||||
assertThatToStringGeneratesValidCompositeProfileExpression("(spring & kotlin) | (spring & java)");
|
||||
assertThatToStringGeneratesValidCompositeProfileExpression("spring & kotlin", "spring & java");
|
||||
assertThatToStringGeneratesValidCompositeProfileExpression("spring & kotlin", "spring & java", "cat | dog");
|
||||
}
|
||||
|
||||
private static void assertThatToStringGeneratesValidCompositeProfileExpression(String... profileExpressions) {
|
||||
Profiles profiles = Profiles.of(profileExpressions);
|
||||
assertThat(profiles.matches(activeProfiles("spring", "java"))).isTrue();
|
||||
assertThat(profiles.matches(activeProfiles("kotlin"))).isFalse();
|
||||
|
||||
Profiles compositeProfiles = Profiles.of(profiles.toString());
|
||||
assertThat(compositeProfiles.matches(activeProfiles("spring", "java"))).isTrue();
|
||||
assertThat(compositeProfiles.matches(activeProfiles("kotlin"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user