blob: 4e566e22dcef462526b913ec51fe90415ee3eed7 [file] [log] [blame]
Dave Borowitzbcd753d2013-02-08 11:10:19 -08001// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
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
15package com.google.gitiles;
16
17import static com.google.gitiles.Paths.simplifyPathUpToRoot;
Dave Borowitzd40bdf12014-04-19 19:33:56 -070018import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNull;
20
21import org.junit.Test;
Dave Borowitz3dc854f2014-11-04 16:19:37 -080022import org.junit.runner.RunWith;
23import org.junit.runners.JUnit4;
Dave Borowitzbcd753d2013-02-08 11:10:19 -080024
25/** Tests for {@link Paths}. */
Dave Borowitz3dc854f2014-11-04 16:19:37 -080026@RunWith(JUnit4.class)
Dave Borowitzd40bdf12014-04-19 19:33:56 -070027public class PathsTest {
28 @Test
29 public void simplifyPathUpToRootSimplifiesPath() throws Exception {
Dave Borowitzbcd753d2013-02-08 11:10:19 -080030 String root = "a/b/c";
31 assertNull(simplifyPathUpToRoot("/foo", root));
32 assertEquals("a", simplifyPathUpToRoot("../../", root));
33 assertEquals("a", simplifyPathUpToRoot(".././../", root));
34 assertEquals("a", simplifyPathUpToRoot("..//../", root));
35 assertEquals("a/d", simplifyPathUpToRoot("../../d", root));
36 assertEquals("", simplifyPathUpToRoot("../../..", root));
37 assertEquals("a/d/e", simplifyPathUpToRoot("../../d/e", root));
38 assertEquals("a/b", simplifyPathUpToRoot("../d/../e/../", root));
39 assertNull(simplifyPathUpToRoot("../../../../", root));
40 assertNull(simplifyPathUpToRoot("../../a/../../..", root));
41 }
42
Dave Borowitzd40bdf12014-04-19 19:33:56 -070043 @Test
44 public void simplifyPathUpToNullRootDetectsNullRoot() throws Exception {
Dave Borowitzbcd753d2013-02-08 11:10:19 -080045 assertNull(simplifyPathUpToRoot("/foo", null));
46 assertNull(simplifyPathUpToRoot("../", null));
47 assertNull(simplifyPathUpToRoot("../../", null));
48 assertNull(simplifyPathUpToRoot(".././../", null));
49 assertEquals("a/b", simplifyPathUpToRoot("a/b", null));
50 assertEquals("a/c", simplifyPathUpToRoot("a/b/../c", null));
51 }
52}