FSTAssertTests.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "Firestore/Source/Util/FSTAssert.h"
  17. #import <XCTest/XCTest.h>
  18. @interface FSTAssertTests : XCTestCase
  19. @end
  20. @implementation FSTAssertTests
  21. - (void)testFail {
  22. @try {
  23. [self failingMethod];
  24. XCTFail("Should not have succeeded");
  25. } @catch (NSException *ex) {
  26. XCTAssertEqualObjects(ex.name, NSInternalInconsistencyException);
  27. XCTAssertEqualObjects(ex.reason, @"FIRESTORE INTERNAL ASSERTION FAILED: 0:foo:bar");
  28. }
  29. }
  30. // A method guaranteed to fail. Note that the return type is intentionally something that is
  31. // not actually returned in the body to ensure that the function attribute declarations in the
  32. // macro properly mark this macro invocation as never returning.
  33. - (int)failingMethod {
  34. FSTFail(@"%d:%s:%@", 0, "foo", @"bar");
  35. }
  36. - (void)testCFail {
  37. @try {
  38. failingFunction();
  39. XCTFail("Should not have succeeded");
  40. } @catch (NSException *ex) {
  41. XCTAssertEqualObjects(ex.name, NSInternalInconsistencyException);
  42. XCTAssertEqualObjects(ex.reason, @"FIRESTORE INTERNAL ASSERTION FAILED: 0:foo:bar");
  43. }
  44. }
  45. // A function guaranteed to fail. Note that the return type is intentionally something that is
  46. // not actually returned in the body to ensure that the function attribute declarations in the
  47. // macro properly mark this macro invocation as never returning.
  48. int failingFunction() {
  49. FSTCFail(@"%d:%s:%@", 0, "foo", @"bar");
  50. }
  51. - (void)testAssertNonFailing {
  52. @try {
  53. FSTAssert(YES, @"shouldn't fail");
  54. } @catch (NSException *ex) {
  55. XCTFail("Should not have failed, but got %@", ex);
  56. }
  57. }
  58. - (void)testAssertFailing {
  59. @try {
  60. FSTAssert(NO, @"should fail");
  61. XCTFail("Should not have succeeded");
  62. } @catch (NSException *ex) {
  63. XCTAssertEqualObjects(ex.name, NSInternalInconsistencyException);
  64. XCTAssertEqualObjects(ex.reason, @"FIRESTORE INTERNAL ASSERTION FAILED: should fail");
  65. }
  66. }
  67. - (void)testCAssertNonFailing {
  68. @try {
  69. nonAssertingFunction();
  70. } @catch (NSException *ex) {
  71. XCTFail("Should not have failed, but got %@", ex);
  72. }
  73. }
  74. int nonAssertingFunction() {
  75. FSTCAssert(YES, @"shouldn't fail");
  76. return 0;
  77. }
  78. - (void)testCAssertFailing {
  79. @try {
  80. assertingFunction();
  81. XCTFail("Should not have succeeded");
  82. } @catch (NSException *ex) {
  83. XCTAssertEqualObjects(ex.name, NSInternalInconsistencyException);
  84. XCTAssertEqualObjects(ex.reason, @"FIRESTORE INTERNAL ASSERTION FAILED: should fail");
  85. }
  86. }
  87. int assertingFunction() {
  88. FSTCAssert(NO, @"should fail");
  89. }
  90. @end