|
| 1 | +/* |
| 2 | + * C++ Community Plugin (cxx plugin) |
| 3 | + * Copyright (C) 2010-2023 SonarOpenCommunity |
| 4 | + * http://github.com/SonarOpenCommunity/sonar-cxx |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.cxx.utils; |
| 21 | + |
| 22 | +import static org.assertj.core.api.Assertions.*; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +public class CxxReportLocationTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + void testConstructor() { |
| 29 | + var loc0 = new CxxReportLocation(null, null, null, "info"); |
| 30 | + assertThat(loc0.getFile()).isNull(); |
| 31 | + assertThat(loc0.getLine()).isNull(); |
| 32 | + assertThat(loc0.getColumn()).isNull(); |
| 33 | + assertThat(loc0.getInfo()).isEqualTo("info"); |
| 34 | + assertThat(loc0.toString()).isEqualTo("CxxReportLocation [file=null, line=null, column=null, info=info]"); |
| 35 | + |
| 36 | + var loc1 = new CxxReportLocation("file", "line", "column", "info"); |
| 37 | + assertThat(loc1.getFile()).isEqualTo("file"); |
| 38 | + assertThat(loc1.getLine()).isEqualTo("line"); |
| 39 | + assertThat(loc1.getColumn()).isEqualTo("column"); |
| 40 | + assertThat(loc1.getInfo()).isEqualTo("info"); |
| 41 | + assertThat(loc1.toString()).isEqualTo("CxxReportLocation [file=file, line=line, column=column, info=info]"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testPathSanitize() { |
| 46 | + var loc = new CxxReportLocation("file", null, null, ""); |
| 47 | + |
| 48 | + loc = new CxxReportLocation("/dir/File", null, null, ""); |
| 49 | + assertThat(loc.getFile()).isEqualTo("/dir/File"); |
| 50 | + loc = new CxxReportLocation("/dir/./File", null, null, ""); |
| 51 | + assertThat(loc.getFile()).isEqualTo("/dir/File"); |
| 52 | + loc = new CxxReportLocation("/dir/../File", null, null, ""); |
| 53 | + assertThat(loc.getFile()).isEqualTo("/File"); |
| 54 | + loc = new CxxReportLocation("dir/File", null, null, ""); |
| 55 | + assertThat(loc.getFile()).isEqualTo("dir/File"); |
| 56 | + loc = new CxxReportLocation("./dir/File", null, null, ""); |
| 57 | + assertThat(loc.getFile()).isEqualTo("dir/File"); |
| 58 | + loc = new CxxReportLocation("../dir/File", null, null, ""); |
| 59 | + assertThat(loc.getFile()).isEqualTo("File"); |
| 60 | + loc = new CxxReportLocation("../../File", null, null, ""); |
| 61 | + assertThat(loc.getFile()).isEqualTo("File"); |
| 62 | + |
| 63 | + loc = new CxxReportLocation("c:\\dir\\file.ext", null, null, ""); |
| 64 | + assertThat(loc.getFile()).isEqualTo("c:/dir/file.ext"); |
| 65 | + loc = new CxxReportLocation("C:\\dir\\.\\file.ext", null, null, ""); |
| 66 | + assertThat(loc.getFile()).isEqualTo("C:/dir/file.ext"); |
| 67 | + loc = new CxxReportLocation("c:\\dir\\..\\file.ext", null, null, ""); |
| 68 | + assertThat(loc.getFile()).isEqualTo("c:/file.ext"); |
| 69 | + loc = new CxxReportLocation("dir\\file.ext", null, null, ""); |
| 70 | + assertThat(loc.getFile()).isEqualTo("dir/file.ext"); |
| 71 | + loc = new CxxReportLocation(".\\dir\\file.ext", null, null, ""); |
| 72 | + assertThat(loc.getFile()).isEqualTo("dir/file.ext"); |
| 73 | + loc = new CxxReportLocation("..\\dir\\file.ext", null, null, ""); |
| 74 | + assertThat(loc.getFile()).isEqualTo("file.ext"); |
| 75 | + loc = new CxxReportLocation("..\\..\\file.ext", null, null, ""); |
| 76 | + assertThat(loc.getFile()).isEqualTo("file.ext"); |
| 77 | + } |
| 78 | +} |
0 commit comments