adacl-test_cases-parameters.ads

1--------------------------------------------------------------- {{{1 ----------
2--: Copyright © 2023 … 2023 Martin Krischik «krischik@users.sourceforge.net»
3------------------------------------------------------------------------------
4--: This library is free software; you can redistribute it and/or modify it
5--: under the terms of the GNU Library General Public License as published by
6--: the Free Software Foundation; either version 2 of the License, or (at your
7--: option) any later version.
8--:
9--: This library is distributed in the hope that it will be useful, but
10--: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11--: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12--: License for more details.
13--:
14--: You should have received a copy of the GNU Library General Public License
15--: along with this library; if not, write to the Free Software Foundation,
16--: Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17--------------------------------------------------------------- }}}1 ----------
18pragma License (Modified_Gpl);
19pragma Ada_2022;
20
21with AUnit;
22with AUnit.Test_Cases;
23private with Ada.Containers.Indefinite_Ordered_Maps;
24
25---
26-- @summary
27-- Calling AUnit test with parameters.
28--
29-- @description
30-- It's tedious to create a large amount of very similar AUnit tests who only differ in the input and expected data.
31-- This package tries to remedies this by aiding you in registering the test procedure multiple and then providing
32-- test data for each call.
33--
34--: @formal Input_Type type holding the input data.
35--: @formal Expected_Type type holding the expected result.
36--
37generic
38 type Input_Type (<>) is private;
39 type Expected_Type (<>) is private;
40package AdaCL.Test_Cases.Parameters is
41 use type AUnit.Message_String;
42
43 ---
44 -- Collection of input and expected parameter
45 --
46 type Parameter is tagged private;
47
48 ---
49 -- get input data to use.
50 --
51 --: @param P the collection of input and expected parameter
52 --: @param Routine_Name the name of the test. Only the first word is used to locate the data.
53 --: @return the input data
54 --
55 function Input
56 (P : in Parameter;
57 Routine_Name : AUnit.Message_String)
58 return Input_Type with
59 Pre => Routine_Name /= null;
60
61 ---
62 -- get the expected value
63 --
64 --: @param P the collection of input and expected parameter
65 --: @param Routine_Name the name of the test. Only the first word is used to locate the data.
66 --: @return the expected result
67 --
68 function Expected
69 (P : in Parameter;
70 Routine_Name : AUnit.Message_String)
71 return Expected_Type with
72 Pre => Routine_Name /= null;
73
74 ---
75 -- Register a test for given data.
76 --
77 -- Call multiple times with different names to call execute the test with different data
78 --
79 --: @param P the collection of input and expected parameter
80 --: @param T Test case to register the test with.
81 --: @param Routine Routine to call.
82 --: @param Name Name of the test. Must be unique and one word only.
83 --: @param Description Additional description. Is appended to the name to create the Routine_Name
84 --: @param Input Input test data.
85 --: @param Expected Expected test result.
86 procedure Register_Routine
87 (P : in out Parameter;
88 T : in out AUnit.Test_Cases.Test_Case'Class;
89 Routine : AUnit.Test_Cases.Test_Routine;
90 Name : in String;
91 Description : in String;
92 Input : in Input_Type;
93 Expected : in Expected_Type) with
94 Pre => Name'Length > 0 and then (for all I in Name'Range => (Name (I) /= ' '));
95
96private
97 ---
98 -- Map to hold the input values
99 --
100 package Input_Maps is new Ada.Containers.Indefinite_Ordered_Maps (String, Input_Type);
101
102 ---
103 -- Map to hold the expected values
104 --
105 package Expected_Maps is new Ada.Containers.Indefinite_Ordered_Maps (String, Expected_Type);
106
107 ---
108 -- Collection of input and expected parameter
109 --
110 --: @field Input collection of input parameter
111 --: @field Expected collection of expected parameter
112 --
113 type Parameter is tagged record
114 Input : Input_Maps.Map;
115 Expected : Expected_Maps.Map;
116 end record;
117
118end AdaCL.Test_Cases.Parameters;
119
120---------------------------------------------------------------- {{{ ----------
121--: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab :
122--: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr :
123--: vim: set spell spelllang=en_gb