Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Split ScheduleHelpers module in smaller files

Christophe Bliardca63083e3ab5
diff --git a/spec/support/schedule_helpers.rb b/spec/support/schedule_helpers.rb
index 2c8781242f8..8e7836a12b7 100644
--- a/spec/support/schedule_helpers.rb
+++ b/spec/support/schedule_helpers.rb
@@ -26,416 +26,7 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module ScheduleHelpers
- # Contains work packages and relations information from a chart
- # representation.
- #
- # The work package information are:
- # * subject
- # * start_date
- # * due_date
- #
- # The relations information are limited to follows relations and are retrieved
- # with +#predecessors_by_follower+
- #
- # The chart uses different symbols in the timeline to represent a work package
- # start and due dates:
- # * +X+: a day of the work package duration. The first +X+ is the start date,
- # the last +X+ is the due date.
- # * +[+: the work package start date. Can be used instead of +X+ when the work
- # package has no due date.
- # * +]+: the work package due date. Can be used instead of +X+ when the work
- # package has no start date.
- # * +_+: ignored but useful as a placeholder to highlight particular days, for
- # instance to highlight the previous dates of a work package.
- class Chart
- attr_reader :first_day, :last_day, :monday
-
- def self.for(representation)
- builder = ChartBuilder.new
- builder.parse(representation)
- end
-
- def self.from_work_packages(work_packages)
- ChartBuilder.new.use_work_packages(Array(work_packages))
- end
-
- def initialize
- self.monday = next_monday
- end
-
- # Sets the origin of the calendar, represented by +M+ on the first line (M as
- # in Monday).
- def monday=(monday)
- raise ArgumentError, "#{monday} is not a Monday" unless monday.wday == 1
-
- extend_calendar_range(monday, monday + 6.days)
- @monday = monday
- end
-
- def validate
- work_packages.each_key do |follower|
- predecessors_by_follower(follower).each do |predecessor|
- if !work_packages.has_key?(predecessor)
- raise "unable to find predecessor #{predecessor.inspect} " \
- "in property \"follows #{predecessor}\" " \
- "for work package #{follower.inspect}"
- end
- end
- end
- end
-
- def work_packages
- @work_packages ||= {}
- end
-
- def work_package_attributes(name)
- work_packages[name.to_sym]
- end
-
- def delay_between(predecessor:, follower:)
- delays_between.fetch([predecessor, follower])
- end
-
- def predecessors_by_follower(follower)
- @predecessors_by_follower ||= Hash.new { |h, k| h[k] = [] }
- @predecessors_by_follower[follower]
- end
-
- def add_work_package(attributes)
- name = attributes[:subject].to_sym
- extend_calendar_range(*attributes.values_at(:start_date, :due_date))
- work_packages[name] = attributes
- end
-
- def add_follows_relation(predecessor:, follower:, delay:)
- predecessors_by_follower(follower) << predecessor
- delays_between[[predecessor, follower]] = delay
- end
-
- def to_s
- representer = ChartRepresenter.new
- representer.add_row
- representer.add_cell('days')
- representer.add_cell(spaced_at(monday, 'MTWTFSS'))
- work_packages.each do |name, attributes|
- representer.add_row
- representer.add_cell(name.to_s)
- representer.add_cell(span(attributes))
- end
- representer.to_s
- end
-
- private
-
- def extend_calendar_range(*dates)
- @first_day = [@first_day, *dates].compact.min
- @last_day = [@last_day, *dates].compact.max
- end
-
- def spaced_at(date, text)
- nb_days = date - first_day
- (" " * nb_days) + text
- end
-
- def span(attributes)
- case attributes
- in { start_date: nil, due_date: nil }
- ''
- in { start_date:, due_date: nil }
- spaced_at(start_date, '[')
- in { start_date: nil, due_date: }
- spaced_at(due_date, ']')
- in { start_date:, due_date: }
- days = WorkPackages::Shared::WorkingDays.new
- span = (start_date..due_date).map do |date|
- days.working?(date) ? 'X' : '.'
- end.join
- spaced_at(start_date, span)
- end
- end
-
- def next_monday
- date = Time.zone.today
- date += 1.day while date.wday != 1
- date
- end
-
- def delays_between
- @delays_between ||= Hash.new(0)
- end
- end
-
- class ChartRepresenter
- LINE = "%<id>s | %<days>s |".freeze
-
- def add_row
- rows << []
- end
-
- def add_cell(text)
- rows.last << text
- end
-
- def rows
- @rows ||= []
- end
-
- def to_s
- line_template = "%<id>-#{columns_size[0]}s | %<days>-#{columns_size[1]}s |"
- rows.map do |row|
- line_template % { id: row[0], days: row[1] }
- end.join("\n")
- end
-
- def columns
- rows.transpose
- end
-
- def columns_size
- columns.map { |column| column.map(&:length).max }
- end
- end
-
- # Builds a +Chart+ instance from a visual chart representation.
- #
- # Example:
- #
- # ChartBuilder.new.parse(<<~CHART)
- # days | MTWTFSS |
- # main | XX |
- # follower | XXX | follows main
- # start_only | [ |
- # due_only | ] |
- # no_dates | |
- # CHART
- class ChartBuilder
- attr_reader :chart
-
- def initialize
- @chart = Chart.new
- end
-
- def parse(representation)
- lines = representation.split("\n")
- header = lines.shift
- parse_header(header)
- lines.each do |line|
- parse_line(line)
- end
- chart.validate
- chart
- end
-
- def use_work_packages(work_packages)
- work_packages.each do |work_package|
- chart.add_work_package(work_package.slice(:subject, :start_date, :due_date))
- end
- chart
- end
-
- private
-
- def parse_header(header)
- _, week_days = header.split(' | ', 2)
- unless week_days.include?('MTWTFSS')
- raise ArgumentError, "First header line of schedule chart must contain MTWTFSS to indicate day names and have an origin"
- end
-
- @nb_days_from_origin_monday = week_days.index('M')
- end
-
- def parse_line(line)
- case line
- when ''
- # noop
- when / \| /
- parse_work_package_line(line)
- else
- raise "unable to parse line #{line.inspect}"
- end
- end
-
- def parse_work_package_line(line)
- name, timespan, properties = line.split(' | ', 3)
- name.strip!
- attributes = { subject: name }
- attributes.update(parse_timespan(timespan))
- chart.add_work_package(attributes)
-
- properties.to_s.split(',').map(&:strip).each do |property|
- parse_properties(name, property)
- end
- end
-
- def parse_properties(name, property)
- case property
- when /^follows (\w+)(?: with delay (\d+))?/
- chart.add_follows_relation(
- predecessor: $1.to_sym,
- follower: name.to_sym,
- delay: $2.to_i
- )
- else
- raise "unable to parse property #{property.inspect} for line #{name.inspect}"
- end
- end
-
- def parse_timespan(timespan)
- start_pos = timespan.index('[') || timespan.index('X')
- due_pos = timespan.rindex(']') || timespan.rindex('X')
- {
- start_date: start_pos && (chart.monday - @nb_days_from_origin_monday + start_pos),
- due_date: due_pos && (chart.monday - @nb_days_from_origin_monday + due_pos)
- }
- end
- end
-
- module LetSchedule
- # Declare work packages and relations from a visual chart representation.
- #
- # For instance:
- #
- # let_schedule(<<~CHART)
- # days | MTWTFSS |
- # main | XX |
- # follower | XXX | follows main
- # start_only | [ |
- # due_only | ] |
- # CHART
- #
- # is equivalent to:
- #
- # let!(:main) do
- # create(:work_package, subject: 'main', start_date: next_monday, due_date: next_monday + 1.day)
- # end
- # let!(:follower) do
- # create(:work_package, subject: 'follower', start_date: next_monday + 2.days, due_date: next_monday + 4.days) }
- # end
- # let!(:relation_follower_follows_main) do
- # create(:follows_relation, from: follower, to: main, delay: 0) }
- # end
- # let!(:start_only) do
- # create(:work_package, subject: 'start_only', start_date: next_monday + 1.day) }
- # end
- # let!(:due_only) do
- # create(:work_package, subject: 'due_only', due_date: next_monday + 3.days) }
- # end
- def let_schedule(chart_representation, **extra_attributes)
- # To be able to use `travel_to` in a before hook, the dates in the chart
- # must be lazy evaluated in a let statement.
- let(:schedule_chart) { Chart.for(chart_representation) }
-
- # we still need to parse the chart to get the work package names and relations
- chart = Chart.for(chart_representation)
- chart.work_packages.each_key do |name|
- let!(name) do
- create(:work_package, schedule_chart.work_package_attributes(name).reverse_merge(extra_attributes))
- end
- chart.predecessors_by_follower(name).each do |predecessor|
- relation_alias = "relation_#{name}_follows_#{predecessor}"
- let!(relation_alias) do
- create(:follows_relation,
- from: send(name),
- to: send(predecessor),
- delay: schedule_chart.delay_between(predecessor:, follower: name))
- end
- end
- end
- end
- end
-
- module ExampleMethods
- # Update the given work packages according to the given chart representation.
- # Work packages are changed without being saved.
- #
- # For instance:
- #
- # before do
- # change_schedule([main], <<~CHART)
- # days | MTWTFSS |
- # main | XX |
- # CHART
- # end
- #
- # is equivalent to:
- #
- # before do
- # main.start_date = monday
- # main.due_date = tuesday
- # end
- def update_schedule(work_packages, chart)
- change_schedule(work_packages, chart)
- work_packages.each(&:save!)
- end
-
- # Change the given work packages according to the given chart representation.
- # Work packages are changed without being saved.
- #
- # For instance:
- #
- # before do
- # change_schedule([main], <<~CHART)
- # days | MTWTFSS |
- # main | XX |
- # CHART
- # end
- #
- # is equivalent to:
- #
- # before do
- # main.start_date = monday
- # main.due_date = tuesday
- # end
- def change_schedule(work_packages, chart)
- by_id = work_packages.index_by(&:subject)
- chart = Chart.for(chart)
- chart.work_packages.each do |name, attributes|
- raise ArgumentError, "unable to find WorkPackage :#{name}" unless respond_to?(name)
-
- work_package = send(name)
- work_package = by_id[work_package.id] if by_id.has_key?(work_package.id)
- attributes.slice(:start_date, :due_date).each do |attribute, value|
- work_package.send(:"#{attribute}=", value)
- end
- end
- end
-
- # Expect the given work packages to match a visual chart representation.
- #
- # For instance:
- #
- # it 'is scheduled' do
- # expect_schedule(work_packages, <<~CHART)
- # days | MTWTFSS |
- # main | XX |
- # follower | XXX |
- # CHART
- # end
- #
- # is equivalent to:
- #
- # it 'is scheduled' do
- # main = work_packages.find { _1.id == main.id } || main
- # expect(main.start_date).to eq(next_monday)
- # expect(main.due_date).to eq(next_monday + 1.day)
- # follower = work_packages.find { _1.id == follower.id } || follower
- # expect(follower.start_date).to eq(next_monday + 2.days)
- # expect(follower.due_date).to eq(next_monday + 4.days)
- # end
- def expect_schedule(work_packages, chart)
- by_id = work_packages.index_by(&:id)
- chart = Chart.for(chart)
- chart.work_packages.each do |name, attributes|
- raise ArgumentError, "unable to find WorkPackage :#{name}" unless respond_to?(name)
-
- work_package = send(name)
- work_package = by_id[work_package.id] if by_id.has_key?(work_package.id)
- expect(work_package).to have_attributes(attributes.slice(:subject, :start_date, :due_date))
- end
- end
- end
-end
+Dir[Rails.root.join('spec/support/schedule_helpers/*.rb')].each { |f| require f }
RSpec.configure do |config|
config.extend ScheduleHelpers::LetSchedule
diff --git a/spec/support/schedule_helpers/chart.rb b/spec/support/schedule_helpers/chart.rb
new file mode 100644
index 00000000000..c8f7b8c192a
--- /dev/null
+++ b/spec/support/schedule_helpers/chart.rb
@@ -0,0 +1,168 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ # Contains work packages and relations information from a chart
+ # representation.
+ #
+ # The work package information are:
+ # * subject
+ # * start_date
+ # * due_date
+ #
+ # The relations information are limited to follows relations and are retrieved
+ # with +#predecessors_by_follower+
+ #
+ # The chart uses different symbols in the timeline to represent a work package
+ # start and due dates:
+ # * +X+: a day of the work package duration. The first +X+ is the start date,
+ # the last +X+ is the due date.
+ # * +[+: the work package start date. Can be used instead of +X+ when the work
+ # package has no due date.
+ # * +]+: the work package due date. Can be used instead of +X+ when the work
+ # package has no start date.
+ # * +_+: ignored but useful as a placeholder to highlight particular days, for
+ # instance to highlight the previous dates of a work package.
+ class Chart
+ attr_reader :first_day, :last_day, :monday
+
+ def self.for(representation)
+ builder = ChartBuilder.new
+ builder.parse(representation)
+ end
+
+ def self.from_work_packages(work_packages)
+ ChartBuilder.new.use_work_packages(Array(work_packages))
+ end
+
+ def initialize
+ self.monday = next_monday
+ end
+
+ # Sets the origin of the calendar, represented by +M+ on the first line (M as
+ # in Monday).
+ def monday=(monday)
+ raise ArgumentError, "#{monday} is not a Monday" unless monday.wday == 1
+
+ extend_calendar_range(monday, monday + 6.days)
+ @monday = monday
+ end
+
+ def validate
+ work_packages.each_key do |follower|
+ predecessors_by_follower(follower).each do |predecessor|
+ if !work_packages.has_key?(predecessor)
+ raise "unable to find predecessor #{predecessor.inspect} " \
+ "in property \"follows #{predecessor}\" " \
+ "for work package #{follower.inspect}"
+ end
+ end
+ end
+ end
+
+ def work_packages
+ @work_packages ||= {}
+ end
+
+ def work_package_attributes(name)
+ work_packages[name.to_sym]
+ end
+
+ def delay_between(predecessor:, follower:)
+ delays_between.fetch([predecessor, follower])
+ end
+
+ def predecessors_by_follower(follower)
+ @predecessors_by_follower ||= Hash.new { |h, k| h[k] = [] }
+ @predecessors_by_follower[follower]
+ end
+
+ def add_work_package(attributes)
+ name = attributes[:subject].to_sym
+ extend_calendar_range(*attributes.values_at(:start_date, :due_date))
+ work_packages[name] = attributes
+ end
+
+ def add_follows_relation(predecessor:, follower:, delay:)
+ predecessors_by_follower(follower) << predecessor
+ delays_between[[predecessor, follower]] = delay
+ end
+
+ def to_s
+ representer = ChartRepresenter.new
+ representer.add_row
+ representer.add_cell('days')
+ representer.add_cell(spaced_at(monday, 'MTWTFSS'))
+ work_packages.each do |name, attributes|
+ representer.add_row
+ representer.add_cell(name.to_s)
+ representer.add_cell(span(attributes))
+ end
+ representer.to_s
+ end
+
+ private
+
+ def extend_calendar_range(*dates)
+ @first_day = [@first_day, *dates].compact.min
+ @last_day = [@last_day, *dates].compact.max
+ end
+
+ def spaced_at(date, text)
+ nb_days = date - first_day
+ (" " * nb_days) + text
+ end
+
+ def span(attributes)
+ case attributes
+ in { start_date: nil, due_date: nil }
+ ''
+ in { start_date:, due_date: nil }
+ spaced_at(start_date, '[')
+ in { start_date: nil, due_date: }
+ spaced_at(due_date, ']')
+ in { start_date:, due_date: }
+ days = WorkPackages::Shared::WorkingDays.new
+ span = (start_date..due_date).map do |date|
+ days.working?(date) ? 'X' : '.'
+ end.join
+ spaced_at(start_date, span)
+ end
+ end
+
+ def next_monday
+ date = Time.zone.today
+ date += 1.day while date.wday != 1
+ date
+ end
+
+ def delays_between
+ @delays_between ||= Hash.new(0)
+ end
+ end
+end
diff --git a/spec/support/schedule_helpers/chart_builder.rb b/spec/support/schedule_helpers/chart_builder.rb
new file mode 100644
index 00000000000..51775b9e941
--- /dev/null
+++ b/spec/support/schedule_helpers/chart_builder.rb
@@ -0,0 +1,123 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ # Builds a +Chart+ instance from a visual chart representation.
+ #
+ # Example:
+ #
+ # ChartBuilder.new.parse(<<~CHART)
+ # days | MTWTFSS |
+ # main | XX |
+ # follower | XXX | follows main
+ # start_only | [ |
+ # due_only | ] |
+ # no_dates | |
+ # CHART
+ class ChartBuilder
+ attr_reader :chart
+
+ def initialize
+ @chart = Chart.new
+ end
+
+ def parse(representation)
+ lines = representation.split("\n")
+ header = lines.shift
+ parse_header(header)
+ lines.each do |line|
+ parse_line(line)
+ end
+ chart.validate
+ chart
+ end
+
+ def use_work_packages(work_packages)
+ work_packages.each do |work_package|
+ chart.add_work_package(work_package.slice(:subject, :start_date, :due_date))
+ end
+ chart
+ end
+
+ private
+
+ def parse_header(header)
+ _, week_days = header.split(' | ', 2)
+ unless week_days.include?('MTWTFSS')
+ raise ArgumentError, "First header line of schedule chart must contain MTWTFSS to indicate day names and have an origin"
+ end
+
+ @nb_days_from_origin_monday = week_days.index('M')
+ end
+
+ def parse_line(line)
+ case line
+ when ''
+ # noop
+ when / \| /
+ parse_work_package_line(line)
+ else
+ raise "unable to parse line #{line.inspect}"
+ end
+ end
+
+ def parse_work_package_line(line)
+ name, timespan, properties = line.split(' | ', 3)
+ name.strip!
+ attributes = { subject: name }
+ attributes.update(parse_timespan(timespan))
+ chart.add_work_package(attributes)
+
+ properties.to_s.split(',').map(&:strip).each do |property|
+ parse_properties(name, property)
+ end
+ end
+
+ def parse_properties(name, property)
+ case property
+ when /^follows (\w+)(?: with delay (\d+))?/
+ chart.add_follows_relation(
+ predecessor: $1.to_sym,
+ follower: name.to_sym,
+ delay: $2.to_i
+ )
+ else
+ raise "unable to parse property #{property.inspect} for line #{name.inspect}"
+ end
+ end
+
+ def parse_timespan(timespan)
+ start_pos = timespan.index('[') || timespan.index('X')
+ due_pos = timespan.rindex(']') || timespan.rindex('X')
+ {
+ start_date: start_pos && (chart.monday - @nb_days_from_origin_monday + start_pos),
+ due_date: due_pos && (chart.monday - @nb_days_from_origin_monday + due_pos)
+ }
+ end
+ end
+end
diff --git a/spec/support/schedule_helpers/chart_representer.rb b/spec/support/schedule_helpers/chart_representer.rb
new file mode 100644
index 00000000000..6a329bd0818
--- /dev/null
+++ b/spec/support/schedule_helpers/chart_representer.rb
@@ -0,0 +1,60 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ class ChartRepresenter
+ LINE = "%<id>s | %<days>s |".freeze
+
+ def add_row
+ rows << []
+ end
+
+ def add_cell(text)
+ rows.last << text
+ end
+
+ def rows
+ @rows ||= []
+ end
+
+ def to_s
+ line_template = "%<id>-#{columns_size[0]}s | %<days>-#{columns_size[1]}s |"
+ rows.map do |row|
+ line_template % { id: row[0], days: row[1] }
+ end.join("\n")
+ end
+
+ def columns
+ rows.transpose
+ end
+
+ def columns_size
+ columns.map { |column| column.map(&:length).max }
+ end
+ end
+end
diff --git a/spec/support/schedule_helpers/example_methods.rb b/spec/support/schedule_helpers/example_methods.rb
new file mode 100644
index 00000000000..edb612433c8
--- /dev/null
+++ b/spec/support/schedule_helpers/example_methods.rb
@@ -0,0 +1,119 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ module ExampleMethods
+ # Update the given work packages according to the given chart representation.
+ # Work packages are changed without being saved.
+ #
+ # For instance:
+ #
+ # before do
+ # change_schedule([main], <<~CHART)
+ # days | MTWTFSS |
+ # main | XX |
+ # CHART
+ # end
+ #
+ # is equivalent to:
+ #
+ # before do
+ # main.start_date = monday
+ # main.due_date = tuesday
+ # end
+ def update_schedule(work_packages, chart)
+ change_schedule(work_packages, chart)
+ work_packages.each(&:save!)
+ end
+
+ # Change the given work packages according to the given chart representation.
+ # Work packages are changed without being saved.
+ #
+ # For instance:
+ #
+ # before do
+ # change_schedule([main], <<~CHART)
+ # days | MTWTFSS |
+ # main | XX |
+ # CHART
+ # end
+ #
+ # is equivalent to:
+ #
+ # before do
+ # main.start_date = monday
+ # main.due_date = tuesday
+ # end
+ def change_schedule(work_packages, chart)
+ Chart.for(chart).work_packages.each do |name, attributes|
+ work_package = work_packages.find { |wp| wp.subject == attributes[:subject] }
+ unless work_package
+ raise ArgumentError, "unable to find a WorkPackage with name :#{name}. Got #{work_packages.pluck(:subject).to_sentence}"
+ end
+
+ attributes.slice(:start_date, :due_date).each do |attribute, value|
+ work_package.send(:"#{attribute}=", value)
+ end
+ end
+ end
+
+ # Expect the given work packages to match a visual chart representation.
+ #
+ # For instance:
+ #
+ # it 'is scheduled' do
+ # expect_schedule(work_packages, <<~CHART)
+ # days | MTWTFSS |
+ # main | XX |
+ # follower | XXX |
+ # CHART
+ # end
+ #
+ # is equivalent to:
+ #
+ # it 'is scheduled' do
+ # main = work_packages.find { _1.id == main.id } || main
+ # expect(main.start_date).to eq(next_monday)
+ # expect(main.due_date).to eq(next_monday + 1.day)
+ # follower = work_packages.find { _1.id == follower.id } || follower
+ # expect(follower.start_date).to eq(next_monday + 2.days)
+ # expect(follower.due_date).to eq(next_monday + 4.days)
+ # end
+ def expect_schedule(work_packages, chart)
+ by_id = work_packages.index_by(&:id)
+ chart = Chart.for(chart)
+ chart.work_packages.each do |name, attributes|
+ raise ArgumentError, "unable to find WorkPackage :#{name}" unless respond_to?(name)
+
+ work_package = send(name)
+ work_package = by_id[work_package.id] if by_id.has_key?(work_package.id)
+ expect(work_package).to have_attributes(attributes.slice(:subject, :start_date, :due_date))
+ end
+ end
+ end
+end
diff --git a/spec/support/schedule_helpers/let_schedule.rb b/spec/support/schedule_helpers/let_schedule.rb
new file mode 100644
index 00000000000..ff436eea65f
--- /dev/null
+++ b/spec/support/schedule_helpers/let_schedule.rb
@@ -0,0 +1,83 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ module LetSchedule
+ # Declare work packages and relations from a visual chart representation.
+ #
+ # For instance:
+ #
+ # let_schedule(<<~CHART)
+ # days | MTWTFSS |
+ # main | XX |
+ # follower | XXX | follows main
+ # start_only | [ |
+ # due_only | ] |
+ # CHART
+ #
+ # is equivalent to:
+ #
+ # let!(:main) do
+ # create(:work_package, subject: 'main', start_date: next_monday, due_date: next_monday + 1.day)
+ # end
+ # let!(:follower) do
+ # create(:work_package, subject: 'follower', start_date: next_monday + 2.days, due_date: next_monday + 4.days) }
+ # end
+ # let!(:relation_follower_follows_main) do
+ # create(:follows_relation, from: follower, to: main, delay: 0) }
+ # end
+ # let!(:start_only) do
+ # create(:work_package, subject: 'start_only', start_date: next_monday + 1.day) }
+ # end
+ # let!(:due_only) do
+ # create(:work_package, subject: 'due_only', due_date: next_monday + 3.days) }
+ # end
+ def let_schedule(chart_representation, **extra_attributes)
+ # To be able to use `travel_to` in a before hook, the dates in the chart
+ # must be lazy evaluated in a let statement.
+ let(:schedule_chart) { Chart.for(chart_representation) }
+
+ # we still need to parse the chart to get the work package names and relations
+ chart = Chart.for(chart_representation)
+ chart.work_packages.each_key do |name|
+ let!(name) do
+ create(:work_package, schedule_chart.work_package_attributes(name).reverse_merge(extra_attributes))
+ end
+ chart.predecessors_by_follower(name).each do |predecessor|
+ relation_alias = "relation_#{name}_follows_#{predecessor}"
+ let!(relation_alias) do
+ create(:follows_relation,
+ from: send(name),
+ to: send(predecessor),
+ delay: schedule_chart.delay_between(predecessor:, follower: name))
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/schedule_helpers/chart_builder_spec.rb b/spec/support_spec/schedule_helpers/chart_builder_spec.rb
new file mode 100644
index 00000000000..b177eee8695
--- /dev/null
+++ b/spec/support_spec/schedule_helpers/chart_builder_spec.rb
@@ -0,0 +1,147 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ScheduleHelpers::ChartBuilder do
+ include ActiveSupport::Testing::TimeHelpers
+
+ let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
+ let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
+ let(:tuesday) { Date.new(2022, 6, 21) }
+ let(:wednesday) { Date.new(2022, 6, 22) }
+ let(:thursday) { Date.new(2022, 6, 23) }
+ let(:friday) { Date.new(2022, 6, 24) }
+ let(:saturday) { Date.new(2022, 6, 25) }
+ let(:sunday) { Date.new(2022, 6, 26) }
+
+ subject(:builder) { described_class.new }
+
+ describe 'happy path' do
+ let(:next_tuesday) { tuesday + 7.days }
+
+ before do
+ travel_to(fake_today)
+ end
+
+ it 'reads a chart and convert it into objects with attributes' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ main | XX |
+ other | XX..XX |
+ follower | XXX | follows main
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+ expect(chart.work_packages).to eq(
+ {
+ main: { subject: 'main', start_date: monday, due_date: tuesday },
+ other: { subject: 'other', start_date: thursday, due_date: next_tuesday },
+ follower: { subject: 'follower', start_date: wednesday, due_date: friday },
+ start_only: { subject: 'start_only', start_date: tuesday, due_date: nil },
+ due_only: { subject: 'due_only', start_date: nil, due_date: friday },
+ no_dates: { subject: 'no_dates', start_date: nil, due_date: nil }
+ }
+ )
+ expect(chart.predecessors_by_follower(:main)).to eq([])
+ expect(chart.predecessors_by_follower(:other)).to eq([])
+ expect(chart.predecessors_by_follower(:follower)).to eq([:main])
+ end
+ end
+
+ describe 'origin day' do
+ before do
+ travel_to(fake_today)
+ end
+
+ it 'is identified by the M in MTWTFSS and corresponds to next monday' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ CHART
+ expect(chart.monday).to eq(monday)
+ expect(chart.monday).to eq(chart.first_day)
+ end
+
+ it 'is not identified by mtwtfss which can be used as documentation instead' do
+ chart = builder.parse(<<~CHART)
+ days | mtwtfssMTWTFSSmtwtfss |
+ wp | X |
+ CHART
+ expect(chart.monday).to eq(monday)
+ expect(chart.first_day).to eq(chart.work_packages.dig(:wp, :start_date))
+ end
+ end
+
+ describe 'properties' do
+ describe 'follows <name>' do
+ it 'adds a follows relation to the named' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ main | |
+ follower | | follows main
+ CHART
+ expect(chart.predecessors_by_follower(:follower)).to eq([:main])
+ expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(0)
+ end
+
+ it 'can be declared in any order' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ follower | | follows main
+ main | |
+ CHART
+ expect(chart.predecessors_by_follower(:follower)).to eq([:main])
+ expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(0)
+ end
+ end
+
+ describe 'follows <name> with delay <n>' do
+ it 'adds a follows relation to the named with a delay' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ main | |
+ follower | | follows main with delay 3
+ CHART
+ expect(chart.predecessors_by_follower(:follower)).to eq([:main])
+ expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(3)
+ end
+ end
+ end
+
+ describe 'error handling' do
+ it 'raises an error if the relation references a non-existing work package predecessor' do
+ expect do
+ builder.parse(<<~CHART)
+ | MTWTFSS |
+ follower | XX | follows main
+ CHART
+ end.to raise_error(RuntimeError, /unable to find predecessor :main in property "follows main" for work package :follower/)
+ end
+ end
+end
diff --git a/spec/support_spec/schedule_helpers/chart_spec.rb b/spec/support_spec/schedule_helpers/chart_spec.rb
new file mode 100644
index 00000000000..468b53c7d99
--- /dev/null
+++ b/spec/support_spec/schedule_helpers/chart_spec.rb
@@ -0,0 +1,181 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ScheduleHelpers::Chart do
+ include ActiveSupport::Testing::TimeHelpers
+
+ let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
+ let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
+ let(:tuesday) { Date.new(2022, 6, 21) }
+ let(:wednesday) { Date.new(2022, 6, 22) }
+ let(:thursday) { Date.new(2022, 6, 23) }
+ let(:friday) { Date.new(2022, 6, 24) }
+ let(:saturday) { Date.new(2022, 6, 25) }
+ let(:sunday) { Date.new(2022, 6, 26) }
+
+ subject(:chart) { described_class.new }
+
+ before do
+ travel_to(fake_today)
+ end
+
+ describe '#first_day' do
+ context 'without work packages' do
+ it 'returns the first day represented on the graph, which is next Monday' do
+ expect(chart.first_day).to eq(monday)
+ end
+ end
+
+ context 'with work packages' do
+ it 'returns the minimum between work packages dates and origin Monday' do
+ expect(chart.first_day).to eq(monday)
+
+ chart.add_work_package(subject: 'wp1', start_date: tuesday)
+ expect(chart.first_day).to eq(monday)
+
+ chart.add_work_package(subject: 'wp2', start_date: monday - 3.days)
+ expect(chart.first_day).to eq(monday - 3.days)
+
+ chart.add_work_package(subject: 'wp3', start_date: sunday)
+ expect(chart.first_day).to eq(monday - 3.days)
+
+ chart.add_work_package(subject: 'wp4', due_date: monday - 6.days)
+ expect(chart.first_day).to eq(monday - 6.days)
+ end
+ end
+
+ it 'can be set to an earlier date by setting the origin monday to an earlier date' do
+ expect(chart.first_day).to eq(monday)
+
+ # no change when origin is moved forward
+ expect { chart.monday = monday + 14.days }
+ .not_to change(chart, :first_day)
+
+ # change when origin is moved backward
+ expect { chart.monday = monday - 14.days }
+ .to change(chart, :first_day).to(monday - 14.days)
+ end
+ end
+
+ describe '#last_day' do
+ context 'without work packages' do
+ it 'returns the last day represented on the graph, which is the Sunday following origin Monday' do
+ expect(chart.last_day).to eq(sunday)
+ end
+ end
+
+ context 'with work packages' do
+ it 'returns the maximum between work packages dates and the Sunday following origin Monday' do
+ expect(chart.last_day).to eq(sunday)
+
+ chart.add_work_package(subject: 'wp1', due_date: tuesday + 7.days)
+ expect(chart.last_day).to eq(tuesday + 7.days)
+
+ chart.add_work_package(subject: 'wp2', start_date: monday - 3.days)
+ expect(chart.last_day).to eq(tuesday + 7.days)
+
+ chart.add_work_package(subject: 'wp3', start_date: monday + 20.days)
+ expect(chart.last_day).to eq(monday + 20.days)
+ end
+ end
+
+ it 'can be set to an later date by setting the origin Monday to a later date' do
+ expect(chart.last_day).to eq(sunday)
+
+ # no change when origin is moved backward
+ expect { chart.monday = monday - 14.days }
+ .not_to change(chart, :last_day)
+
+ # change when origin is moved forward
+ expect { chart.monday = monday + 14.days }
+ .to change(chart, :last_day).to(sunday + 14.days)
+ end
+ end
+
+ describe '#to_s' do
+ let!(:week_days) { create(:week_days) }
+
+ context 'with a chart built from ascii representation' do
+ let(:chart) do
+ ScheduleHelpers::ChartBuilder.new.parse(<<~CHART)
+ days | MTWTFSS |
+ main | X..X |
+ other | XXX..X |
+ follower | XXX | follows main
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+ end
+
+ it 'returns the same ascii representation without properties information' do
+ expect(chart.to_s).to eq(<<~CHART.chomp)
+ days | MTWTFSS |
+ main | X..X |
+ other | XXX..X |
+ follower | XXX |
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+ end
+ end
+
+ context 'with a chart built from real work packages' do
+ let(:work_package1) { build_stubbed(:work_package, subject: 'main', start_date: monday, due_date: tuesday) }
+ let(:work_package2) { build_stubbed(:work_package, subject: 'other', start_date: tuesday, due_date: monday + 7.days) }
+ let(:work_package3) { build_stubbed(:work_package, subject: 'start_only', start_date: monday - 3.days) }
+ let(:work_package4) { build_stubbed(:work_package, subject: 'due_only', due_date: wednesday) }
+ let(:work_package5) { build_stubbed(:work_package, subject: 'no_dates') }
+ let(:chart) do
+ ScheduleHelpers::ChartBuilder.new.use_work_packages(
+ [
+ work_package1,
+ work_package2,
+ work_package3,
+ work_package4,
+ work_package5
+ ]
+ )
+ end
+
+ it 'returns the same ascii representation without properties information' do
+ expect(chart.to_s).to eq(<<~CHART.chomp)
+ days | MTWTFSS |
+ main | XX |
+ other | XXXX..X |
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/schedule_helpers/example_methods_spec.rb b/spec/support_spec/schedule_helpers/example_methods_spec.rb
new file mode 100644
index 00000000000..4ca696d7fd7
--- /dev/null
+++ b/spec/support_spec/schedule_helpers/example_methods_spec.rb
@@ -0,0 +1,153 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ScheduleHelpers::ExampleMethods do
+ include ActiveSupport::Testing::TimeHelpers
+
+ create_shared_association_defaults_for_work_package_factory
+
+ let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
+ let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
+ let(:tuesday) { Date.new(2022, 6, 21) }
+ let(:wednesday) { Date.new(2022, 6, 22) }
+ let(:thursday) { Date.new(2022, 6, 23) }
+ let(:friday) { Date.new(2022, 6, 24) }
+ let(:saturday) { Date.new(2022, 6, 25) }
+ let(:sunday) { Date.new(2022, 6, 26) }
+
+ describe 'expect_schedule' do
+ let_schedule(<<~CHART)
+ | MTWTFSS |
+ main | XX |
+ other | XXX |
+ CHART
+
+ it 'checks the work packages properties according to the given work packages and chart representation' do
+ expect do
+ expect_schedule([main, other], <<~CHART)
+ | MTWTFSS |
+ main | XX |
+ other | XXX |
+ CHART
+ end.not_to raise_error
+ end
+
+ it 'raises an error if start_date is wrong' do
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFSS |
+ main | X |
+ CHART
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
+ end
+
+ it 'raises an error if due_date is wrong' do
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFSS |
+ main | XXXXX |
+ CHART
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
+ end
+
+ it 'raises an error if no work package exists for a given name' do
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFSS |
+ unknown | XX |
+ CHART
+ end.to raise_error(ArgumentError, "unable to find WorkPackage :unknown")
+ end
+
+ it 'checks against the given work packages rather than the ones from the let! definitions' do
+ expect do
+ expect_schedule([], <<~CHART)
+ | MTWTFSS |
+ main | XX |
+ CHART
+ end.not_to raise_error
+ end
+
+ it 'uses the work package from the let! definitions if it is not given as parameter' do
+ a_modified_instance_of_main = WorkPackage.find(main.id)
+ a_modified_instance_of_main.due_date += 2.days
+ expect do
+ expect_schedule([main], <<~CHART)
+ | MTWTFSS |
+ main | XX |
+ CHART
+ end.not_to raise_error
+ expect do
+ expect_schedule([a_modified_instance_of_main], <<~CHART)
+ | MTWTFSS |
+ main | XXXX |
+ CHART
+ end.not_to raise_error
+ expect do
+ expect_schedule([], <<~CHART)
+ | MTWTFSS |
+ main | XX |
+ CHART
+ end.not_to raise_error
+ end
+ end
+
+ describe 'change_schedule' do
+ before do
+ travel_to(fake_today)
+ end
+
+ it 'applies dates changes to a group of work packages from a visual chart representation' do
+ main = build_stubbed(:work_package, subject: 'main')
+ second = build_stubbed(:work_package, subject: 'second')
+ change_schedule([main, second], <<~CHART)
+ days | MTWTFSS |
+ main | XX |
+ second | XX |
+ CHART
+ expect(main.start_date).to eq(monday)
+ expect(main.due_date).to eq(tuesday)
+ expect(second.start_date).to eq(thursday)
+ expect(second.due_date).to eq(friday)
+ end
+
+ it 'does not save changes' do
+ main = create(:work_package, subject: 'main')
+ expect(main.persisted?).to be(true)
+ expect(main.has_changes_to_save?).to be(false)
+ change_schedule([main], <<~CHART)
+ days | MTWTFSS |
+ main | XX |
+ CHART
+ expect(main.has_changes_to_save?).to be(true)
+ expect(main.changes).to eq('start_date' => [nil, monday], 'due_date' => [nil, tuesday])
+ end
+ end
+end
diff --git a/spec/support_spec/schedule_helpers/let_schedule_spec.rb b/spec/support_spec/schedule_helpers/let_schedule_spec.rb
new file mode 100644
index 00000000000..e661b99fbc8
--- /dev/null
+++ b/spec/support_spec/schedule_helpers/let_schedule_spec.rb
@@ -0,0 +1,90 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+
+describe ScheduleHelpers::LetSchedule do
+ include ActiveSupport::Testing::TimeHelpers
+
+ create_shared_association_defaults_for_work_package_factory
+
+ let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
+ let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
+ let(:tuesday) { Date.new(2022, 6, 21) }
+ let(:wednesday) { Date.new(2022, 6, 22) }
+ let(:thursday) { Date.new(2022, 6, 23) }
+ let(:friday) { Date.new(2022, 6, 24) }
+ let(:saturday) { Date.new(2022, 6, 25) }
+ let(:sunday) { Date.new(2022, 6, 26) }
+
+ describe 'let_schedule' do
+ let_schedule(<<~CHART)
+ days | MTWTFSS |
+ main | XX |
+ follower | XXX | follows main with delay 2
+ CHART
+
+ it 'creates let! call for :schedule_chart which returns the chart' do
+ next_monday = (Time.zone.today..(Time.zone.today + 7.days)).find { |d| d.wday == 1 }
+ expect(schedule_chart.first_day).to eq(next_monday)
+ end
+
+ it 'creates let! calls for each work package' do
+ expect([main, follower]).to all(be_an_instance_of(WorkPackage))
+ expect([main, follower]).to all(be_persisted)
+ expect(main).to have_attributes(
+ subject: 'main',
+ start_date: schedule_chart.monday,
+ due_date: schedule_chart.monday + 1.day
+ )
+ expect(follower).to have_attributes(
+ subject: 'follower',
+ start_date: schedule_chart.monday + 2.days,
+ due_date: schedule_chart.monday + 4.days
+ )
+ end
+
+ it 'creates let! calls for follows relations between work packages' do
+ expect(follower.follows_relations.count).to eq(1)
+ expect(relation_follower_follows_main).to be_an_instance_of(Relation)
+ expect(relation_follower_follows_main.delay).to eq(2)
+ end
+
+ context 'with additional attributes' do
+ let_schedule(<<~CHART, done_ratio: 50, schedule_manually: true)
+ days | MTWTFSS |
+ main | XX |
+ follower | XXX | follows main
+ CHART
+
+ it 'applies additional attributes to all created work packages' do
+ expect([main, follower]).to all(have_attributes(done_ratio: 50, schedule_manually: true))
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/schedule_helpers_spec.rb b/spec/support_spec/schedule_helpers_spec.rb
deleted file mode 100644
index 953631d78e2..00000000000
--- a/spec/support_spec/schedule_helpers_spec.rb
+++ /dev/null
@@ -1,415 +0,0 @@
-#-- copyright
-# OpenProject is an open source project management software.
-# Copyright (C) 2012-2022 the OpenProject GmbH
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License version 3.
-#
-# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
-# Copyright (C) 2006-2013 Jean-Philippe Lang
-# Copyright (C) 2010-2013 the ChiliProject Team
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; either version 2
-# of the License, or (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-#
-# See COPYRIGHT and LICENSE files for more details.
-#++
-
-require 'spec_helper'
-
-describe ScheduleHelpers do
- include ActiveSupport::Testing::TimeHelpers
-
- create_shared_association_defaults_for_work_package_factory
-
- let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
- let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
- let(:tuesday) { Date.new(2022, 6, 21) }
- let(:wednesday) { Date.new(2022, 6, 22) }
- let(:thursday) { Date.new(2022, 6, 23) }
- let(:friday) { Date.new(2022, 6, 24) }
- let(:saturday) { Date.new(2022, 6, 25) }
- let(:sunday) { Date.new(2022, 6, 26) }
-
- describe ScheduleHelpers::Chart do
- let(:chart) { described_class.new }
-
- before do
- travel_to(fake_today)
- end
-
- describe '#first_day' do
- context 'without work packages' do
- it 'returns the first day represented on the graph, which is next Monday' do
- expect(chart.first_day).to eq(monday)
- end
- end
-
- context 'with work packages' do
- it 'returns the minimum between work packages dates and origin Monday' do
- expect(chart.first_day).to eq(monday)
-
- chart.add_work_package(subject: 'wp1', start_date: tuesday)
- expect(chart.first_day).to eq(monday)
-
- chart.add_work_package(subject: 'wp2', start_date: monday - 3.days)
- expect(chart.first_day).to eq(monday - 3.days)
-
- chart.add_work_package(subject: 'wp3', start_date: sunday)
- expect(chart.first_day).to eq(monday - 3.days)
-
- chart.add_work_package(subject: 'wp4', due_date: monday - 6.days)
- expect(chart.first_day).to eq(monday - 6.days)
- end
- end
-
- it 'can be set to an earlier date by setting the origin monday to an earlier date' do
- expect(chart.first_day).to eq(monday)
-
- # no change when origin is moved forward
- expect { chart.monday = monday + 14.days }
- .not_to change(chart, :first_day)
-
- # change when origin is moved backward
- expect { chart.monday = monday - 14.days }
- .to change(chart, :first_day).to(monday - 14.days)
- end
- end
-
- describe '#last_day' do
- context 'without work packages' do
- it 'returns the last day represented on the graph, which is the Sunday following origin Monday' do
- expect(chart.last_day).to eq(sunday)
- end
- end
-
- context 'with work packages' do
- it 'returns the maximum between work packages dates and the Sunday following origin Monday' do
- expect(chart.last_day).to eq(sunday)
-
- chart.add_work_package(subject: 'wp1', due_date: tuesday + 7.days)
- expect(chart.last_day).to eq(tuesday + 7.days)
-
- chart.add_work_package(subject: 'wp2', start_date: monday - 3.days)
- expect(chart.last_day).to eq(tuesday + 7.days)
-
- chart.add_work_package(subject: 'wp3', start_date: monday + 20.days)
- expect(chart.last_day).to eq(monday + 20.days)
- end
- end
-
- it 'can be set to an later date by setting the origin Monday to a later date' do
- expect(chart.last_day).to eq(sunday)
-
- # no change when origin is moved backward
- expect { chart.monday = monday - 14.days }
- .not_to change(chart, :last_day)
-
- # change when origin is moved forward
- expect { chart.monday = monday + 14.days }
- .to change(chart, :last_day).to(sunday + 14.days)
- end
- end
-
- describe '#to_s' do
- let!(:week_days) { create(:week_days) }
-
- context 'with a chart built from ascii representation' do
- let(:chart) do
- ScheduleHelpers::ChartBuilder.new.parse(<<~CHART)
- days | MTWTFSS |
- main | X..X |
- other | XXX..X |
- follower | XXX | follows main
- start_only | [ |
- due_only | ] |
- no_dates | |
- CHART
- end
-
- it 'returns the same ascii representation without properties information' do
- expect(chart.to_s).to eq(<<~CHART.chomp)
- days | MTWTFSS |
- main | X..X |
- other | XXX..X |
- follower | XXX |
- start_only | [ |
- due_only | ] |
- no_dates | |
- CHART
- end
- end
-
- context 'with a chart built from real work packages' do
- let(:work_package1) { build_stubbed(:work_package, subject: 'main', start_date: monday, due_date: tuesday) }
- let(:work_package2) { build_stubbed(:work_package, subject: 'other', start_date: tuesday, due_date: monday + 7.days) }
- let(:work_package3) { build_stubbed(:work_package, subject: 'start_only', start_date: monday - 3.days) }
- let(:work_package4) { build_stubbed(:work_package, subject: 'due_only', due_date: wednesday) }
- let(:work_package5) { build_stubbed(:work_package, subject: 'no_dates') }
- let(:chart) do
- ScheduleHelpers::ChartBuilder.new.use_work_packages(
- [
- work_package1,
- work_package2,
- work_package3,
- work_package4,
- work_package5
- ]
- )
- end
-
- it 'returns the same ascii representation without properties information' do
- expect(chart.to_s).to eq(<<~CHART.chomp)
- days | MTWTFSS |
- main | XX |
- other | XXXX..X |
- start_only | [ |
- due_only | ] |
- no_dates | |
- CHART
- end
- end
- end
- end
-
- describe ScheduleHelpers::ChartBuilder do
- let(:builder) { described_class.new }
-
- describe 'happy path' do
- let(:next_tuesday) { tuesday + 7.days }
-
- before do
- travel_to(fake_today)
- end
-
- it 'reads a chart and convert it into objects with attributes' do
- chart = builder.parse(<<~CHART)
- days | MTWTFSS |
- main | XX |
- other | XX..XX |
- follower | XXX | follows main
- start_only | [ |
- due_only | ] |
- no_dates | |
- CHART
- expect(chart.work_packages).to eq(
- {
- main: { subject: 'main', start_date: monday, due_date: tuesday },
- other: { subject: 'other', start_date: thursday, due_date: next_tuesday },
- follower: { subject: 'follower', start_date: wednesday, due_date: friday },
- start_only: { subject: 'start_only', start_date: tuesday, due_date: nil },
- due_only: { subject: 'due_only', start_date: nil, due_date: friday },
- no_dates: { subject: 'no_dates', start_date: nil, due_date: nil }
- }
- )
- expect(chart.predecessors_by_follower(:main)).to eq([])
- expect(chart.predecessors_by_follower(:other)).to eq([])
- expect(chart.predecessors_by_follower(:follower)).to eq([:main])
- end
- end
-
- describe 'origin day' do
- before do
- travel_to(fake_today)
- end
-
- it 'is identified by the M in MTWTFSS and corresponds to next monday' do
- chart = builder.parse(<<~CHART)
- days | MTWTFSS |
- CHART
- expect(chart.monday).to eq(monday)
- expect(chart.monday).to eq(chart.first_day)
- end
-
- it 'is not identified by mtwtfss which can be used as documentation instead' do
- chart = builder.parse(<<~CHART)
- days | mtwtfssMTWTFSSmtwtfss |
- wp | X |
- CHART
- expect(chart.monday).to eq(monday)
- expect(chart.first_day).to eq(chart.work_packages.dig(:wp, :start_date))
- end
- end
-
- describe 'properties' do
- describe 'follows <name>' do
- it 'adds a follows relation to the named' do
- chart = builder.parse(<<~CHART)
- days | MTWTFSS |
- main | |
- follower | | follows main
- CHART
- expect(chart.predecessors_by_follower(:follower)).to eq([:main])
- expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(0)
- end
-
- it 'can be declared in any order' do
- chart = builder.parse(<<~CHART)
- days | MTWTFSS |
- follower | | follows main
- main | |
- CHART
- expect(chart.predecessors_by_follower(:follower)).to eq([:main])
- expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(0)
- end
- end
-
- describe 'follows <name> with delay <n>' do
- it 'adds a follows relation to the named with a delay' do
- chart = builder.parse(<<~CHART)
- days | MTWTFSS |
- main | |
- follower | | follows main with delay 3
- CHART
- expect(chart.predecessors_by_follower(:follower)).to eq([:main])
- expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(3)
- end
- end
- end
-
- describe 'error handling' do
- it 'raises an error if the relation references a non-existing work package predecessor' do
- expect do
- builder.parse(<<~CHART)
- | MTWTFSS |
- follower | XX | follows main
- CHART
- end.to raise_error(RuntimeError, /unable to find predecessor :main in property "follows main" for work package :follower/)
- end
- end
- end
-
- describe 'let_schedule helper' do
- let_schedule(<<~CHART)
- days | MTWTFSS |
- main | XX |
- follower | XXX | follows main with delay 2
- CHART
-
- it 'creates let! call for :schedule_chart which returns the chart' do
- next_monday = (Time.zone.today..(Time.zone.today + 7.days)).find { |d| d.wday == 1 }
- expect(schedule_chart.first_day).to eq(next_monday)
- end
-
- it 'creates let! calls for each work package' do
- expect([main, follower]).to all(be_an_instance_of(WorkPackage))
- expect([main, follower]).to all(be_persisted)
- expect(main).to have_attributes(
- subject: 'main',
- start_date: schedule_chart.monday,
- due_date: schedule_chart.monday + 1.day
- )
- expect(follower).to have_attributes(
- subject: 'follower',
- start_date: schedule_chart.monday + 2.days,
- due_date: schedule_chart.monday + 4.days
- )
- end
-
- it 'creates let! calls for follows relations between work packages' do
- expect(follower.follows_relations.count).to eq(1)
- expect(relation_follower_follows_main).to be_an_instance_of(Relation)
- expect(relation_follower_follows_main.delay).to eq(2)
- end
-
- context 'with additional attributes' do
- let_schedule(<<~CHART, done_ratio: 50, schedule_manually: true)
- days | MTWTFSS |
- main | XX |
- follower | XXX | follows main
- CHART
-
- it 'applies additional attributes to all created work packages' do
- expect([main, follower]).to all(have_attributes(done_ratio: 50, schedule_manually: true))
- end
- end
- end
-
- describe 'expect_schedule helper' do
- let_schedule(<<~CHART)
- | MTWTFSS |
- main | XX |
- other | XXX |
- CHART
-
- it 'checks the work packages properties according to the given work packages and chart representation' do
- expect do
- expect_schedule([main, other], <<~CHART)
- | MTWTFSS |
- main | XX |
- other | XXX |
- CHART
- end.not_to raise_error
- end
-
- it 'raises an error if start_date is wrong' do
- expect do
- expect_schedule([main], <<~CHART)
- | MTWTFSS |
- main | X |
- CHART
- end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
- end
-
- it 'raises an error if due_date is wrong' do
- expect do
- expect_schedule([main], <<~CHART)
- | MTWTFSS |
- main | XXXXX |
- CHART
- end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
- end
-
- it 'raises an error if no work package exists for a given name' do
- expect do
- expect_schedule([main], <<~CHART)
- | MTWTFSS |
- unknown | XX |
- CHART
- end.to raise_error(ArgumentError, "unable to find WorkPackage :unknown")
- end
-
- it 'checks against the given work packages rather than the ones from the let! definitions' do
- expect do
- expect_schedule([], <<~CHART)
- | MTWTFSS |
- main | XX |
- CHART
- end.not_to raise_error
- end
-
- it 'uses the work package from the let! definitions if it is not given as parameter' do
- a_modified_instance_of_main = WorkPackage.find(main.id)
- a_modified_instance_of_main.due_date += 2.days
- expect do
- expect_schedule([main], <<~CHART)
- | MTWTFSS |
- main | XX |
- CHART
- end.not_to raise_error
- expect do
- expect_schedule([a_modified_instance_of_main], <<~CHART)
- | MTWTFSS |
- main | XXXX |
- CHART
- end.not_to raise_error
- expect do
- expect_schedule([], <<~CHART)
- | MTWTFSS |
- main | XX |
- CHART
- end.not_to raise_error
- end
- end
-end