@@ -1,49 +1,49 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | module Redmine #:nodoc: |
|
19 | 19 | module CoreExtensions #:nodoc: |
|
20 | 20 | module String #:nodoc: |
|
21 | 21 | # Custom string conversions |
|
22 | 22 | module Conversions |
|
23 | 23 | # Parses hours format and returns a float |
|
24 | 24 | def to_hours |
|
25 | 25 | s = self.dup |
|
26 | 26 | s.strip! |
|
27 | 27 | if s =~ %r{^(\d+([.,]\d+)?)h?$} |
|
28 | 28 | s = $1 |
|
29 | 29 | else |
|
30 | 30 | # 2:30 => 2.5 |
|
31 | 31 | s.gsub!(%r{^(\d+):(\d+)$}) { $1.to_i + $2.to_i / 60.0 } |
|
32 | 32 | # 2h30, 2h, 30m => 2.5, 2, 0.5 |
|
33 | s.gsub!(%r{^((\d+)\s*(h|hours?))?\s*((\d+)\s*(m|min)?)?$}) { |m| ($1 || $4) ? ($2.to_i + $5.to_i / 60.0) : m[0] } | |
|
33 | s.gsub!(%r{^((\d+)\s*(h|hours?))?\s*((\d+)\s*(m|min)?)?$}i) { |m| ($1 || $4) ? ($2.to_i + $5.to_i / 60.0) : m[0] } | |
|
34 | 34 | end |
|
35 | 35 | # 2,5 => 2.5 |
|
36 | 36 | s.gsub!(',', '.') |
|
37 | 37 | begin; Kernel.Float(s); rescue; nil; end |
|
38 | 38 | end |
|
39 | 39 | |
|
40 | 40 | # Object#to_a removed in ruby1.9 |
|
41 | 41 | if RUBY_VERSION > '1.9' |
|
42 | 42 | def to_a |
|
43 | 43 | [self.dup] |
|
44 | 44 | end |
|
45 | 45 | end |
|
46 | 46 | end |
|
47 | 47 | end |
|
48 | 48 | end |
|
49 | 49 | end |
@@ -1,128 +1,129 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class TimeEntryTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :issues, :projects, :users, :time_entries, |
|
22 | 22 | :members, :roles, :member_roles, |
|
23 | 23 | :trackers, :issue_statuses, |
|
24 | 24 | :projects_trackers, |
|
25 | 25 | :journals, :journal_details, |
|
26 | 26 | :issue_categories, :enumerations, |
|
27 | 27 | :groups_users, |
|
28 | 28 | :enabled_modules, |
|
29 | 29 | :workflows |
|
30 | 30 | |
|
31 | 31 | def test_hours_format |
|
32 | 32 | assertions = { "2" => 2.0, |
|
33 | 33 | "21.1" => 21.1, |
|
34 | 34 | "2,1" => 2.1, |
|
35 | 35 | "1,5h" => 1.5, |
|
36 | 36 | "7:12" => 7.2, |
|
37 | 37 | "10h" => 10.0, |
|
38 | 38 | "10 h" => 10.0, |
|
39 | 39 | "45m" => 0.75, |
|
40 | 40 | "45 m" => 0.75, |
|
41 | 41 | "3h15" => 3.25, |
|
42 | 42 | "3h 15" => 3.25, |
|
43 | 43 | "3 h 15" => 3.25, |
|
44 | 44 | "3 h 15m" => 3.25, |
|
45 | 45 | "3 h 15 m" => 3.25, |
|
46 | 46 | "3 hours" => 3.0, |
|
47 | 47 | "12min" => 0.2, |
|
48 | "12 Min" => 0.2, | |
|
48 | 49 | } |
|
49 | 50 | |
|
50 | 51 | assertions.each do |k, v| |
|
51 | 52 | t = TimeEntry.new(:hours => k) |
|
52 | 53 | assert_equal v, t.hours, "Converting #{k} failed:" |
|
53 | 54 | end |
|
54 | 55 | end |
|
55 | 56 | |
|
56 | 57 | def test_hours_should_default_to_nil |
|
57 | 58 | assert_nil TimeEntry.new.hours |
|
58 | 59 | end |
|
59 | 60 | |
|
60 | 61 | def test_spent_on_with_blank |
|
61 | 62 | c = TimeEntry.new |
|
62 | 63 | c.spent_on = '' |
|
63 | 64 | assert_nil c.spent_on |
|
64 | 65 | end |
|
65 | 66 | |
|
66 | 67 | def test_spent_on_with_nil |
|
67 | 68 | c = TimeEntry.new |
|
68 | 69 | c.spent_on = nil |
|
69 | 70 | assert_nil c.spent_on |
|
70 | 71 | end |
|
71 | 72 | |
|
72 | 73 | def test_spent_on_with_string |
|
73 | 74 | c = TimeEntry.new |
|
74 | 75 | c.spent_on = "2011-01-14" |
|
75 | 76 | assert_equal Date.parse("2011-01-14"), c.spent_on |
|
76 | 77 | end |
|
77 | 78 | |
|
78 | 79 | def test_spent_on_with_invalid_string |
|
79 | 80 | c = TimeEntry.new |
|
80 | 81 | c.spent_on = "foo" |
|
81 | 82 | assert_nil c.spent_on |
|
82 | 83 | end |
|
83 | 84 | |
|
84 | 85 | def test_spent_on_with_date |
|
85 | 86 | c = TimeEntry.new |
|
86 | 87 | c.spent_on = Date.today |
|
87 | 88 | assert_equal Date.today, c.spent_on |
|
88 | 89 | end |
|
89 | 90 | |
|
90 | 91 | def test_spent_on_with_time |
|
91 | 92 | c = TimeEntry.new |
|
92 | 93 | c.spent_on = Time.now |
|
93 | 94 | assert_equal Date.today, c.spent_on |
|
94 | 95 | end |
|
95 | 96 | |
|
96 | 97 | def test_validate_time_entry |
|
97 | 98 | anon = User.anonymous |
|
98 | 99 | project = Project.find(1) |
|
99 | 100 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, |
|
100 | 101 | :priority => IssuePriority.all.first, :subject => 'test_create', |
|
101 | 102 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
102 | 103 | assert issue.save |
|
103 | 104 | activity = TimeEntryActivity.find_by_name('Design') |
|
104 | 105 | te = TimeEntry.create(:spent_on => '2010-01-01', |
|
105 | 106 | :hours => 100000, |
|
106 | 107 | :issue => issue, |
|
107 | 108 | :project => project, |
|
108 | 109 | :user => anon, |
|
109 | 110 | :activity => activity) |
|
110 | 111 | assert_equal 1, te.errors.count |
|
111 | 112 | end |
|
112 | 113 | |
|
113 | 114 | def test_set_project_if_nil |
|
114 | 115 | anon = User.anonymous |
|
115 | 116 | project = Project.find(1) |
|
116 | 117 | issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => anon.id, :status_id => 1, |
|
117 | 118 | :priority => IssuePriority.all.first, :subject => 'test_create', |
|
118 | 119 | :description => 'IssueTest#test_create', :estimated_hours => '1:30') |
|
119 | 120 | assert issue.save |
|
120 | 121 | activity = TimeEntryActivity.find_by_name('Design') |
|
121 | 122 | te = TimeEntry.create(:spent_on => '2010-01-01', |
|
122 | 123 | :hours => 10, |
|
123 | 124 | :issue => issue, |
|
124 | 125 | :user => anon, |
|
125 | 126 | :activity => activity) |
|
126 | 127 | assert_equal project.id, te.project.id |
|
127 | 128 | end |
|
128 | 129 | end |
General Comments 0
You need to be logged in to leave comments.
Login now