Skip to content

Commit

Permalink
indentList + test refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Carleslc committed Feb 3, 2022
1 parent f958b0a commit 7b883dc
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 382 deletions.
2 changes: 1 addition & 1 deletion Simple-Configuration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<artifactId>Simple-YAML-Parent</artifactId>
<groupId>me.carleslc.simpleyaml</groupId>
<version>1.7.2</version>
<version>1.7.3</version>
</parent>
<artifactId>Simple-Configuration</artifactId>
<groupId>me.carleslc</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ public ConfigurationOptions copyDefaults(final boolean value) {

/**
* Gets how much spaces should be used to indent each line.
* <p>
* The minimum value this may be is 2, and the maximum is 9.
*
* @return How much to indent by
*/
Expand All @@ -110,8 +108,6 @@ public int indent() {

/**
* Sets how much spaces should be used to indent each line.
* <p>
* The minimum value this may be is 2, and the maximum is 9.
*
* @param value New indent
* @return This object, for chaining
Expand All @@ -126,13 +122,14 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ConfigurationOptions)) return false;
ConfigurationOptions that = (ConfigurationOptions) o;
return pathSeparator == that.pathSeparator &&
return indent == that.indent &&
pathSeparator == that.pathSeparator &&
copyDefaults == that.copyDefaults &&
Objects.equals(configuration, that.configuration);
}

@Override
public int hashCode() {
return Objects.hash(configuration, pathSeparator, copyDefaults);
return Objects.hash(indent, pathSeparator, copyDefaults, configuration);
}
}
8 changes: 4 additions & 4 deletions Simple-Yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
<parent>
<artifactId>Simple-YAML-Parent</artifactId>
<groupId>me.carleslc.simpleyaml</groupId>
<version>1.7.2</version>
<version>1.7.3</version>
</parent>
<artifactId>Simple-Yaml</artifactId>
<groupId>me.carleslc</groupId>
<name>Simple-Yaml</name>
<version>1.7.2</version>
<version>1.7.3</version>

<dependencies>
<dependency>
<groupId>me.carleslc</groupId>
<artifactId>Simple-Configuration</artifactId>
<version>1.7.2</version>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.29</version>
<version>1.30</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,15 @@ public String saveToString() throws IOException {
}

protected String dump() {
this.yamlOptions.setIndent(this.options().indent());
this.yamlOptions.setIndicatorIndent(this.options().indent());
this.yamlOptions.setAllowUnicode(this.options().isUnicode());

this.yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

this.yamlOptions.setIndent(this.options().indent());
this.yamlOptions.setIndicatorIndent(this.options().indentList());
this.yamlOptions.setIndentWithIndicator(true);

String dump = this.yaml.dump(this.getValues(false));

if (dump.equals(YamlConfiguration.BLANK_CONFIG)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
* Various settings for controlling the input and output of a {@link YamlConfiguration}
*
* @author Bukkit
* @author Carlos Lazaro Costa (added indentList option)
* @see <a href="https://github.com/Bukkit/Bukkit/tree/master/src/main/java/org/bukkit/configuration/file/YamlConfigurationOptions.java">Bukkit Source</a>
*/
public class YamlConfigurationOptions extends FileConfigurationOptions {

private int indent = 2;
private int indentList = 2;

protected YamlConfigurationOptions(final YamlConfiguration configuration) {
super(configuration);
Expand Down Expand Up @@ -47,6 +48,14 @@ public YamlConfigurationOptions copyHeader(final boolean value) {
return this;
}

/**
* Sets how much spaces should be used to indent each line.
* <p>
* The minimum value this may be is 2, and the maximum is 9.
*
* @param value New indent
* @return This object, for chaining
*/
@Override
public YamlConfigurationOptions indent(final int value) {
Validate.isTrue(value >= 2, "Indent must be at least 2 characters");
Expand All @@ -56,17 +65,42 @@ public YamlConfigurationOptions indent(final int value) {
return this;
}

/**
* Gets how much spaces should be used to indent each list element, in addition to the line indent.
*
* @return the list elements indentation
*/
public int indentList() {
return this.indentList;
}

/**
* Sets how much spaces should be used to indent each list element, in addition to the line indent.
* <p>
* The minimum value this may be is 0, and the maximum is the same as the {@link YamlConfigurationOptions#indent()}.
*
* @param value New list indentation
* @return This object, for chaining
*/
public YamlConfigurationOptions indentList(final int value) {
Validate.isTrue(value >= 0, "List indent must be at least 0 characters");
Validate.isTrue(value <= this.indent(), "List indent cannot be greater than the indent");

this.indentList = value;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof YamlConfigurationOptions)) return false;
if (!super.equals(o)) return false;
YamlConfigurationOptions that = (YamlConfigurationOptions) o;
return indent == that.indent;
return indentList == that.indentList;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), indent);
return Objects.hash(super.hashCode(), indentList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,26 @@ void indent() {
);
}

@Test
void indentList() {
final YamlConfiguration configuration = new YamlConfiguration();
final YamlConfigurationOptions options = new YamlConfigurationOptions(configuration);

MatcherAssert.assertThat(
"Default indentList is not 2!",
options.indentList(),
new IsEqual<>(2)
);

options.indentList(0);

MatcherAssert.assertThat(
"List indent has not changed!",
options.indentList(),
new IsEqual<>(0)
);
}

@Test
void charset() {
final YamlConfiguration configuration = new YamlConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,7 @@ void saveToString() throws IOException {
final InputStreamOf stream = new InputStreamOf(
new ResourceOf("test.yml"));
final YamlConfiguration configuration = YamlConfiguration.loadConfiguration(stream);
final String content = "test:\n" +
" number: 5\n" +
" string: Hello world\n" +
" boolean: true\n" +
" list:\n" +
" - Each\n" +
" - word\n" +
" - will\n" +
" - be\n" +
" - in\n" +
" - a\n" +
" - separated\n" +
" - entry\n" +
"math:\n" +
" pi: 3.141592653589793\n" +
"timestamp:\n" +
" canonicalDate: 2020-07-04T13:18:04.458Z\n" +
" formattedDate: 04/07/2020 15:18:04\n";
final String content = YamlFileTest.testContent();

MatcherAssert.assertThat(
"Couldn't get the content of the file!",
Expand All @@ -82,7 +65,7 @@ void loadFromString() throws InvalidConfigurationException {
final InputStreamOf stream = new InputStreamOf(
new ResourceOf("test.yml"));
final YamlConfiguration configuration = YamlConfiguration.loadConfiguration(stream);
final String newcontent = "test:\n" +
final String newContent = "test:\n" +
" number: 10\n" +
" string: Hello world!\n" +
" boolean: false\n" +
Expand Down Expand Up @@ -123,7 +106,7 @@ void loadFromString() throws InvalidConfigurationException {
"a", "separated", "entry"
)
);
configuration.loadFromString(newcontent);
configuration.loadFromString(newContent);
MatcherAssert.assertThat(
"Couldn't load the input stream!",
configuration.getInt("test.number"),
Expand Down Expand Up @@ -214,30 +197,13 @@ void convertMapsToSections() {

@Test
void parseHeader() {
final String content = "# test123\n" +
"test:\n" +
" number: 5\n" +
" string: Hello world\n" +
" boolean: true\n" +
" list:\n" +
" - Each\n" +
" - word\n" +
" - will\n" +
" - be\n" +
" - in\n" +
" - a\n" +
" - separated\n" +
" - entry\n" +
"math:\n" +
" pi: 3.141592653589793\n" +
"timestamp:\n" +
" canonicalDate: 2020-07-04T13:18:04.458Z\n" +
" formattedDate: 04/07/2020 15:18:04\n";
final String header = YamlFileTest.testCommentsHeader();
final String content = YamlFileTest.testWithHeader();

MatcherAssert.assertThat(
"Couldn't parse the header of the content!",
YamlConfiguration.parseHeader(content),
new IsEqual<>("# test123")
new IsEqual<>(header)
);
}

Expand Down
Loading

0 comments on commit 7b883dc

Please sign in to comment.