<commit_before>-----------------------------------------------------------------------
--  util-strings-tests -- Unit tests for strings
--  Copyright (C) 2009, 2010, 2011, 2012, 2015, 2018, 2020, 2021, 2022 <NAME>
--  Written by <NAME> (<EMAIL>)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Strings;
with Ada.Strings.Unbounded;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed.Hash;
with Ada.Strings.Unbounded.Hash;
with Ada.Containers;
with Util.Test_Caller;
with Util.Strings.Transforms;
with Util.Strings.Maps;
with Util.Strings.Vectors;
with Util.Strings.Formats;
with Util.Perfect_Hash;
with Util.Strings.Tokenizers;
with Ada.Streams;
with Util.Measures;
package body Util.Strings.Tests is

   use Ada.Strings.Unbounded;
   use Util.Tests;
   use Util.Strings.Transforms;

   package Caller is new Util.Test_Caller (Test, "Strings");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Escape_Javascript",
                       Test_Escape_Javascript'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Escape_Java",
                       Test_Escape_Java'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Escape_Xml",
                       Test_Escape_Xml'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Unescape_Xml",
                       Test_Unescape_Xml'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Capitalize",
                       Test_Capitalize'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.To_Upper_Case",
                       Test_To_Upper_Case'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.To_Lower_Case",
                       Test_To_Lower_Case'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.To_Hex",
                       Test_To_Hex'Access);
      Caller.Add_Test (Suite, "Test Measure",
                       Test_Measure_Copy'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Index",
                       Test_Index'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Rindex",
                       Test_Rindex'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Benchmark",
                       Test_Measure_Hash'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.String_Ref",
                       Test_String_Ref'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Starts_With",
                       Test_Starts_With'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Ends_With",
                       Test_Ends_With'Access);
      Caller.Add_Test (Suite, "Test perfect hash",
                       Test_Perfect_Hash'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Tokenizers.Iterate_Token",
                       Test_Iterate_Token'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Vectors perf",
                       Test_Perf_Vector'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Formats",
                       Test_Format'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Replace",
                       Test_Replace'Access);
   end Add_Tests;

   procedure Test_Escape_Javascript (T : in out Test) is
      Result : Unbounded_String;
   begin
      Escape_Javascript (Content => ASCII.LF & " ""a string"" a 'single quote'",
                         Into    => Result);
      Assert_Equals (T, "\n \""a string\"" a \'single quote\'", Result);

      Result := To_Unbounded_String ("");
      Escape_Javascript (Content => ASCII.ESC & "[m " & Character'Val (255),
                         Into    => Result);
      Assert_Equals (T, "\u001B[m " & Character'Val (255), Result);
      Assert_Equals (T, "\u001B[m " & Character'Val (255),
                     Escape_Javascript (ASCII.ESC & "[m " & Character'Val (255)));
   end Test_Escape_Javascript;

   procedure Test_Escape_Java (T : in out Test) is
      Result : Unbounded_String;
   begin
      Escape_Java (Content => ASCII.LF & " ""a string"" a 'single quote'",
                   Into    => Result);
      Assert_Equals (T, "\n \""a string\"" a 'single quote'", Result);

      Result := To_Unbounded_String ("");
      Escape_Javascript (Content => ASCII.CR & ASCII.LF & ASCII.HT & ASCII.BS
                           & ASCII.FF & "m " & Character'Val (255),
                         Into    => Result);
      Assert_Equals (T, "\r\n\t\b\fm " & Character'Val (255), Result);
      Assert_Equals (T, "\r\n\t\b\fm " & Character'Val (255),
                     Escape_Javascript (ASCII.CR & ASCII.LF & ASCII.HT & ASCII.BS
                                          & ASCII.FF & "m " & Character'Val (255)));
   end Test_Escape_Java;

   procedure Test_Escape_Xml (T : in out Test) is
      Result : Unbounded_String;
   begin
      Escape_Xml (Content => ASCII.LF & " < ""a string"" a 'single quote' >& ",
                  Into    => Result);
      Assert_Equals (T, ASCII.LF & " &lt; ""a string"" a &apos;single quote&apos; &gt;&amp; ",
                     Result);

      Result := To_Unbounded_String ("");
      Escape_Xml (Content => ASCII.ESC & "[m " & Character'Val (255),
                  Into    => Result);
      Assert_Equals (T, ASCII.ESC & "[m &#255;", Result);
<commit_msg>Add test for Escape_Java function<commit_after>-----------------------------------------------------------------------
--  util-strings-tests -- Unit tests for strings
--  Copyright (C) 2009, 2010, 2011, 2012, 2015, 2018, 2020, 2021, 2022 <NAME>
--  Written by <NAME> (<EMAIL>)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Ada.Strings;
with Ada.Strings.Unbounded;
with Ada.Strings.Fixed;
with Ada.Strings.Fixed.Hash;
with Ada.Strings.Unbounded.Hash;
with Ada.Containers;
with Util.Test_Caller;
with Util.Strings.Transforms;
with Util.Strings.Maps;
with Util.Strings.Vectors;
with Util.Strings.Formats;
with Util.Perfect_Hash;
with Util.Strings.Tokenizers;
with Ada.Streams;
with Util.Measures;
package body Util.Strings.Tests is

   use Ada.Strings.Unbounded;
   use Util.Tests;
   use Util.Strings.Transforms;

   package Caller is new Util.Test_Caller (Test, "Strings");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Escape_Javascript",
                       Test_Escape_Javascript'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Escape_Java",
                       Test_Escape_Java'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Escape_Xml",
                       Test_Escape_Xml'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Unescape_Xml",
                       Test_Unescape_Xml'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.Capitalize",
                       Test_Capitalize'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.To_Upper_Case",
                       Test_To_Upper_Case'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.To_Lower_Case",
                       Test_To_Lower_Case'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Transforms.To_Hex",
                       Test_To_Hex'Access);
      Caller.Add_Test (Suite, "Test Measure",
                       Test_Measure_Copy'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Index",
                       Test_Index'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Rindex",
                       Test_Rindex'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Benchmark",
                       Test_Measure_Hash'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.String_Ref",
                       Test_String_Ref'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Starts_With",
                       Test_Starts_With'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Ends_With",
                       Test_Ends_With'Access);
      Caller.Add_Test (Suite, "Test perfect hash",
                       Test_Perfect_Hash'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Tokenizers.Iterate_Token",
                       Test_Iterate_Token'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Vectors perf",
                       Test_Perf_Vector'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Formats",
                       Test_Format'Access);
      Caller.Add_Test (Suite, "Test Util.Strings.Replace",
                       Test_Replace'Access);
   end Add_Tests;

   procedure Test_Escape_Javascript (T : in out Test) is
      Result : Unbounded_String;
   begin
      Escape_Javascript (Content => ASCII.LF & " ""a string"" a 'single quote'",
                         Into    => Result);
      Assert_Equals (T, "\n \""a string\"" a \'single quote\'", Result);

      Result := To_Unbounded_String ("");
      Escape_Javascript (Content => ASCII.ESC & "[m " & Character'Val (255),
                         Into    => Result);
      Assert_Equals (T, "\u001B[m " & Character'Val (255), Result);
      Assert_Equals (T, "\u001B[m " & Character'Val (255),
                     Escape_Javascript (ASCII.ESC & "[m " & Character'Val (255)));
   end Test_Escape_Javascript;

   procedure Test_Escape_Java (T : in out Test) is
      Result : Unbounded_String;
   begin
      Escape_Java (Content => ASCII.LF & " ""a string"" a 'single quote'",
                   Into    => Result);
      Assert_Equals (T, "\n \""a string\"" a 'single quote'", Result);

      Assert_Equals (T, "\n \""a string\"" a 'single quote'",
                     Escape_Java (ASCII.LF & " ""a string"" a 'single quote'"));

      Result := To_Unbounded_String ("");
      Escape_Javascript (Content => ASCII.CR & ASCII.LF & ASCII.HT & ASCII.BS
                           & ASCII.FF & "m " & Character'Val (255),
                         Into    => Result);
      Assert_Equals (T, "\r\n\t\b\fm " & Character'Val (255), Result);
      Assert_Equals (T, "\r\n\t\b\fm " & Character'Val (255),
                     Escape_Javascript (ASCII.CR & ASCII.LF & ASCII.HT & ASCII.BS
                                          & ASCII.FF & "m " & Character'Val (255)));
   end Test_Escape_Java;

   procedure Test_Escape_Xml (T : in out Test) is
      Result : Unbounded_String;
   begin
      Escape_Xml (Content => ASCII.LF & " < ""a string"" a 'single quote' >& ",
                  Into    => Result);
      Assert_Equals (T, ASCII.LF & " &lt; ""a string"" a &apos;single quote&apos; &gt;&amp; ",
                     Result);

      Result := To_Unbounded_String ("");
      Escape_Xml (Content => ASCII.ESC & "[m " & Character'Val (255),
                  Into    => Result);
      Assert_Equals (T, ASCII.ESC & "[m &#255;", Result);

{'fraction_non_alphanumeric': 0.13906953476738368, 'fraction_numerical': 0.012673003168250792, 'mean_word_length': 2.1285863328116847, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '498370', 'n_tokens_mistral': 3773, 'n_tokens_neox': 3448, 'n_words': 873}