func TestIf(t *testing.T) { questionExample := NewInputQuestion(expr.NewStringLiteral("What was the selling price?"), vari.NewVarDecl(vari.NewVarID("sellingPrice"), expr.NewIntegerType())) ifBodyExample := NewStmtList([]interfaces.Question{questionExample}, []interfaces.Conditional{}) ifCondExample := expr.NewBoolLiteral(true) ifExample := NewIf(ifCondExample, ifBodyExample) assert.True(t, util.AreStmtListsEqual(ifExample.Body(), ifBodyExample)) assert.Equal(t, ifExample.Condition(), ifCondExample) assert.Equal(t, true, ifExample.EvalConditionAsBool(nil)) }
func (suite *TypeCheckerTestSuite) TestThatCorrectFormYieldsNoErrorsOrWarnings() { firstQuestion := stmt.NewInputQuestion(expr.NewStringLiteral("Did you sell a house in 2010?"), vari.NewVarDecl(vari.NewVarID("hasSoldHouse"), expr.NewBoolType())) firstQuestionBody := stmt.NewInputQuestion(expr.NewStringLiteral("What was the selling price?"), vari.NewVarDecl(vari.NewVarID("sellingPrice"), expr.NewIntegerType())) ifBody := stmt.NewStmtList([]interfaces.Question{firstQuestionBody}, []interfaces.Conditional{}) elseBody := stmt.NewStmtList([]interfaces.Question{firstQuestionBody}, []interfaces.Conditional{}) ifExample := stmt.NewIfElse(expr.NewBoolLiteral(true), ifBody, elseBody) exampleBody := stmt.NewStmtList([]interfaces.Question{firstQuestion}, []interfaces.Conditional{ifExample}) exampleForm := stmt.NewForm(vari.NewVarID("TestForm"), exampleBody) suite.testThatNumberOfErrorsOfPassedTypeArePresentForForm(exampleForm, errors.TypeCheckError{}, 0) }
func TestFormIfElse(t *testing.T) { exampleFormInput := "form TestForm { \"Did you sell a house in 2010?\" hasSoldHouse: boolean if (true) { \"What was the selling price?\" sellingPrice: integer } else { \"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{}) elseBodyOutput := stmt.NewStmtList([]interfaces.Question{firstQuestionBodyInput}, []interfaces.Conditional{}) ifOutput := stmt.NewIfElse(expr.NewBoolLiteral(true), ifBodyOutput, elseBodyOutput) exampleBodyOutput := stmt.NewStmtList([]interfaces.Question{firstQuestionOutput}, []interfaces.Conditional{ifOutput}) exampleOutputForm := stmt.NewForm(vari.NewVarID("TestForm"), exampleBodyOutput) testStmtParse(t, exampleFormInput, exampleOutputForm) }
func TestStmtList(t *testing.T) { questionExample := NewInputQuestion(expr.NewStringLiteral("Did you sell a house in 2010?"), vari.NewVarDecl(vari.NewVarID("hasSoldHouse"), expr.NewBoolType())) questionListExample := []interfaces.Question{questionExample} ifExample := NewIf(expr.NewBoolLiteral(true), NewEmptyStmtList()) conditionalListExample := []interfaces.Conditional{ifExample} stmtListExample := NewStmtList(questionListExample, conditionalListExample) assert.Equal(t, len(stmtListExample.Questions()), len(questionListExample)) assert.Equal(t, len(stmtListExample.Conditionals()), len(conditionalListExample)) }
// VisitBoolType creates a GUI element for booleans when question type is boolean func (this *QuestionTypeToGUIElementVisitor) VisitBoolType(bo interfaces.BoolType, context interface{}) interface{} { var UIEntity ui.Control checkbox := createCheckboxConditional(this.disabled) checkbox.OnToggled(func(*ui.Checkbox) { log.WithFields(log.Fields{"value": checkbox.Checked()}).Debug("Checkbox value changed") if this.callback != nil { this.callback(expr.NewBoolLiteral(checkbox.Checked()), nil) } }) UIEntity = checkbox return UIEntity }
func TestIfElse(t *testing.T) { ifQuestionExample := NewInputQuestion(expr.NewStringLiteral("Did you sell a house in 2010?"), vari.NewVarDecl(vari.NewVarID("hasSoldHouse"), expr.NewBoolType())) ifBodyExample := NewStmtList([]interfaces.Question{ifQuestionExample}, []interfaces.Conditional{}) ifCondExample := expr.NewBoolLiteral(true) elseQuestionExample := NewInputQuestion(expr.NewStringLiteral("What was the selling price?"), vari.NewVarDecl(vari.NewVarID("sellingPrice"), expr.NewIntegerType())) elseBodyExample := NewStmtList([]interfaces.Question{elseQuestionExample}, []interfaces.Conditional{}) ifElseExample := NewIfElse(ifCondExample, ifBodyExample, elseBodyExample) assert.True(t, util.AreStmtListsEqual(ifElseExample.IfBody(), ifBodyExample)) assert.True(t, util.AreStmtListsEqual(ifElseExample.ElseBody(), elseBodyExample)) assert.Equal(t, ifElseExample.Condition(), ifCondExample) assert.Equal(t, true, ifElseExample.EvalConditionAsBool(nil)) }
func (suite *TypeCheckerTestSuite) TestInvalidOperandsCheckerForInvalidUnaryOperationWithInt() { exampleExpr := expr.NewNeg(expr.NewBoolLiteral(true)) suite.testThatNumberOfErrorsOfPassedTypeArePresentForExpr(exampleExpr, errors.OperandWithUnexpectedTypeError{}, 1) }
func (suite *TypeCheckerTestSuite) TestInvalidOperandsCheckerForDifferentOperandEvalTypes() { exampleExpr := expr.NewSub(expr.NewBoolLiteral(true), expr.NewIntegerLiteral(10)) suite.testThatNumberOfErrorsOfPassedTypeArePresentForExpr(exampleExpr, errors.OperandWithUnexpectedTypeError{}, 1) }
func NewBoolLiteralNode(value bool, sourcePosInfo attrib) (interfaces.Expr, error) { expr := expr.NewBoolLiteral(value) expr.SetSourceInfo(sourcePosInfo.(token.Pos)) return expr, nil }