diff --git a/src/spdx2/Makefile b/src/spdx2/Makefile index 1c5e299040..4470f0b7c2 100644 --- a/src/spdx2/Makefile +++ b/src/spdx2/Makefile @@ -22,7 +22,7 @@ all: VERSIONFILE $(call DIR_LOOP, ) test: all - $(MAKE) -C $(TESTDIR) test + $(call DIR_LOOP,test) coverage: all @echo "nothing to do" diff --git a/src/spdx2/agent/spdx2utils.php b/src/spdx2/agent/spdx2utils.php index d41c608a95..48d70d9f04 100644 --- a/src/spdx2/agent/spdx2utils.php +++ b/src/spdx2/agent/spdx2utils.php @@ -80,12 +80,10 @@ static public function addPrefixOnDemandKeys($licenses, $spdxValidityChecker = n static public function addPrefixOnDemandList($licenses, $spdxValidityChecker = null) { - $ret = array(); - foreach($licenses as $license) + return array_map(function ($license) use ($spdxValidityChecker) { - $ret[] = self::addPrefixOnDemand($license, $spdxValidityChecker); - } - return $ret; + return SpdxTwoUtils::addPrefixOnDemand($license, $spdxValidityChecker); + },$licenses); } /** @@ -101,10 +99,7 @@ static public function implodeLicenses($licenses, $spdxValidityChecker = null) return ""; } - $licenses = array_map(function ($license) use ($spdxValidityChecker) - { - return SpdxTwoUtils::addPrefixOnDemand($license, $spdxValidityChecker); - },$licenses); + $licenses = self::addPrefixOnDemandList($licenses, $spdxValidityChecker); sort($licenses, SORT_NATURAL | SORT_FLAG_CASE); if(count($licenses) == 3 && diff --git a/src/spdx2/agent_tests/Unit/spdx2utilTest.php b/src/spdx2/agent_tests/Unit/spdx2utilTest.php index c68fe12472..c8e09bc3e2 100644 --- a/src/spdx2/agent_tests/Unit/spdx2utilTest.php +++ b/src/spdx2/agent_tests/Unit/spdx2utilTest.php @@ -54,7 +54,63 @@ public function testPreWorkOnArgsFlpRealWork() assertThat($result["key2"], equalTo("anotherValue")); } - public function provideLicenseSet() + public function testAddPrefixOnDemandNoChecker() + { + assertThat(SpdxTwoUtils::addPrefixOnDemand("LIC1"), equalTo("LIC1")); + } + + public function provideLicenseSetAddPrefixOnDemand() + { + $constTrue = function ($licId) { return true; }; + $constFalse = function ($licId) { return false; }; + + return array( + 'null' => array("LIC1", null, "LIC1"), + 'const false' => array("LIC1", $constFalse, SpdxTwoUtils::$prefix . "LIC1"), + 'const true' => array("LIC1", $constTrue, "LIC1") + ); + } + + /** + * @dataProvider provideLicenseSetAddPrefixOnDemand + * @param string $licenseId + * @param closure $checker + * @param string $expected + */ + public function testAddPrefixOnDemand($licenseId, $checker, $expected) + { + assertThat(SpdxTwoUtils::addPrefixOnDemand($licenseId, $checker), equalTo($expected)); + } + + public function provideLicenseSetAddPrefixOnDemandList() + { + $constTrue = function ($licId) { return true; }; + $constFalse = function ($licId) { return false; }; + + return array( + 'single with null' => array(array("LIC1"), null, array("LIC1")), + 'single with const false' => array(array("LIC1"), $constFalse, array(SpdxTwoUtils::$prefix . "LIC1")), + 'single with const true' => array(array("LIC1"), $constTrue, array("LIC1")), + 'multiple with null' => array(array("LIC1","LIC2","LIC3"), null, array("LIC1", "LIC2", "LIC3")), + 'multiple with const false' => array(array("LIC1","LIC2","LIC3"), $constFalse, array(SpdxTwoUtils::$prefix . "LIC1", SpdxTwoUtils::$prefix . "LIC2", SpdxTwoUtils::$prefix . "LIC3")), + 'multiple with const true' => array(array("LIC1","LIC2","LIC3"), $constTrue, array("LIC1","LIC2","LIC3")), + 'two licenses with one prefix (A)' => array(array("LIC1","LIC2"), function ($licId) { return $licId === 'LIC2';}, array(SpdxTwoUtils::$prefix.'LIC1', 'LIC2')), + 'two licenses with one prefix (2)' => array(array("LIC1","LIC2"), function ($licId) { return $licId === 'LIC1';}, array('LIC1', SpdxTwoUtils::$prefix.'LIC2')) + ); + } + + /** + * @dataProvider provideLicenseSetAddPrefixOnDemandList + * @param array $licenseIds + * @param closure $checker + * @param string $expected + */ + public function testAddPrefixOnDemandList($licenseIds, $checker, $expected) + { + assertThat(SpdxTwoUtils::addPrefixOnDemandList($licenseIds, $checker), equalTo($expected)); + } + + public function provideLicenseSetImplodeLicenses() { $constTrue = function ($licId) { return true; }; $constFalse = function ($licId) { return false; }; @@ -79,7 +135,7 @@ public function provideLicenseSet() } /** - * @dataProvider provideLicenseSet + * @dataProvider provideLicenseSetImplodeLicenses * @param array $lics * @param string $prefix * @param string $expected