private void assertNotRootDescribeBlock(String blockType) { TestDefinitionContext context = assertIsInTestDefinitionContext(blockType); if (context.getCurrentDescribeBlock().equals(context.rootBuilder)) { throw new CuppaException( "'" + blockType + "' must be nested within a 'describe' or 'when' block"); } }
/** * Registers a test function to be run. * * @param behaviour If {@link Behaviour#SKIP} then this test will be skipped. * @param description The description of the test function. * @param function The test function. * @param options The set of options applied to the test. */ public void it( Behaviour behaviour, String description, Optional<TestFunction> function, Options options) { TestDefinitionContext context = assertIsInTestDefinitionContext("it"); assertNotRootDescribeBlock("it"); Test test = new org.forgerock.cuppa.model.TestBuilder() .setBehaviour(behaviour) .setTestClass(context.testClass) .setDescription(description) .setFunction(function) .setOptions(options) .build(); context.getCurrentDescribeBlock().addTest(test); }
/** * Returns a builder for registering a described suite of tests to be run. * * @param type The type of the test block. * @param behaviour If {@link Behaviour#SKIP} then this test will be skipped. * @param description The description of the 'describe' block. * @param options The set of options applied to the test block. */ void testBlock( TestBlockType type, Behaviour behaviour, String description, TestBlockFunction function, Options options) { TestDefinitionContext context = assertIsInTestDefinitionContext("describe"); InternalTestBlockBuilder testBlockBuilder = new InternalTestBlockBuilder(type, behaviour, context.testClass, description, options); context.stack.addLast(testBlockBuilder); try { function.apply(); } finally { context.stack.removeLast(); context.getCurrentDescribeBlock().addTestBlock(testBlockBuilder.build()); } }
/** * Registers a 'afterEach' block to be run. * * @param description The description of the 'afterEach' block. * @param function The 'afterEach' block. */ public void afterEach(String description, HookFunction function) { TestDefinitionContext context = assertIsInTestDefinitionContext("afterEach"); assertNotRootDescribeBlock("afterEach"); context.getCurrentDescribeBlock().addAfterEach(Optional.ofNullable(description), function); }