|
| 1 | +// This file is part of arduino-check. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-check. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package checkfunctions |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + "regexp" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/arduino/arduino-check/check/checkdata" |
| 24 | + "github.com/arduino/arduino-check/check/checkresult" |
| 25 | + "github.com/arduino/arduino-check/project" |
| 26 | + "github.com/arduino/arduino-check/project/projecttype" |
| 27 | + "github.com/arduino/go-paths-helper" |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | +) |
| 30 | + |
| 31 | +var testDataPath *paths.Path |
| 32 | +var schemasPath *paths.Path |
| 33 | + |
| 34 | +func init() { |
| 35 | + workingDirectory, _ := os.Getwd() |
| 36 | + testDataPath = paths.New(workingDirectory, "testdata", "libraries") |
| 37 | + schemasPath = paths.New(workingDirectory, "..", "..", "etc", "schemas") |
| 38 | +} |
| 39 | + |
| 40 | +type checkFunctionTestTable struct { |
| 41 | + testName string |
| 42 | + libraryFolderName string |
| 43 | + expectedCheckResult checkresult.Type |
| 44 | + expectedOutputQuery string |
| 45 | +} |
| 46 | + |
| 47 | +func checkCheckFunction(checkFunction Type, testTables []checkFunctionTestTable, t *testing.T) { |
| 48 | + for _, testTable := range testTables { |
| 49 | + expectedOutputRegexp := regexp.MustCompile(testTable.expectedOutputQuery) |
| 50 | + |
| 51 | + testProject := project.Type{ |
| 52 | + Path: testDataPath.Join(testTable.libraryFolderName), |
| 53 | + ProjectType: projecttype.Library, |
| 54 | + SuperprojectType: projecttype.Library, |
| 55 | + } |
| 56 | + |
| 57 | + checkdata.Initialize(testProject, schemasPath) |
| 58 | + |
| 59 | + result, output := checkFunction() |
| 60 | + assert.Equal(t, testTable.expectedCheckResult, result, testTable.testName) |
| 61 | + assert.True(t, expectedOutputRegexp.MatchString(output), testTable.testName) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func TestLibraryPropertiesNameFieldDuplicate(t *testing.T) { |
| 66 | + testTables := []checkFunctionTestTable{ |
| 67 | + {"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""}, |
| 68 | + {"Duplicate", "Indexed", checkresult.Fail, "^Servo$"}, |
| 69 | + {"Not duplicate", "NotIndexed", checkresult.Pass, ""}, |
| 70 | + } |
| 71 | + |
| 72 | + checkCheckFunction(LibraryPropertiesNameFieldDuplicate, testTables, t) |
| 73 | +} |
| 74 | + |
| 75 | +func TestLibraryPropertiesNameFieldNotInIndex(t *testing.T) { |
| 76 | + testTables := []checkFunctionTestTable{ |
| 77 | + {"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""}, |
| 78 | + {"In index", "Indexed", checkresult.Pass, ""}, |
| 79 | + {"Not in index", "NotIndexed", checkresult.Fail, "^NotIndexed$"}, |
| 80 | + } |
| 81 | + |
| 82 | + checkCheckFunction(LibraryPropertiesNameFieldNotInIndex, testTables, t) |
| 83 | +} |
| 84 | + |
| 85 | +func TestLibraryPropertiesDependsFieldNotInIndex(t *testing.T) { |
| 86 | + testTables := []checkFunctionTestTable{ |
| 87 | + {"Unable to load", "InvalidLibraryProperties", checkresult.NotRun, ""}, |
| 88 | + {"Dependency not in index", "DependsNotIndexed", checkresult.Fail, "^NotIndexed$"}, |
| 89 | + {"Dependency in index", "DependsIndexed", checkresult.Pass, ""}, |
| 90 | + {"No depends", "NoDepends", checkresult.NotRun, ""}, |
| 91 | + } |
| 92 | + |
| 93 | + checkCheckFunction(LibraryPropertiesDependsFieldNotInIndex, testTables, t) |
| 94 | +} |
0 commit comments