Skip to content

Commit

Permalink
Extract increment_identifier, implement increment_build, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minhqdao committed Mar 2, 2023
1 parent aca529f commit 330842d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/version_f.f90
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module version_f
contains

procedure :: to_string, increment_major, increment_minor, increment_patch, &
& increment_prerelease
& increment_prerelease, increment_build

generic :: create => try_create
procedure, private :: try_create
Expand Down Expand Up @@ -231,24 +231,38 @@ pure subroutine increment_patch(this)
if (allocated(this%build)) deallocate (this%build)
end

!> If the last prerelease identifier is numeric, increment it by 1. Otherwise
!> add a new prerelease identifier with the value 1. Clear build metadata.
!> Increment prerelease and reset build data.
pure subroutine increment_prerelease(this)
class(version_t), intent(inout) :: this

if (allocated(this%prerelease)) then
if (this%prerelease(size(this%prerelease))%is_numeric()) then
this%prerelease = [this%prerelease(1:size(this%prerelease) - 1), &
& string_t(trim(int2s(this%prerelease(size(this%prerelease))%num() + 1)))]
call increment_identifier(this%prerelease)
if (allocated(this%build)) deallocate (this%build)
end

!> Increment build metadata.
pure 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
this%prerelease = [this%prerelease, string_t('1')]
ids = [ids, string_t('1')]
end if
else
allocate (this%prerelease(1))
this%prerelease(1)%str = '1'
allocate (ids(1))
ids(1)%str = '1'
end if

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

!> Parse a string into a version including prerelease and build data.
Expand Down
48 changes: 48 additions & 0 deletions test/test.f90
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,54 @@ program test
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3, build='1')
call v1%increment_build()
if (v1%to_string() /= '1.2.3+2') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3)
call v1%increment_build()
if (v1%to_string() /= '1.2.3+1') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3, 'abc')
call v1%increment_build()
if (v1%to_string() /= '1.2.3-abc+1') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3, 'abc', '123')
call v1%increment_build()
if (v1%to_string() /= '1.2.3-abc+124') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3, '123', '123')
call v1%increment_build()
if (v1%to_string() /= '1.2.3-123+124') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3, '123', 'abc')
call v1%increment_build()
if (v1%to_string() /= '1.2.3-123+abc.1') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3, '123', '78H')
call v1%increment_build()
if (v1%to_string() /= '1.2.3-123+78H.1') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

v1 = version_t(1, 2, 3, '123', '78-')
call v1%increment_build()
if (v1%to_string() /= '1.2.3-123+78-.1') then
call fail("Parsing failed for '"//v1%to_string()//"'")
end if

!################################### Parse ####################################!

call v1%parse('0', e)
Expand Down

0 comments on commit 330842d

Please sign in to comment.