/**
 * Copyright (c) 2016-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import * as AlgebraicTypeTemplatedMatching from '../../plugins/algebraic-type-templated-matching' ; import * as Code from '../../code' ; import * as CPlusPlus from '../../cplusplus' ; import * as CLangCommon from '../../clang-common' ; import * as AlgebraicType from '../../algebraic-type' ; const Plugin = AlgebraicTypeTemplatedMatching . createAlgebraicTypePlugin ( ) ; describe ( 'Plugins.AlgebraicTypeTemplatedMatching' , function ( ) { describe ( '#additionalFiles' , function ( ) { it ( 'returns a header and implementation for matching on an algebraic type' , function ( ) { const algebraicType = { annotations : { } , name : 'RMTest' , includes : [ ] , excludes : [ ] , typeLookups : [ ] , libraryName : null , comments : [ ] , subtypes : [ AlgebraicType . Subtype . NamedAttributeCollectionDefinition ( { name : 'SomeSubtype' , comments : [ ] , attributes : [ { annotations : { } , name : 'someString' , comments : [ ] , nullability : CLangCommon . Nullability . Inherited ( ) , type : { name : 'NSString' , reference : 'NSString *' , libraryTypeIsDefinedIn : null , fileTypeIsDefinedIn : null , underlyingType : 'NSObject' , conformingProtocol : null , referencedGenericTypes : [ ] } } , { annotations : { } , name : 'someUnsignedInteger' , comments : [ ] , nullability : CLangCommon . Nullability . Inherited ( ) , type : { name : 'NSUInteger' , reference : 'NSUInteger' , libraryTypeIsDefinedIn : null , fileTypeIsDefinedIn : null , underlyingType : null , conformingProtocol : null , referencedGenericTypes : [ ] } } , ] , annotations : { } } ) , AlgebraicType . Subtype . NamedAttributeCollectionDefinition ( { name : 'GreatThing' , comments : [ ] , attributes : [ ] , annotations : { } } ) , AlgebraicType . Subtype . SingleAttributeSubtypeDefinition ( { annotations : { } , name : 'coolSingleAttributeSubtype' , comments : [ ] , nullability : CLangCommon . Nullability . Inherited ( ) , type : { name : 'SingleAttributeType' , reference : 'SingleAttributeType *' , libraryTypeIsDefinedIn : 'SomeLib' , fileTypeIsDefinedIn : null , underlyingType : 'NSObject' , conformingProtocol : null , referencedGenericTypes : [ ] } } ) , ] } ; const additionalFiles = Plugin . additionalFiles ( algebraicType ) ; const expectedFile = { name : 'RMTestTemplatedMatchingHelpers' , type : Code . FileType . ObjectiveCPlusPlus , imports : [ { file : 'Foundation.h' , isPublic : true , library : 'Foundation' , requiresCPlusPlus : false } , { file : 'RMTest.h' , isPublic : true , library : null , requiresCPlusPlus : false } , { file : 'RMTestTemplatedMatchingHelpers.h' , isPublic : false , library : null , requiresCPlusPlus : false } , { file : 'memory' , isPublic : true , library : null , requiresCPlusPlus : true } , ] , enumerations : [ ] , blockTypes : [ ] , comments : [ ] , staticConstants : [ ] , globalVariables : [ ] , forwardDeclarations : [ ] , functions : [ ] , diagnosticIgnores : [ ] , classes : [ ] , structs : [ Code . Struct . ObjectiveCPlusPlusStruct ( { name : 'RMTestMatcher' , templates : [ { templatedTypes : [ { type : CPlusPlus . TemplateType . Typename ( ) , value : 'T' } , ] , code : [ ] } , ] , code : [ [ 'static T match(RMTest *test, T(^someSubtypeMatchHandler)(NSString *someString, NSUInteger someUnsignedInteger), T(^greatThingMatchHandler)(), T(^coolSingleAttributeSubtypeMatchHandler)(SingleAttributeType *coolSingleAttributeSubtype)) {' , '  NSCAssert(test != nil, @"The ADT object test is nil");' , '  __block std::unique_ptr<T> result;' , '' , '  RMTestSomeSubtypeMatchHandler __unsafe_unretained matchSomeSubtype = ^(NSString *someString, NSUInteger someUnsignedInteger) {' , '    result = std::make_unique<T>(someSubtypeMatchHandler(someString, someUnsignedInteger));' , '  };' , '' , '  RMTestGreatThingMatchHandler __unsafe_unretained matchGreatThing = ^(void) {' , '    result = std::make_unique<T>(greatThingMatchHandler());' , '  };' , '' , '  RMTestCoolSingleAttributeSubtypeMatchHandler __unsafe_unretained matchCoolSingleAttributeSubtype = ^(SingleAttributeType *coolSingleAttributeSubtype) {' , '    result = std::make_unique<T>(coolSingleAttributeSubtypeMatchHandler(coolSingleAttributeSubtype));' , '  };' , '' , '  [test matchSomeSubtype:matchSomeSubtype greatThing:matchGreatThing coolSingleAttributeSubtype:matchCoolSingleAttributeSubtype];' , '  return *result;' , '}' , ] , [ 'static T match(T(^someSubtypeMatchHandler)(NSString *someString, NSUInteger someUnsignedInteger), T(^greatThingMatchHandler)(), T(^coolSingleAttributeSubtypeMatchHandler)(SingleAttributeType *coolSingleAttributeSubtype), RMTest *test) {' , '  return match(test, someSubtypeMatchHandler, greatThingMatchHandler, coolSingleAttributeSubtypeMatchHandler);' , '}' , ] , ] } ) , ] , cppClasses : [ ] , namespaces : [ ] , macros : [ ] } ; expect ( additionalFiles [ 0 ] ) . toEqual ( expectedFile ) ; } ) ; } ) ; } ) ;