diff --git a/lib/statifier_examples/signup/validation.ex b/lib/statifier_examples/signup/validation.ex index 49851e92..f2f31d13 100644 --- a/lib/statifier_examples/signup/validation.ex +++ b/lib/statifier_examples/signup/validation.ex @@ -31,12 +31,12 @@ defmodule StatifierExamples.Signup.Validation do The one rule this module keeps is which formats a document may ask for. Riddler checks more than one, and this app has a reader-facing message - for `email`, `integer` and `number`, and for the date and time formats - `date`, `time` and `datetime`, alone, so a `text_question` naming - any other format **raises**, for `StatifierExamplesWeb.SignupElements`' - reason about an unknown element type: a check this app cannot explain to a reader is a - screen that cannot be finished, which is worse than a page that will not - draw. + for `email`, `phone`, `pattern`, `integer` and `number`, and for the date + and time formats `date`, `time` and `datetime`, alone, so a + `text_question` naming any other format **raises**, for + `StatifierExamplesWeb.SignupElements`' reason about an unknown element + type: a check this app cannot explain to a reader is a screen that cannot + be finished, which is worse than a page that will not draw. ## A question's own checks, against the host's context @@ -77,6 +77,8 @@ defmodule StatifierExamples.Signup.Validation do # See the moduledoc. @format_messages %{ "email" => "must look like an email address", + "phone" => "must be a phone number, with at least seven digits", + "pattern" => "must be written in the form this question asks for", "integer" => "must be a whole number", "number" => "must be a number", "date" => "must be a date, written YYYY-MM-DD", @@ -186,7 +188,7 @@ defmodule StatifierExamples.Signup.Validation do moduledoc). iex> StatifierExamples.Signup.Validation.formats() - ["date", "datetime", "email", "integer", "number", "time"] + ["date", "datetime", "email", "integer", "number", "pattern", "phone", "time"] """ @spec formats() :: [String.t()] def formats, do: @formats @@ -247,6 +249,10 @@ defmodule StatifierExamples.Signup.Validation do @spec reader_message(Finding.t(), %{optional(String.t()) => String.t()}) :: String.t() | nil defp reader_message(%Finding{code: "response.required"}, _formats), do: "is required" + # A pattern question declaring no pattern is the author's defect, not the + # reader's: riddler's own sentence says so and is kept. + defp reader_message(%Finding{code: "response.format", field: "pattern"}, _formats), do: nil + defp reader_message(%Finding{code: "response.format", element_key: key}, formats), do: Map.get(@format_messages, Map.get(formats, key)) diff --git a/test/statifier_examples/screen_editor/try_test.exs b/test/statifier_examples/screen_editor/try_test.exs index 240d7d27..d27fb8ae 100644 --- a/test/statifier_examples/screen_editor/try_test.exs +++ b/test/statifier_examples/screen_editor/try_test.exs @@ -86,6 +86,23 @@ defmodule StatifierExamples.ScreenEditor.TryTest do assert done.screen == nil end + # Sabotage: made explained_formats/1 find no unsupported format; the + # Try started on the question and the refusal match went red, then + # reverted from a copy. The editor's own format choices are all + # worded, so the document is built here, as a hand-written one would be. + test "refuses a text question naming a format with no message, starting nothing" do + screen = + screen("blk_try_card", "card_choice", "Your card", [ + element("text_question", "postcode", %{"label" => "Postcode", "format" => "postcode"}), + element("button", "card_next", %{"label" => "Continue", "outcome" => "card_chosen"}) + ]) + + root = Block.new("core.sequence", id: "blk_try_doc_root", slots: %{"body" => [screen]}) + document = Document.new(root, id: "doc_unsupported_format") + + assert Try.start(document) == {:error, {:unsupported_format, ["postcode"]}} + end + # Sabotage: dropped "integer" from Validation's formats; Try.start/1 # refused the fixture as {:unsupported_format, ["age"]}, then reverted # from a copy. diff --git a/test/statifier_examples/signup/validation_test.exs b/test/statifier_examples/signup/validation_test.exs index 05d72f11..0b458e75 100644 --- a/test/statifier_examples/signup/validation_test.exs +++ b/test/statifier_examples/signup/validation_test.exs @@ -104,10 +104,46 @@ defmodule StatifierExamples.Signup.ValidationTest do # The moduledoc's argument: a document asking for a check nobody runs is # a document that silently accepts anything. test "a format this module does not implement raises" do - assert_raise ArgumentError, ~r/no check for format "phone"/, fn -> - Validation.validate([question(%{"format" => "phone"})], %{"q" => "x"}) + assert_raise ArgumentError, ~r/no check for format "postcode"/, fn -> + Validation.validate([question(%{"format" => "postcode"})], %{"q" => "x"}) end end + + # Sabotage: put the phone message as "is invalid"; this test went red, + # then reverted from a copy. + test "phone is put in its own words" do + nodes = [question(%{"format" => "phone"})] + message = "must be a phone number, with at least seven digits" + + for typo <- ["call me", "555", "12-34"] do + assert Validation.validate(nodes, %{"q" => typo}) == [{"q", message}] + end + + assert Validation.validate(nodes, %{"q" => "555-0187"}) == [] + end + + # Sabotage: removed the field "pattern" clause of reader_message/2; the + # author's defect read as the reader's message and this test went red, + # then reverted from a copy. + test "a pattern question declaring no pattern keeps riddler's sentence" do + nodes = [question(%{"format" => "pattern"})] + + assert [{"q", message}] = Validation.validate(nodes, %{"q" => "x"}) + assert message =~ "declares none" + end + + # Sabotage: put the pattern message as "is invalid"; this test went red, + # then reverted from a copy. + test "pattern is put in its own words" do + nodes = [question(%{"format" => "pattern", "pattern" => "[0-9]{4}"})] + message = "must be written in the form this question asks for" + + for typo <- ["12", "12345", "abcd"] do + assert Validation.validate(nodes, %{"q" => typo}) == [{"q", message}] + end + + assert Validation.validate(nodes, %{"q" => "1234"}) == [] + end end describe "what it is given, and what it therefore checks" do diff --git a/test/statifier_examples_web/live/screen_editor_live_test.exs b/test/statifier_examples_web/live/screen_editor_live_test.exs index d96cdd99..eb2a97da 100644 --- a/test/statifier_examples_web/live/screen_editor_live_test.exs +++ b/test/statifier_examples_web/live/screen_editor_live_test.exs @@ -44,19 +44,16 @@ defmodule StatifierExamplesWeb.ScreenEditorLiveTest do refute has_element?(view, "#try-journey") end - # Sabotage (run): removed the format check from Try.start/1; the Try - # started a Journey and the refusal assertion went red. Reverted from a - # copy. - test "refuses a text question format the Journey cannot check, keeping the page", %{ - conn: conn - } do + # Sabotage: put the phone message as "is invalid"; the finding + # assertion went red, then reverted from a copy. + test "tries a phone question and tells a short number in the page's words", %{conn: conn} do {:ok, view, _html} = live(conn, ~p"/screen-editor") details = insert(view, "myapp.screens.screen", "blk_pr_root", "body", 0) configure(view, details, %{"key" => "patron_details", "title" => "Your details"}) - age = insert(view, "myapp.screens.text_question", details, "elements", 0) - configure(view, age, %{"key" => "age", "label" => "Age", "format" => "phone"}) + phone = insert(view, "myapp.screens.text_question", details, "elements", 0) + configure(view, phone, %{"key" => "phone", "label" => "Phone", "format" => "phone"}) next = insert(view, "myapp.screens.button", details, "elements", 1) @@ -68,10 +65,17 @@ defmodule StatifierExamplesWeb.ScreenEditorLiveTest do view |> element("#try") |> render_click() - assert has_element?(view, "#try-refusals li", "age: this page has no check") - refute has_element?(view, "#try-journey") - refute has_element?(view, "#details_next") - assert render(view) =~ ~s(data-block-id="#{age}") + refute has_element?(view, "#try-refusals") + assert has_element?(view, "#try-journey") + + view |> element("#try-responses") |> render_change(%{"responses" => %{"phone" => "555"}}) + view |> element("#details_next") |> render_click() + + assert has_element?( + view, + "#try-finding-phone", + "must be a phone number, with at least seven digits" + ) end end