Skip to content

Commit

Permalink
Remove examples, remove more code, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minhqdao committed Mar 6, 2024
1 parent 4270dad commit b512b0e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 267 deletions.
51 changes: 0 additions & 51 deletions example/ex1.f90

This file was deleted.

66 changes: 0 additions & 66 deletions example/ex2.f90

This file was deleted.

148 changes: 2 additions & 146 deletions src/version_f.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module version_f
implicit none
private

public :: version_t, string_t, error_t, is_version, version_range_t, &
public :: version_t, string_t, error_t, version_range_t, &
comparator_set_t, comparator_t, operator_index

type :: string_t
Expand Down Expand Up @@ -37,9 +37,7 @@ module version_f

contains

procedure :: to_string, increment_major, increment_minor, increment_patch, &
& increment_prerelease, increment_build, is_exactly, satisfies, try_satisfy, &
& satisfies_comp_set, satisfies_comp, is_stable
procedure :: to_string, try_satisfy, satisfies_comp_set, satisfies_comp

generic :: create => try_create
procedure, private :: try_create
Expand Down Expand Up @@ -253,73 +251,6 @@ pure function to_string(this) result(str)
end if
end

!> Increments the major version number and resets the minor and patch number
!> as well as the prerelease and build data.
elemental subroutine increment_major(this)
class(version_t), intent(inout) :: this

this%major = this%major + 1
this%minor = 0
this%patch = 0

if (allocated(this%prerelease)) deallocate (this%prerelease)
if (allocated(this%build)) deallocate (this%build)
end

!> Increments the minor version number and resets patch, prerelease and build.
elemental subroutine increment_minor(this)
class(version_t), intent(inout) :: this

this%minor = this%minor + 1
this%patch = 0

if (allocated(this%prerelease)) deallocate (this%prerelease)
if (allocated(this%build)) deallocate (this%build)
end

!> Increments the patch version number and resets prerelease and build.
elemental subroutine increment_patch(this)
class(version_t), intent(inout) :: this

this%patch = this%patch + 1

if (allocated(this%prerelease)) deallocate (this%prerelease)
if (allocated(this%build)) deallocate (this%build)
end

!> Increment prerelease and reset build data.
elemental subroutine increment_prerelease(this)
class(version_t), intent(inout) :: this

call increment_identifier(this%prerelease)
if (allocated(this%build)) deallocate (this%build)
end

!> Increment build metadata.
elemental subroutine increment_build(this)
class(version_t), intent(inout) :: this

call increment_identifier(this%build)
end

!> Increment prerelease or build identifiers. If the last identifier is
!> numeric, increment it by 1. Otherwise add a new identifier with the value
!> 1.
pure subroutine increment_identifier(ids)
type(string_t), allocatable, intent(inout) :: ids(:)

if (allocated(ids)) then
if (ids(size(ids))%is_numeric()) then
ids = [ids(1:size(ids) - 1), string_t(trim(int2s(ids(size(ids))%num() + 1)))]
else
ids = [ids, string_t('1')]
end if
else
allocate (ids(1))
ids(1)%str = '1'
end if
end

!> Parse a string into a version including prerelease and build data.
!>
!> Wrapper function for `try_parse`.
Expand Down Expand Up @@ -696,53 +627,6 @@ pure logical function is_greater(lhs, rhs)
is_greater = size(lhs) > size(rhs)
end

!> True if both versions are exactly the same including the build metadata.
!> This procedure has been added for conveniece. It is not part of the
!> Semantic Versioning 2.0.0 specification.
elemental logical function is_exactly(self, other)
class(version_t), intent(in) :: self
type(version_t), intent(in) :: other

integer :: i

is_exactly = self == other;
if (.not. is_exactly) return

if (allocated(self%build) .and. allocated(other%build)) then
if (size(self%build) /= size(other%build)) then
is_exactly = .false.; return
end if

do i = 1, size(self%build)
if (self%build(i)%str /= other%build(i)%str) then
is_exactly = .false.; return
end if
end do
else if (allocated(self%build) .or. allocated(other%build)) then
is_exactly = .false.; return
end if
end

!> True if the string can be parsed as a valid `version_t`. Use `parse` if you
!> wish to receive detailed error messages. In strict mode, all major, minor
!> and patch versions must be provided. Implicit zeros are forbidden in strict
!> mode.
logical function is_version(str, strict_mode)

!> Input string.
character(*), intent(in) :: str

!> If true, all major, minor and patch versions must be provided. Implicit
!> zeros are forbidden in strict mode.
logical, optional, intent(in) :: strict_mode

type(version_t) :: version
type(error_t), allocatable :: error

call version%parse(str, error, strict_mode)
is_version = .not. allocated(error)
end

!> Helper function to generate a new `string_t` instance.
elemental function create_string_t(inp_str) result(string)

Expand Down Expand Up @@ -825,24 +709,6 @@ subroutine try_satisfy(this, string, is_satisfied, error)
end do
end

!> Convenience function to determine whether the version meets the comparison.
!>
!> Wrapper function for `try_satisfy`, which returns `.false.` if the
!> comparison fails.
logical function satisfies(this, str)

!> Instance of `version_t` to be evaluated.
class(version_t), intent(in) :: this

!> Input string to be evaluated.
character(*), intent(in) :: str

type(error_t), allocatable :: error

call this%try_satisfy(str, satisfies, error)
if (allocated(error)) satisfies = .false.
end

!> Create sets of comparators that are separated by `||`. An example of a
!> version range is `4.2.3 || 5.0.0 - 7.2.3`.
subroutine parse_version_range(this, string, error)
Expand Down Expand Up @@ -1084,14 +950,4 @@ pure function create_comp_set(comps) result(comp_set)

comp_set%comps = comps
end

!> Returns true if the version is stable. A version is stable if its major
!> version is greater than zero and the version is not a prerelease.
elemental logical function is_stable(version)

!> Instance of `version_t` to be evaluated.
class(version_t), intent(in) :: version

is_stable = version%major > 0 .and. .not. allocated(version%prerelease)
end
end
16 changes: 12 additions & 4 deletions test/version_f_test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ program test

implicit none

type(version_t) :: v1, v2
type(version_t) :: v1
logical :: is_satisfied
type(comparator_t), allocatable :: comps(:)
type(comparator_set_t) :: comp_set
type(version_range_t) :: range
type(error_t), allocatable :: e

!##################################try_satisfy#################################!
Expand Down Expand Up @@ -242,3 +239,14 @@ program test
call v1%try_satisfy('0.1.0+abc', is_satisfied, e)
if (.not. is_satisfied) call fail('satisfy-58 should satisfy.')
if (allocated(e)) call fail('satisfy-58 should not fail.')

print *, achar(10)//achar(27)//'[92m All tests passed.'//achar(27)

contains

subroutine fail(msg)
character(*), intent(in) :: msg
print *, achar(27)//'[31m'//'Test failed: '//msg//achar(27)//'[0m'
stop 1
end
end program

0 comments on commit b512b0e

Please sign in to comment.