Skip to content

Commit 7e746e2

Browse files
committed
Port of TestIsoFields to scalatest
1 parent 4691091 commit 7e746e2

File tree

2 files changed

+164
-118
lines changed

2 files changed

+164
-118
lines changed

jvm/src/test/scala/org/threeten/bp/temporal/TestIsoFields.scala

Lines changed: 0 additions & 118 deletions
This file was deleted.
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
* Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
*
12+
* * Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* * Neither the name of JSR-310 nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
package org.threeten.bp.temporal
33+
34+
import org.scalatest.FunSuite
35+
import org.threeten.bp.DayOfWeek._
36+
import org.threeten.bp.{AssertionsHelper, DayOfWeek, LocalDate}
37+
import org.threeten.bp.format.{DateTimeFormatter, DateTimeFormatterBuilder}
38+
import org.threeten.bp.temporal.ChronoField.DAY_OF_WEEK
39+
40+
/** Test. */
41+
class TestIsoFields extends FunSuite with AssertionsHelper {
42+
test("enum") {
43+
assertTrue(IsoFields.WEEK_OF_WEEK_BASED_YEAR.isInstanceOf[Enum[_]])
44+
assertTrue(IsoFields.WEEK_BASED_YEAR.isInstanceOf[Enum[_]])
45+
assertTrue(IsoFields.WEEK_BASED_YEARS.isInstanceOf[Enum[_]])
46+
}
47+
48+
def data_week: List[List[Any]] = {
49+
List(
50+
List(LocalDate.of(1969, 12, 29), MONDAY, 1, 1970),
51+
List(LocalDate.of(2012, 12, 23), SUNDAY, 51, 2012),
52+
List(LocalDate.of(2012, 12, 24), MONDAY, 52, 2012),
53+
List(LocalDate.of(2012, 12, 27), THURSDAY, 52, 2012),
54+
List(LocalDate.of(2012, 12, 28), FRIDAY, 52, 2012),
55+
List(LocalDate.of(2012, 12, 29), SATURDAY, 52, 2012),
56+
List(LocalDate.of(2012, 12, 30), SUNDAY, 52, 2012),
57+
List(LocalDate.of(2012, 12, 31), MONDAY, 1, 2013),
58+
List(LocalDate.of(2013, 1, 1), TUESDAY, 1, 2013),
59+
List(LocalDate.of(2013, 1, 2), WEDNESDAY, 1, 2013),
60+
List(LocalDate.of(2013, 1, 6), SUNDAY, 1, 2013),
61+
List(LocalDate.of(2013, 1, 7), MONDAY, 2, 2013))
62+
}
63+
64+
test("WOWBY") {
65+
data_week.foreach {
66+
case (date: LocalDate) :: (dow: DayOfWeek) :: (week: Int) :: (wby: Int) :: Nil =>
67+
assertEquals(date.getDayOfWeek, dow)
68+
assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week)
69+
assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week)
70+
case _ =>
71+
fail()
72+
}
73+
}
74+
75+
test("WBY") {
76+
data_week.foreach {
77+
case (date: LocalDate) :: (dow: DayOfWeek) :: (week: Int) :: (wby: Int) :: Nil =>
78+
assertEquals(date.getDayOfWeek, dow)
79+
assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby)
80+
assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby)
81+
case _ =>
82+
fail()
83+
}
84+
}
85+
86+
test("parse_weeks") {
87+
data_week.foreach {
88+
case (date: LocalDate) :: (dow: DayOfWeek) :: (week: Int) :: (wby: Int) :: Nil =>
89+
val f: DateTimeFormatter = new DateTimeFormatterBuilder().appendValue(IsoFields.WEEK_BASED_YEAR).appendLiteral('-').appendValue(IsoFields.WEEK_OF_WEEK_BASED_YEAR).appendLiteral('-').appendValue(DAY_OF_WEEK).toFormatter
90+
val parsed: LocalDate = LocalDate.parse(wby + "-" + week + "-" + dow.getValue, f)
91+
assertEquals(parsed, date)
92+
case _ =>
93+
fail()
94+
}
95+
}
96+
97+
test("loop") {
98+
var date: LocalDate = LocalDate.of(1960, 1, 5)
99+
var year: Int = 1960
100+
var wby: Int = 1960
101+
var weekLen: Int = 52
102+
var week: Int = 1
103+
while (date.getYear < 2400) {
104+
val loopDow: DayOfWeek = date.getDayOfWeek
105+
if (date.getYear != year) {
106+
year = date.getYear
107+
}
108+
if (loopDow eq MONDAY) {
109+
week += 1
110+
if ((week == 53 && weekLen == 52) || week == 54) {
111+
week = 1
112+
val firstDayOfWeekBasedYear: LocalDate = date.plusDays(14).withDayOfYear(1)
113+
val firstDay: DayOfWeek = firstDayOfWeekBasedYear.getDayOfWeek
114+
weekLen = if ((firstDay eq THURSDAY) || ((firstDay eq WEDNESDAY) && firstDayOfWeekBasedYear.isLeapYear)) 53 else 52
115+
wby += 1
116+
}
117+
}
118+
assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.rangeRefinedBy(date), ValueRange.of(1, weekLen), "Failed on " + date + " " + date.getDayOfWeek)
119+
assertEquals(IsoFields.WEEK_OF_WEEK_BASED_YEAR.getFrom(date), week, "Failed on " + date + " " + date.getDayOfWeek)
120+
assertEquals(date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR), week, "Failed on " + date + " " + date.getDayOfWeek)
121+
assertEquals(IsoFields.WEEK_BASED_YEAR.getFrom(date), wby, "Failed on " + date + " " + date.getDayOfWeek)
122+
assertEquals(date.get(IsoFields.WEEK_BASED_YEAR), wby, "Failed on " + date + " " + date.getDayOfWeek)
123+
date = date.plusDays(1)
124+
}
125+
}
126+
127+
def data_quartersBetween: List[List[Any]] = {
128+
List(
129+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 1), 0),
130+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 1, 2), 0),
131+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 2, 1), 0),
132+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 3, 1), 0),
133+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 3, 31), 0),
134+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 4, 1), 1),
135+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 4, 2), 1),
136+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 6, 30), 1),
137+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 7, 1), 2),
138+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 10, 1), 3),
139+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2000, 12, 31), 3),
140+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2001, 1, 1), 4),
141+
List(LocalDate.of(2000, 1, 1), LocalDate.of(2002, 1, 1), 8),
142+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 12, 31), 0),
143+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 10, 2), 0),
144+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 10, 1), -1),
145+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 7, 2), -1),
146+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 7, 1), -2),
147+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 4, 2), -2),
148+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 4, 1), -3),
149+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 1, 2), -3),
150+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1999, 1, 1), -4),
151+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1998, 12, 31), -4),
152+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1998, 10, 2), -4),
153+
List(LocalDate.of(2000, 1, 1), LocalDate.of(1998, 10, 1), -5))
154+
}
155+
156+
test("quarters_between") {
157+
data_quartersBetween.foreach {
158+
case (start: LocalDate) :: (end: LocalDate) :: (expected: Int) :: Nil =>
159+
assertEquals(IsoFields.QUARTER_YEARS.between(start, end), expected)
160+
case _ =>
161+
fail()
162+
}
163+
}
164+
}

0 commit comments

Comments
 (0)