LiteralProgramPoint implements ProgramPoint { @Override public Optional<ProgramState> execute(ProgramState state) { ExpressionStack stack = state.getStack(); ExpressionStack stackAfterExecution = stack.apply(newStack -> { SymbolicValue value = SpecialSymbolicValue.UNDEFINED; if (tree.is(Kind.NULL_LITERAL)) { value = SpecialSymbolicValue.NULL; } else if (tree.is(Kind.NUMERIC_LITERAL, Kind.STRING_LITERAL, Kind.BOOLEAN_LITERAL, Kind.REGULAR_EXPRESSION_LITERAL)) { value = LiteralSymbolicValue.get((LiteralTree) tree); } else if (tree.is(Kind.ARRAY_LITERAL)) { pop(newStack, ((ArrayLiteralTree) tree).elements().size()); value = new SymbolicValueWithConstraint(Constraint.ARRAY); } else if (tree.is(Kind.OBJECT_LITERAL)) { popObjectLiteralProperties(newStack, (ObjectLiteralTree) tree); value = new SymbolicValueWithConstraint(Constraint.OTHER_OBJECT); } else if (tree.is(Kind.TEMPLATE_LITERAL)) { pop(newStack, ((TemplateLiteralTree) tree).expressions().size()); value = new SymbolicValueWithConstraint(Constraint.STRING_PRIMITIVE); } newStack.push(value); }); return Optional.of(state.withStack(stackAfterExecution)); } LiteralProgramPoint(Tree tree); static boolean originatesFrom(Tree element); @Override Optional<ProgramState> execute(ProgramState state);  }
@Test public void numeric_literal() throws Exception { Tree tree = tree("42", Kind.NUMERIC_LITERAL); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(ProgramState.emptyState()); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.POSITIVE_NUMBER_PRIMITIVE); }
@Test public void template_literal() throws Exception { int operandsNumber = 3; Tree tree = tree("`${a} ${b} ${c}`", Kind.TEMPLATE_LITERAL); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.STRING_PRIMITIVE); }
@Test public void string_literal() throws Exception { Tree tree = tree("'str'", Kind.STRING_LITERAL); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(ProgramState.emptyState()); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.TRUTHY_STRING_PRIMITIVE); }
@Test public void boolean_literal() throws Exception { Tree tree = tree("true", Kind.BOOLEAN_LITERAL); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(ProgramState.emptyState()); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.TRUE); }
@Test public void null_literal() throws Exception { Tree tree = tree("null", Kind.NULL_LITERAL); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(ProgramState.emptyState()); assertThat(resultingValue(newState)).isEqualTo(SpecialSymbolicValue.NULL); assertThat(stackSize(newState)).isEqualTo(1); }
@Test public void undefined() throws Exception { Tree tree = tree("undefined", Kind.IDENTIFIER_REFERENCE); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(ProgramState.emptyState()); assertThat(resultingValue(newState)).isEqualTo(SpecialSymbolicValue.UNDEFINED); assertThat(stackSize(newState)).isEqualTo(1); }
@Test public void array_literal() throws Exception { int operandsNumber = 4; Tree tree = tree("[a, b, c, d]", Kind.ARRAY_LITERAL); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.ARRAY); }
@Test public void object_literal() throws Exception { Tree tree; int operandsNumber = 1; tree = tree("x = {...a}", Kind.OBJECT_LITERAL); Optional<ProgramState> newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); tree = tree("x = {a: b}", Kind.OBJECT_LITERAL); newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); operandsNumber = 2; tree = tree("x = {'str': b}", Kind.OBJECT_LITERAL); newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); tree = tree("x = {42: b}", Kind.OBJECT_LITERAL); newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); tree = tree("x = { method(){} }", Kind.OBJECT_LITERAL); newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); tree = tree("x = { a }", Kind.OBJECT_LITERAL); newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); tree = tree("x = { a, b }", Kind.OBJECT_LITERAL); newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); operandsNumber = 3; tree = tree("x = { a, b: foo(), ... rest }", Kind.OBJECT_LITERAL); newState = new LiteralProgramPoint(tree).execute(createProgramState(operandsNumber)); assertThat(resultingConstraint(newState)).isEqualTo(Constraint.OTHER_OBJECT); }