How to use DeclarationState method of com.greghaskins.spectrum.internal.DeclarationState class

Best Spectrum code snippet using com.greghaskins.spectrum.internal.DeclarationState.DeclarationState

Source:Specification.java Github

copy

Full Screen

...6import static com.greghaskins.spectrum.internal.hooks.BeforeHook.before;7import com.greghaskins.spectrum.Block;8import com.greghaskins.spectrum.ThrowingConsumer;9import com.greghaskins.spectrum.ThrowingSupplier;10import com.greghaskins.spectrum.internal.DeclarationState;11import com.greghaskins.spectrum.internal.Suite;12import com.greghaskins.spectrum.internal.blocks.IdempotentBlock;13import com.greghaskins.spectrum.internal.hooks.EagerLetHook;14import com.greghaskins.spectrum.internal.hooks.Hook;15import com.greghaskins.spectrum.internal.hooks.HookContext.AppliesTo;16import com.greghaskins.spectrum.internal.hooks.HookContext.Precedence;17import com.greghaskins.spectrum.internal.hooks.LetHook;18import org.junit.AssumptionViolatedException;19import java.util.function.Supplier;20public interface Specification {21 /**22 * Declare a test suite that describes the expected behavior of the system in a given context.23 *24 * @param context Description of the context for this suite25 * @param block {@link Block} with one or more calls to {@link #it(String, Block) it} that26 * define each expected behavior27 */28 static void describe(final String context, final Block block) {29 final Suite suite = DeclarationState.instance()30 .getCurrentSuiteBeingDeclared()31 .addSuite(context);32 suite.applyConfigurationFromBlock(block);33 DeclarationState.instance().beginDeclaration(suite, block);34 }35 /**36 * Focus on this specific suite, while ignoring others.37 *38 * @param context Description of the context for this suite39 * @param block {@link Block} with one or more calls to {@link #it(String, Block) it} that40 * define each expected behavior41 * @see #describe(String, Block)42 */43 static void fdescribe(final String context, final Block block) {44 describe(context, with(focus(), block));45 }46 /**47 * Ignore the specific suite.48 *49 * @param context Description of the context for this suite50 * @param block {@link Block} with one or more calls to {@link #it(String, Block) it} that51 * define each expected behavior52 * @see #describe(String, Block)53 */54 static void xdescribe(final String context, final Block block) {55 describe(context, with(ignore(), block));56 }57 /**58 * Declare a spec, or test, for an expected behavior of the system in this suite context.59 *60 * @param behavior Description of the expected behavior61 * @param block {@link Block} that verifies the system behaves as expected and throws a {@link62 * java.lang.Throwable Throwable} if that expectation is not met.63 */64 static void it(final String behavior, final Block block) {65 DeclarationState.instance().getCurrentSuiteBeingDeclared().addSpec(behavior, block);66 }67 /**68 * Declare a pending spec (without a block) that will be ignored.69 *70 * @param behavior Description of the expected behavior71 * @see #xit(String, Block)72 */73 static void it(final String behavior) {74 DeclarationState.instance().getCurrentSuiteBeingDeclared().addSpec(behavior, null).ignore();75 }76 /**77 * Focus on this specific spec, while ignoring others.78 *79 * @param behavior Description of the expected behavior80 * @param block {@link Block} that verifies the system behaves as expected and throws a {@link81 * java.lang.Throwable Throwable} if that expectation is not met.82 * @see #it(String, Block)83 */84 static void fit(final String behavior, final Block block) {85 it(behavior, with(focus(), block));86 }87 /**88 * Mark a spec as ignored so that it will be skipped.89 *90 * @param behavior Description of the expected behavior91 * @param block {@link Block} that will not run, since this spec is ignored.92 * @see #it(String, Block)93 */94 static void xit(final String behavior, final Block block) {95 it(behavior);96 }97 /**98 * Declare a {@link Block} to be run before each spec in the suite.99 *100 * <p>101 * Use this to perform setup actions that are common across tests in the context. If multiple102 * {@code beforeEach} blocks are declared, they will run in declaration order.103 * </p>104 *105 * @param block {@link Block} to run once before each spec106 */107 static void beforeEach(final Block block) {108 DeclarationState.instance().addHook(before(block), AppliesTo.ATOMIC_ONLY, Precedence.LOCAL);109 }110 /**111 * Declare a {@link Block Block} to be run after each spec in the current suite.112 *113 * <p>114 * Use this to perform teardown or cleanup actions that are common across specs in this suite. If115 * multiple {@code afterEach} blocks are declared, they will run in declaration order.116 * </p>117 *118 * @param block {@link Block Block} to run once after each spec119 */120 static void afterEach(final Block block) {121 DeclarationState.instance().addHook(after(block), AppliesTo.ATOMIC_ONLY,122 Precedence.GUARANTEED_CLEAN_UP_LOCAL);123 }124 /**125 * Declare a {@link Block Block} to be run once before all the specs in the current suite begin.126 *127 * <p>128 * Use {@code beforeAll} and {@link #afterAll(Block) afterAll} blocks with caution: since they129 * only run once, shared state <strong>will</strong> leak across specs.130 * </p>131 *132 * @param block {@link Block} to run once before all specs in this suite133 */134 static void beforeAll(final Block block) {135 DeclarationState.instance().addHook(before(new IdempotentBlock(block)), AppliesTo.ATOMIC_ONLY,136 Precedence.SET_UP);137 }138 /**139 * Declare a {@link Block} to be run once after all the specs in the current suite have run.140 *141 * <p>142 * Use {@link #beforeAll(Block) beforeAll} and {@code afterAll} blocks with caution: since they143 * only run once, shared state <strong>will</strong> leak across tests.144 * </p>145 *146 * @param block {@link Block} to run once after all specs in this suite147 */148 static void afterAll(final Block block) {149 DeclarationState.instance().addHook(after(block), AppliesTo.ONCE,150 Precedence.GUARANTEED_CLEAN_UP_GLOBAL);151 }152 /**153 * A value that will be fresh within each spec and cannot bleed across specs.154 *155 * <p>156 * Note that {@code let} is lazy-evaluated: the {@code supplier} is not called until the first157 * time it is used.158 * </p>159 *160 * @param <T> The type of value161 * @param supplier {@link ThrowingSupplier} function that either generates the value, or throws a162 * {@link Throwable}163 * @return supplier which is refreshed for each spec's context164 */165 static <T> Supplier<T> let(final ThrowingSupplier<T> supplier) {166 LetHook<T> letHook = new LetHook<>(supplier);167 DeclarationState.instance().addHook(letHook, AppliesTo.ATOMIC_ONLY, Precedence.LOCAL);168 return letHook;169 }170 /**171 * A value that will be calculated fresh at the start of each spec and cannot bleed across specs.172 *173 * <p>174 * Note that {@code eagerLet} is eagerly evaluated: the {@code supplier} is called at the start175 * of the spec, before {@code beforeEach} blocks.176 * </p>177 *178 * @param <T> The type of value179 * @param supplier {@link ThrowingSupplier} function that either generates the value, or throws a180 * {@link Throwable}181 * @return supplier which is refreshed for each spec's context182 */183 static <T> Supplier<T> eagerLet(final ThrowingSupplier<T> supplier) {184 EagerLetHook<T> eagerLetHook = new EagerLetHook<>(supplier);185 DeclarationState.instance().addHook(eagerLetHook, AppliesTo.ATOMIC_ONLY, Precedence.LOCAL);186 return eagerLetHook;187 }188 /**189 * Define a test context. Alias for {@link #describe}.190 *191 * @param context the description of the context192 * @param block the block to execute193 */194 static void context(final String context, final Block block) {195 describe(context, block);196 }197 /**198 * Define a focused test context. Alias for {@link #fdescribe}.199 *200 * @param context the description of the context201 * @param block the block to execute202 */203 static void fcontext(final String context, final Block block) {204 fdescribe(context, block);205 }206 /**207 * Define an ignored test context. Alias for {@link #xdescribe}.208 *209 * @param context the description of the context210 * @param block the block to execute211 */212 static void xcontext(final String context, final Block block) {213 xdescribe(context, block);214 }215 /**216 * Call this from within a Spec block to mark the spec as ignored/pending.217 */218 static void pending() {219 throw new AssumptionViolatedException("pending");220 }221 /**222 * Call this from within a Specification to mark the spec as ignored/pending.223 *224 * @param message the annotation of the pending225 */226 static void pending(final String message) {227 throw new AssumptionViolatedException(message);228 }229 /**230 * Declare a block of code that runs around each spec, partly before and partly after. You must231 * call {@link com.greghaskins.spectrum.Block#run} inside this Consumer. This code is applied to232 * every spec in the current suite.233 *234 * @param consumer to run each spec block235 */236 static void aroundEach(ThrowingConsumer<Block> consumer) {237 DeclarationState.instance().addHook(Hook.from(consumer), AppliesTo.ATOMIC_ONLY,238 Precedence.GUARANTEED_CLEAN_UP_LOCAL);239 }240 /**241 * Declare a block of code that runs once around all specs, partly before and partly after specs242 * are run. You must call {@link Block#run} inside this Consumer. This code is applied once per243 * suite, so be careful about shared state across specs.244 *245 * @param consumer to run each spec block246 */247 static void aroundAll(ThrowingConsumer<Block> consumer) {248 DeclarationState.instance().addHook(Hook.from(consumer), AppliesTo.ONCE, Precedence.OUTER);249 }250}...

Full Screen

Full Screen

Source:LetHook.java Github

copy

Full Screen

1package com.greghaskins.spectrum.internal.hooks;2import com.greghaskins.spectrum.Block;3import com.greghaskins.spectrum.ThrowingSupplier;4import com.greghaskins.spectrum.Variable;5import com.greghaskins.spectrum.internal.DeclarationState;6import com.greghaskins.spectrum.internal.RunReporting;7import org.junit.runner.Description;8import org.junit.runner.notification.Failure;9/**10 * Implementation of {@code let} as a supplying hook.11 *12 * <p>Using {@code let} allows you to define shared values that can be used by multiple tests,13 * without having to worry about cleaning up the values between tests to prevent shared state in14 * one test from affecting the results of another.15 *16 * <p>Values are lazily initialized and then cached, so a value is not calculated until the first17 * time it is needed in a given test. Subsequent fetches of the value within the same test will18 * return the cached value.19 */20public class LetHook<T> implements SupplyingHook<T> {21 private final ThrowingSupplier<T> supplier;22 private final Variable<T> cachedValue = new Variable<>();23 private boolean isCached;24 public LetHook(final ThrowingSupplier<T> supplier) {25 this.supplier = supplier;26 this.isCached = false;27 }28 @Override29 public void accept(final Description description,30 final RunReporting<Description, Failure> reporting, final Block block)31 throws Throwable {32 try {33 block.run();34 } finally {35 clear();36 }37 }38 @Override39 public T get() {40 assertSpectrumIsRunningTestsNotDeclaringThem();41 if (!this.isCached) {42 this.cachedValue.set(supplier.get());43 this.isCached = true;44 }45 return this.cachedValue.get();46 }47 protected String getExceptionMessageIfUsedAtDeclarationTime() {48 return "Cannot use the value from let() in a suite declaration. "49 + "It may only be used in the context of a running spec.";50 }51 private void clear() {52 this.isCached = false;53 this.cachedValue.set(null);54 }55 /**56 * Will throw an exception if this method happens to be called while Spectrum is still defining57 * tests, rather than executing them. Useful to see if a hook is being accidentally used during58 * definition.59 */60 private void assertSpectrumIsRunningTestsNotDeclaringThem() {61 if (DeclarationState.instance().getCurrentSuiteBeingDeclared() != null) {62 throw new IllegalStateException(getExceptionMessageIfUsedAtDeclarationTime());63 }64 }65}...

Full Screen

Full Screen

Source:DeclarationState.java Github

copy

Full Screen

...5import com.greghaskins.spectrum.internal.hooks.HookContext.AppliesTo;6import com.greghaskins.spectrum.internal.hooks.HookContext.Precedence;7import java.util.ArrayDeque;8import java.util.Deque;9public final class DeclarationState {10 private static final ThreadLocal<DeclarationState> instance =11 ThreadLocal.withInitial(DeclarationState::new);12 public static DeclarationState instance() {13 return instance.get();14 }15 private final Deque<Suite> suiteStack = new ArrayDeque<>();16 private DeclarationState() {}17 public Suite getCurrentSuiteBeingDeclared() {18 return suiteStack.peek();19 }20 private int getCurrentDepth() {21 return suiteStack.size();22 }23 public void beginDeclaration(final Suite suite, final Block definitionBlock) {24 suiteStack.push(suite);25 try {26 definitionBlock.run();27 } catch (final Throwable error) {28 suite.removeAllChildren();29 suite.addSpec("encountered an error", () -> {30 throw error;...

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1package com.greghaskpns.spectrua.internal;2imckage com.greghaskins.spectrum.internal;3import com.greghaskins.spectrum.internal.DeclarationState;import com.greghaskins.spectrum.internal.DeclarationState;4import com.greghaskins.spectrum.internal.DeclarationState;5impublicpstaticovoid main(String[] args) {6 DeclarationState declarationState0 = new DeclarationState();7 declarationState0.declare("test");8 }9}

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1import com.greghaskins.spectrum.internal.DeclarationState;2import com.greghaskins.spectrum.internal.DeclarationState;3pu com.clags 1 {4 public sreghaskins.spectrum.internal.DeclarationState;5public class 1 {6 public static void main(String[] args) {7 DeclarationState declarationState0 = new DeclarationState();8 declarationState0.declare("test");9 }10}

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1import com.greghaskins.spectrum.internal.DeclarationState;2import com.greghaskins.spectrum.internal.DeclarationState;3public class 1 {4 public static void main(String[] args) {5 DeclarationState state = new DeclarationState();6 state.declare("test", () -> {});7 state.declare("test", () -> {});8 }9}10import com.greghaskins.spectrum.internal.DeclarationState;11public class 2 {12 public static void main(String[] args) {13 DeclarationState state = new DeclarationState();14 state.declare("test", () -> {});15 state.declare("test", () -> {});16 }17}18import com.greghaskins.spectrum.internal.DeclarationState;19public class 3 {20 public static void main(String[] args) {21 DeclarationState state = new DeclarationState();22 state.declare("test", () -> {});23 state.declare("test", () -> {});24 }25}26import com.greghaskins.spectrum.internal.DeclarationState;27public class 4 {28 public static void main(String[] args) {29 DeclarationState state = new DeclarationState();30 state.declare("test", () -> {});31 state.declare("test", () -> {});32 }33}34import com.greghaskins.spectrum.internal.DeclarationState;35public class 5 {36 public static void main(String[] args) {37 DeclarationState state = new DeclarationState();38 state.declare("test", () -> {});39 state.declare("test", () -> {});40 }41}42import com.greghaskins.spectrum.internal.DeclarationState;43public class 6 {44 public static void main(String[] args) {

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1package test;2import com.greghaskins.spectrum.internal.DeclarationState;3public class 1 {4 public static void main(String[] args) {5 DeclarationState state = new DeclarationState();6 state.describe("test");7 }8}9package test;10import com.greghaskins.spectrum.internal.DeclarationState;11public class 2 {12 public static void main(String[] args) {13 DeclarationState state = new DeclarationState();14 state.describe("test");15 }16}17package test;18import com.greghaskins.spectrum.internal.DeclarationState;19public class 3 {20 public static void main(String[] args) {21 DeclarationState state = new DeclarationState();22 state.describe("test");23 }24}25package test;26import com.greghaskins.spectrum.internal.DeclarationState;27public class 4 {28 public static void main(String[] args) {29 DeclarationState state = new DeclarationState();30 state.describe("test");31 }32}33package test;34import com.greghaskins.spectrum.internal.DeclarationState;35public class 5 {36 public static void main(String[] args) {37 DeclarationState state = new DeclarationState();38 state.describe("test");39 }40}41package test;42import com.greghaskins.spectrum.internal.DeclarationState;43public class 6 {44 public static void main(String[] args) {45 DeclarationState state = new DeclarationState();46 state.describe("test");47 }48}49package test;50import com.greghaskins.spectrum.internal.DeclarationState;51public class 7 {

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1import com.greghaskins.spectrum.internal.DeclarationState;2import com.greghaskins.spectrum.internal.DeclarationState.State;3import com.greghaskins.spectrum.internal.DeclarationState.State;4public class Example{5 public static void main(String[] args){6 DeclarationState.declare(State.SPECIFICATION, "My first test");7 }8}9import com.greghaskins.spectrum.internal.DeclarationState;10import com.greghaskins.spectrum.internal.DeclarationState.State;11import com.greghaskins.spectrum.internal.DeclarationState.State;12public class Example{13 public static void main(String[] args){14 DeclarationState.declare(State.SPECIFICATION, "My first test");15 }16}17import com.greghaskins.spectrum.internal.DeclarationState;18import com.greghaskins.spectrum.internal.DeclarationState.State;19import com.greghaskins.spectrum.internal.DeclarationState.State;20public class Example{21 public static void main(String[] args){22 DeclarationState.declare(State.SPECIFICATION, "My first test");23 }24}25import com.greghaskins.spectrum.internal.DeclarationState;26import com.greghaskins.spectrum.internal.DeclarationState.State;27import com.greghaskins.spectrum.internal.DeclarationState.State;28public class Example{29 public static void main(String[] args){

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1package com.greghaskins.spectrum.internal;2import com.greghaskins.spectrum.Spectrum;3import com.greghaskins.spectrum.Variable;4import com.greghaskins.spectrum.VariableScope;5public class DeclarationState {6 private static final ThreadLocal<DeclarationState> STATE = new ThreadLocal<>();7 public static DeclarationState get() {8 if (STATE.get() == null) {9 STATE.set(new DeclarationState());10 }11 return STATE.get();12 }13 public static void set(DeclarationState state) {14 STATE.set(state);15 }16 private boolean isInsideDescribe = false;17 private boolean isInsideIt = false;18 private boolean isInsideBefore = false;19 private boolean isInsideAfter = false;20 private boolean isInsideBeforeAll = false;21 private boolean isInsideAfterAll = false;22 private boolean isInsideVariable = false;23 private boolean isInsideVariableScope = false;24 public boolean isInsideDescribe() {25 return this.isInsideDescribe;26 }27 public boolean isInsideIt() {28 return this.isInsideIt;29 }30 public boolean isInsideBefore() {31 return this.isInsideBefore;32 }33 public boolean isInsideAfter() {34 return this.isInsideAfter;35 }36 public boolean isInsideBeforeAll() {37 return this.isInsideBeforeAll;38 }39 public boolean isInsideAfterAll() {40 return this.isInsideAfterAll;41 }42 public boolean isInsideVariable() {43 return this.isInsideVariable;44 }45 public boolean isInsideVariableScope() {46 return this.isInsideVariableScope;47 }48 public void setInsideDescribe(final boolean isInsideDescribe) {49 this.isInsideDescribe = isInsideDescribe;50 }51 public void setInsideIt(final boolean isInsideIt) {52 this.isInsideIt = isInsideIt;53 }54 public roid setInsideBefore(final boolean isInsideBefore) {55 this.isInsideBefore = isInsideBefore;56 }57 public void setInsideAfter(final boolean isInsideAfter) {58 this.isInsideAfter = isInsideAfter;59 }60 public void setInsideBeforeAll(final boolean isInsideBeforeAll) {61 this.isInsideBeforeAll = isInsideBeforeAll;62 }63 public void setInsideAfterAll(final boolean

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1package com.greghaskins.spectrum;2import com.greghaskins.spectrum.internal.DeclarationState;3public class TestDeclarationState {4 public static void main(String[] args) {5 DeclarationState.declareTest("test", "suite", TestDeclarationState.class, () -> {});6 DeclarationState.declareTest("test", "suite", TestDeclarationState.class, () -> {}, () -> {});7 }8}9package com.greghaskins.spectrum;10import com.greghaskins.spectrum.internal.DeclarationState;11public class TestDeclarationState {12 public static void main(String[] args) {13 DeclarationState.declareTest("test", "suite", TestDeclarationState.class, () -> {});14 DeclarationState.declareTest("test", "suite", TestDeclarationState.class, () -> {}, () -> {});15 }16}17package com.greghaskins.spectrum;18import com.greghaskins.spectrum.internal.DeclarationState;19public class TestDeclarationState {20 public static void main(String[] args) {21 DeclarationState.declareTest("test", "suite", TestDeclarationState.class, () -> {});22 DeclarationState.declareTest("test", "suite", TestDeclarationState.class, () -> {}, () -> {});23 }24}25package com.greghaskins.spectrum;26import com.greghaskins.spectrum.internal.DeclarationState;27public class TestDeclarationStatetionState.declare(State.SPECIFICATION, "My first test");28 }29}30import com.greghaskins.spectrum.internal.DeclarationState;31import com.greghaskins.spectrum.internal.DeclarationState.State;32import com.greghaskins.spectrum.internal.DeclarationState.State;33public class Example{34 public static void main(String[] args){35 DeclarationState.declare(State.SPECIFICATION, "My first test");36 }37}

Full Screen

Full Screen

DeclarationState

Using AI Code Generation

copy

Full Screen

1package com.greghaskins.spectrum.internal;2import com.greghaskins.spectrum.Spectrum;3import com.greghaskins.spectrum.Variable;4import com.greghaskins.spectrum.VariableScope;5public class DeclarationState {6 private static final ThreadLocal<DeclarationState> STATE = new ThreadLocal<>();7 public static DeclarationState get() {8 if (STATE.get() == null) {9 STATE.set(new DeclarationState());10 }11 return STATE.get();12 }13 public static void set(DeclarationState state) {14 STATE.set(state);15 }16 private boolean isInsideDescribe = false;17 private boolean isInsideIt = false;18 private boolean isInsideBefore = false;19 private boolean isInsideAfter = false;20 private boolean isInsideBeforeAll = false;21 private boolean isInsideAfterAll = false;22 private boolean isInsideVariable = false;23 private boolean isInsideVariableScope = false;24 public boolean isInsideDescribe() {25 return this.isInsideDescribe;26 }27 public boolean isInsideIt() {28 return this.isInsideIt;29 }30 public boolean isInsideBefore() {31 return this.isInsideBefore;32 }33 public boolean isInsideAfter() {34 return this.isInsideAfter;35 }36 public boolean isInsideBeforeAll() {37 return this.isInsideBeforeAll;38 }39 public boolean isInsideAfterAll() {40 return this.isInsideAfterAll;41 }42 public boolean isInsideVariable() {43 return this.isInsideVariable;44 }45 public boolean isInsideVariableScope() {46 return this.isInsideVariableScope;47 }48 public void setInsideDescribe(final boolean isInsideDescribe) {49 this.isInsideDescribe = isInsideDescribe;50 }51 public void setInsideIt(final boolean isInsideIt) {52 this.isInsideIt = isInsideIt;53 }54 public void setInsideBefore(final boolean isInsideBefore) {55 this.isInsideBefore = isInsideBefore;56 }57 public void setInsideAfter(final boolean isInsideAfter) {58 this.isInsideAfter = isInsideAfter;59 }60 public void setInsideBeforeAll(final boolean isInsideBeforeAll) {61 this.isInsideBeforeAll = isInsideBeforeAll;62 }63 public void setInsideAfterAll(final boolean

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Spectrum automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful