Skip to content
Open
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 @@ -66,6 +66,47 @@ public record Style(
/** The AOSP-compliant configuration. */
public static final Style AOSP = builder().aosp().build();

/**
* Returns the predefined styles, in the order they were declared when {@code Style} was an
* enum.
*
* <p>Retained for compatibility with callers compiled against the enum version of {@code
* Style}.
*/
public static Style[] values() {
return new Style[] {GOOGLE, AOSP};
}

/**
* Returns the predefined style with the given name ({@code "GOOGLE"} or {@code "AOSP"}).
*
* <p>Retained for compatibility with callers compiled against the enum version of {@code
* Style}.
*
* @throws IllegalArgumentException if there is no predefined style with the given name
* @throws NullPointerException if {@code name} is null
*/
public static Style valueOf(String name) {
return switch (requireNonNull(name, "Name is null")) {
case "GOOGLE" -> GOOGLE;
case "AOSP" -> AOSP;
default ->
throw new IllegalArgumentException(
"No enum constant " + Style.class.getCanonicalName() + "." + name);
};
}

/**
* Returns the name of the predefined style this style is based on ({@code "GOOGLE"} or {@code
* "AOSP"}), such that {@code valueOf(GOOGLE.name()) == GOOGLE}.
*
* <p>Retained for compatibility with callers compiled against the enum version of {@code
* Style}.
*/
public String name() {
return importOrder().name();
}

/**
* Returns the visual column width of a tab stop.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2026 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package com.google.googlejavaformat.java;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;

import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for {@link JavaFormatterOptions}. */
@RunWith(JUnit4.class)
public final class JavaFormatterOptionsTest {

@Test
public void styleValues() {
assertThat(Style.values()).asList().containsExactly(Style.GOOGLE, Style.AOSP).inOrder();
}

@Test
public void styleValueOf() {
assertThat(Style.valueOf("GOOGLE")).isSameInstanceAs(Style.GOOGLE);
assertThat(Style.valueOf("AOSP")).isSameInstanceAs(Style.AOSP);
}

@Test
public void styleValueOf_invalid() {
assertThrows(IllegalArgumentException.class, () -> Style.valueOf("google"));
assertThrows(IllegalArgumentException.class, () -> Style.valueOf("PALANTIR"));
assertThrows(NullPointerException.class, () -> Style.valueOf(null));
}

@Test
public void styleName() {
for (Style style : Style.values()) {
assertThat(Style.valueOf(style.name())).isSameInstanceAs(style);
}
assertThat(Style.AOSP.toBuilder().maxLineLength(120).useTabs(true).build().name())
.isEqualTo("AOSP");
}
}
Loading