Paragraph extends Job implements Serializable, Cloneable { String extractVariablesFromAngularRegistry(String scriptBody, Map<String, Input> inputs, AngularObjectRegistry angularRegistry) { final String noteId = this.getNote().getId(); final String paragraphId = this.getId(); final Set<String> keys = new HashSet<>(inputs.keySet()); for (String varName : keys) { final AngularObject paragraphScoped = angularRegistry.get(varName, noteId, paragraphId); final AngularObject noteScoped = angularRegistry.get(varName, noteId, null); final AngularObject angularObject = paragraphScoped != null ? paragraphScoped : noteScoped; if (angularObject != null) { inputs.remove(varName); final String pattern = "[$][{]\\s*" + varName + "\\s*(?:=[^}]+)?[}]"; scriptBody = scriptBody.replaceAll(pattern, angularObject.get().toString()); } } return scriptBody; } @VisibleForTesting  Paragraph();  Paragraph(String paragraphId, Note note, JobListener listener,
                   NoteInterpreterLoader replLoader);  Paragraph(Note note, JobListener listener, NoteInterpreterLoader replLoader); String getText(); void setText(String newText); void setEffectiveText(String effectiveText); String getEffectiveText(); AuthenticationInfo getAuthenticationInfo(); void setAuthenticationInfo(AuthenticationInfo authenticationInfo); String getTitle(); void setTitle(String title); void setNote(Note note); Note getNote(); boolean isEnabled(); String getRequiredReplName(); static String getRequiredReplName(String text); String getScriptBody(); static String getScriptBody(String text); NoteInterpreterLoader getNoteReplLoader(); Interpreter getRepl(String name); Interpreter getCurrentRepl(); List<InterpreterCompletion> completion(String buffer, int cursor); void setNoteReplLoader(NoteInterpreterLoader repls); InterpreterResult getResult(); @Override int progress(); @Override Map<String, Object> info(); Map<String, Object> getConfig(); void setConfig(Map<String, Object> config); void setReturn(InterpreterResult value, Throwable t); @Override Object clone(); final GUI settings; }
@Test public void should_extract_variable_from_angular_object_registry() throws Exception { final String noteId = "noteId"; final AngularObjectRegistry registry = mock(AngularObjectRegistry.class); final Note note = mock(Note.class); final Map<String, Input> inputs = new HashMap<>(); inputs.put("name", null); inputs.put("age", null); inputs.put("job", null); final String scriptBody = "My name is ${name} and I am ${age=20} years old. " + "My occupation is ${ job = engineer | developer | artists}"; final Paragraph paragraph = new Paragraph(note, null, null); final String paragraphId = paragraph.getId(); final AngularObject nameAO = AngularObjectBuilder.build("name", "DuyHai DOAN", noteId, paragraphId); final AngularObject ageAO = AngularObjectBuilder.build("age", 34, noteId, null); when(note.getId()).thenReturn(noteId); when(registry.get("name", noteId, paragraphId)).thenReturn(nameAO); when(registry.get("age", noteId, null)).thenReturn(ageAO); final String expected = "My name is DuyHai DOAN and I am 34 years old. " + "My occupation is ${ job = engineer | developer | artists}"; final String actual = paragraph.extractVariablesFromAngularRegistry(scriptBody, inputs, registry); verify(registry).get("name", noteId, paragraphId); verify(registry).get("age", noteId, null); assertEquals(actual, expected); }