|
| 1 | +// This file is part of libraries-repository-engine. |
| 2 | +// |
| 3 | +// Copyright 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published |
| 7 | +// by the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | +// You can be released from the requirements of the above licenses by purchasing |
| 19 | +// a commercial license. Buying such a license is mandatory if you want to |
| 20 | +// modify or otherwise use the software for commercial activities involving the |
| 21 | +// Arduino software without disclosing the source code of your own applications. |
| 22 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 23 | + |
| 24 | +package metadata |
| 25 | + |
| 26 | +import ( |
| 27 | + "fmt" |
| 28 | + "testing" |
| 29 | + |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | +) |
| 32 | + |
| 33 | +func TestParse(t *testing.T) { |
| 34 | + testTables := []struct { |
| 35 | + testName string |
| 36 | + propertiesData []byte |
| 37 | + libraryMetadataAssertion *LibraryMetadata |
| 38 | + errorAssertion assert.ErrorAssertionFunc |
| 39 | + }{ |
| 40 | + { |
| 41 | + testName: "Invalid", |
| 42 | + propertiesData: []byte(`broken`), |
| 43 | + libraryMetadataAssertion: nil, |
| 44 | + errorAssertion: assert.Error, |
| 45 | + }, |
| 46 | + { |
| 47 | + testName: "Compliant", |
| 48 | + propertiesData: []byte(` |
| 49 | +name=WebServer |
| 50 | +version=1.0.0 |
| 51 | +author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com> |
| 52 | +maintainer=Cristian Maglie <c.maglie@example.com> |
| 53 | +sentence=A library that makes coding a Webserver a breeze. |
| 54 | +paragraph=Supports HTTP1.1 and you can do GET and POST. |
| 55 | +category=Communication |
| 56 | +url=http://example.com/ |
| 57 | +architectures=avr |
| 58 | +includes=WebServer.h |
| 59 | +depends=ArduinoHttpClient |
| 60 | + `), |
| 61 | + libraryMetadataAssertion: &LibraryMetadata{ |
| 62 | + Name: "WebServer", |
| 63 | + Version: "1.0.0", |
| 64 | + Author: "Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>", |
| 65 | + Maintainer: "Cristian Maglie <c.maglie@example.com>", |
| 66 | + License: "", |
| 67 | + Sentence: "A library that makes coding a Webserver a breeze.", |
| 68 | + Paragraph: "Supports HTTP1.1 and you can do GET and POST.", |
| 69 | + URL: "http://example.com/", |
| 70 | + Architectures: "avr", |
| 71 | + Category: "Communication", |
| 72 | + Types: nil, |
| 73 | + Includes: "WebServer.h", |
| 74 | + Depends: "ArduinoHttpClient", |
| 75 | + }, |
| 76 | + errorAssertion: assert.NoError, |
| 77 | + }, |
| 78 | + { |
| 79 | + testName: "Invalid version", |
| 80 | + propertiesData: []byte(` |
| 81 | +name=WebServer |
| 82 | +version=foo |
| 83 | +author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com> |
| 84 | +maintainer=Cristian Maglie <c.maglie@example.com> |
| 85 | +sentence=A library that makes coding a Webserver a breeze. |
| 86 | +paragraph=Supports HTTP1.1 and you can do GET and POST. |
| 87 | +category=Communication |
| 88 | +url=http://example.com/ |
| 89 | +architectures=avr |
| 90 | +includes=WebServer.h |
| 91 | +depends=ArduinoHttpClient |
| 92 | + `), |
| 93 | + libraryMetadataAssertion: &LibraryMetadata{ |
| 94 | + Name: "WebServer", |
| 95 | + Version: "foo", |
| 96 | + Author: "Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>", |
| 97 | + Maintainer: "Cristian Maglie <c.maglie@example.com>", |
| 98 | + License: "", |
| 99 | + Sentence: "A library that makes coding a Webserver a breeze.", |
| 100 | + Paragraph: "Supports HTTP1.1 and you can do GET and POST.", |
| 101 | + URL: "http://example.com/", |
| 102 | + Architectures: "avr", |
| 103 | + Category: "Communication", |
| 104 | + Types: nil, |
| 105 | + Includes: "WebServer.h", |
| 106 | + Depends: "ArduinoHttpClient", |
| 107 | + }, |
| 108 | + errorAssertion: assert.NoError, |
| 109 | + }, |
| 110 | + { |
| 111 | + testName: "Non-semver version", |
| 112 | + propertiesData: []byte(` |
| 113 | +name=WebServer |
| 114 | +version=1.0 |
| 115 | +author=Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com> |
| 116 | +maintainer=Cristian Maglie <c.maglie@example.com> |
| 117 | +sentence=A library that makes coding a Webserver a breeze. |
| 118 | +paragraph=Supports HTTP1.1 and you can do GET and POST. |
| 119 | +category=Communication |
| 120 | +url=http://example.com/ |
| 121 | +architectures=avr |
| 122 | +includes=WebServer.h |
| 123 | +depends=ArduinoHttpClient |
| 124 | + `), |
| 125 | + libraryMetadataAssertion: &LibraryMetadata{ |
| 126 | + Name: "WebServer", |
| 127 | + Version: "1.0.0", |
| 128 | + Author: "Cristian Maglie <c.maglie@example.com>, Pippo Pluto <pippo@example.com>", |
| 129 | + Maintainer: "Cristian Maglie <c.maglie@example.com>", |
| 130 | + License: "", |
| 131 | + Sentence: "A library that makes coding a Webserver a breeze.", |
| 132 | + Paragraph: "Supports HTTP1.1 and you can do GET and POST.", |
| 133 | + URL: "http://example.com/", |
| 134 | + Architectures: "avr", |
| 135 | + Category: "Communication", |
| 136 | + Types: nil, |
| 137 | + Includes: "WebServer.h", |
| 138 | + Depends: "ArduinoHttpClient", |
| 139 | + }, |
| 140 | + errorAssertion: assert.NoError, |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + for _, testTable := range testTables { |
| 145 | + metadata, err := Parse(testTable.propertiesData) |
| 146 | + testTable.errorAssertion(t, err, fmt.Sprintf("%s error", testTable.testName)) |
| 147 | + if err == nil { |
| 148 | + assert.Equal(t, testTable.libraryMetadataAssertion, metadata, fmt.Sprintf("%s metadata", testTable.testName)) |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments