aboutsummaryrefslogtreecommitdiffhomepage
path: root/Functions/Example/Tests
diff options
context:
space:
mode:
authorGravatar Paul Beusterien <paulbeusterien@google.com>2018-03-20 11:59:03 -0700
committerGravatar GitHub <noreply@github.com>2018-03-20 11:59:03 -0700
commitb7f35a0b76bb2afd682b806d2b25568611612557 (patch)
treedeb4577d3e54c3fafa2a065605faef228a186c1b /Functions/Example/Tests
parent7e65885762757209e0e14ec28e99ec91380e9c2f (diff)
Initial Firebase Functions (#948)
Diffstat (limited to 'Functions/Example/Tests')
-rw-r--r--Functions/Example/Tests/FIRFunctionsTests.m50
-rw-r--r--Functions/Example/Tests/FUNSerializerTests.m240
-rw-r--r--Functions/Example/Tests/Tests-Info.plist22
-rw-r--r--Functions/Example/Tests/en.lproj/InfoPlist.strings2
4 files changed, 314 insertions, 0 deletions
diff --git a/Functions/Example/Tests/FIRFunctionsTests.m b/Functions/Example/Tests/FIRFunctionsTests.m
new file mode 100644
index 0000000..bec96e6
--- /dev/null
+++ b/Functions/Example/Tests/FIRFunctionsTests.m
@@ -0,0 +1,50 @@
+// Copyright 2017 Google
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+@import XCTest;
+
+#import "FIRFunctions+Internal.h"
+#import "FIRFunctions.h"
+
+#import "FUNFakeApp.h"
+
+@interface FIRFunctionsTests : XCTestCase
+@end
+
+@implementation FIRFunctionsTests
+
+- (void)setUp {
+ [super setUp];
+}
+
+- (void)tearDown {
+ [super tearDown];
+}
+
+- (void)testURLWithName {
+ // TODO(klimt): Add this test back when we add the constructor back.
+ /*
+ id app = [[FUNFakeApp alloc] initWithProjectID:@"my-project"];
+ FIRFunctions *functions = [FIRFunctions functionsForApp:app region:@"my-region"];
+ NSString *url = [functions URLWithName:@"my-endpoint"];
+ XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url);
+ */
+
+ id app = [[FUNFakeApp alloc] initWithProjectID:@"my-project"];
+ FIRFunctions *functions = [FIRFunctions functionsForApp:app];
+ NSString *url = [functions URLWithName:@"my-endpoint"];
+ XCTAssertEqualObjects(@"https://us-central1-my-project.cloudfunctions.net/my-endpoint", url);
+}
+
+@end
diff --git a/Functions/Example/Tests/FUNSerializerTests.m b/Functions/Example/Tests/FUNSerializerTests.m
new file mode 100644
index 0000000..707c3e7
--- /dev/null
+++ b/Functions/Example/Tests/FUNSerializerTests.m
@@ -0,0 +1,240 @@
+// Copyright 2017 Google
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+@import XCTest;
+
+#import "FIRError.h"
+#import "FUNSerializer.h"
+
+@interface FUNSerializerTests : XCTestCase
+@end
+
+@implementation FUNSerializerTests
+
+- (void)setUp {
+ [super setUp];
+}
+
+- (void)tearDown {
+ [super tearDown];
+}
+
+- (void)testEncodeNull {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ XCTAssertEqualObjects([NSNull null], [serializer encode:[NSNull null]]);
+}
+
+- (void)testDecodeNull {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects([NSNull null], [serializer decode:[NSNull null] error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testEncodeInt {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ XCTAssertEqualObjects(@1, [serializer encode:@1]);
+}
+
+- (void)testDecodeInt {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects(@1, [serializer decode:@1 error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testEncodeLong {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSDictionary *expected = @{
+ @"@type" : @"type.googleapis.com/google.protobuf.Int64Value",
+ @"value" : @"-9223372036854775800",
+ };
+ XCTAssertEqualObjects(expected, [serializer encode:@-9223372036854775800L]);
+}
+
+- (void)testDecodeLong {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSDictionary *input = @{
+ @"@type" : @"type.googleapis.com/google.protobuf.Int64Value",
+ @"value" : @"-9223372036854775800",
+ };
+ NSError *error = nil;
+ NSNumber *actual = [serializer decode:input error:&error];
+ XCTAssertEqualObjects(@-9223372036854775800L, actual);
+ // A naive implementation might convert a number to a double and think that's close enough.
+ // We need to make sure it's a long long for accuracy.
+ XCTAssertEqual('q', actual.objCType[0]);
+ XCTAssertNil(error);
+}
+
+- (void)testDecodeInvalidLong {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSDictionary *input = @{
+ @"@type" : @"type.googleapis.com/google.protobuf.Int64Value",
+ @"value" : @"-9223372036854775800 and some other junk",
+ };
+ NSError *error = nil;
+ NSNumber *actual = [serializer decode:input error:&error];
+ XCTAssertNil(actual);
+ XCTAssertNotNil(error);
+ XCTAssertEqualObjects(FIRFunctionsErrorDomain, error.domain);
+ XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
+}
+
+- (void)testEncodeUnsignedLong {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSDictionary *expected = @{
+ @"@type" : @"type.googleapis.com/google.protobuf.UInt64Value",
+ @"value" : @"18446744073709551600",
+ };
+ XCTAssertEqualObjects(expected, [serializer encode:@18446744073709551600UL]);
+}
+
+- (void)testDecodeUnsignedLong {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSDictionary *input = @{
+ @"@type" : @"type.googleapis.com/google.protobuf.UInt64Value",
+ @"value" : @"17446744073709551688",
+ };
+ NSError *error = nil;
+ NSNumber *actual = [serializer decode:input error:&error];
+ XCTAssertEqualObjects(@17446744073709551688UL, actual);
+ // A naive NSNumberFormatter implementation will convert the number to a double and think
+ // that's close enough. We need to make sure it's an unsigned long long for accuracy.
+ XCTAssertEqual('Q', actual.objCType[0]);
+ XCTAssertNil(error);
+}
+
+- (void)testEncodeDouble {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ XCTAssertEqualObjects(@1.2, [serializer encode:@1.2]);
+}
+
+- (void)testDecodeDouble {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects(@1.2, [serializer decode:@1.2 error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testEncodeBool {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ XCTAssertEqualObjects(@YES, [serializer encode:@YES]);
+}
+
+- (void)testDecodeBool {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects(@NO, [serializer decode:@NO error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testEncodeString {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ XCTAssertEqualObjects(@"hello", [serializer encode:@"hello"]);
+}
+
+- (void)testDecodeString {
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects(@"hello", [serializer decode:@"hello" error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testEncodeArray {
+ NSArray *input = @[ @1, @"two", @[ @3, @4L ] ];
+ NSArray *expected = @[
+ @1, @"two",
+ @[
+ @3, @{
+ @"@type" : @"type.googleapis.com/google.protobuf.Int64Value",
+ @"value" : @"4",
+ }
+ ]
+ ];
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ XCTAssertEqualObjects(expected, [serializer encode:input]);
+}
+
+- (void)testDecodeArray {
+ NSArray *input = @[
+ @1, @"two",
+ @[
+ @3, @{
+ @"@type" : @"type.googleapis.com/google.protobuf.Int64Value",
+ @"value" : @"4",
+ }
+ ]
+ ];
+ NSArray *expected = @[ @1, @"two", @[ @3, @4L ] ];
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+
+ XCTAssertEqualObjects(expected, [serializer decode:input error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testEncodeMap {
+ NSDictionary *input = @{ @"foo" : @1, @"bar" : @"hello", @"baz" : @[ @3, @4L ] };
+ NSDictionary *expected = @{
+ @"foo" : @1,
+ @"bar" : @"hello",
+ @"baz" : @[
+ @3, @{
+ @"@type" : @"type.googleapis.com/google.protobuf.Int64Value",
+ @"value" : @"4",
+ }
+ ]
+ };
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ XCTAssertEqualObjects(expected, [serializer encode:input]);
+}
+
+- (void)testDecodeMap {
+ NSDictionary *input = @{
+ @"foo" : @1,
+ @"bar" : @"hello",
+ @"baz" : @[
+ @3, @{
+ @"@type" : @"type.googleapis.com/google.protobuf.Int64Value",
+ @"value" : @"4",
+ }
+ ]
+ };
+ NSDictionary *expected = @{ @"foo" : @1, @"bar" : @"hello", @"baz" : @[ @3, @4L ] };
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects(expected, [serializer decode:input error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testDecodeUnknownType {
+ NSDictionary *input = @{@"@type" : @"unknown", @"value" : @"whatever"};
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects(input, [serializer decode:input error:&error]);
+ XCTAssertNil(error);
+}
+
+- (void)testDecodeUnknownTypeWithoutValue {
+ NSDictionary *input = @{
+ @"@type" : @"unknown",
+ };
+ FUNSerializer *serializer = [[FUNSerializer alloc] init];
+ NSError *error = nil;
+ XCTAssertEqualObjects(input, [serializer decode:input error:&error]);
+ XCTAssertNil(error);
+}
+
+@end
diff --git a/Functions/Example/Tests/Tests-Info.plist b/Functions/Example/Tests/Tests-Info.plist
new file mode 100644
index 0000000..169b6f7
--- /dev/null
+++ b/Functions/Example/Tests/Tests-Info.plist
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>en</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>BNDL</string>
+ <key>CFBundleShortVersionString</key>
+ <string>1.0</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1</string>
+</dict>
+</plist>
diff --git a/Functions/Example/Tests/en.lproj/InfoPlist.strings b/Functions/Example/Tests/en.lproj/InfoPlist.strings
new file mode 100644
index 0000000..477b28f
--- /dev/null
+++ b/Functions/Example/Tests/en.lproj/InfoPlist.strings
@@ -0,0 +1,2 @@
+/* Localized versions of Info.plist keys */
+