Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

#! /usr/bin/env python3 

# -*- coding: utf-8 -*- 

 

#       Copyright 2013, Marten de Vries 

# 

#       This file is part of OpenTeacher. 

# 

#       OpenTeacher is free software: you can redistribute it and/or modify 

#       it under the terms of the GNU General Public License as published by 

#       the Free Software Foundation, either version 3 of the License, or 

#       (at your option) any later version. 

# 

#       OpenTeacher is distributed in the hope that it will be useful, 

#       but WITHOUT ANY WARRANTY; without even the implied warranty of 

#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

#       GNU General Public License for more details. 

# 

#       You should have received a copy of the GNU General Public License 

#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>. 

 

import unittest 

 

class CheckCall: 

        def __init__(self, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self.reset() 

 

        def __call__(self, *args, **kwargs): 

                self.called = True 

                self.args = args 

                self.kwargs = kwargs 

 

        def reset(self): 

                self.called = False 

                self.args = () 

                self.kwargs = {} 

 

class UiEnablerWatcher: 

        def __init__(self, controller, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self._onEnableCheck = CheckCall() 

                controller.enableCheck.handle(self._onEnableCheck) 

                self._onEnableSkip = CheckCall() 

                controller.enableSkip.handle(self._onEnableSkip) 

                self._onEnableInput = CheckCall() 

                controller.enableInput.handle(self._onEnableInput) 

 

        def assertUiHasBeenEnabled(self): 

                assert self._onEnableCheck.called 

                assert self._onEnableSkip.called 

                assert self._onEnableInput.called 

 

class UiDisablerWatcher: 

        def __init__(self, controller, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self._onDisableCheck = CheckCall() 

                controller.disableCheck.handle(self._onDisableCheck) 

                self._onDisableSkip = CheckCall() 

                controller.disableSkip.handle(self._onDisableSkip) 

                self._onDisableInput = CheckCall() 

                controller.disableInput.handle(self._onDisableInput) 

 

        def assertUiHasBeenDisabled(self): 

                assert self._onDisableCheck.called 

                assert self._onDisableSkip.called 

                assert self._onDisableInput.called 

 

class TestCase(unittest.TestCase): 

        @property 

        def _list(self): 

                return { 

                        "tests": [], 

                        "items": [ 

                                { 

                                        "id": 0, 

                                        "questions": [["een"]], 

                                        "answers": [["one"]], 

                                }, 

                                { 

                                        "id": 1, 

                                        "questions": [["twee"]], 

                                        "answers": [["two"]], 

                                }, 

                        ], 

                } 

 

        def setUp(self): 

                self._lessonTypeMod = next(iter(self._mm.mods("active", type="lessonType", testName="allOnce"))) 

 

        @property 

        def _lessonType(self): 

                return self._lessonTypeMod.createLessonType(self._list, [0, 1]) 

 

        @property 

        def _lessonType2(self): 

                return self._lessonTypeMod.createLessonType(self._list, [1]) 

 

        def _getControllers(self): 

                for c in self._getControllersWithoutLessonType(): 

                        c.lessonType = self._lessonType 

                        yield c 

 

        def _getControllersWithoutLessonType(self): 

                """A bunch of tests don't work with the JS version anymore, as the tests 

                were written with a better JS<->Python bridge in mind. Adding support 

                for Python objects in general might improve the situation a bit, but the 

                inability to affect Python objects (other than through side effects of 

                function calls) from JS will probably still break these tests up. 

 

                As a result, the stuff below isn't also run on: 

 

                self._mm.mods("active", type="jsInputTypingLogic") 

 

                """ 

                for mod in self._mm.mods("active", type="inputTypingLogic"): 

                        yield mod.createController() 

 

        def testCallingMethodsWithoutLessonType(self): 

                for controller in self._getControllersWithoutLessonType(): 

                        with self.assertRaises(AttributeError): 

                                controller.checkTriggered("shouldn't matter") 

                        with self.assertRaises(AttributeError): 

                                controller.correctAnywayTriggered() 

                        with self.assertRaises(AttributeError): 

                                controller.skipTriggered() 

                        with self.assertRaises(AttributeError): 

                                controller.userIsTyping() 

 

        def testCallingMethodsWithoutStartingLesson(self): 

                for controller in self._getControllers(): 

                        with self.assertRaises(ValueError): 

                                controller.checkTriggered("whatever") 

                        with self.assertRaises(ValueError): 

                                controller.correctAnywayTriggered() 

                        with self.assertRaises(ValueError): 

                                controller.skipTriggered() 

                        with self.assertRaises(ValueError): 

                                controller.userIsTyping() 

 

        def testSettingOtherLessonTypeWhileShowingACorrection(self): 

                for controller in self._getControllers(): 

                        self._makeUnusedControllerShowACorrection(controller) 

 

                        uiWatcher = UiEnablerWatcher(controller) 

                        onHideCorrection = CheckCall() 

                        controller.hideCorrection.handle(onHideCorrection) 

 

                        controller.lessonType = self._lessonType2 

 

                        self.assertTrue(onHideCorrection.called) 

                        uiWatcher.assertUiHasBeenEnabled() 

 

                        #start again to test if everything was cleaned up properly 

                        controller.lessonType.start() 

 

        def _makeUnusedControllerShowACorrection(self, controller): 

                onShowCorrection = CheckCall() 

                controller.showCorrection.handle(onShowCorrection) 

                onHideCorrection = CheckCall() 

                controller.hideCorrection.handle(onHideCorrection) 

                uiWatcher = UiDisablerWatcher(controller) 

 

                controller.lessonType.start() 

                controller.checkTriggered(u"a wrong answer") 

 

                self.assertTrue(onShowCorrection.called) 

                self.assertEqual(onShowCorrection.args, (u"one",)) 

                self.assertFalse(onHideCorrection.called) 

                uiWatcher.assertUiHasBeenDisabled() 

 

        def testMethodsWhileShowingCorrection(self): 

                for controller in self._getControllers(): 

                        self._makeUnusedControllerShowACorrection(controller) 

 

                        with self.assertRaises(ValueError): 

                                controller.checkTriggered(u"this answer doesn't matter") 

                        with self.assertRaises(ValueError): 

                                controller.skipTriggered() 

                        with self.assertRaises(ValueError): 

                                controller.userIsTyping() 

 

        def testCorrectAnywayWhileShowingCorrection(self): 

                for controller in self._getControllers(): 

                        self._makeUnusedControllerShowACorrection(controller) 

 

                        onHideCorrection = CheckCall() 

                        controller.hideCorrection.handle(onHideCorrection) 

 

                        uiWatcher = UiEnablerWatcher(controller) 

 

                        controller.correctAnywayTriggered() 

                        self.assertTrue(onHideCorrection.called) 

                        uiWatcher.assertUiHasBeenEnabled() 

 

        def testSkip(self): 

                for controller in self._getControllers(): 

                        onNewItem = CheckCall() 

                        controller.lessonType.newItem.handle(onNewItem) 

 

                        controller.lessonType.start() 

                        self.assertEqual((self._list["items"][0],), onNewItem.args) 

                        controller.skipTriggered() 

                        self.assertEqual((self._list["items"][1],), onNewItem.args) 

 

        def testCompleteLesson(self): 

                for controller in self._getControllers(): 

                        controller.lessonType.start() 

                        #right answer both times 

                        controller.checkTriggered(u"one") 

                        controller.checkTriggered(u"two") 

 

        def testCallingCorrectionShowingDoneWhileNoCorrectionIsShown(self): 

                for controller in self._getControllers(): 

                        controller.lessonType.start() 

                        with self.assertRaises(ValueError): 

                                controller.correctionShowingDone() 

 

class TestModule: 

        def __init__(self, moduleManager, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

                self._mm = moduleManager 

 

                self.type = "test" 

                self.requires = ( 

                        self._mm.mods(type="inputTypingLogic"), 

                        self._mm.mods(type="lessonType", testName="allOnce"), 

                ) 

 

        def enable(self): 

                self.TestCase = TestCase 

                self.TestCase._mm = self._mm 

                self.active = True 

 

        def disable(self): 

                self.active = False 

                del self.TestCase 

 

def init(moduleManager): 

        return TestModule(moduleManager)