1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.velocity.tools

File CSSIdentifierSerializerTest.java

 

Code metrics

0
57
3
1
129
81
4
0.07
19
3
1.33

Classes

Class Line # Actions
CSSIdentifierSerializerTest 31 57 0% 4 1
0.9833333598.3%
 

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.Test;
23   
24    import static org.junit.Assert.*;
25   
26    /**
27    * Unit tests for {@link CSSIdentifierSerializer}.
28    *
29    * @version $Id: 1f89945f47acd75a40a4502a3367af3e43c2d21c $
30    */
 
31    public class CSSIdentifierSerializerTest
32    {
33    private CSSIdentifierSerializer cssIdentifierSerializer = new CSSIdentifierSerializer();
34   
35    /**
36    * NOTE: This test was adapted from Mathias Bynens' tests for CSS.escape polyfill, available under the MIT license.
37    *
38    * @see https://drafts.csswg.org/cssom/#serialize-an-identifier
39    * @see https://github.com/mathiasbynens/CSS.escape/blob/master/tests/tests.js
40    */
 
41  1 toggle @Test
42    public void css()
43    {
44  1 assertCssThrowsException("\u0000");
45  1 assertCssThrowsException("a\u0000");
46  1 assertCssThrowsException("\u0000b");
47  1 assertCssThrowsException("a\u0000b");
48   
49  1 assertNull(this.cssIdentifierSerializer.serialize(null));
50  1 assertCss("", "");
51   
52  1 assertCss("\u0001\u0002\u001E\u001F", "\\1 \\2 \\1e \\1f ");
53   
54  1 assertCss("0a", "\\30 a");
55  1 assertCss("1a", "\\31 a");
56  1 assertCss("2a", "\\32 a");
57  1 assertCss("3a", "\\33 a");
58  1 assertCss("4a", "\\34 a");
59  1 assertCss("5a", "\\35 a");
60  1 assertCss("6a", "\\36 a");
61  1 assertCss("7a", "\\37 a");
62  1 assertCss("8a", "\\38 a");
63  1 assertCss("9a", "\\39 a");
64   
65  1 assertCss("a0b", "a0b");
66  1 assertCss("a1b", "a1b");
67  1 assertCss("a2b", "a2b");
68  1 assertCss("a3b", "a3b");
69  1 assertCss("a4b", "a4b");
70  1 assertCss("a5b", "a5b");
71  1 assertCss("a6b", "a6b");
72  1 assertCss("a7b", "a7b");
73  1 assertCss("a8b", "a8b");
74  1 assertCss("a9b", "a9b");
75   
76  1 assertCss("-0a", "-\\30 a");
77  1 assertCss("-1a", "-\\31 a");
78  1 assertCss("-2a", "-\\32 a");
79  1 assertCss("-3a", "-\\33 a");
80  1 assertCss("-4a", "-\\34 a");
81  1 assertCss("-5a", "-\\35 a");
82  1 assertCss("-6a", "-\\36 a");
83  1 assertCss("-7a", "-\\37 a");
84  1 assertCss("-8a", "-\\38 a");
85  1 assertCss("-9a", "-\\39 a");
86   
87  1 assertCss("-", "\\-");
88  1 assertCss("-a", "-a");
89  1 assertCss("--", "--");
90  1 assertCss("--a", "--a");
91   
92  1 assertCss("\u0080\u002D\u005F\u00A9", "\u0080\u002D\u005F\u00A9");
93  1 assertCss("\u007F\u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088"
94    + "\u0089\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092"
95    + "\u0093\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F",
96    "\\7f \u0080\u0081\u0082\u0083\u0084\u0085\u0086\u0087\u0088\u0089"
97    + "\u008A\u008B\u008C\u008D\u008E\u008F\u0090\u0091\u0092\u0093"
98    + "\u0094\u0095\u0096\u0097\u0098\u0099\u009A\u009B\u009C\u009D\u009E\u009F");
99  1 assertCss("\u00A0\u00A1\u00A2", "\u00A0\u00A1\u00A2");
100  1 assertCss("a0123456789b", "a0123456789b");
101  1 assertCss("abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz");
102  1 assertCss("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
103   
104  1 assertCss("\u0020\u0021\u0078\u0079", "\\ \\!xy");
105   
106    // astral symbol (U+1D306 TETRAGRAM FOR CENTRE)
107  1 assertCss("\uD834\uDF06", "\uD834\uDF06");
108    // lone surrogates
109  1 assertCss("\uDF06", "\uDF06");
110  1 assertCss("\uD834", "\uD834");
111   
112  1 assertCss("a#b.c d[e=f]g{h:i;j}k", "a\\#b\\.c\\ d\\[e\\=f\\]g\\{h\\:i\\;j\\}k");
113    }
114   
 
115  47 toggle private void assertCss(String expected, String actual)
116    {
117  47 assertEquals(this.cssIdentifierSerializer.serialize(expected), actual);
118    }
119   
 
120  4 toggle private void assertCssThrowsException(String input)
121    {
122  4 try {
123  4 this.cssIdentifierSerializer.serialize(input);
124  0 fail("Should have thrown IllegalArgumentException!");
125    } catch (IllegalArgumentException e) {
126  4 assertEquals("Invalid character: the input contains U+0000.", e.getMessage());
127    }
128    }
129    }