Skip to content

Commit

Permalink
test(spdx2): add more unit tests for util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhbr committed Jan 26, 2017
1 parent c952821 commit bc15d59
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/spdx2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 4 additions & 9 deletions src/spdx2/agent/spdx2utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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 &&
Expand Down
60 changes: 58 additions & 2 deletions src/spdx2/agent_tests/Unit/spdx2utilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand All @@ -79,7 +135,7 @@ public function provideLicenseSet()
}

/**
* @dataProvider provideLicenseSet
* @dataProvider provideLicenseSetImplodeLicenses
* @param array $lics
* @param string $prefix
* @param string $expected
Expand Down

0 comments on commit bc15d59

Please sign in to comment.