class SwiftGenerator::SwiftUnitTestClass

Attributes

tested_class[RW]
tested_class_name[RW]

Public Class Methods

new(definition_set, tested_class, class_purpose) click to toggle source

@param [SwiftClass] tested_class @param [String] class_purpose

Calls superclass method SwiftGenerator::SwiftClass::new
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 775
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

Public Instance Methods

ensure_test_object_method() click to toggle source

Utility

# File lib/swift_generator/code_generation/swift_class_generation.rb, line 869
def ensure_test_object_method
        @tested_class.ensure_test_object_method
end
generate_copy_test() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 791
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_test() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 835
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_test() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 810
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_generation() click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 783
def prepare_for_generation()
        super()

        generate_copy_test
        generate_marshal_test
        generate_json_round_trip_test
end
test_object_method_call(index=0) click to toggle source
# File lib/swift_generator/code_generation/swift_class_generation.rb, line 874
def test_object_method_call(index=0)
        @tested_class.test_object_method_call( index )
end