/**
 * Test the SUBSTITUTE function compatibility. The Assert.cls implementation depends on it.
 */
// USING Progress.Lang.*.
USING OpenEdge.Core.Assert.

ROUTINE-LEVEL ON ERROR UNDO, THROW.

CLASS unittests.base.redirect.TestRedirected:

   DEFINE STREAM rpt.
   @Before.
   METHOD PUBLIC VOID BeforeClass():
   END METHOD. 

   /**
    * This test case shows that the HIDE ALL statement operates only on the contents
    * of the interactive terminal and has no effect on redirected output.
    */
   @Test.
   METHOD PUBLIC VOID HideAllRedirected2 ():
      DEFINE VARIABLE outfile AS CHARACTER FORMAT "x(20)" NO-UNDO.
      DEFINE VARIABLE x1      AS CHARACTER FORMAT "x(10)" NO-UNDO.
      /* named stream */
      MESSAGE "named stream test".
      ASSIGN
         outfile = "terminal"
         x1      = "test1".

      MESSAGE x1.

      OUTPUT STREAM rpt TO VALUE(outfile).
      DISPLAY STREAM rpt x1 outfile.

      ASSIGN
         outfile = "hideall2.txt"
         x1      = "test2".

      MESSAGE x1.

      OUTPUT STREAM rpt TO VALUE(outfile).
      DISPLAY STREAM rpt x1 outfile.
      HIDE ALL.

      OUTPUT STREAM rpt CLOSE.

      DEFINE VARIABLE content AS LONGCHAR NO-UNDO.
      COPY-LOB FROM FILE "hideall2.txt" TO content.
      Assert:Equals(NormalizeLineEndings("~r~nx---------------outfile---------------~r~n~r~ntest2           hideall2.txt~r~n"), content).
   END METHOD.

   DEFINE STREAM mystr.

   @Test.
   METHOD PUBLIC VOID RedirectedDisplay():
      DEFINE VARIABLE content AS LONGCHAR NO-UNDO.

      /* Setup: redirect output to a temporary file */
      OUTPUT STREAM mystr TO "redirected_training4.txt".

      /* The statement under test */
      DISPLAY STREAM mystr "Hello Outside World!".

      /* Cleanup: close stream */
      OUTPUT STREAM mystr CLOSE.

      /* Read back the file content for assertion */
      COPY-LOB FROM FILE "redirected_training4.txt" TO content.

      /* Assert exact match (DISPLAY adds CRLF on Windows) */
      Assert:Equals(NormalizeLineEndings("~r~nHello Outside World!~r~n"), content).

      /* Clean up test file */
      OS-DELETE VALUE("redirected_training4.txt").
   END METHOD.

   @Test.
   METHOD PUBLIC VOID HideAllRedirected ():
      OUTPUT TO "a.txt" PAGE-SIZE 132.
      
      DISPLAY
        "===========" AT 38
        "==========="
        "==========="
        "==========="
        "==========="
        "==========="
        "==========="
        "===========" SKIP
        "Totals" AT 19
        WITH WIDTH 132 DOWN NO-BOX NO-LABELS FRAME f3.
      
      /* In P2J/FWD, the above DISPLAY is cleared after the HIDE ALL statement below. */
      HIDE ALL.
      
      OUTPUT CLOSE.
      
      DEFINE VARIABLE content AS LONGCHAR NO-UNDO.
      COPY-LOB FROM FILE "a.txt" TO content.
      Assert:Equals(NormalizeLineEndings("                                     ===========     ===========     ===========     ===========     ===========~r~n===========     ===========     ===========~r~n                  Totals~r~n" + CHR(12)), content).
   END METHOD.
      
   DEFINE STREAM r.

   /* ----------------------------------------------------------------------
      Test case: Redirected boxed frame behavior with page headers and DOWN
      ----------------------------------------------------------------------
      This test verifies how a redirected boxed frame with PAGE-TOP header
      behaves when outputting multiple lines with DOWN and DISPLAY.
   */
    @Test.
    METHOD PUBLIC VOID TestRedirectedBoxedFrame():
      DEFINE VARIABLE content AS LONGCHAR NO-UNDO.
      DEFINE VARIABLE i       AS INTEGER  NO-UNDO INIT 0.

      /* Setup: redirect output to a temporary file with page size */
      OUTPUT STREAM r TO "TestRedirectedBoxedFrame.txt" PAGE-SIZE 10.

      /* Define header frame with PAGE-TOP */
      FORM HEADER "stream header"
          WITH PAGE-TOP NO-BOX NO-LABELS FRAME fs.

      /* View the header frame on the redirected stream */
      VIEW STREAM r FRAME fs.

      /* Output 10 lines with DOWN */
      DO i = 1 TO 10:
          DISPLAY STREAM r "test" + STRING(i)
              WITH NO-LABELS FRAME f0.
          DOWN STREAM r WITH FRAME f0.
      END.

      /* Final data line with DOWN */
      DISPLAY STREAM r "data"
          WITH DOWN NO-LABELS FRAME fd.

      /* Cleanup: close stream */
      OUTPUT STREAM r CLOSE.

      /* Read back the file content for assertion */
      COPY-LOB FROM FILE "TestRedirectedBoxedFrame.txt" TO content.
      
      Assert:Equals(NormalizeLineEndings("stream header~r~n~r~ntest1~r~n~r~ntest2~r~n~r~ntest3~r~n~r~ntest4~r~n" + CHR(12)
        + "stream header~r~n~r~ntest5~r~n~r~ntest6~r~n~r~ntest7~r~n~r~ntest8~r~n" + CHR(12)
        + "stream header~r~n~r~ntest9~r~n~r~ntest10~r~n~r~ndata~r~n"
        + CHR(12)), content).
      
       /* Optional: clean up test file */
       // OS-DELETE VALUE("TestRedirectedBoxedFrame.txt").
   END METHOD.

   METHOD PUBLIC LONGCHAR NormalizeLineEndings(INPUT inputString AS LONGCHAR):

      IF OPSYS = "WIN32" THEN
         RETURN inputString.
      ELSE
         RETURN REPLACE(inputString, CHR(13) + CHR(10), CHR(10)).

   END METHOD.

END CLASS.

