OSDN Git Service

na-get-lib,複数ソフトインストールかつハッシュ検証失敗したとき、ハッシュ検証成功したソフトの状態がおかしくなる不具合を修正
[applistation/AppliStation.git] / test-na-get-lib / FunctionalSubTaskTest.cs
1 using System;
2 using System.Threading;
3 using NUnit.Framework;
4
5 using NaGet.SubCommands.SubTask;
6
7 namespace test_na_get_lib
8 {
9         [TestFixture]
10         public class FunctionalSubTaskTest
11         {
12                 [Test]
13                 public void UseProgress()
14                 {
15                         FunctionalSubTask<object> subtask = new FunctionalSubTask<object>(null, null);
16                         Assert.IsFalse(subtask.UseProgress);
17                 }
18                 
19                 [Test]
20                 public void Cancelable()
21                 {
22                         FunctionalSubTask<object> subtask = new FunctionalSubTask<object>(null, null);
23                         Assert.IsFalse(subtask.Cancelable);
24                 }
25                 
26                 [Test]
27                 public void Run()
28                 {
29                         object arg = new object();
30                         bool passBlock = false;
31                         FunctionalSubTask<object> subtask = null;
32                         
33                         // 正常動作
34                         
35                         Action<object> func1 = delegate (object argv) {
36                                 passBlock = true;
37                                 Assert.IsTrue(subtask.Running);
38                                 Assert.IsFalse(subtask.Done);
39                                 Assert.AreEqual(arg, argv);
40                         };
41                         subtask = new FunctionalSubTask<object>(func1, arg);
42                         passBlock = false;
43                         subtask.Run();
44                         Assert.IsFalse(subtask.Running);
45                         Assert.IsFalse(subtask.Cancelled);
46                         Assert.IsTrue(subtask.Done);
47                         Assert.IsTrue(passBlock);
48                         
49                         // 例外時動作
50                         
51                         Action<object> func2 = delegate (object argv) {
52                                 passBlock = true;
53                                 Assert.IsNull(argv);
54                                 throw new TestException("foobar");
55                         };
56                         passBlock = false;
57                         subtask = new FunctionalSubTask<object>(func2, null);
58                         try {
59                                 subtask.Run();
60                                 Assert.IsTrue(passBlock);
61                                 Assert.Fail("shall not be reached here");
62                         } catch (TestException ex) {
63                                 Assert.IsTrue(passBlock);
64                                 Assert.AreEqual("foobar", ex.Message);
65                         } finally {
66                                 // 例外が起こったとしても終了扱い
67                                 Assert.IsTrue(subtask.Done);
68                                 Assert.IsFalse(subtask.Cancelled);
69                         }
70                         
71                         // 異常設定
72                         
73                         subtask = new FunctionalSubTask<object>(null, null);
74                         passBlock = false;
75                         try {
76                                 subtask.Run();
77                                 Assert.Fail("shall not be reached here");
78                         } catch (NullReferenceException) {
79                         } finally {
80                                 // 例外が起こったとしても終了扱い
81                                 Assert.IsTrue(subtask.Done);
82                                 Assert.IsFalse(subtask.Cancelled);
83                         }
84                 }
85                 
86                 private class TestException : Exception
87                 {
88                         public TestException(string msg)
89                                 : base(msg) {
90                         }
91                 }
92         }
93 }