Skip to content

Commit

Permalink
Automated test for string templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
godunko committed Sep 8, 2023
1 parent 8c78eb7 commit a16ea63
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ check_text:
.objs/tests/test_string_split
.objs/tests/test_string_split_lines
.objs/tests/test_string
.objs/tests/test_string_template
.objs/tests/test_string_vector
for f in testsuite/text/w3c-i18n-tests-casing/*.txt; do \
echo " $$f"; .objs/tests/test_string_casing_w3c_i18n $$f || return 1; \
Expand Down
1 change: 1 addition & 0 deletions gnat/tests/vss_text_tests.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ project VSS_Text_Tests is
"test_string_slice",
"test_string_split",
"test_string_split_lines",
"test_string_template",
"test_string_vector",
"test_word_iterators");

Expand Down
211 changes: 211 additions & 0 deletions testsuite/text/test_string_template.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
--
-- Copyright (C) 2023, AdaCore
--
-- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
--

with Interfaces;

with VSS.Strings.Formatters.Generic_Integers;
with VSS.Strings.Formatters.Strings;
with VSS.Strings.Templates;

with Test_Support;

procedure Test_String_Template is

procedure Test_Single_Placeholer;

procedure Test_Multiple_Placeholer;

procedure Test_Integer_Formatter;

----------------------------
-- Test_Integer_Formatter --
----------------------------

procedure Test_Integer_Formatter is

use type VSS.Strings.Virtual_String;

package Integer_Formatters is
new VSS.Strings.Formatters.Generic_Integers (Interfaces.Integer_128);

begin
-- Smallest negative value.

declare
Value : constant Interfaces.Integer_128 :=
Interfaces.Integer_128'First;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));
Image : constant VSS.Strings.Virtual_String :=
VSS.Strings.To_Virtual_String
(Interfaces.Integer_128'Wide_Wide_Image (Value));

begin
Test_Support.Assert (Text = Image);
end;

-- Largest positive value.

declare
Value : constant Interfaces.Integer_128 :=
Interfaces.Integer_128'Last;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
" {}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));
Image : constant VSS.Strings.Virtual_String :=
VSS.Strings.To_Virtual_String
(Interfaces.Integer_128'Wide_Wide_Image (Value));

begin
Test_Support.Assert (Text = Image);
end;

-- Zero value.

declare
Value : constant Interfaces.Integer_128 := 0;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
" {}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));
Image : constant VSS.Strings.Virtual_String :=
VSS.Strings.To_Virtual_String
(Interfaces.Integer_128'Wide_Wide_Image (Value));

begin
Test_Support.Assert (Text = Image);
end;

-- Fixed width, without zero padding, positive value

declare
Value : constant Interfaces.Integer_128 := 12345;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{:+10}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));

begin
Test_Support.Assert (Text = " +12345");
end;

-- Fixed width, with zero padding, positive value

declare
Value : constant Interfaces.Integer_128 := 12345;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{:+010}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));

begin
Test_Support.Assert (Text = "+0000012345");
end;

-- Fixed width, width overflow, positive value, sign padding

declare
-- use type Interfaces.Integer_128;

Value : constant Interfaces.Integer_128 := 1234567890;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{:-8}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));

begin
Test_Support.Assert (Text = " 1234567890");
end;

-- Base, fixed width, grouping

declare
Value : constant Interfaces.Integer_128 := 16#ABCD_1234#;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{:8#16_4}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));

begin
Test_Support.Assert (Text = "ABCD_1234");
end;

-- Base, fixed width, padding, grouping

declare
Value : constant Interfaces.Integer_128 := 16#EF#;
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{:08#16_4}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (Integer_Formatters.Image (Value));

begin
Test_Support.Assert (Text = "0000_00EF");
end;
end Test_Integer_Formatter;

------------------------------
-- Test_Multiple_Placeholer --
------------------------------

procedure Test_Multiple_Placeholer is

use type VSS.Strings.Virtual_String;

Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{}:{}:{}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format
(VSS.Strings.Formatters.Strings.Image ("a"),
VSS.Strings.Formatters.Strings.Image ("b"),
VSS.Strings.Formatters.Strings.Image ("c"));

begin
Test_Support.Assert (Text = "a:b:c");
end Test_Multiple_Placeholer;

----------------------------
-- Test_Single_Placeholer --
----------------------------

procedure Test_Single_Placeholer is

use type VSS.Strings.Virtual_String;

begin
-- Placeholder only

declare
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"{}";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (VSS.Strings.Formatters.Strings.Image ("world"));

begin
Test_Support.Assert (Text = "world");
end;

-- Placeholder inside the text

declare
Template : constant VSS.Strings.Templates.Virtual_String_Template :=
"Hello, {}!";
Text : constant VSS.Strings.Virtual_String :=
Template.Format (VSS.Strings.Formatters.Strings.Image ("world"));

begin
Test_Support.Assert (Text = "Hello, world!");
end;
end Test_Single_Placeholer;

begin
Test_Single_Placeholer;
Test_Multiple_Placeholer;
Test_Integer_Formatter;
end Test_String_Template;

0 comments on commit a16ea63

Please sign in to comment.