1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.velocity.tools

File CSSIdentifierSerializerTest.java

 

Code metrics

0
56
3
1
133
83
3
0.05
18.67
3
1

Classes

Class Line # Actions
CSSIdentifierSerializerTest 34 56 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.velocity.tools;
21   
22    import org.junit.jupiter.api.DisplayName;
23    import org.junit.jupiter.api.Test;
24   
25    import static org.junit.jupiter.api.Assertions.assertEquals;
26    import static org.junit.jupiter.api.Assertions.assertNull;
27    import static org.junit.jupiter.api.Assertions.assertThrows;
28   
29    /**
30    * Unit tests for {@link CSSIdentifierSerializer}.
31    *
32    * @version $Id: 0bebf42eb189caf7d7e38efd6736a34b815af759 $
33    */
 
34    public class CSSIdentifierSerializerTest
35    {
36    private CSSIdentifierSerializer cssIdentifierSerializer = new CSSIdentifierSerializer();
37   
38    /**
39    * NOTE: This test was adapted from Mathias Bynens' tests for CSS.escape polyfill, available under the MIT license.
40    *
41    * @see <a href="https://drafts.csswg.org/cssom/#serialize-an-identifier">
42    * https://drafts.csswg.org/cssom/#serialize-an-identifier</a>
43    * @see <a href="https://github.com/mathiasbynens/CSS.escape/blob/master/tests/tests.js">
44    * https://github.com/mathiasbynens/CSS.escape/blob/master/tests/tests.js</a>
45    */
 
46  1 toggle @Test
47    @DisplayName("Various CSS serialization tests")
48    public void serialize()
49    {
50  1 assertCssThrowsException("\u0000");
51  1 assertCssThrowsException("a\u0000");
52  1 assertCssThrowsException("\u0000b");
53  1 assertCssThrowsException("a\u0000b");
54   
55  1 assertNull(this.cssIdentifierSerializer.serialize(null));
56  1 assertCss("", "");
57   
58  1 assertCss("\u0001\u0002\u001E\u001F", "\\1 \\2 \\1e \\1f ");
59   
60  1 assertCss("0a", "\\30 a");
61  1 assertCss("1a", "\\31 a");
62  1 assertCss("2a", "\\32 a");
63  1 assertCss("3a", "\\33 a");
64  1 assertCss("4a", "\\34 a");
65  1 assertCss("5a", "\\35 a");
66  1 assertCss("6a", "\\36 a");
67  1 assertCss("7a", "\\37 a");
68  1 assertCss("8a", "\\38 a");
69  1 assertCss("9a", "\\39 a");
70   
71  1 assertCss("a0b", "a0b");
72  1 assertCss("a1b", "a1b");
73  1 assertCss("a2b", "a2b");
74  1 assertCss("a3b", "a3b");
75  1 assertCss("a4b", "a4b");
76  1 assertCss("a5b", "a5b");
77  1 assertCss("a6b", "a6b");
78  1 assertCss("a7b", "a7b");
79  1 assertCss("a8b", "a8b");
80  1 assertCss("a9b", "a9b");
81   
82  1 assertCss("-0a", "-\\30 a");
83  1 assertCss("-1a", "-\\31 a");
84  1 assertCss("-2a", "-\\32 a");
85  1 assertCss("-3a", "-\\33 a");
86  1 assertCss("-4a", "-\\34 a");
87  1 assertCss("-5a", "-\\35 a");
88  1 assertCss("-6a", "-\\36 a");
89  1 assertCss("-7a", "-\\37 a");
90  1 assertCss("-8a", "-\\38 a");
91  1 assertCss("-9a", "-\\39 a");
92   
93  1 assertCss("-", "\\-");
94  1 assertCss("-a", "-a");
95  1 assertCss("--", "--");
96  1 assertCss("--a", "--a");
97   
98  1 assertCss("\u0080\u002D\u005F\u00A9", "\u0080\u002D\u005F\u00A9");
99  1 assertCss("\u007F\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088"
100    + "\u0089\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092"
101    + "\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F",
102    "\\7f \u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089"
103    + "\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092\u0093"
104    + "\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F");
105  1 assertCss("\u00A0\u00A1\u00A2", "\u00A0\u00A1\u00A2");
106  1 assertCss("a0123456789b", "a0123456789b");
107  1 assertCss("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
108  1 assertCss("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
109   
110  1 assertCss("\u0020\u0021\u0078\u0079", "\\ \\!xy");
111   
112    // astral symbol (U+1D306 TETRAGRAM FOR CENTRE)
113  1 assertCss("\uD834\uDF06", "\uD834\uDF06");
114    // lone surrogates
115  1 assertCss("\uDF06", "\uDF06");
116  1 assertCss("\uD834", "\uD834");
117   
118  1 assertCss("a#b.c d[e=f]g{h:i;j}k", "a\\#b\\.c\\ d\\[e\\=f\\]g\\{h\\:i\\;j\\}k");
119    }
120   
 
121  47 toggle private void assertCss(String expected, String actual)
122    {
123  47 assertEquals(this.cssIdentifierSerializer.serialize(expected), actual);
124    }
125   
 
126  4 toggle private void assertCssThrowsException(String input)
127    {
128  4 Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
129  4 this.cssIdentifierSerializer.serialize(input);
130    });
131  4 assertEquals("Invalid character: the input contains U+0000.", exception.getMessage());
132    }
133    }