ShaarliOSTests.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // ShaarliOSTests.m
  3. // ShaarliOSTests
  4. //
  5. // Created by Marcus Rohrmoser on 18.03.15.
  6. // Copyright (c) 2015-2016 Marcus Rohrmoser http://mro.name/me. All rights reserved.
  7. //
  8. // This program is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. #import "XCTestCase+Tools.h"
  22. #import "MROStateMachine.h"
  23. @interface ShaarliOSTests : XCTestCase
  24. @property (nonatomic, readwrite, strong) NSString *scheme;
  25. @property (nonatomic, readwrite, assign) BOOL authMissing;
  26. @end
  27. @implementation ShaarliOSTests
  28. -(void)setUp
  29. {
  30. [super setUp];
  31. // Put setup code here. This method is called before the invocation of each test method in the class.
  32. }
  33. -(void)tearDown
  34. {
  35. // Put teardown code here. This method is called after the invocation of each test method in the class.
  36. [super tearDown];
  37. }
  38. #pragma mark - Many states, explicit state 'scheme'
  39. -(void)testUpdateEndpointFSMVerbose
  40. {
  41. MROStateMachine *sm = [[MROStateMachine alloc] initWithTarget:self name:@"Connect Endpoint"];
  42. [sm addTransitionFrom:@"HttpAuth" to:@"error"];
  43. [sm addTransitionFrom:@"HttpAuth" to:@"GetHttpLogin" guard:[NSPredicate predicateWithFormat:@"scheme = 'http'", nil]];
  44. [sm addTransitionFrom:@"HttpAuth" to:@"GetHttpsLogin" guard:[NSPredicate predicateWithFormat:@"scheme = 'https'", nil]];
  45. [sm addTransitionFrom:@"GetHttpLogin" to:@"HttpAuth" guard:[NSPredicate predicateWithFormat:@"YES = authMissing", nil]];
  46. [sm addTransitionFrom:@"GetHttpLogin" to:@"error"];
  47. [sm addTransitionFrom:@"GetHttpLogin" to:@"PostHttpLogin" guard:[NSPredicate predicateWithFormat:@"NO = authMissing"]];
  48. [sm addTransitionFrom:@"GetHttpsLogin" to:@"HttpAuth"];
  49. [sm addTransitionFrom:@"GetHttpsLogin" to:@"error"];
  50. [sm addTransitionFrom:@"GetHttpsLogin" to:@"GetHttpLogin" guard:[NSPredicate predicateWithFormat:@"NO = authMissing"]];
  51. [sm addTransitionFrom:@"GetHttpsLogin" to:@"PostHttpsLogin" guard:[NSPredicate predicateWithFormat:@"NO = authMissing"]];
  52. [sm addTransitionFrom:@"PostHttpLogin" to:@"error"];
  53. [sm addTransitionFrom:@"PostHttpLogin" to:@"success"];
  54. [sm addTransitionFrom:@"PostHttpsLogin" to:@"success"];
  55. [sm addTransitionFrom:@"PostHttpsLogin" to:@"error"];
  56. [sm buildMachineWithStartState:@"GetHttpsLogin" error:nil];
  57. MRLogD(@"%@", [sm descriptionDot], nil);
  58. XCTAssertEqualObjects(@"GetHttpsLogin", sm.currentState.name, @"foo", nil);
  59. self.scheme = HTTP_HTTP;
  60. [sm sendAction:@selector(transitionHttpAuth:)];
  61. [sm sendAction:@selector(transitionGetHttpLogin:)];
  62. // XCTAssertEqualObjects(@"HttpAuth", sm.currentState.name, @"foo", nil);
  63. }
  64. -(void)transitionGetHttpLogin:(MROTransition *)t
  65. {
  66. ;
  67. }
  68. -(void)transitionGetHttpsLogin:(MROTransition *)t
  69. {
  70. NSAssert(NO, @"Not implemented yet.", nil);
  71. }
  72. -(void)transitionPostHttpLogin:(MROTransition *)t
  73. {
  74. NSAssert(NO, @"Not implemented yet.", nil);
  75. }
  76. -(void)transitionPostHttpsLogin:(MROTransition *)t
  77. {
  78. NSAssert(NO, @"Not implemented yet.", nil);
  79. }
  80. #pragma mark - Few states, additional state 'scheme'
  81. -(void)testUpdateEndpointFSMTerse
  82. {
  83. MROStateMachine *sm = [[MROStateMachine alloc] initWithTarget:self name:@"Connect Endpoint"];
  84. [sm addTransitionFrom:@"GetLogin" to:@"error"];
  85. [sm addTransitionFrom:@"GetLogin" to:@"HttpAuth" guard:[NSPredicate predicateWithFormat:@"YES = authMissing"]];
  86. [sm addTransitionAction:@selector(transitionDowngradeHttps:) from:@"GetLogin" to:@"GetLogin" guard:[NSPredicate predicateWithFormat:@"NO = authMissing AND scheme = 'https'"]];
  87. [sm addTransitionFrom:@"GetLogin" to:@"PostLogin" guard:[NSPredicate predicateWithFormat:@"NO = authMissing"]];
  88. [sm addTransitionFrom:@"HttpAuth" to:@"error"];
  89. [sm addTransitionFrom:@"HttpAuth" to:@"GetLogin"];
  90. [sm addTransitionFrom:@"PostLogin" to:@"error"];
  91. [sm addTransitionFrom:@"PostLogin" to:@"success"].toState.didEnter =^(MROTransition *a, id b) {
  92. MRLogD (@"%@", b, nil);
  93. };
  94. [sm buildMachineWithStartState:@"GetLogin" error:nil];
  95. MRLogD (@"%@", [sm descriptionDot], nil);
  96. XCTAssertEqualObjects (@"GetLogin", sm.currentState.name, @"foo", nil);
  97. self.scheme = HTTP_HTTPS;
  98. self.authMissing = YES;
  99. [sm sendAction:@selector(transitionHttpAuth:)];
  100. [sm sendAction:@selector(transitionGetLogin:)];
  101. [sm sendAction:@selector(transitionHttpAuth:)];
  102. [sm sendAction:@selector(transitionGetLogin:)];
  103. [sm sendAction:@selector(transitionPostLogin:)];
  104. [sm sendAction:@selector(transitionSuccess:)];
  105. // XCTAssertEqualObjects(@"HttpAuth", sm.currentState.name, @"foo", nil);
  106. }
  107. -(void)transitionDowngradeHttps:(MROTransition *)t
  108. {
  109. self.scheme = HTTP_HTTP;
  110. }
  111. -(void)transitionError:(MROTransition *)t
  112. {
  113. NSAssert(NO, @"Not implemented yet.", nil);
  114. }
  115. -(void)transitionGetLogin:(MROTransition *)t
  116. {}
  117. -(void)transitionHttpAuth:(MROTransition *)t
  118. {
  119. if( [HTTP_HTTPS isEqualToString:self.scheme] )
  120. self.scheme = HTTP_HTTP;
  121. else
  122. self.authMissing = NO;
  123. }
  124. -(void)transitionPostLogin:(MROTransition *)t
  125. {
  126. ;
  127. }
  128. -(void)transitionSuccess:(MROTransition *)t
  129. {
  130. ;
  131. }
  132. #pragma mark - Few states
  133. -(void)testStatesPost
  134. {
  135. MROStateMachine *sm = [[MROStateMachine alloc] initWithTarget:self name:@"Post Link"];
  136. [sm addTransitionFrom:@"GetLogin" to:@"PostLogin" guard:[NSPredicate predicateWithFormat:@"NO = authMissing", nil]];
  137. [sm addTransitionFrom:@"GetLogin" to:@"HttpAuth" guard:[NSPredicate predicateWithFormat:@"YES = authMissing", nil]];
  138. [sm addTransitionFrom:@"GetLogin" to:@"error"];
  139. [sm addTransitionFrom:@"HttpAuth" to:@"error"];
  140. [sm addTransitionFrom:@"PostLogin" to:@"*PostLink"];
  141. [sm addTransitionFrom:@"PostLogin" to:@"error"];
  142. [sm addTransitionFrom:@"*PostLink" to:@"success"];
  143. [sm addTransitionFrom:@"*PostLink" to:@"error"];
  144. [sm addTransitionFrom:@"HttpAuth" to:@"GetLogin"];
  145. [sm buildMachineWithStartState:@"GetLogin" error:nil];
  146. MRLogD(@"%@", [sm descriptionDot], nil);
  147. }
  148. -(void)transitionPostLink:(MROTransition *)t
  149. {
  150. NSAssert(NO, @"Not implemented yet.", nil);
  151. }
  152. #pragma mark -
  153. -(void)testExample
  154. {
  155. // This is an example of a functional test case.
  156. XCTAssert(YES, @"Pass");
  157. }
  158. -(void)testPerformanceExample
  159. {
  160. // This is an example of a performance test case.
  161. [self measureBlock:^{
  162. // Put the code you want to measure the time of here.
  163. }
  164. ];
  165. }
  166. @end