;; Tests for functions around filling  -*- lexical-binding: t -*-

(add-to-list 'load-path ".")
(load "init" nil t)
(init-rst-ert t)

(ert-deftest fill-asserts ()
  "Check some assertions."
  (should (equal ert-Buf-point-char "\^@"))
  (should (equal ert-Buf-mark-char "\^?"))
  )

(defun auto-fill ()
  "Wrapper to do auto fill."
  (let ((fc fill-column))
    (rst-mode)
    (setq fill-column fc)
    (auto-fill-mode 1)
    (funcall auto-fill-function)))

(ert-deftest auto-fill ()
  "Tests for auto fill."
  (let ((rst-indent-width 2)
	(rst-indent-field 2)
	(rst-indent-literal-normal 3)
	(rst-indent-literal-minimized 2)
	(rst-indent-comment 3)
	(fill-column 20))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
* This is a test with a fill column of 20\^@"
             "
* This is a test
  with a fill column
  of 20\^@"
             ))
    (should (ert-equal-buffer
	     '(auto-fill)
             "
* This is a test
  with a fill column of 20\^@"
             "
* This is a test
  with a fill column
  of 20\^@"
             ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
:Field: Does this work for fields?\^@"
	     "
:Field: Does this
	work for
	fields?\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
:Field: Does this
	work for fields?\^@"
	     "
:Field: Does this
	work for
	fields?\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
.. dir:: Yes, quite fine\^@"
	     "
.. dir:: Yes, quite
	 fine\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
.. dir:: Yes, quite fine\^@
   :f: Field"
	     "
.. dir:: Yes, quite
	 fine\^@
   :f: Field"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
.. |s| r:: Dir ectives had problems\^@"
	     "
.. |s| r:: Dir
	   ectives
	   had
	   problems\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
.. |s| r:: Dir
	   ectives
	   had problems\^@"
	     "
.. |s| r:: Dir
	   ectives
	   had
	   problems\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
.. [CIT] X cit is citations are also filled\^@"
	     "
.. [CIT] X cit is
	 citations
	 are also
	 filled\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
.. [CIT] X cit is
	 citations
	 are also filled\^@"
	     "
.. [CIT] X cit is
	 citations
	 are also
	 filled\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
.. Comments should also fill nicely\^@"
	     "
.. Comments should
   also fill nicely\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
Normal text should also fill as expected\^@"
	     "
Normal text should
also fill as
expected\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
  Normal text should also fill as expected\^@"
	     "
  Normal text should
  also fill as
  expected\^@"
	     ))
    (should (ert-equal-buffer
	     '(auto-fill)
	     "
Normal text should also fill \^@as expected"
	     "
Normal text should
also fill \^@as expected"
	     ))
    ))

(defun explicit-fill ()
  "Wrapper for `fill-paragraph'."
  (let ((fc fill-column))
    (rst-mode)
    (setq fill-column fc)
    (fill-paragraph)
    (untabify (point-min) (point-max))))

(ert-deftest fill-paragraph ()
  "Tests for `fill-paragraph'."
  (let ((rst-indent-width 2)
	(rst-indent-field 2)
	(rst-indent-literal-normal 3)
	(rst-indent-literal-minimized 2)
	(rst-indent-comment 3)
	(fill-column 20))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
* This is a test with a fill column of 20\^@
"
             "
* This is a test
  with a fill column
  of 20\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
* This is a test \^@with a fill column
  of 20
"
             "
* This is a test
  \^@with a fill column
  of 20
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
:Field: Does this work for fields?\^@
"
             "
:Field: Does this
        work for
        fields?\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
:Field: Does this work\^@ for
        fields?
"
             "
:Field: Does this
        work\^@ for
        fields?
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
.. dir:: Yes, quite fine\^@
"
             "
.. dir:: Yes, quite
         fine\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
\^@.. dir:: Yes, quite
         fine
"
             "
\^@.. dir:: Yes, quite
         fine
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
.. dir:: Yes, quite fine\^@
   :f: Field
"
             "
.. dir:: Yes, quite
         fine\^@
   :f: Field
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
.. [CIT] X cit is citations are also filled\^@
"
             "
.. [CIT] X cit is
         citations
         are also
         filled\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
.. [CIT] X cit is
   citations are also filled\^@
"
             "
.. [CIT] X cit is
   citations are
   also filled\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
.. [CIT] X cit is
     citations are also filled\^@
"
             "
.. [CIT] X cit is
     citations are
     also filled\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
.. |s| r:: Dir ectives had problems\^@
"
             "
.. |s| r:: Dir
           ectives
           had
           problems\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
\^@.. |s| r:: Dir ectives had problems
"
             "
\^@.. |s| r:: Dir
           ectives
           had
           problems
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
.. |s| r:: Dir
    ectives had problems\^@
"
             "
.. |s| r:: Dir
    ectives had
    problems\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
Normal \^@text should also fill as expected
"
             "
Normal \^@text should
also fill as
expected
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
\^@Normal text should also fill as expected
"
             "
\^@Normal text should
also fill as
expected
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
  Normal text should also fill as expected\^@
"
             "
  Normal text should
  also fill as
  expected\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
Normal text should also fill as expected

Normal text should also fill as expected\^@
"
             "
Normal text should also fill as expected

Normal text should
also fill as
expected\^@
"
             ))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
\^@Normal text should
also fill as
    expected
"
             "
\^@Normal text should
also fill as
expected
"
             ))
    ))

(ert-deftest fill-paragraph-fixme ()
  "Tests for `fill-paragraph'."
  :expected-result :failed
  (let ((rst-indent-width 2)
	(rst-indent-field 2)
	(rst-indent-literal-normal 3)
	(rst-indent-literal-minimized 2)
	(rst-indent-comment 3)
	(fill-column 20))
    (should (ert-equal-buffer
             '(explicit-fill)
             "
\^@Normal text should
    also fill as
    expected
"
             "
\^@Normal text should
also fill as
expected
"
             ))
    ))

(defun explicit-fill-region ()
  "Wrapper for `fill-region'."
  (let ((fc fill-column))
    (rst-mode)
    (setq fill-column fc)
    (call-interactively 'fill-region)
    (untabify (point-min) (point-max))))

(ert-deftest fill-region ()
  "Tests for `fill-region'."
  (let ((rst-indent-width 2)
        (rst-indent-field 2)
        (rst-indent-literal-normal 3)
        (rst-indent-literal-minimized 2)
        (rst-indent-comment 3)
        (fill-column 20))
    (should (ert-equal-buffer
             '(explicit-fill-region)
             "\^@
* This is a test with a fill column of 20

* This is a test with a fill column
  of 20

:Field: Does this work for fields?

:Field: Does this work for
        fields?

.. dir:: Yes, quite fine

.. dir:: Yes, quite
         fine

.. dir:: Yes, quite fine
   :f: Field for a directive

.. [CIT] X cit is citations are also filled

.. |s| r:: Dir ectives had problems

.. |s| r:: Dir ectives had problems

Normal text should also fill as expected

  Normal text should also fill as expected
\^?"
             "\^@
* This is a test
  with a fill column
  of 20

* This is a test
  with a fill column
  of 20

:Field: Does this
        work for
        fields?

:Field: Does this
        work for
        fields?

.. dir:: Yes, quite
         fine

.. dir:: Yes, quite
         fine

.. dir:: Yes, quite
         fine
   :f: Field for a
       directive

.. [CIT] X cit is
         citations
         are also
         filled

.. |s| r:: Dir
           ectives
           had
           problems

.. |s| r:: Dir
           ectives
           had
           problems

Normal text should
also fill as
expected

  Normal text should
  also fill as
  expected
\^?"
             ))
    (should (ert-equal-buffer
             '(explicit-fill-region)
             "\^@
* This is a test with a fill column of 20
* This is a test with a fill column
  of 20
:Field: Does this work for fields?
:Field: Does this work for
        fields?
.. dir:: Yes, quite fine
.. dir:: Yes, quite
         fine
.. dir:: Yes, quite fine
   :f: Field for a directive
.. [CIT] X cit is citations are also filled
.. |s| r:: Dir ectives had problems
.. |s| r:: Dir ectives had problems

Normal text should also fill as expected

  Normal text should also fill as expected
\^?"
             "\^@
* This is a test
  with a fill column
  of 20
* This is a test
  with a fill column
  of 20
:Field: Does this
        work for
        fields?
:Field: Does this
        work for
        fields?
.. dir:: Yes, quite
         fine
.. dir:: Yes, quite
         fine
.. dir:: Yes, quite
         fine
   :f: Field for a
       directive
.. [CIT] X cit is
         citations
         are also
         filled
.. |s| r:: Dir
           ectives
           had
           problems
.. |s| r:: Dir
           ectives
           had
           problems

Normal text should
also fill as
expected

  Normal text should
  also fill as
  expected
\^?"
             ))
    ))
