Skip to content

Commit ce40c08

Browse files
committed
minor fixes and test for HSLColor by Paul Selormey
1 parent fbebb3d commit ce40c08

File tree

3 files changed

+171
-18
lines changed

3 files changed

+171
-18
lines changed

src/main/java/com/gargoylesoftware/css/dom/HSLColorImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import com.gargoylesoftware.css.parser.LexicalUnit.LexicalUnitType;
2424

2525
/**
26-
* Implementation of RGBColor.
26+
* Implementation of HSLColor.
2727
*
2828
* @author Ronald Brill
2929
*/
@@ -39,13 +39,13 @@ public class HSLColorImpl implements Serializable {
3939
/**
4040
* Constructor that reads the values from the given
4141
* chain of LexicalUnits.
42-
* @param function the name of the function; rgb or rgba
42+
* @param function the name of the function; hsl or hsla
4343
* @param lu the values
4444
* @throws DOMException in case of error
4545
*/
4646
public HSLColorImpl(final String function, final LexicalUnit lu) throws DOMException {
4747
if (function == null) {
48-
throw new DOMException(DOMException.SYNTAX_ERR, "Color space rgb or rgba is requiredc");
48+
throw new DOMException(DOMException.SYNTAX_ERR, "Color space hsl or hsla is required.");
4949
}
5050
final String functionLC = function.toLowerCase(Locale.ROOT);
5151
if (!"hsl".equals(functionLC) && !"hsla".equals(functionLC)) {
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
* Copyright (c) 2019-2021 Ronald Brill.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package com.gargoylesoftware.css.dom;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.fail;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.w3c.dom.DOMException;
22+
23+
import com.gargoylesoftware.css.parser.LexicalUnit;
24+
import com.gargoylesoftware.css.parser.LexicalUnitImpl;
25+
26+
/**
27+
* Unit tests for {@link RGBColorImpl}.
28+
*
29+
* @author Ronald Brill
30+
* @author Paul Selormey
31+
*/
32+
public class HSLColorImplTest {
33+
34+
/**
35+
* @throws Exception if any error occurs
36+
*/
37+
@Test
38+
public void constructByLU() throws Exception {
39+
final LexicalUnit hslLU = LexicalUnitImpl.createDegree(null, 10);
40+
LexicalUnit lu = LexicalUnitImpl.createComma(hslLU);
41+
lu = LexicalUnitImpl.createPercentage(lu, 20);
42+
lu = LexicalUnitImpl.createComma(lu);
43+
LexicalUnitImpl.createPercentage(lu, 30);
44+
45+
final HSLColorImpl hsl = new HSLColorImpl("hsl", hslLU);
46+
assertEquals("hsl(10deg, 20%, 30%)", hsl.toString());
47+
assertEquals("10deg", hsl.getHue().getCssText());
48+
assertEquals("20%", hsl.getSaturation().getCssText());
49+
assertEquals("30%", hsl.getLightness().getCssText());
50+
}
51+
52+
/**
53+
* @throws Exception if any error occurs
54+
*/
55+
@Test
56+
public void constructByLUException() throws Exception {
57+
LexicalUnit hslLU = LexicalUnitImpl.createNumber(null, 10);
58+
LexicalUnit lu = LexicalUnitImpl.createComma(hslLU);
59+
LexicalUnitImpl.createDivide(lu);
60+
61+
try {
62+
HSLColorImpl color = new HSLColorImpl("hsl", hslLU);
63+
fail("DOMException expected: " + color);
64+
}
65+
catch (final DOMException e) {
66+
assertEquals("Saturation part has to be percentage.", e.getMessage());
67+
}
68+
69+
hslLU = LexicalUnitImpl.createDegree(null, 10);
70+
lu = LexicalUnitImpl.createComma(hslLU);
71+
lu = LexicalUnitImpl.createPercentage(lu, 20);
72+
LexicalUnitImpl.createPercentage(lu, 30);
73+
74+
try {
75+
HSLColorImpl color = new HSLColorImpl("hsl", hslLU);
76+
fail("DOMException expected: " + color);
77+
}
78+
catch (final DOMException e) {
79+
assertEquals("hsl parameters must be separated by ','.", e.getMessage());
80+
}
81+
}
82+
83+
/**
84+
* @throws Exception if any error occurs
85+
*/
86+
@Test
87+
public void constructByLUTooManyValuesException() throws Exception {
88+
final LexicalUnit hslLU = LexicalUnitImpl.createNumber(null, 100);
89+
LexicalUnit lu = LexicalUnitImpl.createComma(hslLU);
90+
lu = LexicalUnitImpl.createPercentage(lu, 20);
91+
lu = LexicalUnitImpl.createComma(lu);
92+
lu = LexicalUnitImpl.createPercentage(lu, 30);
93+
lu = LexicalUnitImpl.createComma(lu);
94+
lu = LexicalUnitImpl.createPercentage(lu, 0.1);
95+
LexicalUnitImpl.createComma(lu);
96+
97+
try {
98+
HSLColorImpl color = new HSLColorImpl("hsl", hslLU);
99+
fail("DOMException expected: " + color);
100+
}
101+
catch (final DOMException e) {
102+
assertEquals("Too many parameters for hsl function.", e.getMessage());
103+
}
104+
}
105+
106+
/**
107+
* @throws Exception if any error occurs
108+
*/
109+
@Test
110+
public void getCssText() throws Exception {
111+
final LexicalUnit hslLu = LexicalUnitImpl.createNumber(null, 235);
112+
LexicalUnit lu = LexicalUnitImpl.createComma(hslLu);
113+
lu = LexicalUnitImpl.createPercentage(lu, 20);
114+
lu = LexicalUnitImpl.createComma(lu);
115+
LexicalUnitImpl.createPercentage(lu, 30);
116+
117+
final HSLColorImpl hsl = new HSLColorImpl("hsl", hslLu);
118+
119+
assertEquals("hsl(235, 20%, 30%)", hsl.toString());
120+
}
121+
122+
/**
123+
* @throws Exception if any error occurs
124+
*/
125+
@Test
126+
public void getCssTextFunctionName() throws Exception {
127+
final LexicalUnit hslLu = LexicalUnitImpl.createNumber(null, 45);
128+
129+
try {
130+
HSLColorImpl color = new HSLColorImpl(null, hslLu);
131+
fail("DOMException expected: " + color);
132+
}
133+
catch (final DOMException e) {
134+
assertEquals("Color space hsl or hsla is required.", e.getMessage());
135+
}
136+
137+
try {
138+
HSLColorImpl color = new HSLColorImpl("", hslLu);
139+
fail("DOMException expected: " + color);
140+
}
141+
catch (final DOMException e) {
142+
assertEquals("Color space '' not supported.", e.getMessage());
143+
}
144+
145+
try {
146+
HSLColorImpl color = new HSLColorImpl("xyz", hslLu);
147+
fail("DOMException expected: " + color);
148+
}
149+
catch (final DOMException e) {
150+
assertEquals("Color space 'xyz' not supported.", e.getMessage());
151+
}
152+
}
153+
}

src/test/java/com/gargoylesoftware/css/dom/RGBColorImplTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void constructByLU() throws Exception {
3939
LexicalUnit lu = LexicalUnitImpl.createComma(rgbLU);
4040
lu = LexicalUnitImpl.createNumber(lu, 20);
4141
lu = LexicalUnitImpl.createComma(lu);
42-
lu = LexicalUnitImpl.createNumber(lu, 30);
42+
LexicalUnitImpl.createNumber(lu, 30);
4343

4444
final RGBColorImpl rgb = new RGBColorImpl("rgb", rgbLU);
4545
assertEquals("rgb(10, 20, 30)", rgb.toString());
@@ -55,7 +55,7 @@ public void constructByLU() throws Exception {
5555
public void constructByLUException() throws Exception {
5656
LexicalUnit rgbLU = LexicalUnitImpl.createNumber(null, 10);
5757
LexicalUnit lu = LexicalUnitImpl.createComma(rgbLU);
58-
lu = LexicalUnitImpl.createDivide(lu);
58+
LexicalUnitImpl.createDivide(lu);
5959

6060
try {
6161
new RGBColorImpl("rgb", rgbLU);
@@ -68,11 +68,11 @@ public void constructByLUException() throws Exception {
6868
rgbLU = LexicalUnitImpl.createNumber(null, 10);
6969
lu = LexicalUnitImpl.createComma(rgbLU);
7070
lu = LexicalUnitImpl.createNumber(lu, 20);
71-
lu = LexicalUnitImpl.createNumber(lu, 30);
71+
LexicalUnitImpl.createNumber(lu, 30);
7272

7373
try {
74-
new RGBColorImpl("rgb", rgbLU);
75-
fail("DOMException expected");
74+
RGBColorImpl color = new RGBColorImpl("rgb", rgbLU);
75+
fail("DOMException expected: " + color);
7676
}
7777
catch (final DOMException e) {
7878
assertEquals("rgb parameters must be separated by ','.", e.getMessage());
@@ -91,11 +91,11 @@ public void constructByLUTooManyValuesException() throws Exception {
9191
lu = LexicalUnitImpl.createNumber(lu, 30);
9292
lu = LexicalUnitImpl.createComma(lu);
9393
lu = LexicalUnitImpl.createNumber(lu, 0.1);
94-
lu = LexicalUnitImpl.createComma(lu);
94+
LexicalUnitImpl.createComma(lu);
9595

9696
try {
97-
new RGBColorImpl("rgb", rgbLU);
98-
fail("DOMException expected");
97+
RGBColorImpl color = new RGBColorImpl("rgb", rgbLU);
98+
fail("DOMException expected: " + color);
9999
}
100100
catch (final DOMException e) {
101101
assertEquals("Too many parameters for rgb function.", e.getMessage());
@@ -111,7 +111,7 @@ public void getCssText() throws Exception {
111111
LexicalUnit lu = LexicalUnitImpl.createComma(rgbLu);
112112
lu = LexicalUnitImpl.createNumber(lu, 20);
113113
lu = LexicalUnitImpl.createComma(lu);
114-
lu = LexicalUnitImpl.createNumber(lu, 30);
114+
LexicalUnitImpl.createNumber(lu, 30);
115115

116116
final RGBColorImpl rgb = new RGBColorImpl("rgb", rgbLu);
117117

@@ -126,24 +126,24 @@ public void getCssTextFunctionName() throws Exception {
126126
final LexicalUnit rgbLu = LexicalUnitImpl.createNumber(null, 10);
127127

128128
try {
129-
new RGBColorImpl(null, rgbLu);
130-
fail("DOMException expected");
129+
RGBColorImpl color = new RGBColorImpl(null, rgbLu);
130+
fail("DOMException expected: " + color);
131131
}
132132
catch (final DOMException e) {
133133
assertEquals("Color space rgb or rgba is required.", e.getMessage());
134134
}
135135

136136
try {
137-
new RGBColorImpl("", rgbLu);
138-
fail("DOMException expected");
137+
RGBColorImpl color = new RGBColorImpl("", rgbLu);
138+
fail("DOMException expected: " + color);
139139
}
140140
catch (final DOMException e) {
141141
assertEquals("Color space '' not supported.", e.getMessage());
142142
}
143143

144144
try {
145-
new RGBColorImpl("xyz", rgbLu);
146-
fail("DOMException expected");
145+
RGBColorImpl color = new RGBColorImpl("xyz", rgbLu);
146+
fail("DOMException expected: " + color);
147147
}
148148
catch (final DOMException e) {
149149
assertEquals("Color space 'xyz' not supported.", e.getMessage());

0 commit comments

Comments
 (0)