probo/lib/models/models_test.go

80 lines
1.4 KiB
Go
Raw Normal View History

2023-07-10 13:23:46 +02:00
package models
import (
2023-11-13 21:01:12 +01:00
"fmt"
"reflect"
2023-07-10 13:23:46 +02:00
"testing"
"github.com/remogatto/prettytest"
)
type testSuite struct {
prettytest.Suite
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
2023-11-21 15:12:13 +01:00
new(groupTestSuite),
2023-07-10 13:23:46 +02:00
)
}
2023-11-13 21:01:12 +01:00
func (t *testSuite) TestQuizFromMarkdown() {
markdown := `Question text (1).
Question text (2).
Question text with #tag1 #tag2 (3).
* Answer 1
* Answer 2
* Answer 3
* Answer 4`
expectedQuiz := &Quiz{
Question: &Question{Text: "Question text (1).\n\nQuestion text (2).\n\nQuestion text with #tag1 #tag2 (3)."},
Answers: []*Answer{
{Text: "Answer 1"},
{Text: "Answer 2"},
{Text: "Answer 3"},
{Text: "Answer 4"},
},
CorrectPos: 0,
}
2023-11-20 14:14:09 +01:00
q := new(Quiz)
err := MarkdownToQuiz(q, markdown)
2023-11-13 21:01:12 +01:00
t.Nil(err, fmt.Sprintf("Quiz should be parsed without errors: %v", err))
if !t.Failed() {
2023-11-20 14:14:09 +01:00
t.True(reflect.DeepEqual(q, expectedQuiz), fmt.Sprintf("Expected %+v got %+v", expectedQuiz, q))
2023-11-13 21:01:12 +01:00
}
}
func (t *testSuite) TestMarkdownFromQuiz() {
quiz := &Quiz{
Question: &Question{Text: "Newly created question text."},
Answers: []*Answer{
{Text: "Answer 1"},
{Text: "Answer 2"},
{Text: "Answer 3"},
{Text: "Answer 4"},
},
CorrectPos: 0,
}
md, err := QuizToMarkdown(quiz)
t.Nil(err, fmt.Sprintf("Conversion to markdown should not raise an error: %v", err))
if !t.Failed() {
t.Equal(`Newly created question text.
* Answer 1
* Answer 2
* Answer 3
* Answer 4
`, md)
}
}