|
| 1 | +! Copyright (c) 2024, The Regents of the University of California and Sourcery Institute |
| 2 | +! Terms of use are as specified in LICENSE.txt |
| 3 | + |
| 4 | +#include "language-support.F90" |
| 5 | + |
| 6 | +module assert_test_m |
| 7 | + !! Test Julienne's assert generic interface |
| 8 | + |
| 9 | + use julienne_m, only : & |
| 10 | + assert & |
| 11 | + ,test_diagnosis_t & |
| 12 | + ,test_t & |
| 13 | + ,test_description_t & |
| 14 | + ,test_description_substring & |
| 15 | + ,test_result_t & |
| 16 | + ,operator(.approximates.) & |
| 17 | + ,operator(.within.) |
| 18 | + implicit none |
| 19 | + |
| 20 | + private |
| 21 | + public :: assert_test_t |
| 22 | + |
| 23 | + type, extends(test_t) :: assert_test_t |
| 24 | + contains |
| 25 | + procedure, nopass :: subject |
| 26 | + procedure, nopass :: results |
| 27 | + end type |
| 28 | + |
| 29 | +contains |
| 30 | + |
| 31 | + pure function subject() result(specimen) |
| 32 | + character(len=:), allocatable :: specimen |
| 33 | + specimen = "The assert generic interface" |
| 34 | + end function |
| 35 | + |
| 36 | +#if HAVE_PROCEDURE_ACTUAL_FOR_POINTER_DUMMY |
| 37 | + |
| 38 | + function results() result(test_results) |
| 39 | + type(test_result_t), allocatable :: test_results(:) |
| 40 | + |
| 41 | + associate(descriptions => [ & |
| 42 | + test_description_t("invocation with an test_diagnosis_t expression", check_test_diagnosis_expression) & |
| 43 | + ]) |
| 44 | + associate(substring_in_subject => index(subject(), test_description_substring) /= 0) |
| 45 | + associate(substring_in_assertion_diagnosis => descriptions%contains_text(test_description_substring)) |
| 46 | + associate(matching_descriptions => pack(descriptions, substring_in_subject .or. substring_in_assertion_diagnosis)) |
| 47 | + test_results = matching_descriptions%run() |
| 48 | + end associate |
| 49 | + end associate |
| 50 | + end associate |
| 51 | + end associate |
| 52 | + |
| 53 | + end function |
| 54 | + |
| 55 | +#else |
| 56 | + |
| 57 | + function results() result(test_results) |
| 58 | + use julienne_m, only : diagnosis_function_i |
| 59 | + !! Work around missing Fortran 2008 feature: associating a procedure actual argument with a procedure pointer dummy argument |
| 60 | + type(test_result_t), allocatable :: test_results(:) |
| 61 | + type(test_description_t), allocatable :: descriptions(:) |
| 62 | + procedure(diagnosis_function_i), pointer :: & |
| 63 | + check_test_diagnosis_expression_ptr => check_test_diagnosis_expression |
| 64 | + |
| 65 | + descriptions = [ & |
| 66 | + test_description_t("invocation with an test_diagnosis_t expression", check_test_diagnosis_expression_ptr) & |
| 67 | + ] |
| 68 | + |
| 69 | + block |
| 70 | + logical substring_in_subject |
| 71 | + logical, allocatable :: substring_in_assertion_diagnosis(:) |
| 72 | + type(test_description_t), allocatable :: matching_descriptions(:) |
| 73 | + |
| 74 | + substring_in_subject = index(subject(), test_description_substring) /= 0 |
| 75 | + substring_in_assertion_diagnosis = descriptions%contains_text(test_description_substring) |
| 76 | + matching_descriptions = pack(descriptions, substring_in_subject .or. substring_in_assertion_diagnosis) |
| 77 | + test_results = matching_descriptions%run() |
| 78 | + end block |
| 79 | + end function |
| 80 | + |
| 81 | +#endif |
| 82 | + |
| 83 | + function check_test_diagnosis_expression() result(test_diagnosis) |
| 84 | + type(test_diagnosis_t) test_diagnosis |
| 85 | + call assert(1. .approximates. 2. .within. 3.) |
| 86 | + test_diagnosis = test_diagnosis_t(.true., "") |
| 87 | + end function |
| 88 | + |
| 89 | +end module assert_test_m |
0 commit comments