%% test-common.tex
%% ===============
%% Helpers shared by the test files; \input this file after
%% \usepackage{tcbnestbreak}.
%%
%% \TNBfiller{<n>}
%%   A paragraph of <n> filler sentences.
%% \TNBexpect{<boxes>}{<broken>}
%%   At the end of the document, exactly <boxes> boxes have flowed and
%%   at least <broken> of them are broken.
%% \TNBtogether{<list>}
%%   The boxes of the list start on the same page: each is the first
%%   thing in the next one.  The list has numbers counted back from the
%%   last box that has flowed (0 is this box, 1 the one before).  Logs
%%   "TNB-TOGETHER OK: ..." or "TNB-TOGETHER NG: ...".
%% \TNBatlower{<number>}{<length>}
%%   The box of this number (counted back as for \TNBtogether) is
%%   broken between its upper and its lower part: it has two frames,
%%   the first one has the segmentation and is not as high as
%%   <length>.  Logs "TNB-ATLOWER OK: ..." or "TNB-ATLOWER NG: ...".
%% \TNBonce{<name>}
%%   What is typeset here is shipped out exactly once.  <name> is
%%   expanded; it is written to the .aux file at shipout.  Logs
%%   "TNB-ONCE OK: ..." or "TNB-ONCE NG: ...".
%% \TNBvalue{<counter>}{<number>}
%%   The counter has this value now.  Logs "TNB-VALUE OK: ..." or
%%   "TNB-VALUE NG: ...".
%% \TNBmargin{<name>}
%%   Used in a box that flows, after the boxes inside it: \@totalleftmargin
%%   is kept under <name>, and the frame of the box is checked to be
%%   where the text needs it (see below).
%% \TNBshift{<name 1>}{<name 2>}{<length>}
%%   The \@totalleftmargin of <name 2> is that of <name 1> plus
%%   <length>.  Logs "TNB-SHIFT OK: ..." or "TNB-SHIFT NG: ...".
%% \TNBfootnote{<text>}
%%   \footnote{<text>} with a check that its text is on the page of
%%   its mark (both pages are written to the .aux file at shipout).
%%   Logs "TNB-NOTE OK: ..." or "TNB-NOTE NG: ...".
%% \TNBnosplit (optional, defined before this file is input)
%%   The footnotenosplit package is loaded (footnotes are not split:
%%   \interfootnotelinepenalty=10000), with \footnotetopkern=2pt and an
%%   \@makefntext that starts with \kern\footnotetopkern.
%%
%% For each box that flows, the height of what flows and whether the
%% box has a lower part are written to the .aux file (hook
%% tcbnestbreak/flow); for each frame that is drawn, its state, its
%% page, its position in the part of the outer box and its
%% segmentation are written at shipout (hook tcbnestbreak/frame).
%% When the .aux file is read back at \end{document}, every box is
%% checked:
%%   * its frames are one "unbroken" frame, or a "first" frame, any
%%     number of "middle" frames and a "last" frame;
%%   * these are on pages that follow each other (not checked in
%%     twocolumn);
%%   * a box with a lower part has exactly one frame with the
%%     segmentation (S); the frames before it are of the upper part
%%     (U), those after it of the lower part (L).  A box without a
%%     lower part has frames of the upper part only;
%%   * the frames together are as high as what has flowed: the same
%%     for an unbroken box, and less by at most 40pt for each break
%%     (the glue that is discarded there).
%% When the picture of a frame has been made (hook
%% tcbnestbreak/picture), it is checked to be as wide as the box and
%% as high as the frame shall be, and, after \TNBmargin, to be put
%% where the text of the box is minus the left rule and the padding.
%% Logs "TNB-PICTURE NG: ..." if not.
%% Logs "TNB-BOX OK: ..." or "TNB-BOX NG: ..." for each box, then
%% "TNB-RESULT OK (<n> boxes, <m> broken)" or "TNB-RESULT NG".
%% A single run is enough.
\makeatletter
\ifdefined\TNBnosplit
  \RequirePackage{footnotenosplit}
  \footnotetopkern=2pt
  \renewcommand\@makefntext[1]{%
    \kern\footnotetopkern
    \parindent 1em\noindent
    \hb@xt@1.8em{\hss\@makefnmark}#1}
\fi
\ExplSyntaxOn
\bool_new:N \g__tnb_ng_bool
\int_new:N  \g__tnb_max_int
\int_new:N  \g__tnb_broken_int
\tl_new:N   \g__tnb_expect_tl
\tl_new:N   \l__tnb_states_tl
\tl_new:N   \l__tnb_parts_tl
\dim_new:N  \l__tnb_sum_dim
\int_new:N  \l__tnb_last_int
\bool_new:N \l__tnb_ok_bool

\cs_new_protected:Npn \__tnb_ng:n #1
  {
    \bool_gset_true:N \g__tnb_ng_bool
    \iow_term:e { TNB-BOX~NG:~#1 }
  }

\int_new:N \g__tnb_flow_int
\seq_new:N \g__tnb_together_seq
\tl_new:N \l__tnb_page_tl
\tl_new:N \g__tnb_left_tl
\AddToHookWithArguments { tcbnestbreak/flow }
  {
    \int_gset:Nn \g__tnb_flow_int {#1}
    \iow_now:Ne \@auxout { \exp_not:N \TNB@h {#1} {#2} {#3} }
    \tl_if_empty:NF \g__tnb_left_tl
      {
        \tl_gset_eq:cN { g__tnb_left_#1_tl } \g__tnb_left_tl
        \tl_gclear:N \g__tnb_left_tl
      }
  }
\NewDocumentCommand \TNBmargin { m }
  {
    \tl_gset:ce { g__tnb_margin_#1_tl } { \dim_use:N \@totalleftmargin }
    \tl_gset:Ne \g__tnb_left_tl
      {
        \dim_eval:n
          {
            \@totalleftmargin
            - \kvtcb@left@rule - \kvtcb@boxsep - \kvtcb@leftupper
          }
      }
  }
\prg_new_conditional:Npnn \__tnb_if_near:nn #1#2 { p , TF }
  {
    \dim_compare:nNnTF { \dim_abs:n { (#1) - (#2) } } < { 0.1pt }
      { \prg_return_true: } { \prg_return_false: }
  }
\NewDocumentCommand \TNBshift { m m m }
  {
    \__tnb_if_near:nnTF
      { \tl_use:c { g__tnb_margin_#2_tl } - \tl_use:c { g__tnb_margin_#1_tl } }
      {#3}
      { \iow_term:e { TNB-SHIFT~OK:~#1,~#2:~\dim_eval:n {#3} } }
      {
        \bool_gset_true:N \g__tnb_ng_bool
        \iow_term:e
          {
            TNB-SHIFT~NG:~#1~at~\tl_use:c { g__tnb_margin_#1_tl },~
            #2~at~\tl_use:c { g__tnb_margin_#2_tl },~want~\dim_eval:n {#3}
          }
      }
  }
\NewDocumentCommand \TNBvalue { m m }
  {
    \int_compare:nNnTF { \value {#1} } = {#2}
      { \iow_term:e { TNB-VALUE~OK:~#1~=~#2 } }
      {
        \bool_gset_true:N \g__tnb_ng_bool
        \iow_term:e
          { TNB-VALUE~NG:~#1~=~\int_eval:n { \value {#1} },~want~#2 }
      }
  }
%% #1: number, #2: moved right by, #3, #4: the width it shall have and
%% has, #5, #6: the same for the height
\AddToHookWithArguments { tcbnestbreak/picture }
  {
    \bool_lazy_all:nF
      {
        { \__tnb_if_near_p:nn {#3} {#4} }
        { \__tnb_if_near_p:nn {#5} {#6} }
        {
          \tl_if_exist:cTF { g__tnb_left_#1_tl }
            { \__tnb_if_near_p:nn { \tl_use:c { g__tnb_left_#1_tl } } {#2} }
            { \c_true_bool }
        }
      }
      {
        \bool_gset_true:N \g__tnb_ng_bool
        \iow_term:e
          {
            TNB-PICTURE~NG:~#1:~width~#4~of~#3,~height~#6~of~#5,~
            at~#2
            \tl_if_exist:cT { g__tnb_left_#1_tl }
              { ~of~\tl_use:c { g__tnb_left_#1_tl } }
          }
      }
  }
\seq_new:N \g__tnb_once_seq
\NewDocumentCommand \TNBonce { m }
  { \exp_args:Ne \__tnb_once:n {#1} }
\cs_new_protected:Npn \__tnb_once:n #1
  {
    \seq_if_in:NnF \g__tnb_once_seq {#1}
      { \seq_gput_right:Nn \g__tnb_once_seq {#1} }
    \iow_shipout:Nn \@auxout { \TNB@o {#1} }
  }
\cs_new_eq:NN \TNB@o \use_none:n
\cs_new_protected:Npn \__tnb_record_once:n #1
  {
    \tl_if_exist:cF { g__tnb_once_#1_tl }
      { \tl_new:c { g__tnb_once_#1_tl } }
    \tl_gput_right:cn { g__tnb_once_#1_tl } { + }
  }
\cs_new_protected:Npn \__tnb_check_once:n #1
  {
    \tl_if_exist:cF { g__tnb_once_#1_tl }
      { \tl_new:c { g__tnb_once_#1_tl } }
    \int_compare:nNnTF { \tl_count:c { g__tnb_once_#1_tl } } = { 1 }
      { \iow_term:e { TNB-ONCE~OK:~#1 } }
      {
        \bool_gset_true:N \g__tnb_ng_bool
        \iow_term:e
          {
            TNB-ONCE~NG:~#1~shipped~out~
            \tl_count:c { g__tnb_once_#1_tl }~times
          }
      }
  }
\NewDocumentCommand \TNBtogether { m }
  {
    \seq_gput_right:Ne \g__tnb_together_seq
      {
        \clist_map_function:nN {#1} \__tnb_back:n
      }
  }
\cs_new:Npn \__tnb_back:n #1
  { { \int_eval:n { \g__tnb_flow_int - #1 } } }
%% The page of the first frame of the box #1
\cs_new:Npn \__tnb_first_page:n #1
  {
    \exp_args:Ne \tl_item:nn
      { \seq_item:cn { g__tnb_frames_#1_seq } { 1 } } { 2 }
  }
%% #1: the numbers of the boxes, each in braces
\cs_new_protected:Npn \__tnb_check_together:n #1
  {
    \tl_set:Ne \l__tnb_page_tl
      { \exp_args:Ne \__tnb_first_page:n { \tl_head:n {#1} } }
    \bool_set_true:N \l__tnb_ok_bool
    \tl_map_inline:nn {#1}
      {
        \str_if_eq:eeF { \__tnb_first_page:n {##1} } { \l__tnb_page_tl }
          { \bool_set_false:N \l__tnb_ok_bool }
      }
    \bool_if:NTF \l__tnb_ok_bool
      {
        \iow_term:e
          { TNB-TOGETHER~OK:~\tl_to_str:n {#1}~start~on~p.\l__tnb_page_tl }
      }
      {
        \bool_gset_true:N \g__tnb_ng_bool
        \iow_term:e
          {
            TNB-TOGETHER~NG:~\tl_to_str:n {#1}~do~not~all~start~on~
            p.\l__tnb_page_tl
          }
      }
  }
\seq_new:N \g__tnb_atlower_seq
\NewDocumentCommand \TNBatlower { m m }
  {
    \seq_gput_right:Ne \g__tnb_atlower_seq
      { { \int_eval:n { \g__tnb_flow_int - #1 } } { \dim_eval:n {#2} } }
  }
%% #1: the number of the box, #2: the length.  The frames are kept as
%% {state} {page} {from} {to} {U, S or L}
\cs_new_protected:Npn \__tnb_check_atlower:nn #1#2
  {
    \bool_lazy_all:nTF
      {
        { \int_compare_p:nNn { \seq_count:c { g__tnb_frames_#1_seq } } = { 2 } }
        {
          \str_if_eq_p:ee
            { \exp_last_unbraced:Ne \use_v:nnnnn
                { \seq_item:cn { g__tnb_frames_#1_seq } { 1 } } }
            { S }
        }
        {
          \str_if_eq_p:ee
            { \exp_last_unbraced:Ne \use_v:nnnnn
                { \seq_item:cn { g__tnb_frames_#1_seq } { 2 } } }
            { L }
        }
        {
          \dim_compare_p:nNn
            {
              \exp_last_unbraced:Ne \__tnb_frame_height:nnnnn
                { \seq_item:cn { g__tnb_frames_#1_seq } { 1 } }
            }
            < {#2}
        }
      }
      { \iow_term:e { TNB-ATLOWER~OK:~#1~is~broken~between~its~parts } }
      {
        \bool_gset_true:N \g__tnb_ng_bool
        \iow_term:e { TNB-ATLOWER~NG:~#1~is~not~broken~between~its~parts }
      }
  }
\cs_new:Npn \__tnb_frame_height:nnnnn #1#2#3#4#5 { #4 - #3 }
\AddToHookWithArguments { tcbnestbreak/frame }
  {
    \iow_shipout_e:Nn \@auxout
      {
        \exp_not:N \TNB@f {#1} {#2} { \thepage } {#3} {#4}
          { \str_case:nnF {#5} { { } { U } { L } { L } } { S } }
      }
  }

%% The records are ignored when the .aux file is read at
%% \begin{document} (they come from the previous run)
\cs_new_eq:NN \TNB@h \use_none:nnn
\cs_new_eq:NN \TNB@f \use_none:nnnnnn
\cs_new_protected:Npn \__tnb_record_flow:nnn #1#2#3
  {
    \int_gset:Nn \g__tnb_max_int { \int_max:nn { \g__tnb_max_int } {#1} }
    \tl_gset:cn { g__tnb_height_#1_tl } {#2}
    \tl_gset:cn { g__tnb_lower_#1_tl } {#3}
    \seq_gclear_new:c { g__tnb_frames_#1_seq }
  }
\cs_new_protected:Npn \__tnb_record_frame:nnnnnn #1#2#3#4#5#6
  {
    \seq_if_exist:cTF { g__tnb_frames_#1_seq }
      {
        \seq_gput_right:cn { g__tnb_frames_#1_seq }
          { {#2} {#3} {#4} {#5} {#6} }
      }
      { \__tnb_ng:n { #1:~frame~of~a~box~that~has~not~flowed } }
  }
\AddToHook { enddocument/afterlastpage }
  {
    \cs_gset_eq:NN \TNB@h \__tnb_record_flow:nnn
    \cs_gset_eq:NN \TNB@f \__tnb_record_frame:nnnnnn
    \cs_gset_eq:NN \TNB@n \__tnb_record_note:nnn
    \cs_gset_eq:NN \TNB@o \__tnb_record_once:n
  }
\AddToHook { enddocument/afteraux } { \__tnb_check: }

\int_new:N \g__tnb_note_int
\NewDocumentCommand \TNBfootnote { +m }
  {
    \int_gincr:N \g__tnb_note_int
    \exp_args:Ne \__tnb_footnote:nn { \int_use:N \g__tnb_note_int } {#1}
  }
\cs_new_protected:Npn \__tnb_footnote:nn #1#2
  { \footnote { \__tnb_note:nn { t } {#1} #2 } \__tnb_note:nn { m } {#1} }
\cs_new_protected:Npn \__tnb_note:nn #1#2
  {
    \iow_shipout_e:Nn \@auxout
      { \exp_not:N \TNB@n {#1} {#2} { \thepage } }
  }
\cs_new_eq:NN \TNB@n \use_none:nnn
\cs_new_protected:Npn \__tnb_record_note:nnn #1#2#3
  {
    \tl_if_exist:cF { g__tnb_note_#1_#2_tl }
      { \tl_new:c { g__tnb_note_#1_#2_tl } }
    \tl_gput_right:cn { g__tnb_note_#1_#2_tl } { [#3] }
  }
\cs_new_protected:Npn \__tnb_check_note:n #1
  {
    \tl_if_exist:cF { g__tnb_note_m_#1_tl }
      { \tl_new:c { g__tnb_note_m_#1_tl } }
    \tl_if_exist:cF { g__tnb_note_t_#1_tl }
      { \tl_new:c { g__tnb_note_t_#1_tl } }
    \bool_lazy_and:nnTF
      { ! \tl_if_empty_p:c { g__tnb_note_m_#1_tl } }
      { \tl_if_eq_p:cc { g__tnb_note_m_#1_tl } { g__tnb_note_t_#1_tl } }
      { \iow_term:e { TNB-NOTE~OK:~\__tnb_report_note:n {#1} } }
      {
        \bool_gset_true:N \g__tnb_ng_bool
        \iow_term:e { TNB-NOTE~NG:~\__tnb_report_note:n {#1} }
      }
  }
\cs_new:Npn \__tnb_report_note:n #1
  {
    #1 :~mark~on~p.\tl_use:c { g__tnb_note_m_#1_tl } ,~
    text~on~p.\tl_use:c { g__tnb_note_t_#1_tl }
  }

\NewDocumentCommand \TNBexpect { m m }
  { \tl_gset:Nn \g__tnb_expect_tl { {#1} {#2} } }

\cs_new_protected:Npn \__tnb_check:
  {
    \int_step_function:nN { \g__tnb_max_int } \__tnb_check_one:n
    \int_step_function:nN { \g__tnb_note_int } \__tnb_check_note:n
    \seq_map_inline:Nn \g__tnb_together_seq
      { \__tnb_check_together:n {##1} }
    \seq_map_function:NN \g__tnb_once_seq \__tnb_check_once:n
    \seq_map_inline:Nn \g__tnb_atlower_seq
      { \__tnb_check_atlower:nn ##1 }
    \tl_if_empty:NTF \g__tnb_expect_tl
      { \__tnb_ng:n { no~\token_to_str:N \TNBexpect } }
      { \exp_after:wN \__tnb_check_expect:nn \g__tnb_expect_tl }
    \bool_if:NTF \g__tnb_ng_bool
      { \iow_term:n { TNB-RESULT~NG } }
      {
        \iow_term:e
          {
            TNB-RESULT~OK~(\int_use:N \g__tnb_max_int \c_space_tl boxes,~
            \int_use:N \g__tnb_broken_int \c_space_tl broken)
          }
      }
  }
\cs_new_protected:Npn \__tnb_check_expect:nn #1#2
  {
    \int_compare:nNnF { \g__tnb_max_int } = {#1}
      { \__tnb_ng:n { \int_use:N \g__tnb_max_int \c_space_tl boxes,~want~#1 } }
    \int_compare:nNnT { \g__tnb_broken_int } < {#2}
      {
        \__tnb_ng:n
          { \int_use:N \g__tnb_broken_int \c_space_tl broken,~want~#2~or~more }
      }
  }

\cs_new_protected:Npn \__tnb_check_one:n #1
  {
    \seq_if_exist:cTF { g__tnb_frames_#1_seq }
      {
        \tl_clear:N \l__tnb_states_tl
        \tl_clear:N \l__tnb_parts_tl
        \dim_zero:N \l__tnb_sum_dim
        \int_zero:N \l__tnb_last_int
        \bool_set_true:N \l__tnb_ok_bool
        \seq_map_inline:cn { g__tnb_frames_#1_seq }
          { \__tnb_check_frame:nnnnn ##1 }
        \regex_match:nVF { \A(?:;unbroken|;first(?:;middle)*;last)\Z }
          \l__tnb_states_tl
          { \bool_set_false:N \l__tnb_ok_bool }
        \str_if_eq:eeTF { \tl_use:c { g__tnb_lower_#1_tl } } { 1 }
          {
            \regex_match:nVF { \AU*SL*\Z } \l__tnb_parts_tl
              { \bool_set_false:N \l__tnb_ok_bool }
          }
          {
            \regex_match:nVF { \AU+\Z } \l__tnb_parts_tl
              { \bool_set_false:N \l__tnb_ok_bool }
          }
        \int_compare:nNnT { \seq_count:c { g__tnb_frames_#1_seq } } > { 1 }
          { \int_gincr:N \g__tnb_broken_int }
        \dim_compare:nNnT { \l__tnb_sum_dim }
          > { \tl_use:c { g__tnb_height_#1_tl } + 0.1pt }
          { \bool_set_false:N \l__tnb_ok_bool }
        \dim_compare:nNnT { \l__tnb_sum_dim }
          <
          {
            \tl_use:c { g__tnb_height_#1_tl } - 0.1pt
            - 40pt * ( \seq_count:c { g__tnb_frames_#1_seq } - 1 )
          }
          { \bool_set_false:N \l__tnb_ok_bool }
        \bool_if:NTF \l__tnb_ok_bool
          { \iow_term:e { TNB-BOX~OK:~\__tnb_report:n {#1} } }
          { \__tnb_ng:n { \__tnb_report:n {#1} } }
      }
      { \__tnb_ng:n { #1:~no~record } }
  }
\cs_new:Npn \__tnb_report:n #1
  {
    #1 \l__tnb_states_tl ,~ \l__tnb_parts_tl ,~
    \dim_use:N \l__tnb_sum_dim \c_space_tl of~
    \tl_use:c { g__tnb_height_#1_tl }
  }
%% #1: state, #2: page, #3: from, #4: to, #5: U, S or L
\cs_new_protected:Npn \__tnb_check_frame:nnnnn #1#2#3#4#5
  {
    \tl_put_right:Nn \l__tnb_states_tl { ; #1 }
    \tl_put_right:Nn \l__tnb_parts_tl {#5}
    \dim_add:Nn \l__tnb_sum_dim { #4 - #3 }
    \legacy_if:nF { @twocolumn }
      {
        \int_compare:nNnF { \l__tnb_last_int } = { 0 }
          {
            \int_compare:nNnF {#2} = { \l__tnb_last_int + 1 }
              { \bool_set_false:N \l__tnb_ok_bool }
          }
      }
    \int_set:Nn \l__tnb_last_int {#2}
  }
\ExplSyntaxOff

\newcommand\TNB@filler{%
  This is a filler sentence of plain English text for the test. }
\newcommand\TNBfiller[1]{%
  \count@=0
  \loop\advance\count@ by 1 \TNB@filler\ifnum\count@<#1 \repeat\par}
\makeatother
\endinput
