Skip to content

Package: NumberAwareStringComparator_PTest

NumberAwareStringComparator_PTest

nameinstructionbranchcomplexitylinemethod
NumberAwareStringComparator_PTest()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
houseNumbers()
M: 0 C: 46
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
ignoreCase()
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
mixedSameSegments()
M: 0 C: 82
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
setUp()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.common.sort;
15:
16: import static org.hamcrest.MatcherAssert.assertThat;
17: import static org.hamcrest.Matchers.equalTo;
18: import static org.hamcrest.Matchers.greaterThanOrEqualTo;
19: import static org.hamcrest.Matchers.lessThanOrEqualTo;
20:
21: import org.eclipse.emfforms.spi.common.sort.NumberAwareStringComparator;
22: import org.junit.BeforeClass;
23: import org.junit.Test;
24:
25: /**
26: * Unit tests for {@link NumberAwareStringComparator}. Run as plugin test because otherwise the hamcrest matchers do not
27: * work due to a security exception.
28: *
29: * @author Lucas Koehler
30: *
31: */
32: public class NumberAwareStringComparator_PTest {
33:
34:         private static NumberAwareStringComparator comparator;
35:
36:         @BeforeClass
37:         public static void setUp() {
38:                 comparator = NumberAwareStringComparator.getInstance();
39:         }
40:
41:         @Test
42:         public void houseNumbers() {
43:                 // With normal string compare a letter is bigger than a digit => 100A would be bigger than 1000A
44:                 // With number aware compare 100A should be smaller
45:                 assertThat(comparator.compare("100A", "1000A"), lessThanOrEqualTo(-1)); //$NON-NLS-1$//$NON-NLS-2$
46:
47:                 assertThat(comparator.compare("2A", "10A"), lessThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
48:
49:                 assertThat(comparator.compare("2B", "10A"), lessThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
50:
51:                 assertThat(comparator.compare("2B", "2A"), greaterThanOrEqualTo(1)); //$NON-NLS-1$ //$NON-NLS-2$
52:
53:                 assertThat(comparator.compare("25C", "25C"), equalTo(0)); //$NON-NLS-1$ //$NON-NLS-2$
54:         }
55:
56:         @Test
57:         public void mixedSameSegments() {
58:                 assertThat(comparator.compare("n1a1", "n1aa"), lessThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
59:
60:                 assertThat(comparator.compare("n1aa", "n1a1"), greaterThanOrEqualTo(1)); //$NON-NLS-1$ //$NON-NLS-2$
61:
62:                 assertThat(comparator.compare("n1aa", "n1aa"), equalTo(0)); //$NON-NLS-1$ //$NON-NLS-2$
63:
64:                 assertThat(comparator.compare("n1a1", "n1a1"), equalTo(0)); //$NON-NLS-1$ //$NON-NLS-2$
65:
66:                 assertThat(comparator.compare("n1a1", "n1a"), greaterThanOrEqualTo(1)); //$NON-NLS-1$ //$NON-NLS-2$
67:
68:                 assertThat(comparator.compare("n1a1", "n1aa1"), lessThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
69:
70:                 assertThat(comparator.compare("n1b1", "n1a2"), greaterThanOrEqualTo(1)); //$NON-NLS-1$ //$NON-NLS-2$
71:
72:                 assertThat(comparator.compare("n1a1", "n1a1a"), lessThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
73:
74:                 assertThat(comparator.compare("n1a2", "n1a1a"), greaterThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
75:         }
76:
77:         /** The sorting should ignore case because otherwise z > a > Z which would be really unintuitive for users. */
78:         @Test
79:         public void ignoreCase() {
80:                 assertThat(comparator.compare("a", "z"), lessThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
81:                 assertThat(comparator.compare("a", "Z"), lessThanOrEqualTo(-1)); //$NON-NLS-1$ //$NON-NLS-2$
82:         }
83: }