package models import "testing" func TestQuizDefaults(t *testing.T) { zero, negative := 0, -1 for name, hook := range map[string]func(*Quiz) error{ "BeforeSave": func(q *Quiz) error { return q.BeforeSave(nil) }, "BeforeValidate": func(q *Quiz) error { return q.BeforeValidate(nil) }, } { for _, limit := range []*int{&zero, &negative} { q := &Quiz{Title: "Q", MaxAttempts: limit} if err := hook(q); err != nil { t.Fatalf("%s: %v", name, err) } if q.Mode != ModeLearning || q.Navigation != NavigationFree || q.MaxAttempts != nil { t.Errorf("%s with max_attempts %d = %q %q %v, want learning free nil", name, *limit, q.Mode, q.Navigation, q.MaxAttempts) } } two := 2 q := &Quiz{Title: "Q", Mode: ModeTest, Navigation: NavigationOneWay, MaxAttempts: &two} if err := hook(q); err != nil { t.Fatalf("%s: %v", name, err) } if q.Mode != ModeTest || q.Navigation != NavigationOneWay || q.MaxAttempts == nil || *q.MaxAttempts != 2 { t.Errorf("%s changed a test quiz: %q %q %v", name, q.Mode, q.Navigation, q.MaxAttempts) } } }