Skip to content

Commit

Permalink
Improve documentation and test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Moderocky committed Apr 11, 2024
1 parent dac0fac commit ebbea3c
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/main/java/ch/njol/skript/structures/StructUsing.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public boolean init(Literal<?> @NotNull [] arguments, int pattern, ParseResult r
private void enableExperiment(String name) {
this.experiment = Skript.experiments().find(name.trim());
switch (experiment.phase()) {
case MAINSTREAM:
Skript.warning("The experimental feature '" + name + "' is now included by default and is no longer required.");
break;
case DEPRECATED:
Skript.warning("The experimental feature '" + name + "' is deprecated and may be removed in future versions.");
break;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/ch/njol/skript/test/runner/TestFeatures.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
public enum TestFeatures implements Experiment {
EXAMPLE_FEATURE("example feature", LifeCycle.STABLE),
DEPRECATED_FEATURE("deprecated feature", LifeCycle.DEPRECATED),
TEST_FEATURE("test", LifeCycle.EXPERIMENTAL, "test[ing]", "fizz[ ]buzz")
;

Expand Down
75 changes: 61 additions & 14 deletions src/main/java/org/skriptlang/skript/lang/experiment/Experiment.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ch.njol.skript.patterns.PatternCompiler;
import ch.njol.skript.patterns.SkriptPattern;
import org.jetbrains.annotations.ApiStatus;

import java.util.Objects;

Expand All @@ -31,10 +32,22 @@
*/
public interface Experiment {

@ApiStatus.Internal
static Experiment unknown(String text) {
return new UnmatchedExperiment(text);
}

/**
* A constant experiment provider (designed for the use of addons).
* @param codeName The debug 'code name' of this feature.
* @param phase The stability of this feature.
* @param patterns What the user may write to match the feature. Defaults to the codename if not set.
* @return An experiment flag.
*/
static Experiment constant(String codeName, LifeCycle phase, String... patterns) {
return new ConstantExperiment(codeName, phase, patterns);
}

/**
* @return The patterns that can be written in the `using` structure.
*/
Expand Down Expand Up @@ -71,21 +84,36 @@ default boolean matches(String text) {
}

/**
* The dummy class for an unmatched experiment.
* This is something that was 'used' by a file but was never registered with Skript.
* These are kept so that they *can* be tested for (e.g. by a third-party extension that uses a post-registration
* experiment system).
* A class for constant experiments.
*/
class UnmatchedExperiment implements Experiment {
class ConstantExperiment implements Experiment {

private final String codeName;
private final String[] patterns;
private final SkriptPattern compiledPattern;
private final LifeCycle phase;

UnmatchedExperiment(String codeName) {
ConstantExperiment(String codeName, LifeCycle phase) {
this(codeName, phase, new String[0]);
}

ConstantExperiment(String codeName, LifeCycle phase, String... patterns) {
this.codeName = codeName;
this.patterns = new String[] {codeName};
this.compiledPattern = PatternCompiler.compile(codeName);
this.phase = phase;
switch (patterns.length) {
case 0:
this.patterns = new String[]{codeName};
this.compiledPattern = PatternCompiler.compile(codeName);
break;
case 1:
this.patterns = patterns;
this.compiledPattern = PatternCompiler.compile(patterns[0]);
break;
default:
this.patterns = patterns;
this.compiledPattern = PatternCompiler.compile('(' + String.join("|", patterns) + ')');
break;
}
}

@Override
Expand All @@ -100,19 +128,14 @@ public String[] patterns() {

@Override
public LifeCycle phase() {
return LifeCycle.UNKNOWN;
return phase;
}

@Override
public SkriptPattern compiledPattern() {
return compiledPattern;
}

@Override
public boolean isKnown() {
return false;
}

@Override
public boolean matches(String text) {
return codeName.equals(text);
Expand All @@ -132,3 +155,27 @@ public int hashCode() {
}

}

/**
* The dummy class for an unmatched experiment.
* This is something that was 'used' by a file but was never registered with Skript.
* These are kept so that they *can* be tested for (e.g. by a third-party extension that uses a post-registration
* experiment system).
*/
class UnmatchedExperiment extends ConstantExperiment {

UnmatchedExperiment(String codeName) {
super(codeName, LifeCycle.UNKNOWN);
}

@Override
public LifeCycle phase() {
return LifeCycle.UNKNOWN;
}

@Override
public boolean isKnown() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAddon;
import org.jetbrains.annotations.NotNull;

import java.util.LinkedHashSet;
import java.util.Set;
Expand All @@ -29,7 +30,7 @@
*
* @author moderocky
*/
public class ExperimentManager {
public class ExperimentManager implements Experimented {

private final Skript skript;
private final Set<Experiment> experiments;
Expand All @@ -39,7 +40,12 @@ public ExperimentManager(Skript skript) {
this.experiments = new LinkedHashSet<>();
}

public Experiment find(String text) {
/**
* Finds an experiment matching this name. If none exist, an 'unknown' one will be created.
* @param text The text provided by the user.
* @return An experiment.
*/
public @NotNull Experiment find(String text) {
if (experiments.isEmpty())
return Experiment.unknown(text);
for (Experiment experiment : experiments) {
Expand All @@ -48,6 +54,9 @@ public Experiment find(String text) {
return Experiment.unknown(text);
}

/**
* @return All currently-registered experiments.
*/
public Experiment[] registered() {
return experiments.toArray(new Experiment[0]);
}
Expand All @@ -63,4 +72,14 @@ public void register(SkriptAddon addon, Experiment experiment) {
this.experiments.add(experiment);
}

@Override
public boolean hasExperiment(Experiment experiment) {
return experiments.contains(experiment);
}

@Override
public boolean hasExperiment(String featureName) {
return this.find(featureName).isKnown();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* Something that can have experimental features enabled for.
* The only intended implementation of this is the {@link org.skriptlang.skript.lang.script.Script},
* however it is left open for configuration files, etc. that may use this functionality in future.
* however it is left open for configuration files, etc. that may use this functionality in the future.
*
* @author moderocky
*/
Expand All @@ -41,7 +41,7 @@ public interface Experimented {
* @return Whether this has a feature with the given name.
*/
default boolean hasExperiment(String featureName) {
return this.hasExperiment(Skript.experiments().find(featureName));
return Skript.experiments().find(featureName).isKnown();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public enum LifeCycle {
* Scripts will report a deprecation warning on load if a deprecated feature is used.
*/
DEPRECATED(true),
/**
* Represents a feature that was previously opt-in (or experimental) but is now a part of the default set.
* I.e. it no longer needs to be enabled using a feature flag.
* This will provide a little note to the user on load informing them they no longer need to
* use this feature flag.
*/
MAINSTREAM(true),
/**
* Represents an unregistered, unknown feature.
* This occurs when a user tags a script as {@code using X}, where {@code X} is not a registered
Expand Down
1 change: 1 addition & 0 deletions src/test/skript/tests/syntaxes/structures/StructUsing.sk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using example feature
using fizz buzz
using deprecated feature

test "using":
assert the current script is using "example feature" with "using feature failed"
Expand Down

0 comments on commit ebbea3c

Please sign in to comment.