| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| 17 | import static com.google.gitiles.Paths.simplifyPathUpToRoot; |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertNull; |
| 20 | |
| 21 | import org.junit.Test; |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame^] | 22 | import org.junit.runner.RunWith; |
| 23 | import org.junit.runners.JUnit4; |
| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 24 | |
| 25 | /** Tests for {@link Paths}. */ |
| Dave Borowitz | 3dc854f | 2014-11-04 16:19:37 -0800 | [diff] [blame^] | 26 | @RunWith(JUnit4.class) |
| Dave Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 27 | public class PathsTest { |
| 28 | @Test |
| 29 | public void simplifyPathUpToRootSimplifiesPath() throws Exception { |
| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 30 | 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 Borowitz | d40bdf1 | 2014-04-19 19:33:56 -0700 | [diff] [blame] | 43 | @Test |
| 44 | public void simplifyPathUpToNullRootDetectsNullRoot() throws Exception { |
| Dave Borowitz | bcd753d | 2013-02-08 11:10:19 -0800 | [diff] [blame] | 45 | 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 | } |