func (suite *TypeCheckerTestSuite) TestNonBoolConditionalChecker() { exampleQuestion := stmt.NewInputQuestion(expr.NewStringLiteral("Did you sell a house in 2010?"), vari.NewVarDecl(vari.NewVarID("hasSoldHouse"), expr.NewBoolType())) exampleIf := stmt.NewIf(expr.NewIntegerLiteral(10), stmt.NewStmtList([]interfaces.Question{exampleQuestion}, []interfaces.Conditional{})) exampleBody := stmt.NewStmtList([]interfaces.Question{}, []interfaces.Conditional{exampleIf}) exampleForm := stmt.NewForm(vari.NewVarID("TestForm"), exampleBody) suite.testThatNumberOfErrorsOfPassedTypeArePresentForForm(exampleForm, errors.NonBooleanConditionError{}, 1) }
func (suite *TypeCheckerTestSuite) TestCyclicReferenceCheckerIfConditionRefersToBody() { questionExample := stmt.NewInputQuestion(expr.NewStringLiteral("Did you sell a house in 2010?"), vari.NewVarDecl(vari.NewVarID("hasSoldHouse"), expr.NewBoolType())) ifBodyExample := stmt.NewStmtList([]interfaces.Question{questionExample}, []interfaces.Conditional{}) ifExample := stmt.NewIf(expr.NewVarExpr(vari.NewVarID("hasSoldHouse")), ifBodyExample) exampleFormBody := stmt.NewStmtList([]interfaces.Question{}, []interfaces.Conditional{ifExample}) exampleForm := stmt.NewForm(vari.NewVarID("TestForm"), exampleFormBody) suite.testThatNumberOfErrorsOfPassedTypeArePresentForForm(exampleForm, errors.CyclicDependencyError{}, 1) }
func TestFormIf(t *testing.T) { exampleFormInput := "form TestForm { \"Did you sell a house in 2010?\" hasSoldHouse: boolean if (true) { \"What was the selling price?\" sellingPrice: integer } }" firstQuestionOutput := stmt.NewInputQuestion(expr.NewStringLiteral("Did you sell a house in 2010?"), vari.NewVarDecl(vari.NewVarID("hasSoldHouse"), expr.NewBoolType())) firstQuestionBodyInput := stmt.NewInputQuestion(expr.NewStringLiteral("What was the selling price?"), vari.NewVarDecl(vari.NewVarID("sellingPrice"), expr.NewIntegerType())) ifBodyOutput := stmt.NewStmtList([]interfaces.Question{firstQuestionBodyInput}, []interfaces.Conditional{}) ifOutput := stmt.NewIf(expr.NewBoolLiteral(true), ifBodyOutput) exampleBodyOutput := stmt.NewStmtList([]interfaces.Question{firstQuestionOutput}, []interfaces.Conditional{ifOutput}) exampleOutputForm := stmt.NewForm(vari.NewVarID("TestForm"), exampleBodyOutput) testStmtParse(t, exampleFormInput, exampleOutputForm) }
func NewIfNode(cond attrib, body attrib, sourcePosInfo attrib) (interfaces.If, error) { stmt := stmt.NewIf(cond.(interfaces.Expr), body.(stmt.StmtList)) stmt.SetSourceInfo(sourcePosInfo.(token.Pos)) return stmt, nil }