import * as Text from "../../../dist/amd/System/Text/Utility" ; import * as AU from "../../../dist/amd/System/Collections/Array/Utility" ; import NotImplementedException from "../../../dist/amd/System/Exceptions/NotImplementedException" ; import { ICollection } from "../../../dist/amd/System/Collections/ICollection" ; import { CollectionBase } from "../../../dist/amd/System/Collections/CollectionBase" ; function assertIsNumber ( assert , value , name ) { assert . ok ( ! isNaN ( value ) , Text . format ( "'{0}' must be a real number." , name ) ) ; } function assertAdding < T > ( assert , c , a ) { let count ; for ( let v of a ) { assertIsNumber ( assert , count = c . count , 'count' ) ; c . add ( v ) ; assertIsNumber ( assert , c . count , 'count' ) ; assert . equal ( c . count , count + 1 , "'count' should increment after adding." ) ; assert . ok ( c . contains ( v ) , "'value' must exist after adding." ) ; } } function assertCopyToClear < T > ( assert , c ) { let count ; assertIsNumber ( assert , count = c . count , 'count' ) ; if ( c . count < 2 ) throw "Can't assert '.copyTo()' or '.clear()' without at least (2) entries." ; const a = [ ] ; c . copyTo ( a ) ; assertIsNumber ( assert , c . count , 'count' ) ; assert . equal ( a . length , count , "An empty array's length should match the count if copied to." ) ; c . clear ( ) ; assert . equal ( c . count , 0 , "A collection's count should be zero after calling '.clear()'." ) ; for ( let v1 of a ) c . add ( v1 ) ; const extraSize = 10 ; const b = AU . initialize < T > ( count + extraSize ) ; c . copyTo ( b , 1 ) ; assert . equal ( b . length , count + extraSize , "An array's length should be equal to it's original length if the count added does not exceed the length." ) ; c . copyTo ( b , count + extraSize - 1 ) ; assert . equal ( b . length , 2 * count + extraSize - 1 , "An array's length should be equal to index+count if the count exceeds the length." ) ; c . clear ( ) ; assert . equal ( c . count , 0 , "A collection's count should be zero after calling '.clear()'." ) ; for ( let v2 of a ) c . add ( v2 ) ; assert . equal ( c . count , a . length , "A collection's count should be equal to the number of items added." ) ; } function assertRemoving < T > ( assert , c ) { let count ; assertIsNumber ( assert , count = c . count , 'count' ) ; if ( c . count < 2 ) throw "Can't assert '.remove()' without at least (2) entries." ; const a = [ ] ; c . copyTo ( a ) ; assertIsNumber ( assert , c . count , 'count' ) ; try { for ( let v of a ) { count -= c . remove ( v ) ; assertIsNumber ( assert , c . count , 'count' ) ; assert . equal ( c . count , count , "'count' should increment after removing." ) ; assert . ok ( ! c . contains ( v ) , "'value' must not exist after removing." ) ; } } catch ( ex ) { if ( ( ex ) instanceof ( NotImplementedException ) ) { console . log ( ex ) ; } else { throw ex ; } } } export function Collection < T > ( name , collection , sourceValues ) { if ( sourceValues . indexOf ( < any > null ) !== - 1 ) throw "Source values should not contain null as checking against null is one of the tests." ; QUnit . test ( name , assert => { assertAdding ( assert , collection , sourceValues ) ; assertCopyToClear ( assert , collection ) ; assertRemoving ( assert , collection ) ; assert . ok ( ! collection . contains ( < any > null ) , 'Equality comparison is not strict.' ) ; } ) ; QUnit . test ( name + ".linqAsync()" , ( assert ) => { let accept = assert . async ( ) ; collection . linqAsync ( linq => { assert . ok ( ! ! linq , "Expects a linq enumerable instance." ) ; assert . ok ( ! ! collection . linq , "Expects a linq enumerable instance." ) ; accept ( ) ; } ) ; } ) ; } export function StringCollection ( name , collection ) { Collection ( name + '<' + 'string>' , collection , [ "" , "lorem" , "ipsum" , "dolem" , "ipsum" ] ) ; } export function NumberCollection ( name , collection ) { Collection ( name + '<' + 'number>' , collection , [ 0 , 1 , 1 , 2 , 3 , 5 , 8 , NaN ] ) ; } export function InstanceCollection ( name , collection ) { const repeat = { } ; Collection ( name + '<' + 'Object>' , collection , [ undefined , { } , repeat , { } , repeat ] ) ; }