### Question:
GrammarPreprocessorImpl implements GrammarPreprocessor { @Nonnull @Override public ResultWithErrors<ExtendedDocument> buildExtendedDocument( @Nonnull String uri, @Nonnull String code, @Nonnull Deque<CopybookUsage> copybookStack, @Nonnull CopybookProcessingMode copybookProcessingMode) { Lexer lexer = new CobolPreprocessorLexer(CharStreams.fromString(code)); lexer.removeErrorListeners(); BufferedTokenStream tokens = new CommonTokenStream(lexer); CobolPreprocessor parser = new CobolPreprocessor(tokens); parser.removeErrorListeners(); RuleContext startRule = parser.startRule(); ParseTreeWalker walker = new ParseTreeWalker(); GrammarPreprocessorListener listener = listenerFactory.create(uri, tokens, copybookStack, copybookProcessingMode); walker.walk(listener, startRule); return new ResultWithErrors<>(listener.getResult(), listener.getErrors()); } @Inject  GrammarPreprocessorImpl(GrammarPreprocessorListenerFactory listenerFactory); @Nonnull @Override ResultWithErrors<ExtendedDocument> buildExtendedDocument(
      @Nonnull String uri,
      @Nonnull String code,
      @Nonnull Deque<CopybookUsage> copybookStack,
      @Nonnull CopybookProcessingMode copybookProcessingMode);  }### Answer:
@Test void testBuildingExtendedDocument() { GrammarPreprocessorListenerImpl listener = mock(GrammarPreprocessorListenerImpl.class); GrammarPreprocessorListenerFactory factory = mock(GrammarPreprocessorListenerFactory.class); List<SyntaxError> errors = emptyList(); ArrayDeque<CopybookUsage> copybookStack = new ArrayDeque<>(); NamedSubContext copybooks = new NamedSubContext(); copybooks.addUsage(CPYNAME, CPYNAME_POS.toLocation()); DocumentMapping mainMapping = new DocumentMapping(List.of(CPYNAME_POS, COPY_POS), Map.of(0, 2)); DocumentMapping cpyMapping = new DocumentMapping(List.of(RESULT_POS), Map.of()); ExtendedDocument expectedDocument = new ExtendedDocument( RESULT, copybooks, Map.of(DOCUMENT, mainMapping, CPYNAME, cpyMapping), Map.of()); when(factory.create( eq(DOCUMENT), any(BufferedTokenStream.class), eq(copybookStack), eq(PROCESSING_MODE))) .thenReturn(listener); when(listener.getErrors()).thenReturn(errors); when(listener.getResult()).thenReturn(expectedDocument); GrammarPreprocessor preprocessor = new GrammarPreprocessorImpl(factory); ResultWithErrors<ExtendedDocument> extendedDocument = preprocessor.buildExtendedDocument(DOCUMENT, TEXT, copybookStack, PROCESSING_MODE); verify(factory) .create(eq(DOCUMENT), any(BufferedTokenStream.class), eq(copybookStack), eq(PROCESSING_MODE)); assertEquals(RESULT, extendedDocument.getResult().getText()); assertEquals(copybooks, extendedDocument.getResult().getCopybooks()); assertEquals(mainMapping, expectedDocument.getDocumentMapping().get(DOCUMENT)); assertEquals(cpyMapping, expectedDocument.getDocumentMapping().get(CPYNAME)); assertEquals(errors, extendedDocument.getErrors()); }