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 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 TestFormWithNonEmptyContent(t *testing.T) { identifier := vari.NewVarID("TestForm") questionExample := NewInputQuestion(expr.NewStringLiteral("What was the selling price?"), vari.NewVarDecl(vari.NewVarID("sellingPrice"), expr.NewIntegerType())) questionsListExample := []interfaces.Question{questionExample} stmtListExample := NewStmtList(questionsListExample, []interfaces.Conditional{}) exampleForm := NewForm(identifier, stmtListExample) assert.Equal(t, len(exampleForm.Content().Questions()), 1) assert.True(t, util.AreStmtListsEqual(exampleForm.Content(), stmtListExample)) }
func testStmtParse(t *testing.T, stmtAsString string, formFixture interfaces.Form) stmt.Form { lex := lexer.NewLexer([]byte(stmtAsString)) parser := parser.NewParser() parseResult, err := parser.Parse(lex) if err != nil { t.Fatalf("Encountered fatal error during parse: %s", err) } if parsedForn, parseResultIsForm := parseResult.(stmt.Form); parseResultIsForm { if firstFormIdentifier, secondFormIdentifier := parsedForn.Identifier(), formFixture.Identifier(); firstFormIdentifier != secondFormIdentifier { t.Errorf("Form identifiers not equal: %s and %s", firstFormIdentifier, secondFormIdentifier) } if !util.AreStmtListsEqual(parsedForn.Content(), formFixture.Content()) { t.Errorf("Form content not equal: %v and %v", parsedForn, formFixture) } } else { t.Fatalf("Parse result is not form") } return parseResult.(stmt.Form) }