MakeConstantSymbol extends AbstractRhsFunctionHandler { @Override public Symbol execute(RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { final SymbolFactory syms = context.getSymbols(); final String prefix = arguments.isEmpty() ? "constant" : Concat.concat(arguments); final StringSymbol result = syms.findString(prefix); if(result == null) { return syms.createString(prefix); } return syms.generateUniqueString(prefix, counter); } MakeConstantSymbol(); @Override Symbol execute(RhsFunctionContext context, List<Symbol> arguments);  }
@Test public void testNoArgsCall() throws Exception { MakeConstantSymbol mcs = new MakeConstantSymbol(); StringSymbol result = (StringSymbol) mcs.execute(rhsFuncContext, new ArrayList<Symbol>()); assertNotNull(result); assertEquals("constant", result.getValue()); result = (StringSymbol) mcs.execute(rhsFuncContext, new ArrayList<Symbol>()); assertNotNull(result); assertEquals("constant1", result.getValue()); result = (StringSymbol) mcs.execute(rhsFuncContext, new ArrayList<Symbol>()); assertNotNull(result); assertEquals("constant2", result.getValue()); }
@Test public void testWithArgsCall() throws Exception { List<Symbol> args = Symbols.asList(syms, "s", "1", "hello-", "goodbye-"); MakeConstantSymbol mcs = new MakeConstantSymbol(); final StringSymbol result1 = (StringSymbol) mcs.execute(rhsFuncContext, args); assertEquals("s1hello-goodbye-", result1.getValue()); final StringSymbol result2 = (StringSymbol) mcs.execute(rhsFuncContext, args); assertEquals("s1hello-goodbye-1", result2.getValue()); final StringSymbol result3 = (StringSymbol) mcs.execute(rhsFuncContext, args); assertEquals("s1hello-goodbye-2", result3.getValue()); }