dev.metalisp.sbt/tests/pattern/questionnaire.lisp

175 lines
8.1 KiB
Common Lisp
Raw Normal View History

2023-08-26 14:28:30 +02:00
(defpackage cl-sbt/tests/questionnaire
(:use
:cl
:cl-sbt
:rove)
(:import-from
:cl-sbt/questionnaire
2023-09-20 13:30:35 +02:00
:choicep
2023-08-26 14:28:30 +02:00
:question
:resolve-input-type
2023-09-19 17:26:35 +02:00
:resolve-input-and-choice
2023-09-15 10:35:45 +02:00
:extract-question-components
2023-08-26 14:28:30 +02:00
:questionnaire))
(in-package :cl-sbt/tests/questionnaire)
2023-09-20 13:30:35 +02:00
(deftest test-choicep
(testing "Test for choicep"
(ok (choicep '(:radio "foo" "bar")))
(ok (choicep '(:text "foo")))
(ok (choicep '(:combo "foo" "bar")))
(ng (choicep '(:radio "foo" :bar)))
(ng (choicep '("foo" :bar :radio)))
(ng (choicep '(() nil nil "foo")))))
2023-08-26 14:28:30 +02:00
(deftest test-resolve-input-type
(testing "Test for resolve-input-type"
(ok (string= "radio" (resolve-input-type "single")))
(ok (string= "checkbox" (resolve-input-type "multiple")))
(ok (string= "text" (resolve-input-type "text")))))
2023-09-19 17:26:35 +02:00
(deftest test-resolve-input-and-choice
2023-08-26 14:28:30 +02:00
(testing "Test for resolve-input-and-choices"
(multiple-value-bind (type choices)
2023-09-19 17:26:35 +02:00
(resolve-input-and-choice '(:radio "A" "B"))
2023-08-26 14:28:30 +02:00
(ok (string= type "radio"))
(ok (equal choices '("A" "B"))))
2023-09-20 13:29:47 +02:00
(ok (signals (resolve-input-and-choice '("A" "B"))))))
2023-09-04 15:03:21 +02:00
(deftest test-question-radio
(let ((result (spinneret:with-html-string
(question "What is your favorite color?"
"favcolor"
(:radio "Red" "Green" "Blue")))))
(testing "Generates correct HTML for question using radio checkable"
(ok (search "for=group-favcolor-red" result))
(ok (search "for=group-favcolor-green" result))
(ok (search "for=group-favcolor-blue" result))
(ok (search "id=group-favcolor-red" result))
(ok (search "id=group-favcolor-green" result))
(ok (search "id=group-favcolor-blue" result))
(ok (search "type=radio" result))
(ok (search "What is your favorite color?" result)))))
(deftest test-question-checkbox
(let ((result (spinneret:with-html-string
(question "What is your favorite color?"
"favcolor"
(:checkbox "Red" "Green" "Blue")))))
(testing "Generates correct HTML for question using checkbox checkable"
(ok (search "type=checkbox" result)))))
(deftest test-question-select
(let ((result (spinneret:with-html-string
(question "What is your favorite color?"
"favcolor"
2023-09-16 20:48:03 +02:00
(:combo "Red" "Green" "Blue")))))
(testing "Generates correct HTML for question using select"
(ok (search "select" result))
(ok (search "option value=red" result))
(ok (search "option value=green" result))
(ok (search "option value=blue" result))
(ok (search "What is your favorite color?" result)))))
2023-09-15 10:35:45 +02:00
(deftest test-extract-question-components
(testing "Test for extract-question-components"
(multiple-value-bind (ask1 group1 choices1)
(extract-question-components '(:ask "What is your favorite color?"
:group "favcolor"
:choices (:radio "Red" "Green" "Blue")))
(ok (string= ask1 "What is your favorite color?"))
(ok (string= group1 "favcolor"))
(ok (equal choices1 '(:radio "Red" "Green" "Blue"))))))
(deftest test-extract-question-components-missing-components
(testing "Test for extract-question-components with missing components"
2023-09-20 13:29:47 +02:00
(ok (signals (extract-question-components '(:ask "What is your favorite color?"
:choices (:radio "Red" "Green" "Blue")))))))
2023-09-15 10:35:45 +02:00
(deftest test-extract-question-components-additional-keys
(testing "Test for extract-question-components with additional keys"
2023-09-20 13:29:47 +02:00
(ok (signals (extract-question-components '(:ask "What is your favorite color?"
:group "favcolor"
:choices (:radio "Red" "Green" "Blue")
:extra "some-extra-info"))))))
2023-09-15 10:35:45 +02:00
2023-09-04 15:21:22 +02:00
(deftest test-create-questionnaire-single
2023-09-04 15:03:21 +02:00
(let ((result (spinneret:with-html-string
2023-09-15 10:35:45 +02:00
(questionnaire "/submit"
(:ask "Your Gender?"
:group "gender"
:choices (:single "Male" "Female" "Non-Binary" "Prefer not to say"))))))
2023-09-04 15:21:22 +02:00
(testing "Generates correct HTML for questionnaire with single choices"
2023-09-04 15:03:21 +02:00
(ok (search "<form class=py-5 action=/submit method=post>" result))
2023-09-04 15:21:22 +02:00
(ok (search "<legend>Your Gender?</legend>" result))
2023-09-06 14:30:51 +02:00
(ok (search "class=form-check-input" result))
(ok (search "name=group-gender" result))
(ok (search "value=male" result))
2023-09-04 15:03:21 +02:00
(ok (search "<hr class=my-4>" result))
(ok (search "<button class=\"btn btn-primary\" type=submit>Submit</button>" result)))))
2023-09-04 15:21:22 +02:00
(deftest test-create-questionnaire-multiple
(let ((result (spinneret:with-html-string
2023-09-15 20:11:43 +02:00
(questionnaire "/submit"
(:ask "Which of the following devices do you regularly use to browse the internet?"
:group "device"
:choices (:multiple "Desktop" "Laptop" "Tablet"))))))
2023-09-04 15:21:22 +02:00
(testing "Generates correct HTML for questionnaire with multiple choices"
(ok (search "<form class=py-5 action=/submit method=post>" result))
(ok (search "<legend>Which of the following devices do you regularly use to browse the internet?</legend>" result))
2023-09-06 14:30:51 +02:00
(ok (search "type=checkbox" result))
(ok (search "name=group-device" result))
(ok (search "value=desktop" result))
2023-09-04 15:21:22 +02:00
(ok (search "<hr class=my-4>" result))
(ok (search "<button class=\"btn btn-primary\" type=submit>Submit</button>" result)))))
(deftest test-create-questionnaire-mixed-choices
(let ((result (spinneret:with-html-string
2023-09-15 20:11:43 +02:00
(questionnaire "/submit"
(:ask "Which of the following devices do you regularly use to browse the internet?"
:group "device"
:choices (:multiple "Desktop" "Laptop" "Tablet"
:text "Others (please specify)"))))))
(testing "Generates correct HTML for questionnaire with multiple choices"
(ok (search "<form class=py-5 action=/submit method=post>" result))
(ok (search "<legend>Which of the following devices do you regularly use to browse the internet?</legend>" result))
2023-09-06 14:30:51 +02:00
(ok (search "type=checkbox" result))
(ok (search "name=group-device" result))
(ok (search "class=\"form-label group-device\"" result))
(ok (search "class=form-control" result))
(ok (search "<hr class=my-4>" result))
(ok (search "<button class=\"btn btn-primary\" type=submit>Submit</button>" result)))))
(deftest test-create-questionnaire-single-german
(let* ((spinneret:*html-lang* "de")
(result (spinneret:with-html-string
(questionnaire "/submit"
2023-09-15 20:11:43 +02:00
(:frage "Ihr Geschlecht?"
:gruppe "geschlecht"
:auswahl (:single "Männlich"
"Weiblich"
"Non-Binary"
"Keine Angabe"))))))
(testing "Generates correct HTML for questionnaire with single choices"
(ok (search "<form class=py-5 action=/submit method=post>" result))
(ok (search "<legend>Ihr Geschlecht?</legend>" result))
(ok (search "class=form-check-input" result))
(ok (search "name=group-geschlecht" result))
(ok (search "value=männlich" result))
(ok (search "value=weiblich" result))
(ok (search "value=keine-angabe" result))
(ok (search "<button class=\"btn btn-primary\" type=submit>Absenden</button>" result)))))
(deftest test-create-questionnaire-select
(let ((result (spinneret:with-html-string
(questionnaire "/submit"
(:ask "What is your favorite color?"
:group "favcolor"
2023-09-16 20:48:03 +02:00
:choices (:combo "Red" "Green" "Blue"))))))
(ok (search "select" result))
(ok (search "option value=red" result))
(ok (search "option value=green" result))
(ok (search "option value=blue" result))
(ok (search "What is your favorite color?" result))))