Class: SwiftGenerator::SwiftUnitTestClass

Inherits:
SwiftClass show all
Defined in:
lib/swift_generator/code_generation/swift_class_generation.rb

Instance Attribute Summary collapse

Attributes inherited from SwiftClass

#access_control_modifier, #auto_test_class, #do_generate, #parent_class, #post_super_initializations, #supporting_elements_created, #test_object_method

Attributes inherited from SwiftNonPrimitive

#access_control_modifier, #class_characteristics, #definition_set, #file_name, #inheritance_list, #initializers, #is_test_element, #is_user_editable, #methods, #properties, #source_file, #specified_type_name, #type_name

Instance Method Summary collapse

Methods inherited from SwiftClass

#add_simple_class_property, #create_copy_methods, #create_description_methods, #create_equality_methods, #create_init_methods, #create_test_classes, #create_user_classes, #insert_marshal_expression, #insert_unmarshal_expression, #make_property_type, #post_super_init, #prepare_marshaling_code, #prepare_supporting_elements, #resolve_inheritance, #set_test_values, #super_has_characteristics

Methods inherited from SwiftNonPrimitive

#comparable_properties, #make_property_type, #persistent_properties, #prepare_supporting_elements, #resolve_property_types, #swift_type_symbol, #transient_properties

Constructor Details

#initialize(definition_set, tested_class, class_purpose) ⇒ SwiftUnitTestClass

Returns a new instance of SwiftUnitTestClass.

Parameters:

  • tested_class (SwiftClass)
  • class_purpose (String)


749
750
751
752
753
754
755
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 749

def initialize(definition_set, tested_class, class_purpose)
	@tested_class = tested_class
	@tested_class_name = tested_class.type_name
	class_name = tested_class.specified_type_name + class_purpose + "Test"
	super(definition_set, class_name, ['XCTestCase'], file_name: class_name, is_test_element: true, characteristics:[] )
	@source_file.add_import('XCTest')
end

Instance Attribute Details

#tested_classObject

Returns the value of attribute tested_class.



744
745
746
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 744

def tested_class
  @tested_class
end

#tested_class_nameObject

Returns the value of attribute tested_class_name.



745
746
747
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 745

def tested_class_name
  @tested_class_name
end

Instance Method Details

#ensure_test_object_methodObject

Utility



844
845
846
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 844

def ensure_test_object_method
	@tested_class.ensure_test_object_method
end

#generate_copy_testObject



766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 766

def generate_copy_test
	ensure_test_object_method

	comment = "/// Test copy() implementation. Requires isEqual()"
	m = SwiftMethod.new(self, "testCopying", '', '', comment: comment)
	m << "let original = #{test_object_method_call()}"
	m << "let theCopy = original.copy() as #{@tested_class_name}"

	m << "if( theCopy != original ) {"
	m << "    print(\"original\")"
	m << "    print(original.description())"
	m << "    print(\"theCopy\")"
	m << "    print(theCopy.description())"
	m << "}"

	m << "XCTAssertEqual( theCopy, original, \"copy does not match original\" )"
end

#generate_json_round_trip_testObject



810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 810

def generate_json_round_trip_test
	ensure_test_object_method
	comment = "/// Test JSON round trip for a single object"
	m = SwiftMethod.new(self, 'testJSONRoundTrip', '', nil, comment: comment)
	m << "let original = #{test_object_method_call()}"

	m << "" << "let jsonObject = NSMutableDictionary()"
	m << "original.marshalToJSON( jsonObject )" << ""

	m << "var error: NSError? = nil"
	m << "let jsonData = NSJSONSerialization.dataWithJSONObject(jsonObject, options: nil, error:&error) as NSData?"
	m << "XCTAssertNotNil( jsonData, \"Could not serialize to NSData\" )"

	m << "var deserializedJSON:AnyObject? = NSJSONSerialization.JSONObjectWithData(jsonData!, options: nil, error:&error)"
	m << "XCTAssertNotNil( deserializedJSON, \"Could not serialize to NSData\" )"

	m << "if let newJSONObject = deserializedJSON as? NSDictionary {"
	m._i 	"let theCopy = #{@tested_class_name}()"
	m << 	"theCopy.unmarshalFromJSON( newJSONObject )"

	m << 	"if( theCopy != original ) {"
	m._i 		"print(\"original\")"
	m << 		"print(original.description())"
	m << 		"print(\"theCopy\")"
	m._o 		"print(theCopy.description())"
	m << 	"}"

	m._o 	"XCTAssertEqual( theCopy, original, \"unmarshalled object should be == to original\" )"
	m << "} else {"
	m.ii 	"XCTAssert( false, \"JSON did not deserialize to an NSDictionary\" )"
	m << "}"
end

#generate_marshal_testObject



785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 785

def generate_marshal_test
	ensure_test_object_method

	comment = "/// Test Marshaling to JSON-compatible dictionaries"
	m = SwiftMethod.new(self, 'testMarshaling', '', nil, comment: comment)
	m << "let original = #{test_object_method_call()}"

	m << "" << "let jsonObject = NSMutableDictionary()"
	m << "original.marshalToJSON( jsonObject )" << ""

	m << "let theCopy = #{@tested_class_name}()"
	m << "theCopy.unmarshalFromJSON( jsonObject )" << ""

	m << "if( theCopy != original ) {"
	m._i 	"print(\"original\")"
	m << 	"print(original.description())"
	m << 	"print(\"theCopy\")"
	m._o 	"print(theCopy.description())"
	m << "}"

	m << "XCTAssertEqual( theCopy, original, \"unmarshalled copy does not match original\" )"

end

#prepare_for_generationObject



758
759
760
761
762
763
764
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 758

def prepare_for_generation()
	super()

	generate_copy_test
	generate_marshal_test
	generate_json_round_trip_test
end

#test_object_method_call(index = 0) ⇒ Object



849
850
851
# File 'lib/swift_generator/code_generation/swift_class_generation.rb', line 849

def test_object_method_call(index=0)
	@tested_class.test_object_method_call( index )
end