Skip to content

Conversation

@daxian-dbw
Copy link
Member

Fix #2208

for ($i = 0; $i -lt $Count; $i++)
{
$result[$i] | Should Be $Elements[$i]
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use Compare-Object and exclude Count at all?

Copy link
Member Author

@daxian-dbw daxian-dbw Feb 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the Count key is not necessary, and I updated the tests to avoid it.
Instead of using a loop or Compare-Object, I think $result -join ";" | Should Be ($Elements -join ";") is better here. In case the results are different, I can see the whole set of results and expected values in the log.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
Closed.


@{ Command = {$result = [System.Collections.ObjectModel.Collection[int]]@(1)}; CollectionType = 'Collection`1'; ElementType = "Int32"; Count = 1; Elements = @(1) }
@{ Command = {$result = [System.Collections.ObjectModel.Collection[int]]@(1,2)}; CollectionType = 'Collection`1'; ElementType = "Int32"; Count = 2; Elements = @(1,2) }
@{ Command = {$result = [System.Collections.ObjectModel.Collection[int]]"4"}; CollectionType = 'Collection`1'; ElementType = "Int32"; Count = 1; Elements = @(4) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it makes sense to add tests for complex type items (array, hash, psobject) because code use LanguagePrimitives.ConvertTo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added one more set of tests for [List[FileInfo]] cast from string or array of strings. If you think more tests is needed in this area, feel free to open an issue.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose that's enough.
Closed.

@PowerShellTeam PowerShellTeam added the Review - Needed The PR is being reviewed label Feb 20, 2017
$result -join ";" | Should Be ($Elements -join ";")
}

It "<Command>" -TestCases $testCases2 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we split testCases1 and testCases2? It seems the test code is the same.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point :) I thought the FileInfo test needs to be @($result.Name) -join ";", but $result -join ";" should work too.

@iSazonov
Copy link
Collaborator

LGTM for tests.

@daxian-dbw Please clarify for me: Is the PR related with return ,$arrayVar ?

@daxian-dbw
Copy link
Member Author

@iSazonov this PR is not related to the return ,$array approach to prevent unraveling the output. This PR is only for fixing #2208, so that you can cast a one-element array to a generic collection.

@iSazonov
Copy link
Collaborator

@daxian-dbw Thanks for clarify!

Copy link
Contributor

@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have one comment on the code - it would be good to ensure you know what the regression was from V4 and that we have tests for it - so we ensure this fix (which looks right to me) doesn't regress anything else.

Other than that, just the 2 minor comments on the tests.

$testCases1 = @(
@{ Command = {$result = [Collections.Generic.List[int]]@(1)}; CollectionType = 'List`1'; ElementType = "Int32"; Elements = @(1) }
@{ Command = {$result = [Collections.Generic.List[int]]@(1,2)}; CollectionType = 'List`1'; ElementType = "Int32"; Elements = @(1,2) }
@{ Command = {$result = [Collections.Generic.List[int]]"4"}; CollectionType = 'List`1'; ElementType = "Int32"; Elements = @(4) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also test > 1 string converting to List[int]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Fixed.

param($Command, $CollectionType, $ElementType, $Elements)

$result = $null
. $Command
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be less surprising to me if Command was just a script block and here you wrote:

$result = . $Command

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resulted collection will be unraveled if I use $result = . {[Collections.Generic.List[int]]@(1)}.

In order to prevent unraveling, a comma needs to be prefixed like "{,[Collections.Generic.List[int]]@(1)}".
But then the test case title becomes ",[Collections.Generic.List[int]]@(1)" which I thought was more confusing than "$result = [Collections.Generic.List[int]]@(1)" and that's why I choose the current form.
Do you want me to change back to {, [Collections.Generic.List[int]]@(1)}?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see - neither is great - I'm fine either way then.
Because you tried what I suggested first - maybe it's worth a comment to save someone else the trouble trying to change it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Will add a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added.

@daxian-dbw daxian-dbw merged commit 16ff197 into PowerShell:master Feb 23, 2017
@daxian-dbw daxian-dbw deleted the cast branch February 23, 2017 07:10
@iSazonov iSazonov removed the Review - Needed The PR is being reviewed label Mar 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants