PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Spicelib-Commands/src/test/org/spicefactory/lib/command/CommandDataTest.as

https://bitbucket.org/borekb/parsley-spicelib-complete
ActionScript | 126 lines | 95 code | 13 blank | 18 comment | 0 complexity | b1a2377142b4a0fd2937f829bc2fa125 MD5 | raw file
  1. /*
  2. * Copyright 2011 the original author or authors.
  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. package org.spicefactory.lib.command {
  17. import org.flexunit.assertThat;
  18. import org.hamcrest.collection.arrayWithSize;
  19. import org.hamcrest.core.isA;
  20. import org.hamcrest.object.equalTo;
  21. import org.hamcrest.object.isFalse;
  22. import org.hamcrest.object.isTrue;
  23. import org.hamcrest.object.notNullValue;
  24. import org.spicefactory.lib.command.builder.CommandGroupBuilder;
  25. import org.spicefactory.lib.command.builder.Commands;
  26. import org.spicefactory.lib.command.data.CommandData;
  27. import org.spicefactory.lib.command.impl.AsynchronousCommand;
  28. import org.spicefactory.lib.command.impl.SyncLightDataCommand;
  29. import org.spicefactory.lib.command.impl.SyncLightResultCommand;
  30. import org.spicefactory.lib.command.model.CommandModel;
  31. /**
  32. * @author Jens Halm
  33. */
  34. public class CommandDataTest {
  35. [Test]
  36. public function singleCommand (): void {
  37. var async: AsynchronousCommand = new AsynchronousCommand();
  38. var result:Object;
  39. var resultHandler: Function = function (param: Object): void {
  40. result = param;
  41. };
  42. Commands.wrap(async).result(resultHandler).execute();
  43. async.forceCompletion("foo");
  44. assertThat(result, equalTo("foo"));
  45. }
  46. [Test]
  47. public function sequence (): void {
  48. group(Commands.asSequence());
  49. }
  50. [Test]
  51. public function parallel (): void {
  52. group(Commands.inParallel());
  53. }
  54. private function group (builder: CommandGroupBuilder, numResults: uint = 2): CommandData {
  55. var com1: AsynchronousCommand = new AsynchronousCommand();
  56. var com2: AsynchronousCommand = new AsynchronousCommand();
  57. var allResults:Object;
  58. var allResultsHandler: Function = function (param: Object): void {
  59. allResults = param;
  60. };
  61. var lastResult:Object;
  62. var lastResultHandler: Function = function (param: Object): void {
  63. lastResult = param;
  64. };
  65. builder.add(com1).add(com2).allResults(allResultsHandler).lastResult(lastResultHandler).execute();
  66. com1.forceCompletion("foo");
  67. com2.forceCompletion(7);
  68. assertThat(lastResult, equalTo(7));
  69. assertThat(allResults, isA(CommandData));
  70. var data: CommandData = CommandData(allResults);
  71. assertThat(data.getObject(String), equalTo("foo"));
  72. assertThat(data.getObject(Number), equalTo(7));
  73. assertThat(data.getAllObjects(), arrayWithSize(numResults));
  74. return data;
  75. }
  76. [Test]
  77. public function passThrough (): void {
  78. var data: CommandData = group(Commands.asSequence().data(new Date()), 3);
  79. assertThat(data.getObject(Date), notNullValue());
  80. }
  81. [Test]
  82. public function injection (): void {
  83. var model: CommandModel = new CommandModel("foo");
  84. var com1: AsynchronousCommand = new AsynchronousCommand();
  85. Commands.asSequence().add(com1).create(AsynchronousCommand).execute();
  86. assertThat(model.injected, isFalse());
  87. com1.forceCompletion(model);
  88. assertThat(model.injected, isTrue());
  89. }
  90. [Test]
  91. public function lightCommand (): void {
  92. var com1: SyncLightResultCommand = new SyncLightResultCommand(new CommandModel("foo"));
  93. var com2: SyncLightDataCommand = new SyncLightDataCommand();
  94. var allResults:Object;
  95. var allResultsHandler: Function = function (param: Object): void {
  96. allResults = param;
  97. };
  98. var lastResult:Object;
  99. var lastResultHandler: Function = function (param: Object): void {
  100. lastResult = param;
  101. };
  102. Commands.asSequence().add(com1).add(com2).allResults(allResultsHandler).lastResult(lastResultHandler).execute();
  103. assertThat(lastResult, equalTo("foo"));
  104. assertThat(allResults, isA(CommandData));
  105. var data: CommandData = CommandData(allResults);
  106. assertThat(data.getObject(String), equalTo("foo"));
  107. assertThat(data.getObject(CommandData), notNullValue());
  108. assertThat(data.getAllObjects(), arrayWithSize(2));
  109. assertThat(com1.executed, isTrue());
  110. assertThat(com2.model, notNullValue());
  111. }
  112. }
  113. }