@Subroutine(value = "format") public static LispString format(LispString formatString, @Optional LispObject... objects) { checkFormatCharacters(formatString.getData()); try { Object[] data = new Object[objects.length]; for (int i = 0; i != objects.length; i++) { LispObject object = objects[i]; data[i] = object instanceof LispNumber ? ((LispNumber) object).getData() : (object instanceof LispString ? ((LispString) object).getData() : object.toString()); } return new LispString(String.format(formatString.getData(), data)); } catch (MissingFormatArgumentException e1) { throw new LispException(JelispBundle.message("few.args.for.format.string")); } catch (UnknownFormatConversionException e2) { char c = e2.getMessage().charAt(e2.getMessage().length() - 2); if (c < 'A') throw new LispException(JelispBundle.message("unexpected.format.string.end")); throw new InvalidFormatOperationException(c); } catch (IllegalFormatConversionException e3) { throw new LispException(JelispBundle.message("format.arg.types.mismatch")); } }
@Test public void testMakeHashTableDefault() { LispObject hashTable = evaluateString("(make-hash-table)"); Assert.assertEquals( new LispHashTable("eql", 65, new LispFloat(0.8), LispSymbol.ourNil, 1.5), hashTable); Assert.assertEquals( "#s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8 data ())", hashTable.toString()); }
@Subroutine("message") public static LispString message(@Optional LispObject... args) { if (args.length == 0) throw new WrongNumberOfArgumentsException("message", 0); LispObject formatObj = args[0]; if (formatObj instanceof LispString) { LispObject[] a = new LispObject[args.length - 1]; System.arraycopy(args, 1, a, 0, args.length - 1); myCurrentMessage = format((LispString) formatObj, a); } else if (formatObj.equals(LispSymbol.ourNil)) { myCurrentMessage = new LispString(LispSymbol.ourNil.toString()); } else { throw new WrongTypeArgumentException("stringp", formatObj); } GlobalEnvironment.echo(myCurrentMessage.getData(), GlobalEnvironment.MessageType.OUTPUT); return myCurrentMessage; }
@Test public void testPropertize() { LispObject s = evaluateString("(setq s (propertize \"hello\" 1 2 3 4))"); Assert.assertEquals("#(\"hello\" 0 5 (3 4 1 2))", s.toString()); }